Recap - Wallet Math
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 30 of 70.
Challenge
EasyLet's build a Wallet class that brings together the operator overloading skills you've learned in this chapter. Your wallet will store an amount of money and support two key operations: combining wallets with + and comparing wallets with <code>< and .
You'll organize your code across two files:
Wallet.lua: Define your Wallet class with anamountfield representing how much money it holds. Include a:new(amount)constructor and implement two metamethods:__add: Adding two wallets creates a new wallet containing the combined total__lt: Comparing wallets determines which holds less money
main.lua: Require your Wallet module, create wallet instances, and demonstrate both operations working together.
You will receive three inputs:
- Amount in the first wallet
- Amount in the second wallet
- Amount in the third wallet
In your main file, create three wallets with the given amounts. Then:
- Add the first and second wallets together to create a combined wallet
- Print the combined wallet's amount
- Compare the combined wallet with the third wallet using <code>< and print the result (
trueorfalse) - Compare the combined wallet with the third wallet using
and print the result (trueorfalse)
For example, if the inputs are 50, 30, and 100, the output should be:
80
true
falseThe first wallet (50) plus the second wallet (30) creates a combined wallet with 80. Since 80 is less than 100, the first comparison is true. Since 80 is not greater than 100, the second comparison is false.
Try it yourself
local Wallet = require('Wallet')
-- Read input
local amount1 = tonumber(io.read())
local amount2 = tonumber(io.read())
local amount3 = tonumber(io.read())
-- TODO: Create three wallets with the given amounts
-- TODO: Add the first and second wallets together to create a combined wallet
-- TODO: Print the combined wallet's amount
-- TODO: Compare the combined wallet with the third wallet using < and print the result
-- TODO: Compare the combined wallet with the third wallet using > and print the result
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