Menu
Coddy logo textTech

Deposit Method

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 20 of 70.

challenge icon

Challenge

Easy

Time 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 to self.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:

  1. First deposit amount
  2. Second deposit amount
  3. 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
175

Notice 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