-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocols.clj
35 lines (32 loc) · 1021 Bytes
/
protocols.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
(ns state-examples.protocols)
(defprotocol AccountAPI
(get-name [this] "get the account name")
(get-balance [this] "get the account balance")
(apply-interest [this] "calculate the interest and apply it to the account")
(withdraw [this amt] "withdraw the given amount from the account")
(deposit [this amt] "deposit the given ammount into the account"))
(defrecord Account [name balance interest-rate]
AccountAPI
(get-name [this] (:name this))
(get-balance [this] (:balance this))
(apply-interest
[this]
(conj
this
{:balance (+ (:balance this)
(* (:balance this) (:interest-rate this)))}))
(withdraw
[this amt]
(cond (<= amt (:balance this))
(conj
this
{:balance (- (:balance this) amt)})
:else (throw (Exception. ": Insufficient funds."))))
(deposit
[this amt]
(conj
this
{:balance (+ (:balance this) amt)})))
(defn new-account
[name balance interest-rate]
(->Account name balance interest-rate))