import java.util.*; /** *Main class Payroll that creates new employees based on user input, *also contains validation. A LOT */ public class Payroll { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int current = 1;// number used for the current employee boolean cont = true; // boolean value for continuing the loop until the user enters parameter 6 to exit the program ArrayList employees = new ArrayList(); // ArrayList is used to store all employees whether salaried or hourly while (cont == true) { //the most outter loop of the program used to re-instantiate the menu after options are done System.out.println("What do you want to do?"); System.out.printf("\t1\tCreate a new employee\n"); System.out.printf("\t2\tSet pay\n"); System.out.printf("\t3\tShow weekly pay\n"); System.out.printf("\t4\tShow annual pay\n"); System.out.printf("\t5\tShow summary\n"); System.out.printf("\t6\tQuit\n"); System.out.printf("=====>"); String input = scan.next();//collects the next sequence of input based on what the user enters char in = input.charAt(0);//stores the first character of the input used for teh switch statement /* *Most outter switch statement that contains cases 1-6 based on what the first character is */ switch (in) { case '1': //Case one for a new employee creation, anther case is the used based on the first charachter the user enters //It executes different commands based on whether the user enters s or h or a different first character { scan.nextLine(); System.out.print("What type of employee would you like to create ? "); String resp; resp = scan.next(); char r = resp.charAt(0); boolean invalidresp = true; if (r == 's' || r == 'S' || r == 'h' || r == 'H') { invalidresp = false; } else invalidresp = true; while(invalidresp == true) { System.out.println("***Please enter either 'salaried' or 'hourly'."); System.out.print("What type of employee would you like to create ? "); resp = scan.next(); System.out.println(); r = resp.charAt(0); if (r == 's' || r == 'S' || r == 'h' || r == 'H') { invalidresp = false; } else invalidresp = true; } scan.nextLine(); switch (r) { case 'H': case 'h': { System.out.print("Please enter the name: "); String name = scan.nextLine(); System.out.println(); System.out.print("Please enter the social security number: "); String ssN = scan.next(); System.out.println(); System.out.print("Please enter the hourly rate of pay: "); boolean hasdouble = true; if (scan.hasNextDouble() == false) { hasdouble = false; } String hourlyRate = scan.next(); while (hasdouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { hasdouble = false; } else hasdouble = true; hourlyRate = scan.next(); } double hrRate = Double.parseDouble(hourlyRate); while(hrRate <= 0) { System.out.println("***Please enter a number > 0."); System.out.print("Please enter the hourly rate of pay: "); hasdouble = true; if (scan.hasNextDouble() == false) { hasdouble = false; } hourlyRate = scan.next(); while (hasdouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { hasdouble = false; } else hasdouble = true; hourlyRate = scan.next(); } hrRate = Double.parseDouble(hourlyRate); } System.out.print("Please enter the average number of hours worked per week: "); boolean awdouble = true; if (scan.hasNextDouble() == false) { awdouble = false; } String awRate = scan.next(); while (awdouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { awdouble = false; } else awdouble = true; awRate = scan.next(); } double awHours = Double.parseDouble(awRate); while(awHours <= 0) { System.out.println("***Please enter a number > 0."); System.out.print("Please enter the average number of hours worked per week: "); awHours = scan.nextDouble(); } employees.add(new Hourly(name,ssN,awHours)); //Hourly emp(current) = new Hourly(name, ssN, awHours); employees.get(employees.size()-1).setPay(hrRate); //emp(current).setPay(hrRate); System.out.printf("Employee " + current + " has been created.\n\n"); current++; break; } case 'S': case 's': { System.out.print("Please enter the name: "); String name = scan.nextLine(); System.out.println(); System.out.print("Please enter the social security number: "); String ssN = scan.next(); System.out.print("Please enter the annual pay: "); boolean anndouble = true; if (scan.hasNextDouble() == false) { anndouble = false; } String annRate = scan.next(); while (anndouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { anndouble = false; } else anndouble = true; annRate = scan.next(); } double annSalary = Double.parseDouble(annRate); while (annSalary <= 0) { System.out.println("***Please enter a number > 0."); System.out.print("Please enter the annual pay: "); anndouble = true; if (scan.hasNextDouble() == false) { anndouble = false; } annRate = scan.next(); while (anndouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { anndouble = false; } else anndouble = true; annRate = scan.next(); } annSalary = Double.parseDouble(annRate); } employees.add(new Salaried(name,ssN,annSalary)); //Salaried emp(current) = new Salaried(name, ssN, annSalary); System.out.println("Employee " + current + " has been created"); current++; break; } default: System.out.println("ERROR Bad option: " + resp + " please try again"); } break; } case '2': { if(employees.size() == 0) { System.out.println("***ERROR: No employees have been instantiated, please try again"); break; } System.out.print("Which employee do you wish to use ? "); System.out.println(); boolean isInt = true; if (scan.hasNextInt() == false) { isInt = false; } String intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } int employeeNumb = Integer.parseInt(intNumb); while(employeeNumb == 0 || employeeNumb < 0 || employeeNumb > employees.size()) { System.out.println("***ERROR Please enter an employee number from 1 to " + employees.size() + ": "); System.out.println(); isInt = true; if (scan.hasNextInt() == false) { isInt = false; } intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } employeeNumb = Integer.parseInt(intNumb); } employeeNumb --; System.out.println(); System.out.print("How much would you like to set the annual pay to? "); boolean anndouble = true; if (scan.hasNextDouble() == false) { anndouble = false; } String annRate = scan.next(); while (anndouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { anndouble = false; } else anndouble = true; annRate = scan.next(); } double annPay = Double.parseDouble(annRate); while(annPay <=0) { System.out.println("*** ERROR Please enter an amount > 0."); System.out.println(); System.out.print("How much would you like to set the annual pay to? "); anndouble = true; if (scan.hasNextDouble() == false) { anndouble = false; } annRate = scan.next(); while (anndouble == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextDouble() == false) { anndouble = false; } else anndouble = true; annRate = scan.next(); } annPay = Double.parseDouble(annRate); } System.out.println(); employees.get(employeeNumb).setPay(annPay); break; } case '3': { System.out.println(); if(employees.size() == 0) { System.out.println("***ERROR: No employees have been instantiated, please try again"); break; } System.out.print("Which employee do you wish to use ? "); boolean isInt = true; if (scan.hasNextInt() == false) { isInt = false; } String intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } int employeeNumb = Integer.parseInt(intNumb); System.out.println(); while (employeeNumb <= 0 || employeeNumb > employees.size()) { System.out.print("*** ERROR: Please enter an employee number from 1 to " + employees.size() + ": "); isInt = true; if (scan.hasNextInt() == false) { isInt = false; } intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } employeeNumb = Integer.parseInt(intNumb); System.out.println(); } employeeNumb--; System.out.println(); System.out.println(String.format("The weekly pay is $%,.2f", employees.get(employeeNumb).calcWeeklyPay())); System.out.println(); break; } case '4': { System.out.println(); if(employees.size() == 0) { System.out.println("***ERROR: No employees have been instantiated, please try again"); break; } System.out.print("Which employee do you wish to use ? "); boolean isInt = true; if (scan.hasNextInt() == false) { isInt = false; } String intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } int employeeNumb = Integer.parseInt(intNumb); System.out.println(); while (employeeNumb <= 0 || employeeNumb > employees.size()) { System.out.print("*** ERROR: Please enter an employee number from 1 to " + employees.size() + ": "); isInt = true; if (scan.hasNextInt() == false) { isInt = false; } intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } employeeNumb = Integer.parseInt(intNumb); System.out.println(); } employeeNumb--; System.out.println(); System.out.printf(String.format("The annual pay is $%,.2f", employees.get(employeeNumb).calcAnnualPay())); System.out.println(); break; } case '5': { System.out.println(); if(employees.size() == 0) { System.out.println("***ERROR: No employees have been instantiated, please try again"); break; } System.out.print("Which employee do you wish to use ? "); boolean isInt = true; if (scan.hasNextInt() == false) { isInt = false; } String intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } int employeeNumb = Integer.parseInt(intNumb); System.out.println(); while (employeeNumb <= 0 || employeeNumb > employees.size()) { System.out.print("*** ERROR: Please enter an employee number from 1 to " + employees.size() + ": "); isInt = true; if (scan.hasNextInt() == false) { isInt = false; } intNumb = scan.next(); if (intNumb == null || intNumb == "") { isInt = false; } while (isInt == false) { System.out.print("Pleae enter a number: "); if (scan.hasNextInt() == true) { isInt = true; } else if (intNumb == null) isInt = false; else if (intNumb == "") isInt = false; else isInt = false; intNumb = scan.next(); } employeeNumb = Integer.parseInt(intNumb); System.out.println(); } employeeNumb--; System.out.println(); System.out.printf(employees.get(employeeNumb).toString()); System.out.println(); break; } case '6': System.exit(0); break; default: System.out.println("ERROR Bad Option: " + input + ", please try again."); System.out.println(); break; } } } }