akoven / Manager_Employee

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Long Practice: Class Syntax with Employees and Managers

In this practice, you will create and use classes, inheritance, polymorphism, and commonJS module importing/exporting!

Learning Goals

  • Understand how a child class inherits from a parent class
  • Know how to override a parent class's method
  • Know when and how to use super and extends

Set up

Clone the starter from the Download link at the bottom of this page.

Phase 1: Define Employee and Manager classes

In the starter project directory, set up two files, employee.js and manager.js, for two classes, Employee and Manager.

In the employee.js file, define an Employee class with a constructor that sets an Employee's name, title, salary, and boss.

In the manager.js file, define another class, Manager, as a child class of the Employee class. Manager should have all of the same properties as Employee, and an attribute that holds an array of all Employee's assigned to the Manager.

Note: A Manager may be under another Manager because Managers might report to higher-level Managers.

Reminder: Use CommonJS modules to get the Employee class into the manager.js file.

Phase 2: Dynamically adding employees

Define a method, addEmployee(employee) in the Manager class. This method should add an employee into the Manager's array of employees. Please test this method in the manager.js file.

Now that we have an addEmployee method, think about a way to call it such that whenever an employee is initialized with a manager, the employee is automatically added to the manager's employees array. Please test this using the code below in the manager.js file.

Hint: The method addEmployee must be called in the constructor (yes the constructor) of the Employee class.

let annie = new Manager('Annie', 100000, 'Director')
let alvy = new Employee('Alvy', 75000, 'Analyst', annie)

console.log(annie)

You should get this output:

<ref *1> Manager {
  name: 'Annie',
  salary: 100000,
  title: 'Director',
  manager: null,
  employees: [
    Employee {
      name: 'Alvy',
      salary: 75000,
      title: 'Analyst',
      manager: [Circular *1]
    }
  ]
}

Phase 3: Bonus

Define a method, bonus(multiplier) in the Employee class. Employees who are not a Manager should calculate their bonus like this:

bonus = (employee salary) * multiplier

A Manager should calculate their bonus based on the total salary of all of their Employees, as well as the Manager's Employees' Employees, and the Employees' Employees' Employees, etc. Manager employees should calculate their bonus like this:

bonus = (manager's salary + total salary of all employees under them) * multiplier
Hint: You can extract the logic of calculating the total salary of all a Manager's Employees into a helper method which iterates through each of a Manager's Employees, checking if the Employee is an instance of a Manager or not. If the Employee is a Manager, and add the sum of their Employees' salaries to the total salary. If the Employee is not a Manager, add the Employee's salary to the total salary. This function will require recursion.

Phase 4: Testing

Imagine you have a company structured like this:

Name Salary Title Boss
Hobbes 1000000 Founder null
Calvin 130000 Director Hobbes
Susie 100000 TA Manager Calvin
Lily 90000 TA Susie
Clifford 90000 TA Susie

If Hobbes gets a bonus multiplier of 0.05, their bonus will be $70,500.

If Calvin gets a bonus multiplier of 0.05, their bonus will be $20,500.

If Susie gets a bonus multiplier of 0.05, their bonus will be $14,000.

If Lily gets a bonus multiplier of 0.05, their bonus will be $4,500.

If Clifford gets a bonus multiplier of 0.05, their bonus will be $4,500.

Create a new file called test.js. Create the scenario above and make sure you get the correct bonuses for each employee.

About


Languages

Language:JavaScript 100.0%