-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter16.lisp
24 lines (19 loc) · 894 Bytes
/
chapter16.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(defgeneric withdraw (account amount)
(:documentation "Withdraw the specified amount from the account. Signal an
error if the current balance is less than amount."))
(defmethod withdraw ((account bank-account) amount)
(when (< (balance account) amount)
(error "Account overdrawn."))
(decf (balance account) amount))
(defmethod withdraw ((account checking-account) amount)
(let ((overdraft (- amount (balance account))))
(when (plusp overdraft)
(withdraw(overdraft-account) account)
(incf (balance account) overdraft))
(call-next-method) ; invoke withdraw wtih same arguments on 'bank-account' superclass
;;stealing
(defmethod withdraw ((account (eql *account-of-bank-president*) amount))
(let ((overdraft (- amount (balance account))))
(when (plusp overdraft)
(incf (balance account (embezzle *bank* overdraft))))
(call-next-method)))