Project Setup
Part of the Object Oriented Programming section of Coddy's Lua journey. Lesson 19 of 70.
Challenge
EasyWe are starting the Digital Bank project. Over the next lessons you will build an Account class that can receive deposits, handle withdrawals, print itself and transfer money to other accounts. Everything starts with a clean file structure and a constructor.
Your two files:
Account.lua: define theAccounttable, setAccount.__index = Accountso instances find their methods, and write the:new()constructor. It creates an empty table, sets its metatable toAccount, gives it abalanceof0and returns it. The file ends withreturn Account.main.lua: require the module withrequire('Account'), create two accounts withAccount:new(), print the balance of each one on its own line, then printsavings == checkingto show that the two calls produced two different tables.
Expected output:
0
0
falseEvery account starts at zero. In the next lesson you will add the :deposit(amount) method that makes the balance grow.
Try it yourself
-- TODO: require the Account module (the file is Account.lua)
local Account = nil
-- TODO: create two accounts with Account:new()
-- TODO: print the balance of each account (one per line)
-- TODO: print whether the two accounts are the same table (they should not be)
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