(Accounts Payable System Modification) In this exercise, we modify the accounts payable application of Figs. 10.11–10.15 to include the complete functionality of the payroll application of Figs. 10.4–10.9. The application should still process two Invoice objects, but now should process one object of each of the four Employee subclasses. If the object currently being processed is a BasePlusCommissionEmployee, the application should increase the BasePlusCommissionEmployee’s basesalary by 10%. Finally, the application should output the payment amount for each object. Complete the following steps to create the new application:
a) Modify classes HourlyEmployee (Fig. 10.6) and CommissionEmployee (Fig. 10.7) to place them in the Payable hierarchy as subclasses of the version of Employee (Fig. 10.13) that implements Payable. [Hint: Change the name of method earnings to getPaymentAmount in each subclass so that the class satisfies its inherited contract with interface Payable.]
b) Modify class BasePlusCommissionEmployee (Fig. 10.8) such that it extends the version of class CommissionEmployee created in part (a).
c) Modify PayableInterfaceTest (Fig. 10.15) to polymorphically process two Invoices,one SalariedEmployee, one HourlyEmployee, one CommissionEmployee and one BasePlusCommissionEmployee. First output a String representation of each Payable object.Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally, output the payment amount for each Payable object.
Accounts Payable System Modification in Java
BasePlusCommissionEmployee.java
/*
* Filename: BasePlusCommissionEmployee.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class BasePlusCommissionEmployee extends CommissionEmployee{
private double baseSalary;
// constructor
public BasePlusCommissionEmployee(String first, String last, String ssn,
double sales, double rate, double salary){
super(first, last, ssn, sales, rate);
setBaseSalary(salary);
}
public BasePlusCommissionEmployee(String first, String last, String ssn,
double sales, double rate, double salary, int month, int day, int year){
// call CommissionEmployee constructor
super(first, last, ssn, sales, rate, month, day, year);
setBaseSalary(salary);
}
// SETTERS
public void setBaseSalary(double salary){
if(salary >= 0.0f)
baseSalary = salary;
else
throw new IllegalArgumentException(
"Base salary must be >= 0.0f");
}
// GETTERS
public double getBaseSalary(){
return this.baseSalary;
}
// calculate getPaymentAmount; override method getPaymentAmount in CommissionEmployee
@Override
public double getPaymentAmount(){
return getBaseSalary() + super.getPaymentAmount();
}
// return String representation of object
@Override
public String toString(){
return String.format("%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary());
}
}
CommissionEmployee.java
/*
* Filename: CommissionEmployee.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class CommissionEmployee extends Employee{
private double grossSales;
private double commissionRate;
// constructor
public CommissionEmployee(String first, String last, String ssn,
double sales, double rate){
super(first, last, ssn);
setGrossSales(sales);
setCommissionRate(rate);
}
public CommissionEmployee(String first, String last, String ssn,
double sales, double rate, int month, int day, int year){
super(first, last, ssn, month, day, year);
setGrossSales(sales);
setCommissionRate(rate);
}
// SETTERS
public void setCommissionRate(double rate){
if(rate > 0.0f && rate < 1.0f)
this.commissionRate = rate;
else
throw new IllegalArgumentException(
"Commission rate must be > 0.0f and < 1.0f");
}
public void setGrossSales(double sales){
if(sales >= 0.0f)
this.grossSales = sales;
else
throw new IllegalArgumentException
("Gross sales muse be >= 0.0f");
}
// GETTERS
public double getCommissionRate(){
return this.commissionRate;
}
public double getGrossSales(){
return this.grossSales;
}
// calculate getPaymentAmount; override abstract method getPaymentAmount in Employee
@Override
public double getPaymentAmount(){
return getCommissionRate() * getGrossSales();
}
// String representation of object
@Override
public String toString(){
return String.format("%s: %s\n%s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate());
}
}
Employee.java
/*
* Filename: Employee.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public abstract class Employee implements Payable{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthDay;
// constructor
public Employee(String first, String last, String ssn){
setFirstName(first);
setLastName(last);
setSocialSecurityNumber(ssn);
birthDay = new Date();
}
public Employee(String first, String last, String ssn,
int month, int day, int year){
setFirstName(first);
setLastName(last);
setSocialSecurityNumber(ssn);
birthDay = new Date(month, day, year);
}
// SETTERS
public void setFirstName(String first){
this.firstName = first;
}
public void setLastName(String last){
this.lastName = last;
}
public void setSocialSecurityNumber(String ssn){
this.socialSecurityNumber = ssn;
}
// GETTERS
public String getFirstName(){
return this.firstName;
}
public String getLastName(){
return this.lastName;
}
public String getSocialSecurityNumber(){
return this.socialSecurityNumber;
}
public Date getBirthday(){
return this.birthDay;
}
// return String representation of Employee object
@Override
public String toString(){
return String.format("%s %s\nsocial security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
}
// ABSTRACT METHODS
// not implemented here - getPaymentAmount() in Payable.
}
HourlyEmployee.java
/*
* Filename: HourlyEmployee.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
* =====================================================================================
*/
public class HourlyEmployee extends Employee{
private double wage;
private double hours;
// constructor
public HourlyEmployee(String first, String last, String ssn,
double wage, double hours){
super(first, last, ssn);
setWage(wage);
setHours(hours);
}
public HourlyEmployee(String first, String last, String ssn,
double wage, double hours, int month, int day, int year){
// explicit Employee constructor call
super(first, last, ssn, month, day, year);
setWage(wage);
setHours(hours);
}
// SETTERS
public void setWage(double hourlyWage){
if(hourlyWage >= 0.0f)
this.wage = hourlyWage;
else
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0f");
}
public void setHours(double hoursWorked){
if((hoursWorked >= 0.0f) && (hoursWorked <= 168.0f))
this.hours = hoursWorked;
else
throw new IllegalArgumentException(
"Hours worked must be >= 0.0f and <= 16.0f");
}
// GETTERS
public double getWage(){
return this.wage;
}
public double getHours(){
return this.hours;
}
// calculate getPaymentAmount; override abstract method getPaymentAmount in Employee
@Override
public double getPaymentAmount(){
if(getHours() <= 40)
return getWage() * getHours();
else
return 40 * getWage() + (getHours() - 40) * getWage() * 1.5f;
}
// String representation of object
@Override
public String toString(){
return String.format("hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours());
}
}
PayableInterfaceTest.java
/*
* Filename: PayableInterfaceTest.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class PayableInterfaceTest{
public static void main(String[] args){
// create four element payable array
Payable[] payableObjects = new Payable[6];
// populate array with objects that implement Payable
payableObjects[0] = new Invoice("01234", "seat", 2, 375.00f);
payableObjects[1] = new Invoice("56789", "tire", 4, 79.95f);
payableObjects[2] = new SalariedEmployee(
"John", "Smith", "111-11-1111", 800.0f);
payableObjects[3] = new HourlyEmployee(
"Lisa", "Barnes", "888-88-8888", 25.0f, 45.0f);
payableObjects[4] = new CommissionEmployee(
"Deez", "Nutz", "222-22-2222", 52.0f, 0.1f);
payableObjects[5] = new BasePlusCommissionEmployee(
"Scrooge", "McDuck", "333-33-3333", 25.0f, 0.25f, 100.0f);
System.out.println("Invoices and Employee processed polymorphically:\n");
// generically process each element in array payableObjects
for(Payable currentPayable : payableObjects){
// check for BasePlusCommissionEmployee
// increase base salary by 10%
if(currentPayable instanceof BasePlusCommissionEmployee){
((BasePlusCommissionEmployee)currentPayable).setBaseSalary(
1.10f * ((BasePlusCommissionEmployee)currentPayable).getBaseSalary());
}
// output currentPayable and its appropriate payment amount
System.out.printf("%s \n%s: $%,.2f\n\n",
currentPayable.toString(),
"payment due", currentPayable.getPaymentAmount());
}
}
}
PieceWorker.java
/*
* Filename: PieceWorker.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class PieceWorker extends Employee{
private double wage;
private double pieces;
// constructor
public PieceWorker(String first, String last, String ssn,
double wage, double pieces){
super(first, last, ssn);
setWage(wage);
setPieces(pieces);
}
public PieceWorker(String first, String last, String ssn,
double wage, double pieces, int month, int day, int year){
super(first, last, ssn, month, day, year);
setWage(wage);
setPieces(pieces);
}
// SETTERS
public void setWage(double w){
if(w >= 0.0f)
this.wage = w;
else
throw new IllegalArgumentException(
"Wage must be >= 0.0f");
}
public void setPieces(double p){
if(p >= 0.0f)
this.pieces = p;
else
throw new IllegalArgumentException(
"Pieces must be >= 0.0f");
}
// GETTERS
public double getWage(){
return this.wage;
}
public double getPieces(){
return this.pieces;
}
// calculate getPaymentAmount; override abstract method in Employee
@Override
public double getPaymentAmount(){
return getPieces() * getWage();
}
// String representation of object
@Override
public String toString(){
return String.format("piece worker: %s\n%s: $%,.2f; %s: %,.2f",
super.toString(), "wage", getWage(),
"pieces produced", getPieces());
}
}
SalariedEmployee.java
/*
* Filename: SalariedEmployee.java
*
* Description: Exercise 10.11 - Accounts Payable System Modification
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class SalariedEmployee extends Employee{
private double weeklySalary;
// constructor
public SalariedEmployee(String first, String last, String ssn, double salary){
super(first, last, ssn);
setWeeklySalary(salary);
}
public SalariedEmployee(String first, String last, String ssn, double salary,
int month, int day, int year){
// pass to Employee constructor
super(first, last, ssn, month, day, year);
setWeeklySalary(salary);
}
// SETTERS
public void setWeeklySalary(double salary){
if(salary >= 0.0f)
this.weeklySalary = salary;
else
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0f");
}
// GETTERS
public double getWeeklySalary(){
return this.weeklySalary;
}
// calculate getPaymentAmount; override abstract method getPaymentAmount in Employee
@Override
public double getPaymentAmount(){
return getWeeklySalary();
}
// return String representation of SalriedEmployee object
@Override
public String toString(){
return String.format("salaried employee: %s\n%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
}
}