Transfer Funds
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 24 of 70.
Challenge
EasyYour Digital Bank is almost complete! The final major feature is the ability to transfer money between accounts. This is where your object-oriented design really shines—one account object can interact directly with another account object.
You'll add a :transfer(targetAccount, amount) method that moves money from one account to another. Think about what this operation really involves: it's a withdrawal from the source account followed by a deposit into the target account. The beauty is that you can reuse the methods you've already built!
Continue building on your existing files:
Account.lua: Add a:transfer(targetAccount, amount)method to your Account class. This method should attempt to withdraw the amount fromself, and if successful, deposit that amount into thetargetAccount. If the withdrawal fails (insufficient funds), the transfer should not happen. Keep all your existing functionality (constructor with ID, deposit, withdraw, getBalance, and __tostring).main.lua: Create multiple accounts and demonstrate transfers between them, showing how balances change across different account objects.
You will receive five inputs:
- First account's ID
- Second account's ID
- Initial deposit into the first account
- First transfer amount (from account 1 to account 2)
- Second transfer amount (from account 1 to account 2)
In your main file, create two accounts with the given IDs (both starting at balance 0), deposit the initial amount into the first account, then attempt both transfers from the first account to the second. After each transfer attempt, print both accounts (the __tostring will show their current state).
For example, if the inputs are 101, 102, 500, 200, and 400, the output should be:
Account [101]: Balance = 300
Account [102]: Balance = 200
Insufficient funds
Account [101]: Balance = 300
Account [102]: Balance = 200The first account starts with 500 after the deposit. The first transfer of 200 succeeds, leaving account 101 with 300 and account 102 with 200. The second transfer of 400 fails because account 101 only has 300—so both balances remain unchanged.
Try it yourself
-- Require the Account module
local Account = require('Account')
-- Read the five inputs
local id1 = io.read()
local id2 = io.read()
local initialDeposit = tonumber(io.read())
local transfer1 = tonumber(io.read())
local transfer2 = tonumber(io.read())
-- Create two accounts with IDs
local account1 = Account:new(id1)
local account2 = Account:new(id2)
-- Deposit the initial amount into the first account
account1:deposit(initialDeposit)
-- TODO: Attempt first transfer from account1 to account2
-- TODO: Print both accounts after first transfer
-- TODO: Attempt second transfer from account1 to account2
-- TODO: Print both accounts after second transfer
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