Menu
CoddyTech

Recap - Employee Hierarchy

Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 20 of 65.

challenge icon

Challenge

Medium

Build a company's class hierarchy across five files.

  • Person: person_name and age, set by the constructor. introduce() returns Hi, I'm NAME, AGE years old.
  • Employee extends Person and adds employee_id (String) and salary (yearly, int). Override introduce() to add My employee ID is ID. after the parent's text. monthly_pay() returns the salary divided by 12.0.
  • Manager extends Employee and adds department. Its monthly_pay() is the employee pay times 1.2, and manage_team() returns Managing the DEPARTMENT department.
  • Engineer extends Employee and adds language. code() returns Coding in LANGUAGE.
  • Payroll has two static functions. role(p) returns Manager, Engineer, Employee or Person, the most specific class of p. total_monthly(people) returns the sum of monthly_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.00

Your 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

Practice on your own: Online GDScript compiler