Using the EmployeeException.java and the Employee.java from A01-Employee you will implement a driver class called HR.java to
Question:
Using the EmployeeException.java and the Employee.java from A01-Employee you will implement a driver class called HR.java to manipulate an Array of Employee objects.
When your code begins execution the following menu should be printed
1. add an employee 2. remove an employee 3. print employees that earn more than a given amount 4. print all the employees 0. end this program
Your driver class should:
- be named should be HR.java
- work with your Employee.java and EmployeeException.java (fix them if you did not get full credit otherwise you will get points deducted *again*)
- properly validate user input
- not crash, always use try/catch.
Implementing the looping menu in the driver class. Looping menu total 55 points
- When requesting user input, you must print a user-friendly message requesting it. Your code must be user-friendly. (10 points)
- Your code should continue to loop until the user selects 0. If the program terminates at any other point for any other reason you will lose points. (15 points).
- If the user enters an out of range menu option number, you should print a carefully crafted error message explaining the problem and print the menu again. (15 points).
- If the user enters anything other than a number, you should catch the exception, print a carefully crafted error message explaining the problem and print the menu again. (15 points).
Implementing the menu options
If the user selects 1, (add an employee) ( total 60 points).
- Your code should read the employee information from the user using the Scanner class.
- You should validate everything using the Employee and EmployeeException and in case of invalid input print the appropriate message and go back to print the menu again.
- If the user information is valid then you should make sure that there is no other employee with the same enum. In other words, you should make sure there are no duplicate employees/enums. If the enum already exists you should print a message saying so and go back to the main menu, and continue to loop.
- For the purpose of this assignments a duplicate employee is an employee with the same enum as another regardless of the other instance variables.
If the user selects 2, (remove an employee) (60 points).
- You should read the enum number
- if the input is not valid, print an error message and loop back into the menu
- You should search the array looking for an employee matching the enum entered
- If a match is found you should remove the employee object from the array. You must print an appropriate message and loop back into the menu
- when removing an employee from the array you should be careful about leaving holes in the middle. They may result in an exception if they are not carefully handled.
- it there is no match you should print an appropriate message for the user and loop back into the menu.
If the user selects 3 (print employees that earn more than a given amount) (40 points).
- read the amount from the user
- You should validate it and in case of invalid input print the appropriate message and go back to the menu again.
- If the amount is valid then you should should search the array and print all the employees earning more than the given amount. Make sure to align and format the output professionally.
If the user selects 4 (print all the employees) (20 points).
- you may use the enhanced for loop.
- Make sure that the output is properly formatted; add a title to your printout!
For both print methods (options 3 and 4): You should never print any null positions. If you do this you will get points deducted.
If the user selects 0 (5 points).
- you will print a "thank you" message and end program execution
Employee.java
public static class Employee{ private int enumb; private double salary; private String name; Employee(int enumb, double salary, String name){ setEnumb(enumb); setSalary(salary); setName(name); } public void setEnumb(int enumb) throws Exception{ if(enumb<10001 && enumb>99999){ EmployeeException exc = new EmployeeException(); exc.setMessage("Exception"); throw new Exception(exc.getMessage()); } else{ this.enumb = enumb; } } public void setSalary(double salary) throws Exception{ if(salary<0 && salary>500000){ EmployeeException exc = new EmployeeException(); exc.setMessage("Exception"); throw new Exception(exc.getMessage()); } else{ this.salary = salary; } } public void setName(String name) throws Exception{ if(name.length()==2){ if(name.charAt(0)==' ' && name.charAt(1)==' '){ EmployeeException exc = new EmployeeException(); exc.setMessage("Exception"); throw new Exception(exc.getMessage()); } } else if(name.length()<2){ EmployeeException exc = new EmployeeException(); exc.setMessage("Exception"); throw new Exception(exc.getMessage()); } else{ this.name = name; } } public int getEnumb(){ return enumb; } public double getSalary(){ return salary; } public String getName(){ return name; } @Override public String toString(){ return "Enumb: "+enumb+" Salary: "+"$"+salary+" Name: "+name; } }
EmployeeException.java
public static class EmployeeException{ String message; EmployeeException(){ } public String getMessage(){ return message; } public void setMessage(String message){ this.message = message; } }
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates