The code is-create a calculate class with calcTotal, calcSalesTax, and calcSubTotal methods inside. The user should be prompted to enter in a quantity and price and the tax is 0.7. I need to call this class in Main and then output the subtotal, tax, and total.
So far this is what I have in the calulator class
package com.Nick.Calculator;
public class Calculator {
public double calcSubTotal( double amount, double qty){
double subTotal;
subTotal=qty*amount;
return subTotal;
}
public static double calcSalesTax(double subTotal, double taxAmount){
double tax=0.7;
taxAmount=subTotal*tax;
return taxAmount;
}
public static double calcTotal(double subTotal, double taxAmount){
double total;
total=subTotal+taxAmount;
return total;
}
}
Does anything else need to go in this class, or can I prompt the user in main, and how to I call these functions correctly in Main? Thanks
That class looks fine, I would keep that logic separate from any user prompts. The quickest way will be to prompt the user in your main. In your main instantiate a new Calculator class:
Few observations tho, why is one of your methods in Calculator non-static and the rest static? I would make none of them static as it will be easier to write a test for them. Or some may argue the logic is soo simple static would be fine. Your call but go one way or the other. Also in your calcSalesTax call you set your result to a parameter that is passed in. This is generally bad practice and I would either just return the value directly or make a new object for it.