I am working on an assignment related to class inheritance, and all of this is done and works correctly, except for the string format method. The result of the program should look like this:
Base Salary Plus Commissioned Employee: Sue Smith with ssn: 222-22-2222 Gross Sales: $3000.00 Commission Rate: 0.05 with Base Salary:$300.00 Earnings: $450.00
but it prints like this:
Base Salary Plus Commissioned Employee: Sue Smith with ssn: 222-22-2222 Gross Sales: $3000.00 Commission Rate: 0.05 Earnings: $450.00 with Base Salary:$300.00 Earnings: $450.00
The problem is that the issue of "Earnings: $ 450.00" is printed up to "with a base salary of $ 300." The BasePlusCommissionEmployee class extends from the CommissionEmployee class. Each class has its own toString method, and basecomputerEmployee calls super.toString (), which I believe causes this problem.
I need to be able to inherit a class from CommissionEmployee, but not bring the "Earnings" to the end (as shown).
This is the code for toString in the CommissionEmployee class:
@Override public String toString() { return String.format("%s: %s%n%s: $%.2f \n%s: %.2f \n%s $%.2f", "Commissioned Employee", super.toString(), "Gross Sales", getGrossSales(), "Commission Rate", getCommissionRate(), "Earnings:", earnings()); }
This is the code for toString in the BasePlusCommissionEmployee class (which is distributed from CommissionEmployee):
@Override public String toString() { return String.format("\n%s %s \n%s:$%,.2f \n%s $%.2f", "Base Salary Plus", super.toString(), "with Base Salary", getBaseSalary(), "Earnings:",earnings()); }
This is the test code, run mostly:
CommissionEmployee employee1 = new CommissionEmployee("Fred", "Jones", "111-11-1111", 2000.0, .05); BasePlusCommissionEmployee employee2 = new BasePlusCommissionEmployee("Sue", "Smith", "222-22-2222", 3000.0, .05, 300); System.out.printf("%s%s%s%s%s", employee1, employee2, employee3, employee4, employee5);
All help is much appreciated! Thanks!