Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Payroll System - Phase 4

dickersonka
dickersonka 


For assignment number 4, we are going to add data persistence through the use of serialization. We are also sorting the list of employees in ascending order by last name and first name, and the list of paychecks by pay date. We are adding functionality to be able to search for an Employee with a given last name. Lastly, we are going to add an implementation of a linked list. To sort the list of employees and paychecks, we are adding a method that implements the bubble sort algorithm. Since we want the same method to be able to sort both an ArrayList of Employees and an ArrayList of Paychecks, we will make it a Generic method. For the search functionality, we are implementing the recursive binary search algorithm. Below is the list of modifications we are making to the project (feel free to use the solution I posted in moodle for phase 3). Employee class: Modification 1: Make the class implement the Serializable interface. Modification 2: Make the class implement the Comparable interface. Modification 3: Provide an implementation for the compareTo method such that it compares the value of the lastName instance variable of the two objects. If the lastName variable of the calling object is greater, it returns 1, if it’s smaller it returns -1, and if they both have the same value, the method then compares the first name. If they have the same value for the firstName variable, it returns 0, if the firstName variable of the calling object is greater, it returns 1, else it returns -1. Notes: • You may use the compareTo method of the String class. • Include the documentation comment for the method. The following is the method signature: public int compareTo(Employee employee) { // provide implementation } Modification 4: Modify the toString method so that, first of all, it sorts the list of paychecks and then, it iterates through them to print them one at a time. We don’t want to include the [] around the elements in the ouput. Note: • You’ll do this after adding the sortArrayList method to the PayrollUtility class. Paycheck class: Modification 1: Make the class implement the Serializable interface. Modification 2: Make the class implement the Comparable interface. Modification 3: Provide an implementation for the compareTo method such that it compares the value of the payDate instance variable of the two objects. If the payDate variable of the calling object is greater, it returns 1, if it’s smaller it returns -1, and if they both have the same value, the method returns 0. Note: • You may use the compareTo method of the String class. The following is the method signature: public int compareTo(Paycheck paycheck) { // provide implementation } Department class: Modification 1: Make the class implement the Serializable interface. Modification 2: Modify the toString method so that, first of all, it sorts the list of employees and then, it iterates through them to print them one at a time. We don’t want to include the [] around the elements in the ouput. Note: • You’ll do this after adding the sortArrayList method to the PayrollUtility class. Company class: Modification 1: Make the class implement the Serializable interface. Modification 2: Change the data type of the departmentList instance variable from ArrayList to DepartmentLinkedList. Modification 3: In the generatePayroll method, since departmentList is no longer an ArrayList, change this: for(Department dept: departmentList) { … to this: Department dept; for(int i=0; i < departmentList.size(); i++) { dept = departmentList.getDepartment(i); … DepartmentLinkedList class: Modification 1: Create a class called DepartmentLinkedList that implements a linked list containing a Department object inside each node. Use the LinkedList1 class in the book as a guide (Code Listing 20-2 in page 1178) keeping in mind that the class in the book has a String value inside each node, while your version has a Department object inside each node. Modification 2: Make both the DepartmentLinkedList and the Node classes implement the Serializable interface. Modification 3: Add a find method that has one parameter of type Department and also returns a Department object. This method traverses the nodes in the list looking for a node whose value is the same as the Department object passed as an argument. If such node is found, the node’s value is returned. Otherwise, the method returns null. public Department find(Department dept) { } Modification 4: Add a getDepartment method that has one parameter of type int and returns the Department object stored in the node at the position specified by the index parameter. public Department getDepartment(int index) { } PayrollUtility class Modification 1: Add a public static method called serializeObject that has two parameters the first one is of type Object and that is the object that needs to be serialized. The second parameter is of type String and that is the file name that the serialized object is being written to. This method returns no value. The following is the method signature: public static void serializeObject(Object objectToSerialize, String serializationFileName) Modification 2: Add a public static method called deSerializeObject that has one parameter of type String, which is the file name that the serialized object is being read from. It returns the deserialized object read from the file. The following is the method signature: public static Object deSerializeObject(String serializationFileName) Important: Both of these methods handle the exceptions: they have try and catch blocks and do not have a throws clause after the method name. They also have a finally clause where they call the close method on the FileOutputStream, ObjectOutputStream, FileInputStream, and ObjectInputStream variables respectively. Alternatively, you may use a try-with-resources block. Modification 3: Add a new public, generic method called sortArrayList that has a type parameter declaration to specify that the elements of the ArrayList parameter have to be of a class that implements the Comparable interface. (see section 17.4 of the textbook) This method uses the bubble sort algorithm explained in section 16.1. In particular, you may use Code-Listing 16-3 of the textbook as a guide, except that you are sorting elements in an ArrayList, not an array. public static < E extends Comparable > void sortArrayList (ArrayList list) { int lastPos; int index; E temp; // finish the implementation } Modification 4: Add a new private, generic method called binarySearch that has a type parameter declaration to specify that the elements of the ArrayList parameter have to be of a class that extends the Employee class. This method implements the recursive binary search algorithm explained in section 16.2 of the book to look for elements that have the same value for the last name field as the value passed in the searchLastName method parameter. private static < E extends Employee > int binarySearch(ArrayList list, int firstElem, int lastElem, String searchLastName) { // provide implementation } Modification 5: Add the following public, generic method called search that only calls the binarySearch method. The complete implementation is below: public static < E extends Employee > int search(ArrayList list, String searchLastName) { return binarySearch( list, 0, list.size() - 1, searchLastName ); } Modification 6: Overload the findDepartment method so that there is another version with DepartmentLinkedList as the type of the first parameter, and Department as the type of the second parameter. All this method does is call the find method of the DepartmentLinkedList class and return the value returned by find. public static Department findDepartment(DepartmentLinkedList departmentList, Department dept) { } Attachments Tags Clarifications

Answers

Java - Payroll System - Phase 4 - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - Payroll System - Phase 4 assignment including source and comments Attachments
payrollsystem_phase4.zip (21 K) Download
Preview
The value, as an ArrayList of Department elements, to store * in the list of departments field for this company. */ public void setDepartmentList(DepartmentLinkedList departments) { departmentList = departments; } /** * The generatePayroll method generates the payroll for all the employees in the company * for the

Preview
list of employees and a manager. * * @author Mayelin */ public class Department implements Serializable { // instance variables private String departmentID; private String departmentName; private Manager departmentManager; private ArrayList<Employee> listOfEmployees; /** * This constructor sets the department's id and name, as well as the list of * employees

Preview
== null){ //set next to head node.setNext( head ); //set this node to be first head = node; } else { //get current node Node tmp = head; //loop until end while(tmp.getNext() != null) { //advance next tmp

Preview
identification number. * @param first The employee's first name. * @param last The employee's last name. * @param birth The employee's date of birth. * @param hired The date the employee was hired. */ public Employee(String id, String first, String last, String birth, String hired) { employeeID = id; firstName

Preview
/** * This constructor sets the hourly employee's id, name, date of birth, date hired, * accrued vacation hours, amount earned so far this year, list of * paychecks received, hourly rate, and period hours. * @param id The employee's identification number. * @param first The employee's first

Preview
Manager extends SalariedEmployee { // instance variable private double weeklyBonus; /** * This constructor sets the manager's id, name, date of birth, date hired, * accrued vacation hours, amount earned so far this year, list of * paychecks received, annual salary, and weekly bonus. * @param id

Preview
@param department department */ public Node(Department department){ this.department = department; //initialize next this.next = null; } /** * Get next node * @return node */ public Node getNext() {

Preview
* The calculateTaxDeduction method calculates the tax amount that a person * gets deducted during a pay period. * @return The tax amount deducted for a pay period. */ double calculateTaxDeduction(); /** * The calculateVacationHours method calculates the number of hours that an

Preview
amount field for this paycheck. */ public void setTaxAmount(double taxAmount) { this.taxAmount = taxAmount; } /** * The setBonusAmount method sets the bonus amount for this paycheck. * @param bonusAmount The value to store in the bonus amount field for this paycheck. */ public void setBonusAmount(double bonusAmount) { this.bonusAmount

Preview
PayrollUtility.serializeObject(company, serializationFileName); System.exit(0); } public static void importDataFile() { System.out.println("Loading new data file"); // Prompt the user to select an input data file File selectedFile; if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { selectedFile = fileChooser.getSelectedFile(); // try with resources. The inputFile Scanner object will be closed automatically. try (Scanner

Preview
objectToSerialize object to serialize * @param serializationFileName save to filename */ public static void serializeObject(Object objectToSerialize, String serializationFileName){ try { //open file as stream OutputStream file = new FileOutputStream(serializationFileName); OutputStream buffer = new BufferedOutputStream(file); ObjectOutput output = new ObjectOutputStream(buffer); //write object to file output.writeObject(objectToSerialize); //close file output.close(); } catch

Preview
called from the constructor setAnnualSalary(salary); } /** * This constructor sets the salaried employee's id, name, date of birth, date hired, and annual salary. * The accrued vacation hours and year to date amount are set to 0; the list of * paychecks received is set to

Preview
D,1,Human Resources D,2,Quality Assurance

PayrollSystemPhase4_output.png (17 K) Download Preview

Screenshots
Screenshot
Purchase Answer