Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For phase 3 of the project, we are going to read and parse data from an input file. We are also sorting the list of

For phase 3 of the project, we are going to read and parse data from an input file. We are also sorting the list of employees in ascending order by last name and first name, and the list of paychecks by period begin date. 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 an ArrayList of Employees, and of Paychecks, we will make it a Generic method.

Below is the list of modifications we are making to the project.

***Include javadoc comments for all new methods added to the project.***

PayrollSystem_Phase3 class:

The main method has been modified so that it opens an input file containing data in comma-separated format. As the data from the file is read and processed, the Company object gets populated.

Every line of data in the file begins with a field to identify the type of data: D for Department, H for HourlyEmployee, S for SalariedEmployee, and M for Manager.

You may assume that the departments data will be first in the file, so that when an employee is created, there is already a department to associate them with.

Note: Im already giving you the code to open the file for reading.

Modification 1: Write a method called parseEmployeePaychecks that has two parameters: one of type int and one of type String. The method returns an ArrayList of Paycheck elements.

The int parameter represents an employee id, whereas the String parameter represents the list of paychecks the employee has received and which the method converts to an ArrayList of Paycheck elements.

You may assume that the String parameter has the following format:

periodBeginDate1:periodEndDate1:grossAmount1:taxAmount1:bonusAmount1# periodBeginDate2:periodEndDate2:grossAmount2:taxAmount2:bonusAmount2#...

Im providing you with an algorithm for the method below:

public static ArrayList parseEmployeePaychecks(int empID, String paycheckData)

{

/*

Declare an ArrayList of Paycheck elements and initialize to an empty list.

Split the paycheckData parameter on the # to get each paycheck information.

Write a loop to iterate through the list of paycheck strings, which should have the following format: periodBeginDate:periodEndDate:grossAmount:taxAmount:bonusAmount

Calculate the netAmount to be: grossAmount taxAmount + bonusAmount

Create a Paycheck object using the employee id passed in the first parameter and the data parsed from the second parameter.

Add the Paycheck object to the ArrayList that this method returns.

After the loop, return the ArrayList of Paycheck objects.

*/

}

Modification 2: Write a method called processLineOfData that has a String parameter and the return type is void.

The parameter represents one line of data in the input file, which could be data for a Department, HourlyEmployee, SalariedEmployee, or Manager. See the note below for the expected format for each line.

Im providing you with an algorithm below:

public static void processLineOfData(String line) throws Exception

{

/*

Split the line parameter on the comma. This will return an array of values.

Get the first element in the array to determine the record type:

D -> Department

S -> SalariedEmployee

H -> HourlyEmployee

M -> Manager

If the record type is D, the line parameter has Department data:

Get the department id and name from the array.

Create a Department object passing null for the manager and list of employees.

Call the addDepartment method on the company variable passing the Department object created in step 2.

If the record type is H, the line parameter has HourlyEmployee data:

Get the employee id and the paycheck information from the array.

Call the parseEmployeePaychecks method passing the employee id and the paycheck information.

Create a new HourlyEmployee object with the returned ArrayList of Paychecks along with the rest of the employee data read from the array.

Call the addEmployeeToDepartment method on the company variable passing the department id and the HourlyEmployee object just created.

If the record type is S, the line parameter has SalariedEmployee data:

Get the employee id and the paycheck information from the array.

Call the parseEmployeePaychecks method passing the employee id and the paycheck information.

Create a new SalariedEmployee object with the returned ArrayList of Paychecks along with the rest of the employee data read from the array.

Call the addEmployeeToDepartment method on the company variable passing the department id and the SalariedEmployee object just created.

If the record type is M, the line parameter has Manager data:

Get the employee id and the paycheck information from the array.

Call the parseEmployeePaychecks method passing the employee id and the paycheck information.

Create a new Manager object with the returned ArrayList of Paychecks along with the rest of the employee data read from the array.

Call the setDepartmentManager method on the company variable passing the department id and the Manager object just created.

If the record type is none of the above, throw an Exception object with the message Bad record.

}

Data Formats in the input data file

The following is the expected format for each type of data in the input file:

Department

D,dept id,dept name

HourlyEmployee

H,employee id,first name,last name,hourly rate,period hours,department id,list of paychecks

SalariedEmployee

S,employee id,first name,last name ,salary,department id,list of paychecks

Manager

M,employee id,first name,last name ,salary,bonus,department id,list of paychecks

Each paycheck in the list of paychecks has this format:

periodBeginDate:periodEndDate:grossAmount:taxAmount:bonusAmount

INPUT FILE:

D,3,Quality Assurance

D,2,Marketing

D,1,Human Resources

D,4,Information Technology

H,1,Rosa,Ramirez,18,30,2,01/29/2018:02/02/2018:2430.50:370.75:25#02/05/2018:02/09/2018:2630.50:380.25:60#01/22/2018:01/26/2018:2500.50:375.25:50

M,6,Luisa,Lopez,65000,90,1,02/05/2018:02/09/2018:1250:250:72

H,2,Beatrice,Colton,22,40,3,02/05/2018:02/09/2018:880:132:0:#02/12/2018:02/16/2018:880:132:0:748

S,3,Steven,Estevez,50000,2,02/05/2018:02/09/2018:961.54:192.30:0

S,4,Robert,Barnes,52000,1,02/12/2018:02/16/2018:800:120:0#02/17/2018:02/23/2018:1000:200:0

S,7,Alan,Alvarez,67000,4,02/17/2018:02/23/2018:1288.45:257.70:80

M,5,Michael,Perez,67000,100,3,02/26/2018:03/02/2018:1288.45:257.70:80

H,8,Lucille,Love,22,40,4,03/05/2018:03/09/2018:880:132:0#02/26/2018:03/02/2018:880:132:0

M,9,Cesar,Cisneros,50000,150,4,03/05/2018:03/09/2018:961.54:192.30:0

PayrollSystem_Phase3.java

package payrollsystem_phase3;

import java.io.*;

import java.util.*;

import javax.swing.*;

public class PayrollSystem_Phase3

{

private static Company company;

private static JFileChooser fileChooser = new JFileChooser(".");

public static void main(String[] args)

{

// initialize the company object

company = new Company("Our Company", null);

// Ask the user what they want to do.

String options = "\t [1] to load a new data file "

+ "\t [2] to add a new department "

+ "\t [3] to add a new hourly employee "

+ "\t [4] to add a new salaried employee "

+ "\t [5] to add a new manager "

+ "\t [6] to display company information "

+ "\t [q] to quit the application";

String menuMessage = "Please enter one of the following options: " + options;

String userInput = JOptionPane.showInputDialog(null, menuMessage, "Payroll System", JOptionPane.QUESTION_MESSAGE);

while (userInput != null && !userInput.equalsIgnoreCase("q"))

{

switch (userInput)

{

case "1":

importDataFile();

break;

case "2":

promptUserForData("D");

break;

case "3":

promptUserForData("H");

break;

case "4":

promptUserForData("S");

break;

case "5":

promptUserForData("M");

break;

case "6":

System.out.println("******************************************************************");

System.out.println("********************** COMPANY INFO ******************************");

System.out.println("******************************************************************");

System.out.println(company);

break;

default:

JOptionPane.showMessageDialog(null, "Invalid entry. Please try again.",

"Invalid entry", JOptionPane.INFORMATION_MESSAGE);

break;

}

userInput = JOptionPane.showInputDialog(menuMessage);

}

JOptionPane.showMessageDialog(null, "Quitting application.");

System.exit(0);

}

private 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 inputFile = new Scanner(selectedFile))

{

// read all lines of data from the file and process them

String line;

int lineNumber = 1;

while (inputFile.hasNext())

{

line = inputFile.nextLine();

try

{

processLineOfData(line);

}

catch (Exception ex)

{

String message = "The following error occurred while processing line number "

+ lineNumber + ": " + ex.toString()

+ " Line of data skipped: " + line;

System.out.println(message);

}

lineNumber++;

}

}

catch (FileNotFoundException e)

{

System.out.println(e.toString());

}

System.out.println("Done loading data.");

}

}

private static void promptUserForData(String dataType)

{

String dialogMessage, dialogTitle;

switch (dataType)

{

case "D":

dialogMessage = "Enter department information in this format: dept id,dept name";

dialogTitle = "Department Information";

break;

case "H":

dialogMessage = "Enter hourly employee information in this format: employee id,first name, "

+ "last name,hourly rate,pay period hours,department id,list of paychecks";

dialogTitle = "Hourly Employee Information";

break;

case "S":

dialogMessage = "Enter salaried employee information in this format: employee id,first name, "

+ "last name,salary,department id,list of paychecks";

dialogTitle = "Salaried Employee Information";

break;

case "M":

dialogMessage = "Enter manager information in this format: employee id,first name,last name, "

+ "salary,bonus,department id,list of paychecks";

dialogTitle = "Manager Information";

break;

default:

return;

}

String userInput = JOptionPane.showInputDialog(null, dialogMessage, dialogTitle, JOptionPane.INFORMATION_MESSAGE);

try

{

processLineOfData(dataType + "," + userInput);

}

catch (Exception ex)

{

String errorMessage = "The following error occurred while processing the ";

errorMessage += dialogTitle.toLowerCase() + "you entered: ";

System.out.println(errorMessage + ex.getMessage());

}

}

private static void processLineOfData(String line) throws Exception

{

// provide implementation

}

/**

* The parseEmployeePaychecks method parses the String passed as an argument and returns

* an ArrayList of Paycheck objects.

* @param empID ID of employee to whom the paychecks will be added.

* @param paycheckData A String value that represents the list of paychecks an employee has

* received separated by the # sign; paycheck fields are separated by the : character.

* The following is the format for each paycheck:

* periodBeginDate:periodEndDate:grossAmount:taxAmount:bonusAmount

* @return An equivalent ArrayList of Paycheck objects.

*/

public static ArrayList parseEmployeePaychecks(int empID, String paycheckData)

{

// remove the return statement below and provide correct implementation

return null;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions

Question

List the reasons for performing sensitivity analysis.

Answered: 1 week ago