Menu
Coddy logo textTech

Recap - Inventory System

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

challenge icon

Challenge

Easy

Let'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 a name and a value (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 additional damage attribute. Override :getInfo() to return "{name}: {value} gold, {damage} damage".
  • Potion.lua: Another specialized item inheriting from Item. Potions have a healAmount attribute. 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:

  1. First item type ("weapon" or "potion")
  2. First item name
  3. First item value (a number)
  4. Second item type ("weapon" or "potion")
  5. Second item name
  6. 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 gold

If 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 gold

Try 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