Wednesday, February 22, 2012

A Simple Banking System in Scheme(Example for Modularity)


(define (make-accumilator val)
 (lambda (x)
  (begin
   (set! val (+ val x))
   val
  )
 )
)


;#######----------Banking System----------##########;
;Procedure Usage Counter....

(define (make-monitered f)
 (define ctr 0)
 (define (increment) 
  (set! ctr (+ ctr 1))
 )
 (define (reset)
  (set! ctr 0)
 )
 (define (mf arg)
  (cond ((eq? arg 'how-many-calls?) ctr)
    ((eq? arg 'reset-count)   (reset) )
    (else (begin (increment) (f arg)))
  )
 )
mf)


;Password protected banking system...

(define (make-account password balance)
    (define ctr 0)
    (define (display-account-blkd x) "Account Blocked")
    (define (display-wrong-passwd x) "Wrong Password")
     (define (withdraw amount)
 (if (>= balance amount)
   (begin
     (set! balance (- balance amount))
     balance
   )
   (display "Sufficient balane not in the account...")
 )
      )

      (define (deposit amount)
   (begin
     (set! balance (+ balance amount))
     balance
   )
      )

      (define (main passwd transaction)
 (if (>= ctr 5)
   display-account-blkd
     (if (eq? passwd password)
      
       (begin
    (set! ctr 0)
    (cond ((eq? transaction 'deposit) deposit  )
   ((eq? transaction 'withdraw) withdraw  )
   (else (lambda (x) "Invalid Choice"))
    )
       )
       
       (begin
    (set! ctr (+ ctr 1))
  display-wrong-passwd
       )
     )
 )

      )
main);End of body of let...

No comments:

Post a Comment