Recap - Employee Roles
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 42 of 70.
Challenge
EasyLet'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 of50regardless 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:
- Manager's name
- Manager's salary
- Intern's name
- Intern's salary
- Regular employee's name
- 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: 300Notice 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
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