-- Chapter 30 example: a BankAccount whose methods guard the balance. local Account = {} Account.__index = Account function Account.new(owner) return setmetatable({ owner = owner, balance = 0 }, Account) end function Account:deposit(amount) if amount > 0 then self.balance = self.balance + amount end end function Account:withdraw(amount) if amount > 0 and amount <= self.balance then self.balance = self.balance - amount return true end return false end function Account:getBalance() return self.balance end local acc = Account.new("Keiko") acc:deposit(100) acc:withdraw(30) print(acc:getBalance()) -- 70 -- TODO: add a :canAfford(amount) method that returns true when the -- balance is at least amount, and use it before withdrawing.