I have two classes in this small program. The first part of the program works fine.
public class PayCheck
{
private double hourlyRate, hoursWorked, grossPay, netPay;
final double federalTaxAmount = 0.15;
final double stateTaxAmount = 0.09;
final double FICAAmount = 0.07;
private String lastName, firstName;
public PayCheck()
{
hourlyRate = hoursWorked = grossPay = netPay = 0;
lastName = firstName = "unknown";
}
public PayCheck(double inRate, double inHours, String inLast, String inFirst)
{
hourlyRate = inRate;
hoursWorked = inHours;
lastName = inLast;
firstName = inFirst;
}
public String setLastName()
{
return lastName;
}
public String setFirstName()
{
return firstName;
}
public double setHourlyRate()
{
if (hourlyRate > 5)
{
if (hourlyRate < 100)
{
hourlyRate = hourlyRate;
}
}
else
{
hourlyRate = 0;
}
return hourlyRate;
}
public double setHoursWorked()
{
if (hoursWorked < 0)
if (hoursWorked > 100)
{
hoursWorked = hoursWorked;
}
else
{
hoursWorked = 0;
}
return hoursWorked;
}
public double getGross()
{
grossPay = hoursWorked * hourlyRate;
return grossPay;
}
public double getNet()
{
netPay = grossPay - (grossPay * federalTaxAmount) - (grossPay * stateTaxAmount) - (grossPay * FICAAmount);
return netPay;
}
public String toString()
{
return "Last Name: " + lastName + "\nFirst Name: " + firstName + "\nGross Pay: "
+ this.getGross() + "\nNet Pay: " + this.getNet() + "\nFederal Tax Amount: "
+ (grossPay * federalTaxAmount) + "\nState Tax Amount: " + (grossPay * stateTaxAmount)
+ "\nFICA Amount: " + (grossPay * FICAAmount);
}
}
And then the other part that references the code above has to be in a loop and that loop never ends even though the input is the correct input to terminate the loop.
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
String lastName, firstName, choice;
double hours, rate;
boolean again = true;
PayCheck emp1;
while(true)
{
System.out.print("Please enter the Last Name: ");
lastName = key.next();
System.out.print("Please enter the First Name: ");
firstName = key.next();
System.out.print("Please enter the number of hours: ");
hours = key.nextDouble();
System.out.print("Please enter the pay rate per hour: ");
rate = key.nextDouble();
emp1 = new PayCheck(rate, hours, lastName, firstName);
System.out.println(emp1.toString());
System.out.print("Do you want to another entry (Y/N)? ");
choice = key.next();
if (choice.toLowerCase() == "n")
{
break;
}
}
}
}
Is there a better way to terminate the loop, because this loop is no terminating at all.