Reputation: 243
Help needed to calculate wages for two different pay scales! The error was the net total was incorrect. Thank you to all the comments they were very helpful and now it works :) I hope this part of my code can help other beginners learning to use loops and if statements:
public static void main(String[]args)
{
Scanner input = new Scanner (System.in);
DecimalFormat Currency = new DecimalFormat("###,###,##0.00");
double normHrs = 0, overHrs = 0, bonusHrs = 0, actualHrs = 0, normPay = 0, overPay =0, bonusPay = 0, grossWage = 0,vat23 = 0, netWage =0;
String empName, nextEMP = "y";
System.out.println ("************************");
System.out.println ("HUMAN RESOURCES: PAYROLL"); //
System.out.println ("************************");
while (nextEMP.equalsIgnoreCase("y"))
{
System.out.print("Please enter an employee's first name: ");
empName = input.next();
System.out.print("Please enter their total hours worked: ");
actualHrs = input.nextDouble();
if (actualHrs <=36)
{
normHrs=actualHrs;
}
else
{
normHrs = 36;
bonusHrs = actualHrs - 50;
overHrs = actualHrs -36;
}
if ("bob".equalsIgnoreCase(empName))
{
bonusPay = bonusHrs > 0 ? bonusHrs : 0;
normPay = normHrs * 8; // £8 x 36 hours
overPay = overHrs * 12; // £12 x 36+ hours
}
else
{
bonusPay = bonusHrs >0 ? 20 : 0;
normPay = normHrs * 5.75;
overPay = overHrs * 9.25;
}
}
Upvotes: 0
Views: 108
Reputation: 44871
I think the error is in the part that evaluates if Bob should get bonus.
Since you've already calculated bonusHrs
as actualHrs - 50
, this:
bonusPay = bonusHrs > 50 ? bonusHrs : 0;
will only give Bob bonus pay only when actualHrs
is 100+ . It should probably be:
bonusPay = bonusHrs > 0 ? bonusHrs : 0;
Also, this:
normPay = actualHrs * 8; // £8 x 36 hours
Might be wrong and should maybe be:
normPay = normHrs * 8; // £8 x 36 hours
Upvotes: 1
Reputation: 24443
In the if block you have a mistake when assigning normPay:
if ("bob".equalsIgnoreCase(empName)) {
bonusPay = bonusHrs > 50 ? bonusHrs : 0;
normPay = actualHrs * 8; // £8 x 36 hours
overPay = overHrs * 12; // £12 x 36+ hours
}
It must be
normPay = normHrs * 8;
Upvotes: 2