Menu
Coddy logo textTech

Final Bank Test

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 25 of 70.

challenge icon

Challenge

Easy

Congratulations on building a complete Digital Bank system! Your Account class now handles deposits, withdrawals, balance checks, account identification, and transfers between accounts. Let's put it all to the test with a realistic multi-account banking scenario.

In this final challenge, you'll simulate a small bank with three customers making various transactions. This will verify that all your methods work correctly together and that accounts truly maintain independent state.

Your two files:

  • Account.lua: Your complete Account class with all the functionality you've built throughout this project (constructor with ID, deposit, withdraw, getBalance, __tostring, and transfer).
  • main.lua: A simulation script that creates multiple accounts and runs a series of transactions between them.

You will receive six inputs:

  1. Account 1 ID
  2. Account 2 ID
  3. Account 3 ID
  4. Initial deposit for Account 1
  5. Initial deposit for Account 2
  6. Initial deposit for Account 3

In your main file, simulate the following banking day:

  1. Create three accounts with the given IDs
  2. Deposit the initial amounts into each respective account
  3. Account 1 transfers 100 to Account 2
  4. Account 2 transfers 150 to Account 3
  5. Account 3 transfers 50 to Account 1
  6. Account 1 attempts to transfer 1000 to Account 3 (this should fail)
  7. Print all three accounts in order (Account 1, then 2, then 3)

For example, if the inputs are A001, A002, A003, 500, 300, and 200, the output should be:

Insufficient funds
Account [A001]: Balance = 450
Account [A002]: Balance = 250
Account [A003]: Balance = 300

Let's trace through: Account 1 starts with 500, transfers 100 to Account 2 (now 400), then receives 50 from Account 3 (now 450). Account 2 starts with 300, receives 100 (now 400), transfers 150 to Account 3 (now 250). Account 3 starts with 200, receives 150 (now 350), transfers 50 to Account 1 (now 300). The final transfer of 1000 fails because Account 1 only has 450.

Try it yourself

-- Require the Account module
local Account = require('Account')

-- Read the six inputs
local id1 = io.read()
local id2 = io.read()
local id3 = io.read()
local initialDeposit1 = tonumber(io.read())
local initialDeposit2 = tonumber(io.read())
local initialDeposit3 = tonumber(io.read())

-- Create three accounts with IDs
-- TODO: create account1, account2, account3 using Account:new()

-- Deposit the initial amounts into each respective account
-- TODO: deposit initialDeposit1 into account1, initialDeposit2 into account2, initialDeposit3 into account3

-- Account 1 transfers 100 to Account 2
-- TODO: account1:transfer(account2, 100)

-- Account 2 transfers 150 to Account 3
-- TODO: account2:transfer(account3, 150)

-- Account 3 transfers 50 to Account 1
-- TODO: account3:transfer(account1, 50)

-- Account 1 attempts to transfer 1000 to Account 3 (this should fail)
-- TODO: account1:transfer(account3, 1000)

-- Print all three accounts in order
-- TODO: print account1, account2, account3

All lessons in Object Oriented Programming