I feel like the amount of while loops nested into each other isn't too readable and could be simplified down but I'm not sure how I can simplify it down. Could someone let me know what I would have to do to simplify this. I'm not sure how I would convert these while loops to methods because my variables are in the main method. I'm pretty new to Java so any help would be appreciated.
import java.util.Scanner;
public class Payroll
{
public static void main(String[] args)
{
//init scanner
Scanner reader = new Scanner(System.in);
//var
boolean repeat = true; //keeps or breaks loop
String strHWage; //input for hourly wage
double dblHWage; //clctd for hourly wage from strHWage
String strHours; //input for hours worked
double dblHours; //clctd for hours worked
String strOverHours; //input for overtime hours
double dblOverHours; //clctd for overtime hours
double totalPay; //clctd total pay
//dscrp of prgrm to usr
System.out.println();
System.out.println("Calculate your weekly pay");
System.out.println("Entering \"exit\" at any type will bring you back to the manager");
System.out.println();
//setting repeat to true
repeat = true;
while (repeat == true)
{
//rqst input for hourly wage
System.out.print("Enter hourly wage: $");
strHWage = reader.next();
System.out.println();
outerloop:
while (isNumeric(strHWage))
{
if (strHWage.equalsIgnoreCase("exit"))
{
repeat = false;
//bring to manager
System.out.println("Program terminated, going to manager\n");
Manager.main(args);
}
else if (isNumeric(strHWage))
{
//str -> dbl
dblHWage = Double.parseDouble(strHWage);
//rqst hours worked
System.out.print("Enter hours worked: ");
strHours = reader.next();
System.out.println();
while (isNumeric(strHours))
{
if(strHours.equalsIgnoreCase("exit"))
{
repeat = false;
//bring to manager
System.out.println("Program terminated, going to manager\n");
Manager.main(args);
}
else if (isNumeric(strHours))
{
//str -> dbl
dblHours = Double.parseDouble(strHours);
//rqst overtime hours worked
System.out.print("Enter overtime hours worked: ");
strOverHours = reader.next();
System.out.println();
while (isNumeric(strOverHours))
{
if (strOverHours.equalsIgnoreCase("exit"))
{
repeat = false;
//bring to manager
System.out.println("Program terminated, going to manager\n");
Manager.main(args);
}
else if (isNumeric(strOverHours))
{
//str -> dbl
dblOverHours = Double.parseDouble(strOverHours);
//clctd $ earned
totalPay = (dblHWage * dblHours) + ((dblHWage * 1.5) * dblOverHours);
//prnt total $ earned
System.out.println("your weekly pay is $" + String.format("%.2f", totalPay));
System.out.println();
break outerloop;
}
}
if (!isNumeric(strOverHours))
{
System.out.println("please enter an actual number.\n");
}
}
}
if (!isNumeric(strHours))
{
System.out.println("please enter an actual number.\n");
}
}
}
if (!isNumeric(strHWage))
{
System.out.println("please enter an actual number.\n");
}
}
}
//FUNCITON TO CHECK STR NUMERIC BEFORE PARSING
public static boolean isNumeric(String str)
{
try
{
Double.parseDouble(str); //parses string if string value is numerical
return true;
}
catch(NumberFormatException ex) //returns false and does not parse if string value is not numerical
{
return false;
}
}
}