rendered paste body;; Question 3
(define-struct checking (balance transactions))
;; checking is a structure: (make-checking balance transactions)
;; will create a checking account with a given balance and number of transactions
(define-struct savings (balance transactions rate))
;; savings is a structure: (make-savings balance transactions rate)
;; will create a savings account with a given balance, number of transactions and a
;; yearly interest rate
(define-struct credit (balance limit transactions rate))
;; credit is a structure: (make-credit balance limit transactions rate)
;; will create a credit account with a given balance, credit limit, number of transactions
;; and a yearly interest rate
(define (bank-of-canada-update acct newrate)
(cond
[(credit? acct) (make-credit (credit-balance acct) (credit-limit acct) (credit-transactions acct) (+ newrate 2))]
[(savings? acct) (make-savings (savings-balance acct) (savings-transactions acct)
(cond
[(>= (- newrate 1) 0) (- newrate 1)]
[else 0]
)
)
]
[(checking? acct) (make-checking (checking-balance acct) (checking-transactions acct))]
[else 'Error]
)
)
(define (withdrawal acct amt)
(cond
[(credit? acct) (make-credit
(cond
[(<= (+ (credit-balance acct) amt) (credit-limit acct)) (+ (credit-balance acct) amt)]
[else 'Error]) ;; exceeded balance limit (throw in a defined constant)
(credit-limit acct)
(cond
[(> (- (credit-transactions acct) 1) 0) (- (credit-transactions acct) 1)]
[else 'Error]) ;; exceed transaction limit (throw in a defined constant)
(credit-rate acct)
)
]
[(savings? acct) (make-savings
(cond
[(>= (- (savings-balance acct) amt) 0) (- (savings-balance acct) amt)]
[else 'Error]) ;; exceeded balance limit (throw in a defined constant)
(cond
[(> (- (savings-transactions acct) 1) 0) (- (savings-transactions acct) 1)]
[else 'Error]) ;; exceed transaction limit (throw in a defined constant)
(savings-rate acct)
)
]
[(checking? acct) (make-checking
(cond
[(>= (- (checking-balance acct) amt) (- 1000)) (- (checking-balance acct) amt)]
[else 'Error]) ;; exceeded balance limit (throw in a defined constant)
(cond
[(> (- (checking-transactions acct) 1) 0) (- (checking-transactions acct) 1)]
[else 'Error]) ;; exceed transaction limit (throw in a defined constant)
)
]
[else 'Error]
)
)
(define (year-end-update acct)
(cond
[(checking? acct) (checking-balance acct)]
[(credit? acct) (* (credit-balance acct) (+ 1 (* (credit-rate acct) 0.01)))]
[(savings? acct) (* (savings-balance acct) (+ 1 (* (savings-rate acct) 0.01)))]
[else 'Error]
)
)