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

Commit

Permalink
Add bank-account exercise (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 8, 2023
1 parent f74acae commit c564fe2
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@
],
"difficulty": 2
},
{
"slug": "bank-account",
"name": "Bank Account",
"uuid": "a3964ef3-f2d8-4982-9240-d253345b0988",
"practices": [
"floating-point-numbers"
],
"prerequisites": [
"floating-point-numbers"
],
"difficulty": 4
},
{
"slug": "go-counting",
"name": "Go Counting",
Expand Down
12 changes: 12 additions & 0 deletions exercises/practice/bank-account/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Instructions

Simulate a bank account supporting opening/closing, withdrawals, and deposits of money.
Watch out for concurrent transactions!

A bank account can be accessed in multiple ways.
Clients can make deposits and withdrawals using the internet, mobile phones, etc.
Shops can charge against the account.

Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language).

It should be possible to close an account; operations against a closed account must fail.
17 changes: 17 additions & 0 deletions exercises/practice/bank-account/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/bank_account.cljs"
],
"test": [
"test/bank_account_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Simulate a bank account supporting opening/closing, withdraws, and deposits of money. Watch out for concurrent transactions!"
}
17 changes: 17 additions & 0 deletions exercises/practice/bank-account/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns bank-account)

(defrecord BankAccount [balance open?])

(defn open-account []
(atom (BankAccount. 0 true)))

(defn close-account [bank-account]
(swap! bank-account assoc :open? false))

(defn get-balance [bank-account]
(when (:open? @bank-account)
(:balance @bank-account)))

(defn update-balance [bank-account adjustment]
(locking bank-account
(swap! bank-account assoc :balance (+ (get-balance bank-account) adjustment))))
10 changes: 10 additions & 0 deletions exercises/practice/bank-account/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
17 changes: 17 additions & 0 deletions exercises/practice/bank-account/src/bank_account.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns bank-account)

(defn open-account [] ;; <- arglist goes here
;; your code goes here
)

(defn close-account [] ;; <- arglist goes here
;; your code goes here
)

(defn get-balance [] ;; <- arglist goes here
;; your code goes here
)

(defn update-balance [] ;; <- arglist goes here
;; your code goes here
)
44 changes: 44 additions & 0 deletions exercises/practice/bank-account/test/bank_account_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns bank-account-test
(:require
[clojure.test :refer [deftest testing is use-fixtures]]
[bank-account]))

(defn pcalls
"Executes the no-arg fns in parallel, returning a lazy sequence of
their values"
{:added "1.0"
:static true}
[& fns] (pmap #(%) fns))

#_(defn shutdown-agents-fixture [f]
(f)
(shutdown-agents))

;(use-fixtures :once shutdown-agents-fixture)

(deftest initial-account-state
(testing "Accounts are opened with a balance of 0"
(is (= 0 (-> (bank-account/open-account)
(bank-account/get-balance))))))

(deftest increment-and-get-balance
(testing "Adding money to the account works"
(let [account (bank-account/open-account)]
(is (= 0 (bank-account/get-balance account)))
(bank-account/update-balance account 10)
(is (= 10 (bank-account/get-balance account))))))

(deftest increment-decrement-and-get-balance
(testing "Taking money out of the account works"
(let [account (bank-account/open-account)]
(is (= 0 (bank-account/get-balance account)))
(bank-account/update-balance account 10)
(is (= 10 (bank-account/get-balance account)))
(bank-account/update-balance account -10)
(is (= 0 (bank-account/get-balance account))))))

(deftest closed-accounts-are-nil
(testing "Closing an account makes it nil"
(let [account (bank-account/open-account)]
(bank-account/close-account account)
(is (nil? (bank-account/get-balance account))))))

0 comments on commit c564fe2

Please sign in to comment.