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;
}
}
}
You can simplify your code by breaking it down into smaller functions. I have done some refactoring in your code, but this can further be improved:-
import java.util.Scanner;
public class Payroll {
//setting repeat to true
static Boolean repeat = true;
public static void main(String[] args) {
//init scanner
Scanner reader = new Scanner(System.in);
//dscrp of prgrm to usr
System.out.println("\nCalculate your weekly pay");
System.out.println("Entering \"exit\" at any type will bring you back to the manager\n");
while (repeat) {
//rqst input for hourly wage
System.out.print("Enter hourly wage: $");
String strHWage = reader.next();
System.out.println();
outerloop:
if (isNumeric(strHWage)) {
//str -> dbl
double dblHWage = Double.parseDouble(strHWage);
//rqst hours worked
System.out.print("Enter hours worked: ");
String strHours = reader.next();
System.out.println();
if (isNumeric(strHours)) {
//str -> dbl
double dblHours = Double.parseDouble(strHours);
//rqst overtime hours worked
System.out.print("Enter overtime hours worked: ");
String strOverHours = reader.next();
System.out.println();
if (isNumeric(strOverHours)) {
//str -> dbl
double dblOverHours = Double.parseDouble(strOverHours);
//clctd $ earned
double 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;
} else {
validateInput(strOverHours, args);
}
} else {
validateInput(strHours, args);
}
} else {
validateInput(strHWage, args);
}
}
}
public static void validateInput(String input, String[] args) {
if (input.equalsIgnoreCase("exit")) {
repeat = false;
//bring to manager
System.out.println("Program terminated, going to manager\n");
Manager.main(args);
} else {
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;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.