Avatar of murdho

murdho's solution

to Bank Account in the Clojure Track

Published at Jul 13 2018 · 1 comment
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.


Run the test file, and fix each of the errors in turn. When you get the first test to pass, go to the first pending or skipped test, and make that pass as well. When all of the tests are passing, feel free to submit.

Remember that passing code is just the first step. The goal is to work towards a solution that is as readable and expressive as you can make it.

Have fun!

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


(ns bank-account-test
    [clojure.test :refer [deftest testing is use-fixtures]]

(defn shutdown-agents-fixture [f]

(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)

(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))))))

(deftest check-concurrent-access
  (testing "The account can handle parallel access"
    (let [account (bank-account/open-account)
          add-10 #(bank-account/update-balance account 10)]
      (doall (pcalls add-10 add-10 add-10 add-10 add-10))
      (is (= 50 (bank-account/get-balance account))))))
(ns bank-account)

(defn open-account []
  (atom {:open    true
         :balance 0}))

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

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

(defn update-balance [account amount]
  (when (@account :open)
    (swap! account update :balance + amount)))

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of murdho

This solution won't blow up, when executing update-balance on a closed account.

If you know a better way to do it, considering the missing test case ("update balance on closed account doesn't change the balance"), feel free to comment and share! :)

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?