Have a program that is to take a persons name, hours worked, rate of pay, fed and state taxes, and combine them to get a net pay. However, most of the calculations will be done in a separate class file. main file looks like
the class code looks like
Problem is I keep getting a o.oo for net pay when I should be getting like 296.00.
Code:
Scanner scanner = new Scanner(System.in);
boolean end = false; // is the input name stop?
while (end == false) // as long as end is false, proceed
{
System.out.print("Enter employee name: ");
String name = scanner.nextLine();
if (name.contentEquals("stop"))
{
System.exit(0);
}
System.out.print("Enter number of hours worked in a week: ");
double hours = Double.parseDouble(scanner.nextLine());
while (hours < 0) //if a negative number will loop next command
{
System.out.print("Enter a positive number of hours worked:"); // prompt
hours = Double.parseDouble(scanner.nextLine());
}
System.out.print("Enter hourly pay rate (Do not include dollar sign): ");
double rate = Double.parseDouble(scanner.nextLine());
while (rate < 0) //if a negative number will loop next command
{
System.out.print ("Enter a positive hourly rate of pay:");
rate = Double.parseDouble(scanner.nextLine());
}
/* tax rates should be entered as a decimal point i.e. 20% = .20 */
System.out.print("Enter federal tax withholding rate: ");
double fedtax = Double.parseDouble(scanner.nextLine());
System.out.print("Enter state tax withholding rate: ");
double statetax = Double.parseDouble(scanner.nextLine());
//make object
Employee emp = new Employee (name,hours,rate);
System.out.printf( "The employee %s" , emp.getName());
System.out.printf( "'s weekly pay is $%.2f\n", emp.getNetPay());
}
}
}
Code:
public class Employee {
//fields
String name;
double rate;
double hours;
double gross;
double fedtax;
double statetax;
double deduction;
double netpay;
// constructor
public Employee(String name, double rate, double hours) {
this.name = name;
this.rate = rate;
this.hours = hours;
}
//returns net pay
public double getNetPay() {
return gross - deduction;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getGross() {
return hours*rate;
}
public void setGross(double gross) {
this.gross = gross;
}
public double getFedtax() {
return fedtax*gross;
}
public void setFedtax(double fedtax){
this.fedtax = fedtax;
}
public double getStatetax() {
return statetax*gross;
}
public void setStatetax(double statetax) {
this.statetax = statetax;
}
public double getDeduction() {
return statetax+fedtax;
}
public void setDeduction (double deduction) {
this.deduction = deduction;
}
}
Comment