Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Is it possible to exclude taking something from a super class

I'm working on an assignment dealing with class inheritance and have everything done and working properly except for a string format method. The output 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 is printing 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 in the output, "Earnings: $450.00" is printing before "with Base Salary: $300.00". The class BasePlusCommissionEmployee extends from class CommissionEmployee. Each class has it's own toString method, and the BasePlusCommissionEmployee does call the super.toString(), which I believe this is causing the issue.

I need to be able to have the class inherit from the CommissionEmployee but not bring the "Earnings" until the end (as shown).

This is the code for the 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 the toString in BasePlusCommissionEmployee class (which extends 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 running in the main:

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 greatly appreciated! Thank you!

like image 744
AngeloKwak Avatar asked Jan 31 '18 17:01

AngeloKwak


1 Answers

Almost this exact problem is solved in the first chapter of the book Refactoring by Martin Fowler, section "Decomposing and Redistributing the Statement Method". The toString() method can be decomposed as follows in the base class:

    @Override
    public String toString() {
        return getReportHeader() 
                + getReportBase() 
                + getReportSpecifics() 
                + getReportFooter();
    }

    protected String getReportHeader() {
        return String.format("%s: %s",
                             "Commissioned Employee", super.toString());
    }

    protected String getReportBase() {
        return String.format("\n%s %s \n%s:$%,.2f",
                             "Gross Sales", getGrossSales(),
                             "Commission Rate", getCommissionRate());
    }

    protected String getReportSpecifics() {
        return "";
    }

    protected String getReportFooter() {
        return String.format("\n%s $%.2f", "Earnings:", earnings());
    }

and then the required parts can be overridden in the child class:

    @Override
    protected String getReportSpecifics() {
        return String.format("\n%s %s \n%s:$%,.2f",
                             "Base Salary Plus", getBaseSalaryPlus(),
                             "with Base Salary", getBaseSalary());
    }

I suggest reading the whole book, though. It's worth its price many times over.

like image 76
jihor Avatar answered Nov 10 '22 03:11

jihor