This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f74acae
commit c564fe2
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
exercises/practice/bank-account/test/bank_account_test.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |