; Note the password variable added from the original
; make-account that is given in the book.
(define (make-account balance pwd)
  (define (withdraw amount)
    (if (> balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  (define (dispatch m pass)
    (cond ((not (eq? pass pwd)) (error "WRONG PASSWORD")) 
          ((eq? m 'withdraw) withdraw)
          ((eq? m 'deposit) deposit)
          (else (error "Unknown request -- MAKE-ACCOUNT"
                       m))))
  dispatch)

; (define paul-acc
;   (make-joint peter-acc 'open-sesame 'rosebud))
; Note: If the password is correct, then the deposit procedure
;   is returned.  Since a procedure is not #f, we can use it as
;   a boolean to determine whether to allow the account to be joint.
; Ain't Scheme grand?  Of course it is!
(define (make-joint existing-acc existing-pass new-pass) 
  (cond ((not (existing-acc 'deposit existing-pass))
         (error "Wrong password for original account"))
        (else (lambda (msg pass)
                (cond ((eq? pass new-pass)
                       (existing-acc msg existing-pass))
                      (else (error "WRONG PASSWORD")))))))

; Examples:
> (define jaxon (make-account 1000 'hi))
> ((jaxon 'deposit 'hi) 70)
1070
> ((jaxon 'deposit 'him) 70)
. WRONG PASSWORD
> (define neil (make-joint jaxon 'hi 'there))
> ((neil 'withdraw 'there) 850)
220
> ((neil 'withdraw 'the) 10)
. WRONG PASSWORD