Sales commision not calculating in Java -
i running simple problem driving me insane. trying calculate commission of sales , add salary when run code gives me commission of $0 , compensation of $0. here code have far:
package compensationcalculator; import java.text.numberformat; public class compensationcalculator { private double salary = 75000; private double sales; private double commission; private double compensation; public compensationcalculator() { this.salary = 75000.00; } public void setsales(double sales) { this.sales = sales; } public double getcommission() { this.commission = (sales * 0.2); return commission; } public double getcompensation() { this.compensation = (salary + commission); return compensation; } public string tostring() { numberformat fmt = numberformat.getcurrencyinstance(); return ("annual salary: " + fmt.format(salary) + "\n" + "total sales: " + fmt.format(sales) + "\n" + "total commission: " + fmt.format(commission) + "\n" + "total annual compensation: " + fmt.format(compensation)); } }
now when enter 5000 when prompted sales answer after runs:
annual salary: $75,000.00 total sales: $5,000.00 total commission: $0.00 total annual compensation: $0.00
i sure simple error, have not used java in quite time. have compared of old code years ago , can't see missing. guidance welcome!
the compensation value based on commission
, calculated value.
you either need pre-calculate values when set values or need use values these methods, example...
pre-calculated...
public void setsales(double sales) { this.sales = sales; commission = sales * 0.2; compensation = salary + commission; } public double getcommission() { return commission; } public double getcompensation() { return compensation; }
dependency calculated...
public double getcompensation() { this.compensation = (salary + getcommission()); return compensation; } public string tostring() { numberformat fmt = numberformat.getcurrencyinstance(); return ("annual salary: " + fmt.format(salary) + "\n" + "total sales: " + fmt.format(sales) + "\n" + "total commission: " + fmt.format(getcommission()) + "\n" + "total annual compensation: " + fmt.format(getcompensation())); }
Comments
Post a Comment