Final Bank Test
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 25 of 70.
Challenge
EasyCongratulations 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:
- Account 1 ID
- Account 2 ID
- Account 3 ID
- Initial deposit for Account 1
- Initial deposit for Account 2
- Initial deposit for Account 3
In your main file, simulate the following banking day:
- Create three accounts with the given IDs
- Deposit the initial amounts into each respective account
- Account 1 transfers 100 to Account 2
- Account 2 transfers 150 to Account 3
- Account 3 transfers 50 to Account 1
- Account 1 attempts to transfer 1000 to Account 3 (this should fail)
- 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 = 300Let'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
1The 'Self' Concept
Tables with FunctionsExplicit 'self'The Colon SyntaxDot vs ColonRecap - Moving Point4Project: Digital Bank
Project SetupDeposit Method7Polymorphism & Overriding
Overriding MethodsCalling Parent MethodsDuck TypingCommon InterfaceChecking TypeRecap - Employee Roles10Project: Shape Manager
Project SetupRectangle Class2Class Prototype Pattern
The Prototype ConceptLinking with __indexThe :new() ConstructorInitializing AttributesIndependent InstancesRecap - Car Factory5Operator Overloading in OOP
Adding ObjectsSubtracting ObjectsConcatenating ObjectsComparing Objects (<, >)Recap - Wallet Math8Encapsulation
Naming ConventionsClosures for PrivacyAccess via ClosuresRead-Only TablesValidation LogicRecap - Secure Vault11Design Patterns (Lite)
Factory FunctionsSingleton TableIterator PatternObserver (Listener)Recap - Logger Factory3Object State and Behavior
Instance VariablesGetter MethodsSetter MethodsCalculated PropertiesFormatting StringsEquality ChecksRecap - Student Grade6Inheritance Basics
The Inheritance SetupInheriting MethodsExtending the ConstructorAdding Child MethodsShared vs UniqueRecap - Shape Hierarchy