Question
Changes to EmployeeManager The EmployeeManager is being updated to utilize an ArrayList as the structure for storing Employees; since an ArrayList does all management of
Changes to EmployeeManager
The EmployeeManager is being updated to utilize an ArrayList as the structure for storing Employees; since an ArrayList does all management of the data the EmployeeManager no longer needs to track the number of Employees, so the currentEmployees data member can be removed. An additional change needs to be made to reflect the change made to Employee. A sort method is also being added (changes in bold).
EmployeeManager |
- employees : ArrayList - employeeMax : final int = 10 -currentEmployees : int |
< + 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) : ArrayList - 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() |
Constructor
The constructor will create an ArrayList of initial size of 3. Since the constructor of the ArrayList that takes a size throws a checked InvalidSizeException this will need to be in a try statement. Catching this Exception should then create the ArrayList to the default size using the other constructor.
addEmployee
The constructors of the Employee subtypes throw a checked InvalidEmployeeNumberException. Since we are not going to handle it here, rather pass the Exception on to the driver, the addEmployee method must declare that it throws an InvalidEmployeeNumberException.
We still want to notify the user if an Employee cannot be added due to reaching the employeeMax. Since currentEmployees is no longer a data member this can be done by properly handling the MaximumCapacityException thrown by ArrayLists addItem() method.
findAllBySubstring
The findAllBySubstring method now returns an ArrayList of Employees.
Other changes to methods
All other methods need to be altered to use the ArrayList instead of the array from the previous iteration.
public void sort()
Simply calls upon the ArrayLists sort() method.
public class EmployeeManager { private Employee[] employees; private final int employeeMax = 10; private int currentEmployees; // Method Name : EmployeeManager
public EmployeeManager() { employees = new Employee[employeeMax]; this.currentEmployees = 0; } // Method Name : addEmployee() // Parameters : int type, String fn, String ln, char m, char g, int empnum, boolean ft , double amount // Return value(s) : None // Partners : None // Description : adds employees. Takes an int representing 1 of the types of the employees and decides if it is a type, is a duplicate or if there are to many employees.
public void addEmployee(int type, String fn, String ln, char m, char g, int empnum, boolean ft , double amount) { int x; int check; check = 0; for(int i = 0; i < currentEmployees; i++) { x= employees[i].getEmployeeNumber(); if( x == empnum) { System.out.print(" Duplicate Not Added "); check = -1; i = 1100; } if (currentEmployees == employeeMax) { System.out.print(" Cannot add more Employees "); } } if( check != -1) { if ( type == 1 ) { currentEmployees += 1; employees[currentEmployees - 1]= new HourlyEmployee(fn,ln,m,g,empnum,ft,amount); } else if ( type == 2 ) { currentEmployees += 1; employees[currentEmployees - 1]= new SalaryEmployee(fn,ln,m,g,empnum,ft,amount); } else if ( type == 3) { currentEmployees += 1; employees[currentEmployees - 1]= new CommissionEmployee(fn,ln,m,g,empnum,ft,amount); } else { System.out.print("Invalid Employee Type, None Added "); } } } // Method Name : removeEmployee() // Parameters : int index // Return value(s) : None // Partners : None // Description : Removes an Employee located at the given index from the Employee array
public void removeEmployee(int index) { for (int x = index; x - 1 < currentEmployees; x++) { employees[x] = employees[x + 1]; } employees[currentEmployees--] = null; } // Method Name : listAll() // Parameters : None // Return value(s) : None // Partners : None // Description : Lists all the current Employees. Output none if there are none.
public void listAll() { if (currentEmployees == 0) System.out.println("No Employees. "); else { for (int i = 0; i < currentEmployees; i++) {
if (employees[i] != null) System.out.println(employees[i].toString()); } } } // Method Name : listHourly() // Parameters : None // Return value(s) : None // Partners : None // Description : Lists all the current HourlyEmployees. Outputs none if none.
public void listHourly() { if (currentEmployees == 0) System.out.println("No Employees. "); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { if (employees[i] instanceof HourlyEmployee) System.out.println(employees[i].toString()); } } } } // Method Name : listSalary() // Parameters : None // Return value(s) : None // Partners : None // Description : Lists all the current SalaryEmployees. Outputs none if none.
public void listSalary() { if (currentEmployees == 0) System.out.println("No Employees. "); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { if (employees[i] instanceof SalaryEmployee) System.out.println(employees[i].toString()); } } } } // Method Name : listCommission() // Parameters : None // Return value(s) : None // Partners : None // Description : Lists all the current CommissionEmployees. Outputs none if none.
public void listCommission() { if (currentEmployees == 0) System.out.println("No Employees. "); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { if (employees[i] instanceof CommissionEmployee) System.out.println(employees[i].toString()); } } } } // Method Name : resetWeek() // Parameters : None // Return value(s) : None // Partners : None // Description : Resets the week for all Employees.
public void resetWeek() { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { employees[i].resetWeek(); } } } // Method Name : calculatePayout() // Parameters : None // Return value(s) : return payOut // Partners : None // Description : Returns the total weekly payout for all Employees.
public double calculatePayout() { double payOut = 0; for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { payOut += employees[i].calculateWeeklyPay(); } System.out.print(employees[i].toString()+ " "); } return payOut; } // Method Name : getIndex() // Parameters : int empNum // Return value(s) : returns -1 or i // Partners : None // Description : Given an Employee Number, returns the index of that Employee in the array, if the Employee doesnt exist retuns -1.
public int getIndex(int empNum) { for (int i = 0; i < currentEmployees; i++) { if (employees[i].getEmployeeNumber() == empNum) { return i; } } System.out.print(" No such Employee exists and none have been removed. "); return -1; } // Method Name : annualRaises() // Parameters : None // Return value(s) : None // Partners : None // Description : Applies annual raise to all current Employees.
public void annualRaises() { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { employees[i].annualRaise(); } System.out.print(employees[i].toString()+ " "); } } // Method Name : holidayBonuses() // Parameters : None // Return value(s) : returns bonus // Partners : None // Description : Outputs and returns the total holiday bonus of all Employees.
public double holidayBonuses() { double bonus = 0; for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { bonus += employees[i].holidayBonus(); } System.out.printf(employees[i].toString() + "Bonus Amount: %.2f ", bonus); } return bonus; } // Method Name : increaseHours() // Parameters : int index , double amount // Return value(s) : None // Partners : None // Description : Increase the hours worked of the Employee at the given index by the given double amount.
public void increaseHours(int index, double amount) { if (employees[index] != null && employees[index] instanceof HourlyEmployee) { HourlyEmployee employee = (HourlyEmployee) employees[index]; employee.increaseHours(amount); } } // Method Name : increaseSales() // Parameters : int index, double amount // Return value(s) : None // Partners : None // Description : Increase the sales of the Employee at the given index by the given double amount.
public void increaseSales(int index, double amount) { if (employees[index] != null && employees[index] instanceof CommissionEmployee) { CommissionEmployee employee = (CommissionEmployee) employees[index]; employee.increaseSales(amount); } } // Method Name : findAllBySubstring() // Parameters : String find // Return value(s) : returns output // Partners : None // Description : This method will return an array of all the Employees in the EmployeeManager that contain the substring passed.
public Employee[] findAllBySubstring(String find) { Employee[] output = new Employee[currentEmployees]; int count = 0; for (int i = 0; i < currentEmployees; i ++ ) { String name = employees[i].getFirstName() + employees[i].getLastName();
if(RabinKarp(name, find) > -1) { output[count] = employees[i]; count += 1; } } return output; } // Method Name : RabinKarp // Parameters : String name, String find // Return value(s) : returns search // Partners : None // Description : Does the preprocessing of finding the hash for the substring
private int RabinKarp(String name, String find) { if (find.length() > name.length()) return -1; int findHash = stringHash(find); int [] calc = new int [name.length() - find.length() + 1]; String s = name.substring(0, find.length()); calc[0] = stringHash(s); RabinKarpHashes(name, calc, 0, find.length()); int search = linearSearchRecursive(calc, findHash, 0); return search; } // Method Name : charNumericValue() // Parameters : char c // Return value(s) : returns a-z or exception // Partners : None // Description : Given a character, returns the numeric value of the character, If a letter is not passed this method should create and throw an InvalidCharacterException as provided.
private int charNumericValue(char c) throws InvalidCharacterException { char ch = Character.toUpperCase(c); if (('A' <= ch) && (ch <= 'Z')) { return ch - 'A'; } else { throw new InvalidCharacterException(c); } } // Method Name : RabinKarpHashes() // Parameters : String s, int[] hashes, int pos, int length // Return value(s) : return hash // Partners : None // Description : Finds the hash values of all substrings of size length in the String s, starting at index pos and down.
private int RabinKarpHashes(String s, int[] hashes, int pos, int length) { int hash = 0; if (pos >= (s.length() - length)) {
hash = stringHash(s.substring(pos, pos + length)); } else { hash = RabinKarpHashes(s, hashes, pos + 1, length);
int newHash = charNumericValue(s.charAt(pos)); newHash *= Math.pow(26, length - 1); hash /= 26; hash += newHash; } hashes[pos] = hash; return hash; }
// Method Name : stringHash() // Parameters : String s // Return value(s) : returns value // Partners : None // Description : Given a string, return the hash value of the entire String.
private int stringHash(String s) { int value = 0; for(int i = 0; i < s.length(); i++) { value += charNumericValue(s.charAt(i)) * Math.pow( 26, s.length() - i - 1); } return value; } // Method Name : linearSearchRecursive() // Parameters : int[] data, int key, int pos // Return value(s) : returns pos or -1 // Partners : None // Description : This is a recursive linear search. Return the position of key in the data array, or -1 if it is not present.
private int linearSearchRecursive(int[] data, int key, int pos) { if (pos >= data.length) { return - 1; } else if (data[pos] == key) { return pos; } else { return linearSearchRecursive(data, key, pos + 1); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started