Control the number of password attempts

Modify the make-account procedure of exercise 3.3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops .


There are no comments yet.

Authentication required

You must log in to post a comment.

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

(define wrong-message "Wrong password\n")

(define cops-message "Cops called\n")

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

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