Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootkowiak

rootkowiak's solution

to Bank Account in the Erlang Track

Published at Jan 08 2021 · 0 comments
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!

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Submitting Incomplete Solutions

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

bank_account_tests.erl

-module(bank_account_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

create_test() ->
  BankAccount = bank_account:create(),
  ?assert(bank_account:balance(BankAccount) =:= 0).

close_account_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 1),
  Amount = bank_account:close(BankAccount),
  ?assert(Amount =:= 1),
  ?assertEqual({error, account_closed}, bank_account:balance( BankAccount )).

deposit_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 1),
  ?assert(bank_account:balance(BankAccount) =:= 1).

deposit_fail_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, -1),
  ?assert(bank_account:balance(BankAccount) =:= 0).

deposit_many_test() ->
  BankAccount = bank_account:create(),
  [erlang:spawn( fun () -> bank_account:deposit(BankAccount, X) end ) || X <- lists:seq(1, 10)],
  timer:sleep(100),
  Last = bank_account:balance(BankAccount),
  ?assert(Last =:= 55).

withdraw_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10 ),
  Amount = bank_account:withdraw(BankAccount, 1),
  ?assert(Amount =:= 1),
  ?assert(bank_account:balance(BankAccount) =:= 9).

withdraw_fail_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:withdraw(BankAccount, -1),
  ?assert(Amount =:= 0),
  ?assert(bank_account:balance(BankAccount) =:= 10).

withdraw_excessive_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10 ),
  Amount = bank_account:withdraw(BankAccount, 20),
  ?assert(Amount =:= 10),
  ?assert(bank_account:balance(BankAccount) =:= 0).

withdraw_many_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 55 ),
  [erlang:spawn( fun () -> bank_account:withdraw(BankAccount, X) end ) || X <- lists:seq(1, 10)],
  timer:sleep(100),
  Last = bank_account:balance(BankAccount),
  ?assert(Last =:= 0).

charge_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:charge(BankAccount, 2),
  ?assert(Amount =:= 2),
  ?assert(bank_account:balance(BankAccount) =:= 8).

charge_fail_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:charge(BankAccount, -2),
  ?assert(Amount =:= 0),
  ?assert(bank_account:balance(BankAccount) =:= 10).

charge_excessive_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:charge(BankAccount, 20),
  ?assert(Amount =:= 0),
  ?assert(bank_account:balance(BankAccount) =:= 10).

charge_many_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 55 ),
  [erlang:spawn( fun () -> bank_account:charge(BankAccount, 10) end ) || _X <- lists:seq(1, 10)],
  timer:sleep(100),
  Last = bank_account:balance(BankAccount),
  ?assert(Last =:= 5).
-module(bank_account).
-behaviour(gen_server).

-export([balance/1, charge/2, close/1, create/0, deposit/2, withdraw/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

%% Client APIs
create() ->
    {ok, Pid} = gen_server:start_link(?MODULE, [], []),
    Pid.

close(Pid) ->
    Balance = balance(Pid),
    gen_server:stop(Pid),
    Balance.

balance(Pid) -> 
    case catch gen_server:call(Pid, balance) of
        {'EXIT', {noproc, _}} -> {error, account_closed};
        Resp -> Resp
    end.

deposit(Pid, Amount) ->
    gen_server:cast(Pid, {deposit, Amount}).

withdraw(Pid, Amount) ->
    gen_server:call(Pid, {withdraw, Amount}).

charge(Pid, Amount) ->
    gen_server:call(Pid, {charge, Amount}).

%% gen_server callback functions
init([]) -> {ok, 0}.

handle_cast({deposit, Amount}, Balance) when Amount>0 ->
    {noreply, Balance+Amount};
handle_cast({deposit, _Amount}, Balance) ->
    {noreply, Balance}.

handle_call({withdraw, Amount}, _From, Balance) when Amount>=Balance ->
    {reply, Balance, 0};
handle_call({withdraw, Amount}, _From, Balance) when Amount>0 ->
    {reply, Amount, Balance-Amount};
handle_call({withdraw, _Amount}, _From, Balance) ->
    {reply, 0, Balance};
handle_call({charge, Amount}, _From, Balance) when Amount>=Balance; Amount<0 ->
    {reply, 0, Balance};
handle_call({charge, Amount}, _From, Balance) ->
    {reply, Amount, Balance-Amount};
handle_call(balance, _From, Balance) ->
    {reply, Balance, Balance}.

handle_info(_Msg, Balance) ->
    {noreply, Balance}.
    
terminate(_Reason, _Balance) -> ok.

code_change(_OldVsn, Balance, _Extra) ->
    {ok, Balance}.

Community comments

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

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?