Avatar of murdho
0
0
Genius
0
1

murdho's solution

to Bank Account in the Clojure Track

0
0
Genius
0
1
Instructions
Test suite
Solution

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.

Instructions

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.

bank_account_test.clj

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

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

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

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?

Community comments

See what others have said about this solution
10 months ago
murdho says

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! :)