Question

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 on the company variable passing deptName.
  • If a department is found, call the addEmployee method on it passing the HourlyEmployee object created in the first step.

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]);
}

}
  
}

package payrollsystem_phase3;


public class SalariedEmployee extends Employee
{
// instance variable
private double annualSalary;
  
public SalariedEmployee()
{
annualSalary = 50000.0;
}
  
public SalariedEmployee(String first, String last, double salary)
{
super(first, last);

// set annualSalary if the salary parameter is within range. Otherwise, throw exception
}
  
  
@Override
public double calculateGrossAmount()
{
return annualSalary / 52;
}
  
  
  
public double getAnnualSalary()
{
return annualSalary;
}
  
public void setAnnualSalary(double salary)
{
// set annualSalary if the salary parameter is within range. Otherwise, throw exception
}


@Override
public String toString()
{
return super.toString() +
String.format( "\n%-30s %s",
"Annual Salary:", annualSalary);
}
  
}

package payrollsystem_phase3;


/**
* The HourlyEmployee class is a subclass of the Employee class. It represents employees
* that get paid by the hour.
*
*/
public class HourlyEmployee extends Employee
{
// instance variables
private double hourlyRate;
private double periodHours;
  
  
public HourlyEmployee()
{
hourlyRate = 17.5;
periodHours = 30;
}
  
  
public HourlyEmployee(String first, String last, double rate, double periodHrs) throws Exception
{
super(first, last);

if( rate >= 10.50)
hourlyRate = rate;
else
throw new Exception("Hourly rate must be greater than or equal to 10.50");
  
periodHours = periodHrs;
}
  
@Override
public double calculateGrossAmount()
{
return hourlyRate * periodHours;
}
  
public double getHourlyRate()
{
return hourlyRate;
}
  
public double getPeriodHours()
{
return periodHours;
}
  
public void setHourlyRate(double rate)
{
// set hourlyRate if the rate parameter is greater than or equal to 10.50. Otherwise, throw exception
}
  
  
public void setPeriodHours(double periodHrs)
{
periodHours = periodHrs;
}

@Override
public String toString()
{
return super.toString() +
String.format( "\n%-30s %s \n%-30s %s",
"Hourly Rate:", hourlyRate,
"Period Hours:", periodHours );
}
  
}

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

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.



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
    // STEP1: convert rate and hours to double
    double hourlyRate=Double.parseDouble(rate);
    double numHours=Double.parseDouble(hours);
    // STEP2: Create a new object for hourly employee
    HourlyEmployee employee=new HourlyEmployee(firstName, lastName, hourlyRate, numHours);

    // STEP3: add paychecks to employee by passing the employee and checks as the strings
    parsePaychecks(employee,checks);

    // STEP4: find the department
    dept=company.findDepartment(deptName);

    //STEP5: If a department is found, call the addEmployee method on it
    // passing the HourlyEmployee object created in the first step.
    if(dept!=null){
        dept.addEmployee(employee);
    }
    break;

If there are any doubts, comment down here. We are right here to help you. Happy Learning..!! PLEASE give an UPVOTE I Have Pu

Add a comment
Know the answer?
Add Answer to:
In the processLineOfData, write the code to handle case "H" of the switch statement such that:...
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 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...

  • 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...

  • I need help in getting the second output to show both the commission rate and the...

    I need help in getting the second output to show both the commission rate and the earnings for the String method output package CompemsationTypes; public class BasePlusCommissionEmployee extends CommissionEmployee { public void setGrossSales(double grossSales) { super.setGrossSales(grossSales); } public double getGrossSales() { return super.getGrossSales(); } public void setCommissionRate(double commissionRate) { super.setCommissionRate(commissionRate); } public double getCommissionRate() { return super.getCommissionRate(); } public String getFirstName() { return super.getFirstName(); } public String getLastName() { return super.getLastName(); } public String getSocialSecurityNumber() { return super.getSocialSecurityNumber(); } private...

  • 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...

  • 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...

  • 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;    }   ...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • 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...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • 8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private,...

    8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private, and add appropriate accessor methods to the class named getFirstName, getMiddleInitial, and getLastName. code: class Name { private String firstName; private String middleNames; private String lastName;    //Constructor method public Name(String firstName, String middleNames, String lastName) { this.firstName = firstName; this.middleNames = middleNames; this.lastName = lastName;    } //Accessor for firstName public String getFirstName() { return firstName; } //Accessor for middleNames public String getMiddlesNames()...

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