Question

Modification to be completed by group member 4: In the processLineOfData method, write the code to...

Modification to be completed by group member 4:

In the processLineOfData method, write the code to handle case "M" of the switch statement such that:

  • A Manager object is created using the firstName, lastName, salary, and bonus local variables. Notice that salary and bonus need to be converted from String to double. You may use parseDouble method of the Double class as follows:

              Double.parseDouble(salary)

  • Call the parsePaychecks method in this class passing the Manager object created in the previous step and the checks variable.

                

  • Call the findDepartment method on the company variable passing deptName.

                

  • If a department is found, call the setDepartmentManager method on it passing the Manager object created in the first step.

Code:

package payrollsystem_phase3;

import java.io.*;
import java.util.*;

/*
* [student name] - Group member 2
* [student name] - Group member 3
* [student name] - Group member 4
*/

public class PayrollSystem_Phase3
{
private static Company company;
  
public static void main(String[] args)
{
// initialize the company object
company = new Company("Our Company");

// import data from csv file
importDataFile();
  
System.out.println("\n" + company.toString());
}
  
  
private static void importDataFile()
{
System.out.println("Loading new data file");

// Local variable declarations
String line;
int lineNumber = 1;
File selectedFile = new File("./input.txt");

// 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
while (inputFile.hasNext())
{
line = inputFile.nextLine();
try
{
processLineOfData(line);
}
catch (Exception ex)
{
String message = "The following error occurred while processing line number "
+ lineNumber + ": " + ex.getMessage()
+ "\nLine 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 processLineOfData(String line) throws Exception
{
// Split the line parameter on the comma.
String[] lineElements = line.split(",");
  
// Get the first field to determine the record type:
// D -> Department
// S -> SalariedEmployee
// H -> HourlyEmployee
// M -> Manager
  
String recordType = lineElements[0];

// Local variable declarations
String deptName, firstName, lastName, salary, bonus, rate, hours, checks;
Department dept;
  
  
switch (recordType)
{
case "D":
deptName = lineElements[1];
  
// create Department
dept = new Department(deptName, null);
  
// add department to the company
company.addDepartment(dept);
  
break;
  
case "H":
firstName = lineElements[1];
lastName = lineElements[2];
rate = lineElements[3];
hours = lineElements[4];
deptName = lineElements[5];
checks = lineElements[6];
  
// to be completed by group member 2:
  
// create HourlyEmployee

// add paychecks to employee
  
// find the department
  
// if dept is found, add employee to it
  
break;

case "S":

firstName = lineElements[1];
lastName = lineElements[2];
salary = lineElements[3];
deptName = lineElements[4];
checks = lineElements[5];
  
// to be completed by group member 3:
  
// create SalariedEmployee
  
// add paychecks to employee

// find the department
  
// if dept is found, add employee to it
  
break;
  
case "M":
  
firstName = lineElements[1];
lastName = lineElements[2];
salary = lineElements[3];
bonus = lineElements[4];
deptName = lineElements[5];
checks = lineElements[6];
  
// to be completed by group member 4:
  
// create Manager
  
// add paychecks to manager
  
// find the department
  
// if dept is found, add manager to it
  
break;
  
default:
throw new Exception("Bad record");
}   
  
}
  
  
/**
* The parsePaychecks method parses the String passed as an argument and adds the
* paychecks to the employee.
* @param emp 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
*/
public static void parsePaychecks(Employee emp, String paycheckData)
{
// Local variable declarations
String[] arrayOfPaychecks, paycheckFields;
  
arrayOfPaychecks = paycheckData.split("#");
  
// Iterate through the arrayOfPaychecks, where each paycheck should have the following format:
// periodBeginDate:periodEndDate
for (String paycheckString : arrayOfPaychecks)
{
paycheckFields = paycheckString.split(":");   
emp.addPaycheck( paycheckFields[0], paycheckFields[1]);
}

}
  
}

Mo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package payrollsystem_phase3;

import java.io.*;
import java.util.*;

/*
* [student name] - Group member 2
* [student name] - Group member 3
* [student name] - Group member 4
*/

public class PayrollSystem_Phase3
{
private static Company company;
  
public static void main(String[] args)
{
// initialize the company object
company = new Company("Our Company");

// import data from csv file
importDataFile();
  
System.out.println("\n" + company.toString());
}
  
  
private static void importDataFile()
{
System.out.println("Loading new data file");

// Local variable declarations
String line;
int lineNumber = 1;
File selectedFile = new File("./input.txt");

// 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
while (inputFile.hasNext())
{
line = inputFile.nextLine();
try
{
processLineOfData(line);
}
catch (Exception ex)
{
String message = "The following error occurred while processing line number "
+ lineNumber + ": " + ex.getMessage()
+ "\nLine 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 processLineOfData(String line) throws Exception
{
// Split the line parameter on the comma.
String[] lineElements = line.split(",");
  
// Get the first field to determine the record type:
// D -> Department
// S -> SalariedEmployee
// H -> HourlyEmployee
// M -> Manager
  
String recordType = lineElements[0];

// Local variable declarations
String deptName, firstName, lastName, salary, bonus, rate, hours, checks;
Department dept;
  
  
switch (recordType)
{
case "D":
deptName = lineElements[1];
  
// create Department
dept = new Department(deptName, null);
  
// add department to the company
company.addDepartment(dept);
  
break;
  
case "H":
firstName = lineElements[1];
lastName = lineElements[2];
rate = lineElements[3];
hours = lineElements[4];
deptName = lineElements[5];
checks = lineElements[6];
  
// to be completed by group member 2:
  
// create HourlyEmployee

// add paychecks to employee
  
// find the department
  
// if dept is found, add employee to it
  
break;

case "S":

firstName = lineElements[1];
lastName = lineElements[2];
salary = lineElements[3];
deptName = lineElements[4];
checks = lineElements[5];
  
// to be completed by group member 3:
  
// create SalariedEmployee
  
// add paychecks to employee

// find the department
  
// if dept is found, add employee to it
  
break;
  
case "M":
  
firstName = lineElements[1];
lastName = lineElements[2];
salary = lineElements[3];
bonus = lineElements[4];
deptName = lineElements[5];
checks = lineElements[6];
  
// to be completed by group member 4:
  
// create Manager
Manager mRef=new Manager(firstName, lastName, Double.parseDouble(salary),Double.parseDouble(bonus));
  
// add paychecks to manager
parsePaychecks(mRef);
  
// find the department
boolean isFound=company.findDepartment(deptName);
  
// if dept is found, add manager to it
if(isFound){
setDepartmentManager(mRef);
}
  
break;
  
default:
throw new Exception("Bad record");
}   
  
}
  
  
/**
* The parsePaychecks method parses the String passed as an argument and adds the
* paychecks to the employee.
* @param emp 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
*/
public static void parsePaychecks(Employee emp, String paycheckData)
{
// Local variable declarations
String[] arrayOfPaychecks, paycheckFields;
  
arrayOfPaychecks = paycheckData.split("#");
  
// Iterate through the arrayOfPaychecks, where each paycheck should have the following format:
// periodBeginDate:periodEndDate
for (String paycheckString : arrayOfPaychecks)
{
paycheckFields = paycheckString.split(":");   
emp.addPaycheck( paycheckFields[0], paycheckFields[1]);
}

}
  
}

Screenshot of code

case M: + 0 of 0 firstName = lineElements[1]; lastName = lineElements[2]; salary = lineElements[3]; bonus = lineElements[4]

Add a comment
Know the answer?
Add Answer to:
Modification to be completed by group member 4: In the processLineOfData method, write the code to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • In the processLineOfData method, write the code to handle case "H" of the switch statement such...

    In the processLineOfData method, write the code to handle case "H" of the switch statement such that: • An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows: Double.parseDouble(rate) Call the parsePaychecks method in this class passing the Hourly Employee object created in the previous step and the checks variable. Call the...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • C++ edit: You start with the code given and then modify it so that it does...

    C++ edit: You start with the code given and then modify it so that it does what the following asks for. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each...

    HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • By editing the code below to include composition, enums, toString; must do the following: Prompt the...

    By editing the code below to include composition, enums, toString; must do the following: Prompt the user to enter their birth date and hire date (see Fig. 8.7, 8.8 and 8.9 examples) in addition to the previous user input Create a new class that validates the dates that are input (can copy date class from the book) Incorporate composition into your class with these dates Use enums to identify the employee status as fulltime (40 or more hours worked for...

  • In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate...

    In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate the pension to date as the equivalent of a monthly salary for every year in service. 4) Print the calculated pension for all employees. ************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************ import java.io.*; public class ManagerTest { public static void main(String[] args) { InputStreamReader...

  • Add an HourlyPlusCommissionEmployee class to the PayrollSystem app by subclassing an existing class. A HourlyPlusCommissionEmployee is...

    Add an HourlyPlusCommissionEmployee class to the PayrollSystem app by subclassing an existing class. A HourlyPlusCommissionEmployee is a kind of CommissionEmployee with the following differences and specifications: HourlyPlusCommissionEmployee earns money based on 2 separate calculations: commissions are calculated by the CommissionEmployee base class hourly pay is calculated exactly the same as the HourlyEmployee class, but this is not inherited, it must be duplicated in the added class BasePlusCommissionEmployee inherits from CommissionEmployee and includes (duplicates) the details of SalariedEmployee. Use this as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT