I have to create a program that will generate net and gross pay for both hourly and salary employees. I have been struggling with it for a few days, I'm really stumped. I know its a wreck I'm working on it right now. When I enter annual salary, it returns the same number as the net salary and 0 for Tax. Also I need an error message when the user inputs an hourly rate less than 5.15 or greater than 25. My loop for that is wrong also. Eventually I need to add overtime calculation for after 40 hours but I haven't started that since I'm having so many problems. Please Help, Thank You In Advance.
Test Class
CODE
import java.util.*;
public class TestNetWeeklyPay
{
public static void main(String[] args)
{
double hoursWorked;
double hourlyPay;
double weeklySalary;
double netWeeklyPay = 0;
double tax = 0;
double annualSalary = 0;
double annualPayroll = 0;
double netAnnualPay = 0;
int payType;
String name;
Scanner scan = new Scanner(System.in);
NetWeeklyPay calc = new NetWeeklyPay();
System.out.print("Enter employee name: ");
name = scan.nextLine();
do {
System.out.print("Enter employee pay type (1 for hourly, 2 for salary): ");
payType = scan.nextInt();
if (payType == 1)
{
System.out.print("Enter hours worked during the week: ");
hoursWorked = scan.nextDouble();
System.out.print("Enter hourly rate: ");
hourlyPay = scan.nextDouble();
weeklySalary = calc.weeklyGrossHourly(hoursWorked, hourlyPay);
System.out.print("Weekly payroll information for " + name + "\n");
System.out.print("Gross Pay\t" + weeklySalary +"\n");
System.out.print("Federal Tax\t" + tax +"\n");
System.out.print("Net Pay\t" + netWeeklyPay +"\n");
while((hourlyPay >= 5.15) || (hourlyPay <= 25))
{
System.out.print("error");
}
break;
}
else if (payType == 2)
System.out.print("Enter annual salary: ");
annualSalary = scan.nextDouble();
//annualPayroll = calc.annualPayroll(annualSalary);
System.out.print("Weekly payroll information for " + name + "\n");
System.out.print("Gross Pay\t" + annualSalary +"\n");
tax = calc.tax(annualSalary);
System.out.print("Federal Tax\t" + tax +"\n");
netAnnualPay = calc.annualNet(annualSalary);
System.out.print("Net Pay\t" + netAnnualPay +"\n");
}
while((payType==1) || (payType==2));
}
}
Functional Class
CODE
public class NetWeeklyPay
{
double weeklyGrossHourly;
double annual;
double annualNet;
double tax;
public double weeklyGrossHourly(double hoursWorked, double hourlyPay)
{
weeklyGrossHourly = hoursWorked * hourlyPay;
return weeklyGrossHourly;
}
public double netWeeklyPay()
{
double annual = weeklyGrossHourly * 52;
if((annual < 12000) && (annual >= 18000)) {
annual = (annual - 12000) * 0.14;
return annual;
} else if ((annual < 18000) && (annual >= 29000)) {
annual = 840 + (annual - 18000) * 0.16;
return annual;
} else if (annual < 29000) {
annual = 2600 + (annual - 29000) * 0.19;
return annual;
} else {
return annual;
}
// return annual;
}
public double tax(double annualSalary)
{
if((annualSalary < 12000) && (annualSalary >= 18000)) {
double tax = (annualSalary - 12000) * 0.14;
return tax;
} else if ((annualSalary < 18000) && (annualSalary >= 29000)) {
double tax = 840 + (annualSalary - 18000) * 0.16;
return tax;
} else if (annualSalary < 29000) {
double tax = 2600 + (annualSalary - 29000) * 0.19;
return tax;
} else {
tax = 0;
return tax;
}
}
public double annualNet(double annualSalary)
{
annualNet = annualSalary - tax;
return annualNet;
}
}