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

DataTriny's solution

to Bank Account in the Erlang Track

Published at Mar 31 2020 · 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).

-export([balance/1, charge/2, close/1, create/0, deposit/2, withdraw/2]).

balance(Pid) ->
	send(Pid, request_balance).

charge(Pid, Amount) ->
  send(Pid, {request_charge, Amount}).

close(Pid) ->
	send(Pid, request_close).

create() ->
  spawn(fun() -> loop(false, 0) end).

deposit(Pid, Amount) ->
  send(Pid, {request_deposit, Amount}).

withdraw(Pid, Amount) ->
  send(Pid, {request_withdraw, Amount}).

loop(Closed, Balance) ->
	receive
		{From, request_balance} when not Closed ->
			From ! Balance,
			loop(Closed, Balance);
		{From, {request_charge, Amount}} when not Closed ->
			Charged = case (Amount >= 0) and (Balance >= Amount) of
				true -> Amount;
				_ -> 0
			end,
			From ! Charged,
			loop(Closed, Balance - Charged);
		{From, request_close} when not Closed ->
			From ! Balance,
			loop(true, Balance);
		{From, {request_deposit, Amount}} when not Closed, Amount > 0 ->
			From ! ok,
			loop(Closed, Balance + Amount);
		{From, {request_withdraw, Amount}} when not Closed ->
			Withdrawn = case Amount >= 0 of
				true when Amount > Balance -> Balance;
				true -> Amount;
				_ -> 0
			end,
			From ! Withdrawn,
			loop(Closed, Balance - Withdrawn);
		{From, _} when not Closed ->
			From ! ok,
			loop(Closed, Balance);
		{From, _} when Closed ->
			From ! {error, account_closed},
			loop(Closed, Balance)
	end.

send(To, Message) ->
	To ! {self(), Message},
	receive
		Response -> Response
	end.

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?