Withdraw Method
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 21 of 70.
Challenge
EasyYour Digital Bank is growing! Accounts can now receive deposits, but a real bank also needs to let customers take money out. Let's add a :withdraw(amount) method—but with an important safety check.
Unlike deposits, withdrawals can fail. If a customer tries to withdraw more money than they have, the bank shouldn't allow it. Your withdraw method needs to check if sufficient funds exist before deducting the amount.
You'll continue working with your two files:
Account.lua: Add a:withdraw(amount)method to your existing Account class. If the account has enough balance, subtract the amount and returntrue. If there aren't sufficient funds, printInsufficient fundsand returnfalsewithout changing the balance.main.lua: Create an account, make a deposit to give it some funds, then attempt several withdrawals to see both successful and failed transactions.
You will receive four inputs:
- Initial deposit amount
- First withdrawal amount
- Second withdrawal amount
- Third withdrawal amount
In your main file, create an account, deposit the initial amount, then attempt each withdrawal in order. After each withdrawal attempt, print the current balance on a new line. Remember that failed withdrawals will also print Insufficient funds before you print the balance.
For example, if the inputs are 100, 30, 50, and 40, the output should be:
70
20
Insufficient funds
20The account starts with 100 after the deposit. The first withdrawal of 30 succeeds (balance: 70), the second withdrawal of 50 succeeds (balance: 20), but the third withdrawal of 40 fails because only 20 remains—so the balance stays at 20.
Try it yourself
-- Require the Account module
local Account = require('Account')
-- Read the four inputs
local initialDeposit = tonumber(io.read())
local withdrawal1 = tonumber(io.read())
local withdrawal2 = tonumber(io.read())
local withdrawal3 = tonumber(io.read())
-- Create a single account
local account = Account:new()
-- Deposit the initial amount
account:deposit(initialDeposit)
-- Attempt first withdrawal and print balance
-- TODO: call account:withdraw(withdrawal1) here
print(account.balance)
-- Attempt second withdrawal and print balance
-- TODO: call account:withdraw(withdrawal2) here
print(account.balance)
-- Attempt third withdrawal and print balance
-- TODO: call account:withdraw(withdrawal3) here
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