Refactoring for kickstarters- Message Chain 2
Code Smell of the year
Here is a hidden message chain in this code. But technically it’s the same as before.
Eliminate it.
public class Invoice {
private
private Customer customer;
public Invoice(Customer customer){
this.customer = customer;
}
public void addItem(InvoiceItem invoiceItem){
invoiceItems.add(invoiceItem);
}
public double getTotalPrice(){
double invoiceTotal = 0;
for (Iterator iterator = invoiceItems.iterator(); iterator.hasNext();) {
InvoiceItem invoiceItem = (InvoiceItem) iterator.next();
invoiceTotal += invoiceItem.getSubTotal();
}
Address address = customer.getAddress();
Country country = address.getCountry();
if(!country.isInEurope()){
invoiceTotal += SHIPPING_COST_OUTSIDE_EU;
}
return invoiceTotal;
}
}
You can fork it here from Github.
This is step two of the second part of the Refactoring kickstart beginner series. Here is step 3.