Recap - Inventory System
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 67 of 70.
Challenge
EasyLet's build a complete inventory management system for a role-playing game! You'll create a hierarchy of item types and a manager class to organize them—bringing together inheritance, polymorphism, and composition in one cohesive project.
You'll organize your code across five files:
Item.lua: The base class for all items. Each item has anameand avalue(in gold coins). Include a:getInfo()method that returns a string in the format"{name}: {value} gold".Weapon.lua: A specialized item that inherits from Item. Weapons have an additionaldamageattribute. Override:getInfo()to return"{name}: {value} gold, {damage} damage".Potion.lua: Another specialized item inheriting from Item. Potions have ahealAmountattribute. Override:getInfo()to return"{name}: {value} gold, heals {healAmount} HP".Inventory.lua: A manager class that stores a collection of items. Include an:addItem(item)method to add any type of item, a:listItems()method that prints:getInfo()for each item (one per line), and a:getTotalValue()method that returns the sum of all item values.main.lua: Bring everything together! Create an inventory and populate it with items based on the inputs you receive. Then list all items and print the total inventory value on the last line.
The beauty of this design is polymorphism in action—your Inventory doesn't need to know whether it's storing a Weapon or a Potion. It just calls :getInfo() and each item responds appropriately based on its type.
You will receive six inputs describing two items to add:
- First item type (
"weapon"or"potion") - First item name
- First item value (a number)
- Second item type (
"weapon"or"potion") - Second item name
- Second item value (a number)
For weapons, use a fixed damage of 25. For potions, use a fixed heal amount of 50.
After adding both items, call :listItems() to print each item's info, then print Total: {value} gold on the final line.
For example, if the inputs are weapon, Iron Sword, 150, potion, Health Elixir, and 75, the output should be:
Iron Sword: 150 gold, 25 damage
Health Elixir: 75 gold, heals 50 HP
Total: 225 goldIf the inputs are potion, Mana Potion, 50, weapon, Steel Axe, and 200, the output should be:
Mana Potion: 50 gold, heals 50 HP
Steel Axe: 200 gold, 25 damage
Total: 250 goldTry it yourself
-- main.lua: Bring everything together
local Weapon = require('Weapon')
local Potion = require('Potion')
local Inventory = require('Inventory')
-- Read inputs for first item
local type1 = io.read()
local name1 = io.read()
local value1 = tonumber(io.read())
-- Read inputs for second item
local type2 = io.read()
local name2 = io.read()
local value2 = tonumber(io.read())
-- Fixed values: weapons have 25 damage, potions heal 50 HP
-- TODO: Create an inventory
-- TODO: Create first item based on type1 ("weapon" or "potion")
-- and add it to the inventory
-- TODO: Create second item based on type2 ("weapon" or "potion")
-- and add it to the inventory
-- TODO: Call listItems() to print each item's info
-- TODO: Print "Total: {value} gold" using getTotalValue()
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