user14267810
user14267810

Reputation:

How do I implement a method for calculating overtime pay in an HourlyWorker class

I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:

((hours - 160) * overtime) + (160 * hourlyRate)

But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.

HourlyWorker Class:

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}

}

What I'm testing with:

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);    
      staff[0] = hw1;

   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}

Output is:
1600 (initial monthly rate, with 40 overtime hours and 160 regular hours)
1760 (10% increase to the monthlyPay)

Should be:
2006
2206.6

Upvotes: 0

Views: 568

Answers (1)

Andreas
Andreas

Reputation: 159086

You code has the following issues:

  • Field initializers run before the body of the constructor, so overtime = (1.5 * hourlyRate) uses the default value of 0 for the hourlyRate field, calculating an overtime value of 0, and is never recalculated, since initializers only run once, during initialization.

  • setHourlyRate() updates the hourlyRate field, but doesn't re-calculate the value for the overtime field. Same for increasePay() and decreasePay().

  • There is no point to the monthlyPay field, since you never really use it, given that you only use it as-if it was a local variable in the getMonthlyPay() method.

Get rid of fields monthlyPay and overtime, and make them both local variables in the getMonthlyPay() method.

FYI: Using double for currency amounts is discouraged. The recommended type for currency in Java is BigDecimal.

Upvotes: 2

Related Questions