Recap - Employee Hierarchy
Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 20 of 65.
Challenge
MediumBuild a company's class hierarchy across five files.
Person:person_nameandage, set by the constructor.introduce()returnsHi, I'm NAME, AGE years old.EmployeeextendsPersonand addsemployee_id(String) andsalary(yearly, int). Overrideintroduce()to addMy employee ID is ID.after the parent's text.monthly_pay()returns the salary divided by12.0.ManagerextendsEmployeeand addsdepartment. Itsmonthly_pay()is the employee pay times1.2, andmanage_team()returnsManaging the DEPARTMENT department.EngineerextendsEmployeeand addslanguage.code()returnsCoding in LANGUAGE.Payrollhas two static functions.role(p)returnsManager,Engineer,EmployeeorPerson, the most specific class ofp.total_monthly(people)returns the sum ofmonthly_pay()over the people that are employees (managers and engineers included).
Every constructor passes the parent's values to super(), and every override that extends the parent's result uses super().
The supplied code reads how many people follow, then one line each: person NAME AGE, employee NAME AGE ID SALARY, manager NAME AGE ID SALARY DEPARTMENT or engineer NAME AGE ID SALARY LANGUAGE. For each person it prints the role in brackets and introduce(), the monthly pay for employees, manage_team() or code() where they exist, and at the end the total. For employee Alice 35 E123 60000 and manager Bob 45 M54 85000 Marketing it prints:
[Employee] Hi, I'm Alice, 35 years old. My employee ID is E123.
Monthly pay: 5000.00
[Manager] Hi, I'm Bob, 45 years old. My employee ID is M54.
Monthly pay: 8500.00
Managing the Marketing department.
Total monthly payroll: 13500.00Your code goes in person.gd, employee.gd, manager.gd, engineer.gd and payroll.gd. main.gd holds the supplied input/output code and cannot be edited.
Try it yourself
extends Node
func _ready():
# Supplied input/output code: keep it as it is
var count := int(OS.read_string_from_stdin())
var people: Array[Person] = []
for i in count:
var p := OS.read_string_from_stdin().split(" ")
if p[0] == "manager":
people.append(Manager.new(p[1], int(p[2]), p[3], int(p[4]), p[5]))
elif p[0] == "engineer":
people.append(Engineer.new(p[1], int(p[2]), p[3], int(p[4]), p[5]))
elif p[0] == "employee":
people.append(Employee.new(p[1], int(p[2]), p[3], int(p[4])))
else:
people.append(Person.new(p[1], int(p[2])))
for person in people:
print("[%s] %s" % [Payroll.role(person), person.introduce()])
if person is Employee:
print("Monthly pay: %.2f" % (person as Employee).monthly_pay())
if person is Manager:
print((person as Manager).manage_team())
elif person is Engineer:
print((person as Engineer).code())
print("Total monthly payroll: %.2f" % Payroll.total_monthly(people))
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self KeywordMethodsPropertiesConstructor (_init)Recap - Simple Calculator4Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator2Class Properties
Instance vs Static VariablesStatic FunctionsProperty SettersProperty GettersPrivate by ConventionRecap - Bank Account Manager5Encapsulation
Public and Private MembersValidation with SettersInformation HidingRead-Only PropertiesRecap - Student Records System8Advanced OOP Concepts
Composition vs InheritanceInner ClassesNodes as ComponentsRefCounted vs NodeEnums in Classes11Project: Library Management
Project OverviewBook and User Classes3Inheritance
Basic InheritanceThe super KeywordMethod OverridingInheritance ChainsType Checks with isRecap - Employee Hierarchy6Special Methods
Virtual Methods IntroductionThe _to_string MethodCustom IteratorsRecap - Custom List9Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy PatternPractice on your own: Online GDScript compiler