Menu
Coddy logo textTech

Recap - Employee Roles

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

challenge icon

Challenge

Easy

Let's build a payroll bonus system that demonstrates how different employee types can share the same interface while calculating bonuses differently!

You'll create a small employee hierarchy across four files:

  • Employee.lua: Your base class representing a generic employee. The constructor :new(name, salary) should store both the employee's name and their salary. Include a :calculateBonus() method that returns 10% of the salary (the default bonus for regular employees), and a :getName() method to retrieve the name.
  • Manager.lua: A child class that inherits from Employee. Managers work hard and get rewarded—override :calculateBonus() to return 20% of their salary instead of the default 10%.
  • Intern.lua: Another child class inheriting from Employee. Interns receive a fixed bonus of 50 regardless of their salary—override :calculateBonus() to always return this amount.
  • main.lua: Bring everything together! Read three inputs for employee names and three inputs for their salaries. Create one Manager, one Intern, and one regular Employee. Store all three in a table, then loop through and print each person's name followed by their calculated bonus.

You will receive six inputs:

  1. Manager's name
  2. Manager's salary
  3. Intern's name
  4. Intern's salary
  5. Regular employee's name
  6. Regular employee's salary

For each employee in your collection (in the order: Manager, Intern, Employee), print their name and bonus in this format:

{name}: {bonus}

For example, if the inputs are Alice, 5000, Bob, 2000, Carol, and 3000, the output should be:

Alice: 1000
Bob: 50
Carol: 300

Notice how the same :calculateBonus() call produces different results based on each employee's type—that's polymorphism at work! The Manager gets 20% of 5000, the Intern gets a flat 50, and the regular Employee gets 10% of 3000.

Try it yourself

-- main.lua: Bring everything together

local Employee = require('Employee')
local Manager = require('Manager')
local Intern = require('Intern')

-- Read inputs
local managerName = io.read()
local managerSalary = tonumber(io.read())
local internName = io.read()
local internSalary = tonumber(io.read())
local employeeName = io.read()
local employeeSalary = tonumber(io.read())

-- TODO: Create one Manager, one Intern, and one regular Employee

-- TODO: Store all three in a table (in order: Manager, Intern, Employee)

-- TODO: Loop through the table and print each person's name and bonus
-- Format: {name}: {bonus}

All lessons in Object Oriented Programming