I really neeed help with this Java 2 assignment I am LOST!! Objectives: The focu
ID: 3778751 • Letter: I
Question
I really neeed help with this Java 2 assignment I am LOST!!
Objectives: The focus of this assignment is the use of both character sequential files and Object Serialized files
Program Description:
The EmployeeManager will now have the ability to save the current Employees to a file, as well as load Employees from a previous session. Additionally, it will be capable of processing files that will apply multiple Employee updates.
A total of fourteen classes are required.
Queue – (From previous assignment)
ListNode – (From previous assignment)
LikedList – (From previous assignment)
EmptyListException – (From previous assignment)
ArrayList - (From previous assignment)
InvalidEmployeeNumberException - (From previous assignment)
InvalidSizeException - (From previous assignment)
MaximumCapacityException - (From previous assignment)
Employee - (Altered from previous assignment)
HourlyEmployee - (From previous assignment)
SalaryEmployee - (From previous assignment)
CommissionEmployee - (From previous assignment)
EmployeeManager - (Altered from previous assignment)
EmployeeDriver - (Altered from previous assignment)
Changes to Employee
The Employee class must now implement the Serializable interface in order to be used with Object Serialization
Employee {abstract} implements Comparable<Employee>, Serializable
- String firstName
- String lastName
- char middleInitial
- boolean fulltime
- char gender
- int employeeNum
<<constructor>> Employee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean ) throws InvalidEmployeeNumberException
+ getEmployeeNumber() : int
+ setEmployeeNumber(empNum : int)
+ getFirstName() : String
+ getLastName() : String
+ setFirstName(fn: String)
+ setLastName(ln : String)
+ setMiddleI(m : char)
+ setGender(g : char)
+ equals(e2 : Object) : Boolean
+ toString() : String
+ calculateWeeklyPay() : double {abstract}
+ annualRaise() {abstract}
+ holidayBonus() : double {abstract}
+ resetWeek() {abstract}
+ compareTo(Employee e)
Changes to EmployeeManager
The EmployeeManager will now be able to save and load the Employees from session to session using Serialized files. It will also be able to process text files to quickly update multiple Employees.
EmployeeManager
- employees : ArrayList<Employee>
- employeeMax : final int = 10
- hourlyList : LinkedList<Employee>
- salaryList : LinkedList<Employee>
- commissionList : LinkedList<Employee>
- vacationRequests : Queue<Employee>
<<constructor>> EmployeeManager()
+ addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) throws InvalidEmployeeNumberException
+ removeEmployee( index : int)
+ listAll()
+ listHourly()
+ listSalary()
+ listCommision()
+ resetWeek()
+ calculatePayout() : double
+ getIndex( empNum : int ) : int
+ annualRaises()
+ holidayBonuses() : double
+ increaseHours( index : int, amount : double)
+ increaseSales( index : int, amount : double)
+ findAllBySubstring(find : String) : Employee[]
- RabinKarp(name : String, find : String) : int
- stringHash(s : String) : int
- charNumericValue(c : char) : int
- RabinKarpHashes(s : String, hashes : int[], pos : int, length : int) : int
- linearSearchRecursive(nameHashes : int[], findHash : int, pos : int) : int
+ sort()
+ addRequest(empNum : int) : boolean
+ viewNextRequest() : Employee
+ grantNextRequest() : Employee
+ outputRequests()
+ loadEmployees(employeeFile : String, requestFile : String) boolean
+saveEmployees(employeeFile : String, requestFile : String) boolean
+processUpdates(fileName : String) boolean
public boolean loadEmployees(String employeeFile, String requestFile)
Given the name of a serialized file (employeeFile), this method attempts to read in Employees from a previous session. This method will return true if the load of Employees was successful and false if it was not successful (file doesn’t exist, ClassNotFoundException, or other IOException). Before reading in the Employees it will clear all of the lists to ensure it is loading into an empty set of Employees. This includes the ArrayList, the LinkedLists, and the Queue. If a MaximumCapacityException is encountered while adding Employees output “Maximum Capacity Reached”, and return true. The method will also open the text file (requestFile) to rebuild the vacation request Queue.
public boolean saveEmployees(String employeeFile, String requestFile)
This method will store all of the Employees as Serialized objects in the employeeFile and the Employee numbers in the correct order of those in the vacation Queue as plain text, placing a new line after every number.
public boolean processUpdates(String fileName)
Given the name of a text file, this method will process the update requests within the file. A valid update request is in the form:
EMPLOYEE_NUMBER AMOUNT
Where the two values are separated by a space. Employee numbers are ints, amount can be a double. It will skip updates with a negative amount. If an improperly formatted update request is reached it will skip that update and proceed to the next update.
To correctly apply an update you must call upon either increaseHours or increaseSales method of the EmployeeManager, depending on the type of employee that has the given Employee number. Employee numbers that do not exist or Employee numbers of SalaryEmployees are skipped.
Changes to EmployeeDriver
Before the Main Menu loop call upon the EmployeeManager’s loadEmployees with the file name of “employees.ser” and “requests.dat”. If the load is successful output “Employees Loaded”, if it was not successful output “Employees Not Loaded”.
Upon exit call upon the EmployeeManager’s saveEmployees. After the “Thank you for using the Employee Manager!” message, if the save was successful output “Employees stored”, if unsuccessful output “Employes not stored”.
Add a new menu option, “Employee Updates” after “Grant Request”. Employee Updates will now be option 13, moving Exit to option 14. This option will ask the user for the name of an update file, read it in and call upon the EmployeeManager’s processUpdates method. If this is done successfully output “Updates processed successfully”, otherwise output “Updates not processed”.
The first time you execute the loading of files will not succeed since there is no “employees.ser” file. After that it should always succeed.
If you need to retest your program from scratch, that is from the first run, you can remove the .ser file that is created with the rm command.
rm employees.ser
rm requests.dat
Other Notes:
Import classes to files as necessary
When loading Employees be sure to add them to their own lists as well!
Name the file for the Serialized Employees “employees.ser” and the vacation request Employee numbers “requests.dat”
Example (user input underlined, bolded comments not in actual output)
To begin with there is no “employees.ser” file. The contents for the update files used for this example are at the bottom
Employees Not Loaded No files exists
Main Menu
No Employees.
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Find Employee
9. Sort
10. View Vacation Requests
11. Add Vacation Request
12. Grant Vacation Request
13. Employee Updates
14. Quit
Enter Choice:
After adding some Employees and exiting
Main Menu
12348
Doe, John A.
Gender: M
Status: Full Time
Wage: 14.75
Hours Worked: 0.00
12345
Cavanaugh, Patrick B.
Gender: M
Status: Full Time
Wage: 10.00
Hours Worked: 0.00
12346
Bohning, Edith C.
Gender: F
Status: Part Time
Salary: 50000.00
12347
Pohnal, Rickie A.
Gender: F
Status: Full Time
Rate: 2.50
Sales: 0.00
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Find Employee
9. Sort
10. View Vacation Requests
11. Add Vacation Request
12. Grant Vacation Request
13. Employee Updates
14. Quit
Enter Choice: 14
Thank you for using the Employee Manager!
Employees stored Employees successfully stored
Starting the driver again
Employees Loaded Employees from previous session loaded successfully
Main Menu
12348
Doe, John A.
Gender: M
Status: Full Time
Wage: 14.75
Hours Worked: 0.00
12345
Cavanaugh, Patrick B.
Gender: M
Status: Full Time
Wage: 10.00
Hours Worked: 0.00
12346
Bohning, Edith C.
Gender: F
Status: Part Time
Salary: 50000.00
12347
Pohnal, Rickie A.
Gender: F
Status: Full Time
Rate: 2.50
Sales: 0.00
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Find Employee
9. Sort
10. View Vacation Requests
11. Add Vacation Request
12. Grant Vacation Request
13. Employee Updates
14. Quit
Enter Choice: 13 Process update file
Enter name of update file: update1
Updates processed successfully
Main Menu
12348
Doe, John A.
Gender: M
Status: Full Time
Wage: 14.75
Hours Worked: 35.75
12345
Cavanaugh, Patrick B.
Gender: M
Status: Full Time
Wage: 10.00
Hours Worked: 20.56
12346
Bohning, Edith C.
Gender: F
Status: Part Time
Salary: 50000.00
12347
Pohnal, Rickie A.
Gender: F
Status: Full Time
Rate: 2.50
Sales: 10000.54
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Find Employee
9. Sort
10. View Vacation Requests
11. Add Vacation Request
12. Grant Vacation Request
13. Employee Updates
14. Quit
Enter Choice: 13 Process another
Enter name of update file: update2
Updates processed successfully
Main Menu
12348 Note the update with a negative value was not processed, and the update
Doe, John A. after the incorrectly formatted update still occurred
Gender: M
Status: Full Time
Wage: 14.75
Hours Worked: 35.75
12345
Cavanaugh, Patrick B.
Gender: M
Status: Full Time
Wage: 10.00
Hours Worked: 20.56
12346
Bohning, Edith C.
Gender: F
Status: Part Time
Salary: 50000.00
12347
Pohnal, Rickie A.
Gender: F
Status: Full Time
Rate: 2.50
Sales: 10500.54
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Find Employee
9. Sort
10. View Vacation Requests
11. Add Vacation Request
12. Grant Vacation Request
13. Employee Updates
14. Quit
Enter Choice: 14 Exit
Thank you for using the Employee Manager!
Employees stored
Restarting one more time to show updates are also stored
Employees Loaded
Main Menu
12348
Doe, John A.
Gender: M
Status: Full Time
Wage: 14.75
Hours Worked: 35.75
12345
Cavanaugh, Patrick B.
Gender: M
Status: Full Time
Wage: 10.00
Hours Worked: 20.56
12346
Bohning, Edith C.
Gender: F
Status: Part Time
Salary: 50000.00
12347
Pohnal, Rickie A.
Gender: F
Status: Full Time
Rate: 2.50
Sales: 10500.54
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Find Employee
9. Sort
10. View Vacation Requests
11. Add Vacation Request
12. Grant Vacation Request
13. Employee Updates
14. Quit
Enter Choice: 14
Thank you for using the Employee Manager!
Employees stored
Contents of update files used for this example:
update1
12348 35.75
12345 20.56
12346 90.12
12347 10000.54
update2
12345 -15.25
30.5 12348
12347 500
These are two sample updates. They do not encapsulate all possibilities and cases to check for. Be sure to create and test your own update files.
Employee {abstract} implements Comparable<Employee>, Serializable
- String firstName
- String lastName
- char middleInitial
- boolean fulltime
- char gender
- int employeeNum
<<constructor>> Employee (fn : String, ln : String, m : char, g : char, empNum : int, ft : boolean ) throws InvalidEmployeeNumberException
+ getEmployeeNumber() : int
+ setEmployeeNumber(empNum : int)
+ getFirstName() : String
+ getLastName() : String
+ setFirstName(fn: String)
+ setLastName(ln : String)
+ setMiddleI(m : char)
+ setGender(g : char)
+ equals(e2 : Object) : Boolean
+ toString() : String
+ calculateWeeklyPay() : double {abstract}
+ annualRaise() {abstract}
+ holidayBonus() : double {abstract}
+ resetWeek() {abstract}
+ compareTo(Employee e)
Explanation / Answer
import java.util.Scanner;
import java.util.InputMismatchException;
import exceptions.InvalidEmployeeNumberException;
import exceptions.MaximumCapacityException;
import employeeType.subTypes.HourlyEmployee;
import employeeType.subTypes.SalaryEmployee;
import employeeType.subTypes.CommissionEmployee;
import employeeType.employee.Employee;
import dataStructures.ArrayList;
import dataStructures.Queue;
import exceptions.InvalidCharacterException;
public class EmployeeDriver
{
static Scanner in = new Scanner(System.in);
public static int menu(String... options)
{
int choice = 0;
for(int line = 0; line < options.length; line++)
System.out.printf("%d. %s ", line+1,options[line]);
do
{
boolean a = true;
while (a == true)
{
try
{
System.out.print("Enter Choice: ");
choice = in.nextInt();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
}
while(!(choice > 0 && choice <= options.length));
return choice;
}
public static void main(String args[])
{
int mainInput;//Input for main menu
int subInput1;//Input for submenu
int subInput2;//Input for sub-submenu
int en = 0; //Inputting an employee number
int index = 0;
double amount = 0;
EmployeeManager em = new EmployeeManager(); //The EmployeManager object
/*****/
// em.loadEmployees("employees.ser","requests.dat");
if (em.loadEmployees("employees.ser","requests.dat") == true)
{
System.out.print("Employees Loaded ");
}
else
{
System.out.print("Employees Not Loaded ");
}
/*****/
//Main control loop, keep coming back to the
//Main menu after each selection is finished
do
{
//This is the main menu. Displays menu
//and asks for a choice, validaties that
//what is entered is a valid choice
System.out.println(" Main Menu ");
em.listAll();
mainInput = menu("Employee Submenu", "Add Employee", "Remove Employee", "Calculate Weekly Payout", "Calculate Bonus", "Annual Raises", "Reset Week", "Find Employee", "Sort", "View Vacation Requests", "Add Vacation Request", "Grant Vacation Request", "Employee Updated", "Quit");
//Perform the correct action based upon Main menu input
switch(mainInput)
{
//Employee Submenu
case 1:
do
{
subInput1 = menu("Hourly Employees", "Salary Employee", "Comission Employees", "Back");
switch(subInput1)
{
case 1:
em.listHourly();
do
{
subInput2 = menu("Add Hours", "Back");
if( subInput2 == 1)
{
boolean b = true;
while (b == true)
{
try
{
System.out.println("Employee Number: ");
en = in.nextInt();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
index = em.getIndex(en);
if(index != -1)
{
boolean c = true;
while(c == true)
{
try
{
System.out.print("Enter Hours: ");
amount = in.nextDouble();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
em.increaseHours(index, amount);
}
else
{
System.out.println("Employee not found!");
}
}
}while(subInput2 != 2);
break;
case 2:
em.listSalary();
subInput2 = menu("Back");
break;
case 3:
em.listCommission();
do
{
subInput2 = menu("Add Sales", "Back");
if( subInput2 == 1)
{
boolean d = true;
while( d == true)
{
try
{
System.out.println("Employee Number: ");
en = in.nextInt();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
index = em.getIndex(en);
if(index != -1)
{
boolean ee = true;
while(ee == true)
{
try
{
System.out.print("Enter Sales: ");
amount = in.nextDouble();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
em.increaseSales(index, amount);
}
else
{
System.out.println("Employee not found!");
}
}
}while(subInput2 != 2);
break;
}
}while(subInput1 != 4);
break;
//Add Employee
case 2:
String fn = "b"; String ln = "a";
char mi, g, f;
boolean ft = true;
subInput1 = menu("Hourly", "Salary", "Commission");
boolean ff = true;
while (ff == true)
{
try
{
System.out.print("Enter Last Name: ");
ln = in.next();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
boolean gi = true;
while (gi == true)
{
try
{
System.out.print("Enter First Name: ");
fn = in.next();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
System.out.print("Enter Middle Initial: ");
mi = in.next().charAt(0);
System.out.print("Enter Gender: ");
g = in.next().charAt(0);
boolean h = true;
while (h == true)
{
try
{
System.out.print("Enter Employee Number: ");
en = in.nextInt();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
try
{
System.out.print("Full Time? (y/n): ");
f = in.next().charAt(0);
}
catch (InputMismatchException e)
{
System.out.print("Full Time? (y/n): ");
f = in.next().charAt(0);
}
if(f == 'n' || f == 'N')
{
ft = false;
}
if(subInput1 == 1)
{
System.out.print("Enter wage: ");
}
else if(subInput1 == 2)
{
System.out.print("Enter salary: ");
}
else
{
System.out.print("Enter rate: ");
}
boolean invalidNumber = true;
while(invalidNumber == true)
{
try
{
amount = in.nextDouble();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
boolean xt = true;
while (xt == true)
{
try
{
em.addEmployee(subInput1, fn, ln , mi, g, en, ft, amount);
break;
}
catch (InvalidEmployeeNumberException e)
{
System.out.print("Invalid employee number please re-enter: ");
boolean yt = true;
while (yt == true)
{
try
{
en = in.nextInt();
break;
}
catch (InputMismatchException P)
{
in.nextLine();
}
}
}
catch (MaximumCapacityException e)
{
System.out.print("Too many emloyees to add");
e.printStackTrace();
}
}
break;
//Remove Employee
case 3:
boolean q = true;
while (q == true)
{
try
{
System.out.print("Enter Employee Number to Remove: ");
en = in.nextInt();
break;
}
catch (InputMismatchException e)
{
in.nextLine();
}
}
index = em.getIndex(en);
em.removeEmployee(index);
break;
//Calculate Weekly Payout
case 4:
System.out.printf("Total weekly payout is %.2f ", em.calculatePayout());
break;
//Calculate Bonus
case 5:
amount = em.holidayBonuses();
System.out.printf("Total holiday bonus payout is %.2f ", amount);
break;
//Apply Annual Raises
case 6:
em.annualRaises();
System.out.println("Annual Raises applied.");
break;
//Reset the weeks values
case 7:
em.resetWeek();
System.out.println("Weekly values reset.");
break;
//Find Employee
case 8:
System.out.print("Enter substring of Employee name: ");
String substring = in.next();
ArrayList<Employee> ret = null;
try
{
ret = em.findAllBySubstring(substring);
}
catch(InvalidCharacterException ICE)
{
System.out.println("Invalid character found in search");
}
System.out.println("Matches found:");
if(ret != null)
for(int i = 0; i < ret.lengthIs() && ret.getItem(i) != null; i++)
System.out.println(ret.getItem(i));
break;
case 9:
System.out.print("Sort");
em.sort();
break;
case 10:
System.out.print("Viewing Requests");
em.outputRequests();
break;
case 11:
int emp = 0;
System.out.print("Enter Employee number for request: ");
emp = in.nextInt();
if (em.addRequest(emp) == false)
{
System.out.print("No such Employee");
}
break;
case 12:
System.out.print("Granting request");
em.grantNextRequest();
break;
case 13:
System.out.print("Please enter name of an update file");
String input = in.next();
em.processUpdates(input);
if (em.processUpdates(input) == true)
{
System.out.print("Updates processed sucessfully. ");
}
else
{
System.out.print("Updates not processed. ");
}
break;
//Exit
case 14:
System.out.println(" Thank you for using the Employee Manager! ");
/*****/
em.saveEmployees("employees.ser","requests.dat");
if (em.saveEmployees("employees.ser","requests.dat") == true)
{
System.out.print("Employees stored. ");
}
else
{
System.out.print("Employees not stored .");
}
/*****/
}
}while(mainInput != 14);
}
}