Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit c564fe2

Browse files
Add bank-account exercise (#115)
1 parent f74acae commit c564fe2

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

config.json

+12
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,18 @@
585585
],
586586
"difficulty": 2
587587
},
588+
{
589+
"slug": "bank-account",
590+
"name": "Bank Account",
591+
"uuid": "a3964ef3-f2d8-4982-9240-d253345b0988",
592+
"practices": [
593+
"floating-point-numbers"
594+
],
595+
"prerequisites": [
596+
"floating-point-numbers"
597+
],
598+
"difficulty": 4
599+
},
588600
{
589601
"slug": "go-counting",
590602
"name": "Go Counting",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Instructions
2+
3+
Simulate a bank account supporting opening/closing, withdrawals, and deposits of money.
4+
Watch out for concurrent transactions!
5+
6+
A bank account can be accessed in multiple ways.
7+
Clients can make deposits and withdrawals using the internet, mobile phones, etc.
8+
Shops can charge against the account.
9+
10+
Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language).
11+
12+
It should be possible to close an account; operations against a closed account must fail.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/bank_account.cljs"
8+
],
9+
"test": [
10+
"test/bank_account_test.cljs"
11+
],
12+
"example": [
13+
".meta/src/example.cljs"
14+
]
15+
},
16+
"blurb": "Simulate a bank account supporting opening/closing, withdraws, and deposits of money. Watch out for concurrent transactions!"
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(ns bank-account)
2+
3+
(defrecord BankAccount [balance open?])
4+
5+
(defn open-account []
6+
(atom (BankAccount. 0 true)))
7+
8+
(defn close-account [bank-account]
9+
(swap! bank-account assoc :open? false))
10+
11+
(defn get-balance [bank-account]
12+
(when (:open? @bank-account)
13+
(:balance @bank-account)))
14+
15+
(defn update-balance [bank-account adjustment]
16+
(locking bank-account
17+
(swap! bank-account assoc :balance (+ (get-balance bank-account) adjustment))))
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:deps
2+
{org.clojure/clojure {:mvn/version "1.10.1"}
3+
org.clojure/clojurescript {:mvn/version "1.10.773"}}
4+
5+
:aliases
6+
{:test
7+
{:extra-paths ["test"]
8+
:extra-deps
9+
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
10+
:main-opts ["-m" "cljs-test-runner.main"]}}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(ns bank-account)
2+
3+
(defn open-account [] ;; <- arglist goes here
4+
;; your code goes here
5+
)
6+
7+
(defn close-account [] ;; <- arglist goes here
8+
;; your code goes here
9+
)
10+
11+
(defn get-balance [] ;; <- arglist goes here
12+
;; your code goes here
13+
)
14+
15+
(defn update-balance [] ;; <- arglist goes here
16+
;; your code goes here
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(ns bank-account-test
2+
(:require
3+
[clojure.test :refer [deftest testing is use-fixtures]]
4+
[bank-account]))
5+
6+
(defn pcalls
7+
"Executes the no-arg fns in parallel, returning a lazy sequence of
8+
their values"
9+
{:added "1.0"
10+
:static true}
11+
[& fns] (pmap #(%) fns))
12+
13+
#_(defn shutdown-agents-fixture [f]
14+
(f)
15+
(shutdown-agents))
16+
17+
;(use-fixtures :once shutdown-agents-fixture)
18+
19+
(deftest initial-account-state
20+
(testing "Accounts are opened with a balance of 0"
21+
(is (= 0 (-> (bank-account/open-account)
22+
(bank-account/get-balance))))))
23+
24+
(deftest increment-and-get-balance
25+
(testing "Adding money to the account works"
26+
(let [account (bank-account/open-account)]
27+
(is (= 0 (bank-account/get-balance account)))
28+
(bank-account/update-balance account 10)
29+
(is (= 10 (bank-account/get-balance account))))))
30+
31+
(deftest increment-decrement-and-get-balance
32+
(testing "Taking money out of the account works"
33+
(let [account (bank-account/open-account)]
34+
(is (= 0 (bank-account/get-balance account)))
35+
(bank-account/update-balance account 10)
36+
(is (= 10 (bank-account/get-balance account)))
37+
(bank-account/update-balance account -10)
38+
(is (= 0 (bank-account/get-balance account))))))
39+
40+
(deftest closed-accounts-are-nil
41+
(testing "Closing an account makes it nil"
42+
(let [account (bank-account/open-account)]
43+
(bank-account/close-account account)
44+
(is (nil? (bank-account/get-balance account))))))

0 commit comments

Comments
 (0)