Using the code you created from last week, you will be adding exception handling for all input and calculations in the code. The following items will have to be implemented in your code; Create a try-catch block for all choices in navigating your menu Create a try catch blocks for input acceptance for variables Create a try-catch block for all newly created objects (Create Function) Create a try-catch block for updating objects (Update Function) Create a try catch blocks for reading request objects (Read Function); this will be to ensure that you can find and return the object. If you cannot, then it should catch this as an error. *** Previous Code *** public class EmployeeManagementApplication { // Create a storage system to store a list of employees in memory private static List employees = new ArrayList<>(); public static void main(String[] args) { // Display the main menu mainMenu(employees); } private static void mainMenu(List employees) { while (true) { System.out.println("Main Menu:"); System.out.println("1. Create Employee"); System.out.println("2. Read Employee"); System.out.println("3. Update Employee"); System.out.println("4. Delete Employee"); System.out.println("5. Quit"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(System.console().readLine()); switch (choice) { case 1: createEmployee(employees); break; case 2: readEmployee(employees); break; case 3: updateEmployee(employees); break; case 4: deleteEmployee(employees); break; case 5: System.exit(0); default: System.out.println("Invalid choice."); } // Check if the user wants to quit System.out.print("Do you want to quit? (Y/N): "); char quit = System.console().readLine().charAt(0); if (quit == 'Y' || quit == 'y') { System.exit(0); } } } private static void createEmployee(List employees) { System.out.print("Enter the employee's name: "); String name = System.console().readLine(); System.out.print("Enter the employee's ID: "); int employeeId = Integer.parseInt(System.console().readLine()); System.out.print("Enter the employee's role: "); String role = System.console().readLine(); // Create the appropriate employee object based on the role EmployeeRole employee = null; if (role.equalsIgnoreCase("Professor")) { System.out.print("Enter the employee's base salary: "); double baseSalary = Double.parseDouble(System.console().readLine()); System.out.print("Enter the employee's bonus: "); double bonus = Double.parseDouble(System.console().readLine()); System.out.print("Enter the employee's extra pay: "); double extraPay = Double.parseDouble(System.console().readLine()); employee = new Professor(name, employeeId, baseSalary, bonus, extraPay); } else if (role.equalsIgnoreCase("Administrator")) { System.out.print("Enter the employee's base salary: "); double baseSalary = Double.parseDouble(System.console().readLine()); System.out.print("Enter the employee's bonus: "); double bonus = Double.parseDouble(System.console().readLine()); employee = new Administrator(name, employeeId, baseSalary, bonus); } else if (role.equalsIgnoreCase("Faculty")) { System.out.print("Enter the employee's base salary: "); double baseSalary = Double.parseDouble(System.console().readLine()); employee = new Faculty(name, employeeId, baseSalary); } else { System.out.println("Invalid role."); return; } // Add the employee to the list employees.add(employee); } private static void readEmployee(List employees) { System.out.print("Enter the employee's ID: "); int employeeId = Integer.parseInt(System.console().readLine()); // Find the employee with the given ID EmployeeRole employee = null; for (EmployeeRole employeeRole : employees) { if (employeeRole.getEmployeeId() == employeeId) { employee = employeeRole; break; } } // If the employee was found, print their information
Using the code you created from last week, you will be adding exception handling for all input and calculations in the code. The following items will have to be implemented in your code;
- Create a try-catch block for all choices in navigating your menu
- Create a try catch blocks for input acceptance for variables
- Create a try-catch block for all newly created objects (Create Function)
- Create a try-catch block for updating objects (Update Function)
- Create a try catch blocks for reading request objects (Read Function); this will be to ensure that you can find and return the object. If you cannot, then it should catch this as an error.
*** Previous Code ***
public class EmployeeManagementApplication {
// Create a storage system to store a list of employees in memory
private static List<EmployeeRole> employees = new ArrayList<>();
public static void main(String[] args) {
// Display the main menu
mainMenu(employees);
}
private static void mainMenu(List<EmployeeRole> employees) {
while (true) {
System.out.println("Main Menu:");
System.out.println("1. Create Employee");
System.out.println("2. Read Employee");
System.out.println("3. Update Employee");
System.out.println("4. Delete Employee");
System.out.println("5. Quit");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(System.console().readLine());
switch (choice) {
case 1:
createEmployee(employees);
break;
case 2:
readEmployee(employees);
break;
case 3:
updateEmployee(employees);
break;
case 4:
deleteEmployee(employees);
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice.");
}
// Check if the user wants to quit
System.out.print("Do you want to quit? (Y/N): ");
char quit = System.console().readLine().charAt(0);
if (quit == 'Y' || quit == 'y') {
System.exit(0);
}
}
}
private static void createEmployee(List<EmployeeRole> employees) {
System.out.print("Enter the employee's name: ");
String name = System.console().readLine();
System.out.print("Enter the employee's ID: ");
int employeeId = Integer.parseInt(System.console().readLine());
System.out.print("Enter the employee's role: ");
String role = System.console().readLine();
// Create the appropriate employee object based on the role
EmployeeRole employee = null;
if (role.equalsIgnoreCase("Professor")) {
System.out.print("Enter the employee's base salary: ");
double baseSalary = Double.parseDouble(System.console().readLine());
System.out.print("Enter the employee's bonus: ");
double bonus = Double.parseDouble(System.console().readLine());
System.out.print("Enter the employee's extra pay: ");
double extraPay = Double.parseDouble(System.console().readLine());
employee = new Professor(name, employeeId, baseSalary, bonus, extraPay);
} else if (role.equalsIgnoreCase("Administrator")) {
System.out.print("Enter the employee's base salary: ");
double baseSalary = Double.parseDouble(System.console().readLine());
System.out.print("Enter the employee's bonus: ");
double bonus = Double.parseDouble(System.console().readLine());
employee = new Administrator(name, employeeId, baseSalary, bonus);
} else if (role.equalsIgnoreCase("Faculty")) {
System.out.print("Enter the employee's base salary: ");
double baseSalary = Double.parseDouble(System.console().readLine());
employee = new Faculty(name, employeeId, baseSalary);
} else {
System.out.println("Invalid role.");
return;
}
// Add the employee to the list
employees.add(employee);
}
private static void readEmployee(List<EmployeeRole> employees) {
System.out.print("Enter the employee's ID: ");
int employeeId = Integer.parseInt(System.console().readLine());
// Find the employee with the given ID
EmployeeRole employee = null;
for (EmployeeRole employeeRole : employees) {
if (employeeRole.getEmployeeId() == employeeId) {
employee = employeeRole;
break;
}
}
// If the employee was found, print their information
Step by step
Solved in 3 steps