Java: is it possible to exclude something from a superclass

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!

+5
source share
3 answers

Almost this exact problem is solved in the first chapter of Martin Fowler's Refactoring book, section "Decomposition and Redistribution of Approval Methods". 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 redefined 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. It is worth the price many times over.

+4
source

We are going to store all the key-value pairs that should be printed on the Map. For this purpose, we will use a modified LinkedHashMap (to prevent the insertion order in the case of duplicates - for example, Earnings: .

 class TweakedHashMap<K,V> extends LinkedHashMap<K,V> { @Override public V put(K key, V value) { super.remove(key); return super.put(key, value); } } 

Now use the following methods in

 class CommissionEmployee{ ... ... public Map<Object,Object> getToStringMap(){ Map<Object,Object> map = new TweakedHashMap<>(); map.put("Commissioned Employee",super.toString()); map.put("Gross Sales",getGrossSales()); map.put("Commission Rate",getCommissionRate()); map.put("Earnings:",earnings()); returun map; } @Override public String toString() { return getToStringMap().entrySet().stream().map(e -> e.getKey()+ " : " + e.getValue()).collect( Collectors.joining(",\n")); } } class BasePlusCommissionEmployee extends CommissionEmployee{ ... ... @Override public Map<Object,Object> getToStringMap(){ Map<Object,Object> superMap = super.getToStringMap(); map.put("Base Salary Plus Commissioned Employee",superMap.get("Commissioned Employee")); map.put("with Base Salary",getBaseSalary()); map.put("Earnings:",earnings()); returun map; } @Override public String toString() { return getToStringMap().entrySet().stream().map(e -> e.getKey()+ " : " + e.getValue()).collect( Collectors.joining(",\n")); } } 

The following will be the conclusion:

 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 
0
source

You need to override the toString behavior in the subclass without calling super.toString ()

This will give you the freedom to choose the fields to display and in what order. But the biggest drawback is that it duplicates the code, which, in my opinion, is not worth it (since you also need to take care of printing the data from the parent class of the CommissionEmployee).

0
source

Source: https://habr.com/ru/post/1275080/


All Articles