-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.clj
38 lines (31 loc) · 873 Bytes
/
data.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns state-examples.data)
(defn new-account [name balance interest-rate]
{:name name
:balance balance
:interest-rate interest-rate})
(defn get-name [data]
(data :name))
(defn get-balance [data]
(data :balance))
(defn get-interest-rate [data]
(data :interest-rate))
(defn apply-interest [data]
(let [balance (get-balance data)
interest-rate (get-interest-rate data)]
(new-account
(get-name data)
(+ balance (* balance interest-rate))
interest-rate)))
(defn withdraw [data amt]
(let [balance (get-balance data)]
(cond (<= amt balance)
(new-account
(get-name data)
(- (get-balance data) amt)
(get-interest-rate data))
:else (throw (Exception. ": Insufficient funds.")))))
(defn deposit [data amt]
(new-account
(get-name data)
(+ (get-balance data) amt)
(get-interest-rate data)))