Created
June 28, 2011 22:37
-
-
Save jwdunne/1052427 to your computer and use it in GitHub Desktop.
Bank account implementation in CLisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defclass customer () | |
((balance :accessor customer-balance | |
:initarg :balance) | |
(acct-no :accessor customer-number | |
:initarg :ac-no) | |
(name :accessor customer-name | |
:initarg :name))) | |
; Constructor | |
(defun make-customer (acct-no name) | |
(make-instance 'customer :ac-no acct-no | |
:name name)) | |
(defmethod set-balance ((customer customer) balance) | |
(if (< balance 0) | |
'negative-amount-error | |
(setf (customer-balance customer) balance))) | |
(defmethod get-balance ((customer customer)) | |
(customer-balance customer)) | |
(defmethod withdrawable? ((customer customer) amount) | |
(if (or (> amount (customer-balance customer)) | |
(< amount 0)) | |
nil | |
t)) | |
(defmethod withdraw ((customer customer) amount) | |
(if (withdrawable? customer amount) | |
(set-balance customer (- (get-balance customer) amount)) | |
'not-enough-funds)) | |
; We allow for negative values in this method | |
; since banks are usually evil like that. | |
(defmethod deposit ((customer customer) amount) | |
(set-balance customer (+ (get-balance customer) amount))) | |
(setf my-customer (make-customer "James" 1)) | |
(set-balance my-customer 200) ; Output 200 | |
(get-balance my-customer) ; Output 200 | |
(withdrawable? my-customer 300) ; Output NIL | |
(withdrawable? my-customer 200) ; Output T | |
(withdrawable? my-customer 100) ; Output T | |
(withdraw my-customer 100) ; Output 100 (200 - 100 lol) | |
(withdraw my-customer 200) ; Output NOT-ENOUGH-FUNDS | |
(withdraw my-customer 300) ; Output NOT-ENOUGH-FUNDS | |
(deposit my-customer 100) ; Output 200 | |
(withdraw my-customer 200) ; Output 0 | |
(withdraw my-customer 1) ; Output NOT-ENOUGH-FUNDS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment