|
| 1 | +#lang racket |
| 2 | +(define (make-account balance password) |
| 3 | + (let ((failed-count 0)) |
| 4 | + (define (withdraw amount) |
| 5 | + (if (>= balance amount) |
| 6 | + (begin (set! balance (- balance amount)) |
| 7 | + balance) |
| 8 | + "Insufficient funds")) |
| 9 | + |
| 10 | + (define (deposit amount) |
| 11 | + (set! balance (+ balance amount)) |
| 12 | + balance) |
| 13 | + |
| 14 | + (define (call-the-cops) |
| 15 | + (error "Calling 911")) |
| 16 | + |
| 17 | + (define (dispatch pwd m) |
| 18 | + (if (eq? pwd password) |
| 19 | + ;; reset the failed count once the password is correct |
| 20 | + (begin (set! failed-count 0) |
| 21 | + (cond ((eq? m 'withdraw) withdraw) |
| 22 | + ((eq? m 'deposit) deposit) |
| 23 | + (else (error "Unknown request --MAKE_ACCOUNT" m)))) |
| 24 | + (begin (set! failed-count (+ failed-count 1)) |
| 25 | + (if (> failed-count 7) |
| 26 | + (call-the-cops) |
| 27 | + (error "Incorrect password"))) |
| 28 | + )) |
| 29 | + dispatch)) |
| 30 | + |
| 31 | +(module+ test |
| 32 | + (require rackunit) |
| 33 | + (require rackunit/text-ui) |
| 34 | + (define acc (make-account 100 'secret-password)) |
| 35 | + (define acc2 (make-account 100 'secret-password)) |
| 36 | + (define acc3 (make-account 100 'secret-password)) |
| 37 | + |
| 38 | + (define module-test |
| 39 | + (test-suite |
| 40 | + "Tests for make-account with password and limited attempt" |
| 41 | + (check-equal? ((acc 'secret-password 'withdraw) 40) 60) |
| 42 | + (check-equal? ((acc 'secret-password 'withdraw) 40) 20) |
| 43 | + ;; Test that an exception is thrown with the correct message |
| 44 | + (check-exn exn:fail? (lambda () ((acc 'invalid-password 'deposit) 40))) |
| 45 | + (check-exn (regexp "Incorrect password") (lambda () ((acc 'invalid-password 'deposit) 40))) |
| 46 | + |
| 47 | + "Tests for make-account, 7 consecutive attmepts failed, call cops eventually" |
| 48 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 49 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 50 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 51 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 52 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 53 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 54 | + (check-exn (regexp "Incorrect password") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 55 | + (check-exn (regexp "Calling 911") (lambda () ((acc2 'invalid-password 'deposit) 40))) |
| 56 | + |
| 57 | + "Tests for make-account, 1 attempt succeeded, but subsequence attempt failed, call cops eventually" |
| 58 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 59 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 60 | + ;; successful attempt reset the failed count |
| 61 | + (check-equal? ((acc3 'secret-password 'withdraw) 40) 60) |
| 62 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 63 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 64 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 65 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 66 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 67 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 68 | + (check-exn (regexp "Incorrect password") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 69 | + ;; call-the-cops after consecutive 7 failed attempts |
| 70 | + (check-exn (regexp "Calling 911") (lambda () ((acc3 'invalid-password 'deposit) 40))) |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + (run-tests module-test)) |
0 commit comments