In: Computer Science
Assignment 3 - Enhanced Employee Hierarchy
For this assignment, you are going to enhance the Employee Hierarchy that you created in Java in Programming Assignment 2 by adding an interface called Compensation with the following two methods:
Create the abstract class CompensationModel which implements the Compensation interface. Create the following classes as subclasses of CompensationModel:
Each of these classes will also have a toString() method to display their compensation information as illustrated in the sample output below.
Modify the Employee class of Assignment 2 to have an instance variable of type CompensationModel instead of CommissionCompensationModel, Make any other necessary changes in the Employee class because of this. Also add the raise (double percent) method to the Employee class which simply calls the raise method of the CompensationModel.
Use the following code in your main function to test your classes, just copy and paste it into your main method:
// Create the four
employees with their compensation models.
CommissionCompensationModel commissionModel = new
CommissionCompensationModel(2000.00, 0.04);
BasePlusCommissionCompensationModel basePlusCommissionModel = new
BasePlusCommissionCompensationModel(2000.00, 0.05, 600.00);
SalariedCompensationModel salariedCompensationModel = new
SalariedCompensationModel(2500.00);
HourlyCompensationModel
hourlyCommissionModel = new HourlyCompensationModel(10.00,
35.0);
Employee employee1 = new
Employee("John", "Smith", "111-11-1111", commissionModel);
Employee employee2 = new
Employee("Sue", "Jones", "222-22-2222",
basePlusCommissionModel);
Employee employee3 = new
Employee("Jim", "Williams", "333-33-3333",
salariedCompensationModel);
Employee employee4 = new
Employee("Nancy", "Johnson", "444-44-4444",
hourlyCommissionModel);
// Print the information
about the four employees.
System.out.println("The
employee information initially.");
System.out.printf("%s%n%s%n%s%n%s%n", employee1, employee2,
employee3, employee4);
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 four employees.
CommissionCompensationModel commissionModelNew = new
CommissionCompensationModel(5000.00, 0.04);
BasePlusCommissionCompensationModel basePlusCommissionModelNew =
new BasePlusCommissionCompensationModel(4000.00, 0.05,
800.00);
SalariedCompensationModel salariedCompensationModelNew = new
SalariedCompensationModel(3500.00);
HourlyCompensationModel
hourlyCommissionModeNewl = new HourlyCompensationModel(10.00,
50);
// Set the new
compensation models for the employees.
employee1.setCompensation(basePlusCommissionModelNew);
employee2.setCompensation(commissionModelNew);
employee3.setCompensation(hourlyCommissionModeNewl);
employee4.setCompensation(salariedCompensationModelNew);
// Print out the new
information for the four employees.
System.out.println("The
employee information after changing compensation models.");
System.out.printf("%s%n%s%n%s%n%s%n", employee1, employee2,
employee3, employee4);
// Declare an array of
employees and assign the four employees to it.
Employee[] employees =
new Employee[4];
employees[0] =
employee1;
employees[1] =
employee2;
employees[2] =
employee3;
employees[3] =
employee4;
// Loop thru the array
giving each employee a 2% raise polymorphically;
for (Employee employee :
employees)
{
employee.raise(.02);
}
// Print out their new
earnings.
System.out.println("The
employee information after raises of 2 percent.");
System.out.printf("%s%n%s%n%s%n%s%n", employee1, employee2,
employee3, employee4);
The output from your program should look like the following (there will be additional blank lines in the output which canvas removes for me, unwanted, in this display):
run:
The employee information initially.
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
Jim Williams
Social Security Number: 333-33-3333
Salaried Compensation with:
Weekly Salary of: 2500.00
Earnings: 2500.00
Nancy Johnson
Social Security Number: 444-44-4444
Hourly Compensation with:
Wage of: 10.00
Hours Worked of:35.00
Earnings: 350.00
Earnings for John Smith: 80.00
The employee information after changing compensation
models.
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
Jim Williams
Social Security Number: 333-33-3333
Hourly Compensation with:
Wage of: 10.00
Hours Worked of:50.00
Earnings: 550.00
Nancy Johnson
Social Security Number: 444-44-4444
Salaried Compensation with:
Weekly Salary of: 3500.00
Earnings: 3500.00
The employee information after raises of 2 percent.
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: 816.00
Earnings: 1016.00
Sue Jones
Social Security Number: 222-22-2222
Commission Compensation with:
Gross Sales of: 5000.00
Commission Rate of: 0.04
Earnings: 204.00
Jim Williams
Social Security Number: 333-33-3333
Hourly Compensation with:
Wage of: 10.20
Hours Worked of:50.00
Earnings: 561.00
Nancy Johnson
Social Security Number: 444-44-4444
Salaried Compensation with:
Weekly Salary of: 3570.00
Earnings: 3570.00
Assignment 2 Code:
Images are on this link due to the images being too small: https://imgur.com/a/4a5z1S4 (Can be zoomed in by clicking on the image
Compensation Interface:
public interface Compensation
{
double earnings();
void raise(double percent);
}
CompensationModel Abstract class:
abstract class CompensationModel implements Compensation
{
}
SalariedCompensationModel :
public class SalariedCompensationModel extends
CompensationModel
{
public SalariedCompensationModel()
{
this.weeklySalary=0.0;
}
public SalariedCompensationModel(double weeklySalary)
{
this.weeklySalary=weeklySalary;
}
private double weeklySalary;
public void setWeeklySalary(double weeklySalary)
{
this.weeklySalary=weeklySalary;
}
public double earnings()
{
return this.weeklySalary;
}
public void raise(double percent)
{
this.weeklySalary=((this.weeklySalary*(100+percent))/100);
}
}
HourlyCompensationModel :
public class HourlyCompensationModel extends
CompensationModel
{
private double hours;
private double wage;
private static final double workingHours=40.00;
public HourlyCompensationModel()
{
}
public HourlyCompensationModel(double hours,double wage)
{
this.hours=hours;
this.wage=wage;
}
public void setHours(double hours)
{
this.hours=hours;
}
public double earnings()
{
double earnings;
if(hours>workingHours)
{
earnings=(workingHours*wage)+((hours-workingHours)*(wage*1.5));
}
else
{
earnings=hours*wage;
}
return earnings;
}
public void raise(double percent)
{
this.wage=((this.wage*(100+percent))/100);
}
}
CommissionCompensationModel :
As you have not provided your Assignment 2 implemented class so
I am unable to write the implementation of this class.
But According to your assignment, there is no major change in this
class.
You need to extend your CommissionCompensationClass by
CompensationModel class using extends keyword as done in
other sub classes of CompensationModel class.
You also need to declare a method raise as implemented below
public void raise(double percent)
{
this.commissionRate=((this.commissionRate*(100+percent))/100);
//I am writing the above implementation by assuming class
CommissionCompensationModel has an instance variable called
commissionRate.
}
BasePlusCommissionCompensationModel :
public class BasePlusCommissionCompensationModel extends
CommissionCompensationModel
{
private double baseSalary;
// This class is also implemented in your assignement 2 so you have
to declare the class as shown above and add the
// implementation of raise method
public void raise(double percent)
{
this.baseSalary=((this.baseSalary*(100+percent))/100);
// Assuming baseSalary is an instance variable of this class.
}
}
Employee :
// Add the below implementation of Employee class in your
assignment 2 implementation of Employee class
public class Employee
{
private CompensationModel compensationModel;
// setter Method for CompensationModel attribute
public void setCompensationModel(CompensationModel
compensationModel)
{
this.compensationModel=compensationModel;
}
public void raise(double percent)
{
this.compensationModel.raise(percent);
}
}