Deposit Method
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 20 of 70.
Challenge
EasyTime to add functionality to your Digital Bank! Now that accounts can be created with a starting balance of 0, let's give them the ability to receive money through deposits.
Building on your existing Account class, you'll add a :deposit(amount) method that increases the account's balance by the specified amount. This is a fundamental banking operation—money goes in, and the balance grows!
Your two files:
Account.lua: Extend your Account class by adding a:deposit(amount)method. This method should add the given amount toself.balance. Keep everything from the previous lesson (the class structure and:new()constructor).main.lua: Require your Account module, create an account, make some deposits, and show how the balance changes.
You will receive three inputs:
- First deposit amount
- Second deposit amount
- Third deposit amount
In your main file, create a single account and perform the three deposits in order. After each deposit, print the current balance on a new line.
For example, if the inputs are 100, 50, and 25, the output should be:
100
150
175Notice how each deposit builds on the previous balance—the account starts at 0, then grows to 100 after the first deposit, 150 after the second, and 175 after the third. Your :deposit() method makes this accumulation happen!
Try it yourself
-- Require the Account module
local Account = require('Account')
-- Read the three deposit amounts
local deposit1 = tonumber(io.read())
local deposit2 = tonumber(io.read())
local deposit3 = tonumber(io.read())
-- Create a single account
local account = Account:new()
-- Perform the three deposits and print balance after each
-- TODO: deposit deposit1 and print account.balance
-- TODO: deposit deposit2 and print account.balance
-- TODO: deposit deposit3 and print account.balance
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