public class TaxCalculator { private Taxable element; private BigDecimal taxRate, salesTax, afterTaxPrice; private static DecimalFormat formatter = new DecimalFormat("0.00"); //etc (more code follows) }
public TaxCalculator(Taxable aElement, String aTaxRate) {
this.element = aElement;
this.taxRate = new BigDecimal(aTaxRate);
}
public void calculate() {
BigDecimal taxFactor, before, tax, after;
taxFactor = getTaxRate();
before = getElement().beforeTaxPrice();
tax = before.multiply(taxFactor);
after = before.add(tax);
this.salesTax = tax;
this.afterTaxPrice = after;
}
package taxcalc.model; import java.math.BigDecimal; import java.text.DecimalFormat; public class TaxCalculator { private Taxable element; private BigDecimal taxRate, salesTax, afterTaxPrice; private static DecimalFormat formatter = new DecimalFormat("0.00"); public TaxCalculator(Taxable aElement, String aTaxRate) { this.element = aElement; this.taxRate = new BigDecimal(aTaxRate); } public Taxable getElement() { return element; } public BigDecimal getTaxRate() { return taxRate; } public BigDecimal getSalesTax() { return salesTax; } public BigDecimal getAfterTaxPrice() { return afterTaxPrice; } public String getSalesTaxFormatted() { return formatter.format(this.getSalesTax()); } public String getAfterTaxPriceFormatted() { return formatter.format(this.getAfterTaxPrice()); } public void calculate() { BigDecimal taxFactor, before, tax, after; taxFactor = getTaxRate(); before = getElement().beforeTaxPrice(); tax = before.multiply(taxFactor); after = before.add(tax); this.salesTax = tax; this.afterTaxPrice = after; } @Override public String toString() { return "Taxable Element -- " + getElement().toString(); } }