Menu
Coddy logo textTech

Project Setup

Part of the Object Oriented Programming section of Coddy's Lua journey. Lesson 19 of 70.

challenge icon

Challenge

Easy

We 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 the Account table, set Account.__index = Account so instances find their methods, and write the :new() constructor. It creates an empty table, sets its metatable to Account, gives it a balance of 0 and returns it. The file ends with return Account.
  • main.lua: require the module with require('Account'), create two accounts with Account:new(), print the balance of each one on its own line, then print savings == checking to show that the two calls produced two different tables.

Expected output:

0
0
false

Every 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

Practice on your own: Online Lua compiler