I have the double fields hourlyPayRate and hoursWorked.
I am trying to write a constructor and mutator to have specific conditions, those being to not allow a pay rate less than 7.25 or greater than 25. Do not allow hours worked less than 0 or greater than 70. If a value below the minimum is passed, then set the field to the minimum. If a value above the maximum is passed, then set the field to the maximum.
With the code I have, the test for it is passing 7.24, and expecting 7.25 and yet my code is returning 7.24. I cannot see why. The test is also checking my mutator by setting the pay to 8.0, then changing it to 7.25 but my code is returning 8.0 without changing, even though it looks like it should return the 7.25 value. What am I doing wrong?
public class ProductionWorker extends Employee
{
private double hourlyPayRate;
private double hoursWorked;
public ProductionWorker(String name, String idNumber,
String hireDate, ShiftType shift, double hourlyPayRate, double hoursWorked)
{
super(name, idNumber, hireDate, shift);
this.hourlyPayRate = hourlyPayRate;
this.hoursWorked = hoursWorked;
if (hourlyPayRate > 25)
{
hourlyPayRate = 25;
}
else if (hourlyPayRate < 7.25)
{
hourlyPayRate = 7.25;
}
else if (hourlyPayRate < 25 && hourlyPayRate > 7.25)
{
this.hourlyPayRate = hourlyPayRate;
}
else if (hoursWorked < 0)
{
hoursWorked = 0;
}
else if (hoursWorked > 70)
{
hoursWorked = 70;
}
}
public double getHourlyPayRate()
{
return hourlyPayRate;
}
public void setHourlyPayRate(double hourlyPayRate)
{
//this.hourlyPayRate = hourlyPayRate;
if (hourlyPayRate > 25)
{
hourlyPayRate = 25;
}
else if (hourlyPayRate < 7.25)
{
hourlyPayRate = 7.25;
}
else if (hourlyPayRate < 25 && hourlyPayRate > 7.25)
{
this.hourlyPayRate = hourlyPayRate;
}
}
public double getHoursWorked()
{
return hoursWorked;
}
public void setHoursWorked(double hoursWorked)
{
this.hoursWorked = hoursWorked;
if (hoursWorked < 0)
{
hoursWorked = 0;
}
else if (hoursWorked > 70)
{
hoursWorked = 70;
}
else if (hoursWorked > 0 && hoursWorked < 70)
{
this.hoursWorked = hoursWorked;
}
}
}
Also I thought that I was supposed to end if-else statements with just else, but when I do that here the compiler warns me that it isn't a statement?