Computer Science Homework Help

CMPSC 221 Pennsylvania State University Main Campus Employee Hierarchy Project

 

In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object.

A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee’s compensation:

  • CommissionCompensationModel – For Employees who are paid by commission, the CommissionCompensationModel class would contain grossSales and commissionRate instance variables, and would define an earnings method to return grossSales * commissionRate.
  • BasePlusCommissionCompensationModel – For Employees who are paid a base salary and commission, this subclass of CommissionCompensationModel would contain an instance variable of baseSalary and would define the earnings method to return super.earnings() + baseSalary.

Each of these classes would contain a toString() method that displays the Compensation Model information as illustrated in the sample output.

This approach is more flexible than our original hierarchy. For example, consider an Employee who gets promoted. With the approach described here, you can simply change that Employee’s CompensationModel by assigning the composed CompensationModel reference an appropriate subclass object. With the CommissionEmployee – BasePlusCommissionEmployee hierarchy, you’d need to change the Employee’s type by creating a new object of the appropriate class and moving data from the old object to the new one.

Implement the Employee class and CompensationModel hierarchy discussed in this exercise. In addition to the firstName, lastName, socialSecurityNumber and CommisionCompensationModel instance variables, class Employee should provide:

  • A constructor that receives three Strings and a CommissionCompensationModel to initialize the instance variables.
  • A set method that allows the client code to change an Employee’s CompensationModel.
  • An earnings method that calls the CompensationModel’s earning method and returns the result.
  • A toString() method that displays all the information about the Employee as illustrated in the sample output.

Your code in the subclasses should call methods in the super classes whenever possible to reduce the amount of code in the subclasses and utilize the code already developed in the super classes as in the code demonstrated in Figures 9.10 and 9.11 in the book.

Use the following code in your main function to test your classes, just copy and paste it into your main method:

// Create the two employees with their compensation models.

CommissionCompensationModel commissionModel = new CommissionCompensationModel(2000.00, 0.04);
BasePlusCommissionCompensationModel basePlusCommissionModel = new BasePlusCommissionCompensationModel(2000.00, 0.05, 600.00);

Employee employee1 = new Employee(“John”, “Smith”, “111-11-1111”, commissionModel);
Employee employee2 = new Employee(“Sue”, “Jones”, “222-22-2222”, basePlusCommissionModel);

System.out.printf(“%s%n%s%n”, employee1, employee2);
System.out.printf(“%s%s%s%s%s%8.2f%n%n”, “Earnings for “, employee1.getFirstName(), ” “, employee1.getLastName(), “: “, employee1.earnings());

// Change the compensation model for the two employees.

CommissionCompensationModel commissionModelNew = new CommissionCompensationModel(5000.00, 0.04);
BasePlusCommissionCompensationModel basePlusCommissionModelNew = new BasePlusCommissionCompensationModel(4000.00, 0.05, 800.00);

// Set the new compensation models for the employees.
employee1.setCompensation(basePlusCommissionModelNew);
employee2.setCompensation(commissionModelNew);

// Print out the new information for the two employees.
System.out.printf(“%s%n%s%n”, employee1, employee2);

The output from your program should look like the following:

John Smith
Social Security Number: 111-11-1111
Commission Compensation with:
Gross Sales of: 2000.00
Commission Rate of: 0.04
Earnings: 80.00

Sue Jones

Social Security Number: 222-22-2222
Base Plus Commission Compensation with:
Gross Sales of: 2000.00
Commission Rate of: 0.05
Base Salary of: 600.00
Earnings: 700.00

Earnings for John Smith: 80.00

John Smith
Social Security Number: 111-11-1111
Base Plus Commission Compensation with:
Gross Sales of: 4000.00
Commission Rate of: 0.05
Base Salary of: 800.00
Earnings: 1000.00

Sue Jones
Social Security Number: 222-22-2222
Commission Compensation with:
Gross Sales of: 5000.00
Commission Rate of: 0.04
Earnings: 200.00