Password-protected account

Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in

(define acc (make-account 100 'secret-password))

The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint:

((acc 'secret-password 'withdraw) 40)
60
((acc 'some-other-password 'deposit) 50)
"Incorrect password"

There are no comments yet.

Authentication required

You must log in to post a comment.

Login
(define incorrect-message "Incorrect password\n")

(define insufficient-message "Insufficient funds")

(define acc (make-account 100 'secret-password))

(define (wrong-password-attempt)
  (with-handlers ([exn:fail?
                    (lambda (e) (exn->string e))])
     ((acc 'some-other-password 'deposit) 50)))

(define (insufficient-funds-attempt)
  (with-handlers ([exn:fail?
                    (lambda (e) (exn->string e))])
     ((acc 'secret-password 'withdraw) 5000)))

(check-equal? ((acc 'secret-password 'withdraw) 40) 60)
(check-equal? ((acc 'secret-password 'deposit) 40) 100)
(check-equal? (wrong-password-attempt) incorrect-message)
(check-equal? (insufficient-funds-attempt) insufficient-message)