Dan Zaleski Dan Zaleski - 4 months ago 10
Java Question

Gross Pay and Net Pay not figuring right

I can't seem to figure out what I'm doing wrong in trying to figure GrossPay and NetPay. The values aren't right. Am I overlooking something simple here? I'm pulling my hair out!! I can't figure out why these two values aren't configuring correctly. As far as I can tell, I'm doing the math right, yes?

// Payroll.java
// This program calculates an employee's net pay
// minus Federal tax (11% off the gross) with Overtime
// Daniel Zaleski
// 08/24/2014

/** Import Statements */
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

/** Create Class */
public class PayrollWeekFinal
{

/** Create Employee Class */
private static class Employee
{
private String name;
private double hourlyRate;
private double hoursWorked;
private double overtimeHours;
private double overtimePay;
private double taxPay;
private double grossPay;
private double netPay;

/** Employee Constructor */
Employee()
{
setName("Unknown");
setHourlyRate(0);
setHoursWorked(0);
setOvertimeHours(0);
setOvertimePay(0);
setTaxPay(0);
setGrossPay(0);
setNetPay(0);
}

/** Define Constructor Variables */
Employee(String name, double hourlyRate, double hoursWorked, double overtimeHours, double overtimePay, double taxPay, double grossPay, double netPay)
{
setName(name);
setHourlyRate(hourlyRate);
setHoursWorked(hoursWorked);
setOvertimeHours(overtimeHours);
setOvertimePay(overtimePay);
setTaxPay(taxPay);
setGrossPay(grossPay);
setNetPay(netPay);

}

/** Method to Set EmployeeName */
public void setName (String name)
{
this.name = name;
}

/** Method to Set HourlyRate */
public void setHourlyRate (double hourlyRate)
{
this.hourlyRate = hourlyRate;
}

/** Method to Set HoursWorked */
public void setHoursWorked (double hoursWorked)
{
this.hoursWorked = hoursWorked;
}

/** Method to Set OvertimeHours */
public void setOvertimeHours (double overtimeHours)
{
this.overtimeHours = overtimeHours;
}

/** Method to Set OvertimePay */
public void setOvertimePay (double overtimePay)
{
this.overtimePay = overtimePay;
}

/** Method to Set TaxPay */
public void setTaxPay (double TaxPay)
{
this.taxPay = taxPay;
}

/** Method to Set GrossPay */
public void setGrossPay (double grossPay)
{
this.grossPay = grossPay;
}

/** Method to Set NetPay */
public void setNetPay (double netPay)
{
this.netPay = netPay;
}

/** Method to Get EmployeeName */
public String getName()
{
return name;
}

/** Method to Get HourlyRate */
public double getHourlyRate()
{
return hourlyRate;
}

/** Method to Get HoursWorked */
public double getHoursWorked()
{
return hoursWorked;
}

/** Method to Get OvertimeHours */
public double getOvertimeHours ()
{
return (hoursWorked - 40);
}

/** Method to Get OvertimePay */
public double getOvertimePay ()
{
return (hoursWorked - 40) * hourlyRate * 1.5;
}

/** Method to Get TaxPay */
public double getTaxPay ()
{
return (hoursWorked * hourlyRate + overtimePay) * .011;
}

/** Method to Get GrossPay */
public double getGrossPay()
{
return (hourlyRate * hoursWorked) + overtimePay;
}

/** Method to Get NetPay */
public double getNetPay()
{
return (grossPay - taxPay);
}

} // End Employee Class

/** Main Method */
public static void main( String args[] )
{
String employeeName;
double hoursWorked;
double hourlyRate;
double overtimeHours = 0;
double overtimePay = 0;
double taxPay = 0;
double grossPay = 0;
double netPay = 0;

/** Create Scanner Object */
Scanner input = new Scanner( System.in );

/** Set Currency Format */
NumberFormat nfc = NumberFormat.getCurrencyInstance(Locale.US);

/** Print Declarations */
System.out.println( "Hey There, Let's Figure-up How Much You Made This Week!");

/** Begin Loop */
while(true)
{

/** Prompt for EmployeeName */
System.out.print( "Enter Your Full Name, or Enter Quit to Exit the Program): " );
employeeName = input.nextLine();

if ( employeeName.equalsIgnoreCase("quit") )
{
break;
}

do
{
/** Prompt for HourlyRate */
System.out.print( "Please Enter Your Hourly Rate: " );
hourlyRate = input.nextDouble();
if (hourlyRate < 0)
{
System.out.println( "Please enter a positive value. (Greater Than ZERO)" );
}
} while ( hourlyRate < 0 );

do
{
/** Prompt for HoursWorked */
System.out.print( "Please Enter Your Hours Worked This Cycle: " );
hoursWorked = input.nextDouble();
if (hoursWorked < 0)
{
System.out.println("Please enter a positive value. (Greater Than ZERO)" );
}
} while ( hoursWorked < 0 );

/** Create Employee Object */
Employee emp = new Employee(employeeName, hourlyRate, hoursWorked, overtimeHours, overtimePay, taxPay, grossPay, netPay);

/** Print Declarations */
System.out.println( "Hello " + emp.getName() );
System.out.println( "FYI, You Will Pay 11% Federal Tax This Cycle");
System.out.println( "Your Hours Worked This Cycle Were: " + emp.getHoursWorked());
System.out.println( "Your Hourly Pay This Cycle is: " + nfc.format(emp.getHourlyRate()));
System.out.println( "Your Overtime Hours Worked for This Cycle Were: " + (emp.getOvertimeHours()));
System.out.println( "Your Overtime Pay for This Cycle Was: " + nfc.format(emp.getOvertimePay()));
System.out.println( "Your Gross Pay for This Cycle is: " + nfc.format(emp.getGrossPay()));
System.out.println( "Amount You Paid in Taxes This Cycle: " + nfc.format(emp.getTaxPay()));
System.out.println( "Your NET PAY This Cycle is " + nfc.format(emp.getNetPay()));
System.out.println( "The word quit was not detected. Let's do it again!");

} // End While Loop

} // End Main Method

} // End Payroll Class


The output is such:

> Hey There, Let's Figure-up How Much You Made This Week! Enter Your
> Full Name, or Enter Quit to Exit the Program): DZ Please Enter Your
> Hourly Rate: 10 Please Enter Your Hours Worked This Cycle: 41 Hello DZ
> FYI, You Will Pay 11% Federal Tax This Cycle Your Hours Worked This
> Cycle Were: 41.0 Your Hourly Pay This Cycle is: $10.00 Your Overtime
> Hours Worked for This Cycle Were: 1.0 Your Overtime Pay for This Cycle
> Was: $15.00 Your Gross Pay for This Cycle is: $410.00 (SHOULD BE $415)
> Amount You Paid in Taxes This Cycle: $4.51 Your NET PAY This Cycle is
> $0.00 (SHOULD NOT BE 0) The word quit was not detected. Let's do it
> again!


To be even more specific....grossPay is not adding overtimePay to the total, and netPay isn't adding Anything.

Answer

overtimePay is never assigned a value and nor should it, it's a calculated value.

So instead of...

 return (hoursWorked * hourlyRate + overtimePay) * .011;

You should be using something more like...

return (hoursWorked * hourlyRate + getOvertimePay()) * .011;

In fact,

return (getHoursWorked() * getHourlyRate() + getOvertimePay()) * .011;

would be better....

This goes for all you other values as well, like getGrossPay and getTaxPay for example. There is never any need to "assign" a value to these properties, they are calculated based on other values, so should use the appropriate getters to do their work...

Basically, you can get rid of the fields overtimeHours, overtimePay, taxPay, grossPay and netPay, these are all calculated fields based on other values. You can also get rid of there setters, as they shold never be set.

This makes the remainder of the methods look more like...

/**
 * Method to Get OvertimeHours
 */
public double getOvertimeHours() {
    return (getHoursWorked() - 40);
}

/**
 * Method to Get OvertimePay
 */
public double getOvertimePay() {
    return (getHoursWorked() - 40) * getHourlyRate() * 1.5;
}

/**
 * Method to Get TaxPay
 */
public double getTaxPay() {
    return (getHoursWorked() * getHourlyRate() + getOvertimePay()) * .011;
}

/**
 * Method to Get GrossPay
 */
public double getGrossPay() {
    return (getHourlyRate() * getHoursWorked()) + getOvertimePay();
}

/**
 * Method to Get NetPay
 */
public double getNetPay() {
    return (getGrossPay() - getTaxPay());
}