|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class PayrollApp { |
| 4 | + |
| 5 | + private static ArrayList<Payroll> list = new ArrayList<Payroll>(); |
| 6 | + |
| 7 | + |
| 8 | + /************************************************************************************************************* |
| 9 | + * Gets information for the Payroll object then displays them afterwards allows the user to remove one then |
| 10 | + * add another one, then display the resulting list |
| 11 | + * |
| 12 | + * Jacob Dokos |
| 13 | + * |
| 14 | + * Project 9 |
| 15 | + **************************************************************************************************************/ |
| 16 | + public static void main(String[] args) { |
| 17 | + |
| 18 | + Scanner s = new Scanner(System.in); |
| 19 | + boolean rerunprog = false; |
| 20 | + |
| 21 | + do |
| 22 | + { |
| 23 | + addToList(); // method to add to array |
| 24 | + String struserin = " "; |
| 25 | + |
| 26 | + //gets input from user if they want to continue program |
| 27 | + System.out.print("\nRun program again? (y/n) "); |
| 28 | + struserin = s.nextLine(); |
| 29 | + |
| 30 | + if (struserin.charAt(0) == 'Y' || struserin.charAt(0) == 'y') |
| 31 | + { |
| 32 | + rerunprog = true; |
| 33 | + System.out.print("\n"); |
| 34 | + } |
| 35 | + else if (struserin.charAt(0) == 'N' || struserin.charAt(0) == 'n') |
| 36 | + rerunprog = false; |
| 37 | + |
| 38 | + |
| 39 | + } while (rerunprog); |
| 40 | + |
| 41 | + |
| 42 | + //display all the employees |
| 43 | + displayList(); |
| 44 | + |
| 45 | + String temp = ""; |
| 46 | + |
| 47 | + boolean validID = false; |
| 48 | + do |
| 49 | + { |
| 50 | + System.out.print("Please enter an ID number: "); |
| 51 | + temp = s.nextLine(); |
| 52 | + //if you find the ID # then remove it |
| 53 | + for (int i = 0; i < list.size(); i++) |
| 54 | + { |
| 55 | + if (list.get(i).getIdNumber() == Integer.parseInt(temp) ) |
| 56 | + { |
| 57 | + list.remove(i); |
| 58 | + validID = true; |
| 59 | + } |
| 60 | + } |
| 61 | + if (!validID) |
| 62 | + System.out.println("Please enter a valid ID number"); |
| 63 | + |
| 64 | + } while (!validID); |
| 65 | + //get input from user |
| 66 | + |
| 67 | + |
| 68 | + //add in one more employee then display them all |
| 69 | + addToList(); |
| 70 | + displayList(); |
| 71 | + } |
| 72 | + |
| 73 | + /************************************************************************************************************* |
| 74 | + * Description: Used to display all the objects in the arraylist to the screen |
| 75 | + * |
| 76 | + * Parameters: none |
| 77 | + * |
| 78 | + * Return: none |
| 79 | + **************************************************************************************************************/ |
| 80 | + public static void displayList() |
| 81 | + { |
| 82 | + for (int i = 0; i < list.size(); i++) |
| 83 | + { |
| 84 | + System.out.println(list.get(i).toString()); |
| 85 | + System.out.println(); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /************************************************************************************************************* |
| 92 | + * Description: Gets input from the user to add to the array list. |
| 93 | + * |
| 94 | + * Parameters: none |
| 95 | + * |
| 96 | + * Return: none |
| 97 | + **************************************************************************************************************/ |
| 98 | + public static void addToList() |
| 99 | + { |
| 100 | + Payroll temp = new Payroll(); |
| 101 | + Scanner s = new Scanner(System.in); |
| 102 | + boolean success = false; |
| 103 | + while (!success) |
| 104 | + { |
| 105 | + System.out.print("What is the employees name? "); |
| 106 | + try |
| 107 | + { |
| 108 | + temp.setName(s.nextLine()); |
| 109 | + if (temp.getName().length() == 0) |
| 110 | + throw new Exception(); |
| 111 | + success = true; |
| 112 | + } |
| 113 | + catch (Exception e) |
| 114 | + { |
| 115 | + System.out.println("You must enter a valid name, please try again. "); |
| 116 | + |
| 117 | + } |
| 118 | + } |
| 119 | + success = false; |
| 120 | + while (!success) |
| 121 | + { |
| 122 | + System.out.print("What is the employees ID? "); |
| 123 | + try |
| 124 | + { |
| 125 | + temp.setIdNumber(Integer.parseInt(s.nextLine())); |
| 126 | + success = true; |
| 127 | + |
| 128 | + } |
| 129 | + catch (ClassCastException f) |
| 130 | + { |
| 131 | + System.out.println("Invalid character, please try again. "); |
| 132 | + } |
| 133 | + catch (NumberFormatException e) |
| 134 | + { |
| 135 | + System.out.println("Invalid character, please try again. "); |
| 136 | + } |
| 137 | + } |
| 138 | + success = false; |
| 139 | + while (!success) |
| 140 | + { |
| 141 | + System.out.print("What is the employees Pay Rate? "); |
| 142 | + try |
| 143 | + { |
| 144 | + temp.setPayRate(Double.parseDouble(s.nextLine())); |
| 145 | + success = true; |
| 146 | + |
| 147 | + } |
| 148 | + catch (NumberFormatException e) |
| 149 | + { |
| 150 | + System.out.println("Invalid character, please try again. "); |
| 151 | + } |
| 152 | + } |
| 153 | + success = false; |
| 154 | + while (!success) |
| 155 | + { |
| 156 | + System.out.print("What is the employees hours worked? "); |
| 157 | + try |
| 158 | + { |
| 159 | + temp.setHoursWorked(Double.parseDouble(s.nextLine())); |
| 160 | + success = true; |
| 161 | + |
| 162 | + } |
| 163 | + catch (NumberFormatException e) |
| 164 | + { |
| 165 | + System.out.println("Invalid character, please try again. "); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + list.add(temp); |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments