Account Info
Part of the Object Oriented Programming section of Coddy's Lua journey. Lesson 23 of 70.
Challenge
EasyYour Digital Bank is becoming more professional! Now let's make your accounts display themselves nicely when printed. By adding a __tostring metamethod, you can control exactly how an account appears—showing both an account ID and the current balance in a clean, readable format.
You'll continue building on your existing files:
Account.lua: Extend your Account class with two new features:- Modify the
:new()constructor to accept anidparameter that identifies each account - Add a
__tostringmetamethod that returns a formatted string showing the account's information
- Modify the
main.lua: Create accounts with IDs and demonstrate how they display themselves when printed.
The __tostring method should return a string in this exact format:
Account [ID]: Balance = BALANCEWhere ID is the account's ID and BALANCE is the current balance.
You will receive four inputs:
- First account's ID
- First account's deposit amount
- Second account's ID
- Second account's deposit amount
In your main file, create two accounts with the given IDs, deposit the respective amounts into each, then print each account directly using print(). The __tostring metamethod will automatically be called when you print the account object.
For example, if the inputs are 1001, 500, 1002, and 750, the output should be:
Account [1001]: Balance = 500
Account [1002]: Balance = 750Each account now has a unique identity and can describe itself clearly—a much more professional banking experience!
Try it yourself
-- Require the Account module
local Account = require('Account')
-- Read the four inputs
local id1 = io.read()
local deposit1 = tonumber(io.read())
local id2 = io.read()
local deposit2 = tonumber(io.read())
-- Create two accounts with IDs
local account1 = Account:new(id1)
local account2 = Account:new(id2)
-- Deposit the respective amounts
account1:deposit(deposit1)
account2:deposit(deposit2)
-- Print each account (uses __tostring)
print(account1)
print(account2)
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 ClassCircle ClassPerimeter MethodShape CollectionTotal AreaFilter Shapes2Class 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 HierarchyPractice on your own: Online Lua compiler