Prompt Detail:
To implement the Accounts Payable System Modification, we need to modify the Employee class to implement the Payable interface and add a getPaymentAmount method to invoke the earnings method for the subclasses. Here's the modified Employee class:
typescriptpublic abstract class Employee implements Payable {
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
public Employee(String firstName, String lastName, String socialSecurityNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
@Override
public double getPaymentAmount() {
return earnings();
}
public abstract double earnings();
@Override
public String toString() {
return String.format("%s %s%nsocial security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
}
}
Now we can use this modified Employee class to implement the Accounts Payable System. Here's the modified version of Exercise 10.15:
csharpimport java.util.ArrayList;
public class AccountsPayableSystemModification {
public static void main(String[] args) {
ArrayList<Payable> payableObjects = new ArrayList<>();
// add some random Payable objects to the list
payableObjects.add(new Invoice("01234", "seat", 2, 375.00));
payableObjects.add(new Invoice("56789", "tire", 4, 79.95));
payableObjects.add(new SalariedEmployee("John", "Smith", "111-11-1111", 800.00));
payableObjects.add(new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75, 40));
payableObjects.add(new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000, .06));
payableObjects.add(new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000, .04, 300));
System.out.println("Invoices and Employees processed polymorphically:\n");
for (Payable payable : payableObjects) {
System.out.printf("%n%s %n%s: $%,.2f%n",
payable.toString(),
"payment due", payable.getPaymentAmount());
}
}
}
As you can see, we create an ArrayList of Payable objects and add some random objects to it. Then we use a for-each loop to iterate over the list and invoke the toString and getPaymentAmount methods on each object. Since the Employee class now implements the Payable interface and has a getPaymentAmount method that invokes the earnings method for the appropriate subclass, the correct payment amount is calculated and displayed for each object.