In: Computer Science
Code in Java
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:
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:
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:
run:
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
JAVA PROGRAM
/////////////////////CommissionCompensationModel.java//////////////////////////////
package employee.commission;
//vbase class: CommissionCompensationModel
public class CommissionCompensationModel {
private double grossSales;
private double commissionRate;
//parametrized constructor
public CommissionCompensationModel(double
grossSales,double commissionRate){
this.grossSales = grossSales;
this.commissionRate =
commissionRate;
}
//method: earnings
public double earnings(){
return
grossSales*commissionRate;
}
//getters
public double getGrossSales() {
return grossSales;
}
public double getCommissionRate() {
return commissionRate;
}
//toString() method
@Override
public String toString() {
String str = "\nCommission
Compensation with:";
str = str + "\nGross Sales of:
"+grossSales;
str = str + "\nCommission Rate of:
"+commissionRate;
str = str + "\nEarnings:
"+earnings();
return str;
}
}
////////////////////////////////BasePlusCommissionCompensationModel.java///////////////////////////////
package employee.commission;
//class:BasePlusCommissionCompensationModel
//inhertis : CommissionCompensationModel
public class BasePlusCommissionCompensationModel extends
CommissionCompensationModel{
private double baseSalary;
//parametrized constructor
public BasePlusCommissionCompensationModel(double
grossSales,double commissionRate,
double
baseSalary){
super(grossSales,commissionRate);
this.baseSalary = baseSalary;
}
//earnings() method
public double earnings(){
return super.earnings() +
baseSalary;
}
@Override
public String toString() {
String str = "\nBase Plus
Commission Compensation with:";
str = str + "\nGross Sales of:
"+getGrossSales(); //inherited method
str = str + "\nCommission Rate of:
"+getCommissionRate();//inherited method
str = str + "\nBase salary of:
"+baseSalary;
str = str + "\nEarnings:
"+earnings();
return str;
}
}
///////////////////////Employee.java///////////////////////
package employee.commission;
//class : Employee
public class Employee {
//private fields
private String firstName;
private String lastName;
private String socialSecurityNumber;
private CommissionCompensationModel
compensation;
//Parametried constructor
public Employee(String firstName, String lastName,
String socialSecurityNumber,
CommissionCompensationModel commissionModel) {
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber =
socialSecurityNumber;
this.compensation =
commissionModel;
}
//setter for Compensation
public void
setCompensation(CommissionCompensationModel compensation) {
this.compensation =
compensation;
}
//the getters
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
public CommissionCompensationModel
getCompensation() {
return compensation;
}
//method: earnings()
public double earnings(){
return
this.compensation.earnings();
}
//toString method
@Override
public String toString() {
String str = "";
str = str+firstName+"
"+lastName;
str = str+"\n"+"Social Security
Number:"+socialSecurityNumber;
str = str+
compensation.toString();
return str;
}
}
/////////////////////////Main.java////////////////////////
package employee.commission;
public class Main {
public static void main(String[] args){
// 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);
}
}
======================================
OUTPUT
======================================
John Smith
Social Security Number:111-11-1111
Commission Compensation with:
Gross Sales of: 2000.0
Commission Rate of: 0.04
Earnings: 80.0
Sue Jones
Social Security Number:222-22-2222
Base Plus Commission Compensation with:
Gross Sales of: 2000.0
Commission Rate of: 0.05
Base salary of: 600.0
Earnings: 700.0
Earnings for John Smith: 80.00
John Smith
Social Security Number:111-11-1111
Base Plus Commission Compensation with:
Gross Sales of: 4000.0
Commission Rate of: 0.05
Base salary of: 800.0
Earnings: 1000.0
Sue Jones
Social Security Number:222-22-2222
Commission Compensation with:
Gross Sales of: 5000.0
Commission Rate of: 0.04
Earnings: 200.0