Avatar of JaeHyoLee

JaeHyoLee's solution

to Bank Account in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

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.

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 the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Submitting Incomplete Solutions

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

bank-account_spec.lua

local BankAccount = require('bank-account')

describe('bank-ccount', function()
  it('should have a balance of zero after opening a new account', function()
    local account = BankAccount:new()
    assert.equal(0, account:balance())
  end)

  it('should allow deposits', function()
    local account = BankAccount:new()
    account:deposit(45)
    assert.equal(45, account:balance())
  end)

  it('should allow multiple deposits', function()
    local account = BankAccount:new()
    account:deposit(10)
    account:deposit(25)
    assert.equal(35, account:balance())
  end)

  it('should require deposits to be positive', function()
    local account = BankAccount:new()
    assert.has.errors(function()
      account:deposit(0)
    end)
    assert.has.errors(function()
      account:deposit(-1)
    end)
  end)

  it('should allow withdrawals', function()
    local account = BankAccount:new()
    account:deposit(20)
    account:withdraw(10)
    assert.equal(10, account:balance())
  end)

  it('should allow multiple withdrawals', function()
    local account = BankAccount:new()
    account:deposit(100)
    account:withdraw(10)
    account:withdraw(25)
    assert.equal(65, account:balance())
  end)

  it('should require withdrawals to be positive', function()
    local account = BankAccount:new()
    account:deposit(100)
    assert.has.errors(function()
      account:withdraw(0)
    end)
    assert.has.errors(function()
      account:withdraw(-1)
    end)
  end)

  it('should not allow accounts to be overdrawn', function()
    local account = BankAccount:new()
    assert.has.errors(function()
      account:withdraw(1)
    end)
  end)

  it('should allow multiple independent accounts to be created', function()
    local account1 = BankAccount:new()
    local account2 = BankAccount:new()
    account1:deposit(100)
    account2:deposit(42)
    assert.are.equal(100, account1:balance())
    assert.are.equal(42, account2:balance())
  end)

  it('should allow accounts to be closed', function()
    local account = BankAccount:new()
    account:close()
  end)

  it('should now allow deposits to a closed account', function()
    local account = BankAccount:new()
    account:close()
    assert.has.errors(function()
      account:deposit(1)
    end)
  end)

  it('should not allow withdrawals from a closed account', function()
    local account = BankAccount:new()
    account:deposit(10)
    account:close()
    assert.has.errors(function()
      account:withdraw(1)
    end)
  end)
end)
local bank_account = {}
bank_account.__index = bank_account

function bank_account:new(number)
  local o = setmetatable({}, bank_account)
  o.balances = 0
  return o
end

function bank_account:balance()
  return self.balances
end

function bank_account:deposit(money)
  assert(money > 0, 'Invalid deposit')
  assert(not self._closed, 'Account is closed')
  self.balances = self.balances + money
end

function bank_account:withdraw(money)
  assert(money > 0, 'Invalid withdraw')
  assert(not self._closed, 'Account is closed')
  assert(self.balances >= money, 'Overdrawn')
  self.balances = self.balances - money
end

function bank_account:close()
  self._closed = true 
end

return bank_account

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?