Start learning free

LESSON 651 OF 671 · OBJECT-ORIENTED PROGRAMMING - CHALLENGES

Employees and Managers

Practice O(1)

A company pays every employee a salary. A manager is an employee too, but earns a bonus on top of the salary. Go has no classes and no extends: as in the lesson Inheritance, Manager embeds Employee, so it gets the employee's fields and methods and changes only how its pay is worked out.

Write the struct Employee:

  • its fields name and salary, and the constructor NewEmployee(name, salary);
  • getName() returns the name;
  • pay() returns the salary.

the struct Manager, which embeds Employee:

  • the embedded Employee and a field bonus, and the constructor NewManager(name, salary, bonus), which builds the embedded Employee with NewEmployee;
  • its own pay(), which returns the embedded employee's pay plus the bonus. Call the parent's version as in the lesson Calling the Parent Method, this.Employee.pay(): it is what knows how an employee is paid;
  • getName() is not written again: it is promoted from the embedded Employee, unchanged.

and the function describe(w Worker), which returns the text <name> earns <pay> for any Worker, where the amount is what w.pay() returns, for example Ana earns 3000. Worker is an interface with the methods getName() and pay(); both structs satisfy it.

Why describe is a function in Go. In Java or Python, describe would be a method of Employee that a manager inherits, and its call to pay() would run the manager's pay. Go's embedding works differently: inside a method of Employee, this is the embedded Employee, so this.pay() is always Employee's pay, even when that employee sits inside a manager (the lesson Method Overriding: the shadowing goes by the static type). A function that takes the interface calls the pay of the value it is given: Manager's own pay for a manager.

The input is a list of people, one per line: employee name salary or manager name salary bonus. For each line, the code that tests your structs creates that value with NewEmployee or NewManager and prints what describe returns for it, on a line of its own.

 

Example 1:

Input:
    employee Ana 3000
    manager Bob 4000 1000
Output:
    Ana earns 3000
    Bob earns 5000
Explanation:
Ana's pay is her salary. Bob's pay is the embedded employee's pay, his salary 4000, plus his bonus 1000.
describe gets a Manager, so w.pay() runs Manager's pay.

Example 2:

Input:
    manager Cleo 5000 0
    employee Dan 5000
Output:
    Cleo earns 5000
    Dan earns 5000
Explanation: A manager whose bonus is 0 earns exactly the salary, like an employee.

Example 3:

Input:
    manager Eve 7000 2500
    employee Finn 2800
    employee Gus 3100
    manager Hana 6000 500
Output:
    Eve earns 9500
    Finn earns 2800
    Gus earns 3100
    Hana earns 6500
Explanation: Employees and managers come in any order, and each value keeps its own name and numbers.

 

Constraints:

  • 0 <= salary <= 106
  • 0 <= bonus <= 106
  • A name is one word of 1 to 20 English letters. Two people may have the same name.
  • At most 100 people per test case.
STEP 1 OF 10
    Ctrl + Enter to check