Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with getting the filereader to do the needed choosing operation whil

ID: 3623729 • Letter: I

Question

I need help with getting the filereader to do the needed choosing operation while it is reading each line and chooses between C,H,S and then reads the next lines according to that one. I also need help with getting to bubblesort to do strings and not just integers. I'm sure that I'll need help with other stuff as I continue working on it, but this is it for now. Any help appreciated. If you need the other classes as well just let me know, and I will post them.


What the Main program needs to do

1. You will begin by declaring an array of type employee. This will contain the information read from the main data file for each employee.
2. You will open the employee file, and read each line, since there are 3 different kinds of employees, there are 3 different line formats one for each category of employee, these are:

C 1234 Sam Jones Salesman 45000.00 10.00 7000.00
S 2312 Larry Smith Manager 54000.00
H 3212 Sally Smith Decorator 6.50 Y

Data lines containing Commission based employees begin with a C, Salaried an S , and Hourly an H.
• Declare local variables for each possible data field, emp_type, empID, firstName, lastName, title, commissionRate, threshold, salary, hourly rate & overtimeEligible.
• Open a scanner to the data file (maybe called input)
• “While the data file has more data to read”
i. Read the employee type empType =( input.next()).charAt(0); reads a single character from a string..(remember next() stops at the first blank space.) Then based on the letter read, extract the remaining information from the line. For example if empType=’S’

empID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
hourlyRate = input.nextFloat();
input.nextLine();

3. Once all of the employees’ data has been read, and inserted into the array, you will need to sort the array. You may use any sorting algorithm you wish, including the bubble sort.
4. Open the data file for hourly employees, and create a scanner to that file. Each line has the following format

EmployeeID HoursWorked

IE:
3212 92.50

• Read each line of the file.
• Based on employee ID that is the first data item, write a method that performs a RECURSIVE binary search algorithm to find the correct employee, and returns the index # of the array element that contains the information for that employee.
• then use the setter to store the hours worked into the object found by the binary search.

5. Open the data file for commissionBased employees, and create a scanner to that file. Each line has the following format

EmployeeID Sales

IE:
1234 11765,00

• Read each line of the file.
• Use the binary search method to “find” the correct employee
• then use the setter to store the commission sales into the object



6. Finally, it is time to calculate pay, and print the report.
i. For each employee in your array, loop through all elements applying the calculatePay() method to each
ii. Print the report title, and the column headings for commission based employees.
iii. You will begin looping through all of the employees in your array
iv. While the employee type of the employee retrieved is “C”
1. For each employee call the print() method to print out the object contents.
2. add this employees total Pay to the total pay for all employees of this type
v. When an employee of type hourly, is found, Print the total pay for the previous type, then print column headings for hourly employees
vi. While the employee type of the employee is “H”
1. For each employee call the print() method to print out the object contents.
2. add this employees total Pay to the total pay for all employees of this type
vii. When an employee of type salaried is retrieved, Print the total pay for the previous type, then print column headings for salaried employees
1. For each employee call the print() method to print out the object contents.
2. add this employees total Pay to the total pay for all employees of this type

MAIN

import java.util.*;
import java.io.*;
public class Employee_test
{


public static void main(String[]agrs) throws IOException
{
Employee[] emp = new Employee[];

public char type;
public int ID;
public String firstName;
public String lastName;
public String title;
public Float commissionRate;
public Float threshold;
public Float salary;
public Float hourlyRate;
public Boolean overtimeEligible;

while
{
FileReader input = new FileReader("Employee.dat");
}
type = input.next().charAt(0);

if (type=='S')
{
ID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
salary = input.nextFloat();
input.nextLine();

else if(type =='H')
ID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
hourlyRate = input.nextFloat();
overtimeEligible = input.nextBoolean();
input.nextLine();

else (type == 'C')
ID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
salary = input.nextInt();
commissionRate = input.nextFloat();
threshold = input.nextFloat();
input.nextLine();
}
}
}

public static void bubbleSort(int[] list)
{
boolean swapped;
do
{
swapped = false;
for(int i=1; i < list.length; i++)
{
if(list[i-1]>list[i])
{
int temp =list[i];
list[i] = list[i-1];
list[i-1] = temp;
swapped = true;
}
}


}while (swapped);
}

Explanation / Answer

Sorting the employees with bubble sort: You can keep the code for sorting ints, and change
"if (list[i-1]>list[i])"
to
"if (list[i-1].getLastName().compareTo(list[i].getLastName()) > 0 ||
     (list[i-1].getLastName().compareTo(list[i].getLastName() == 0 &&
      list[i-1].getFirstName().compareTo(list[i].getFirstName() > 0))"

This will compare Employee names by alphabetical order instead of comparing the size of ints. If a and b are String objects, a.compareTo(b) is -1 if a is earlier in alphabetical order than b, 1 if a is later than b, and 0 if a equals b. My code also checks the first names if the last names are the same. Also replace "int" with "Employee" everywhere it occurs in the bubble sort code. That's all you need to do.

------------------

The code that branches based on employee type should look like this:

if (type=='S')
{
empID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
salary = input.nextFloat();
input.nextLine();

} else if(type =='H') {
empID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
hourlyRate = input.nextFloat();
String overtimeEligibleString = input.next();
overtimeEligible = overtimeEligibleString.equals("Y");
input.nextLine();

} else if (type == 'C') {
empID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
salary = input.nextInt();
commissionRate = input.nextFloat();
threshold = input.nextFloat();
input.nextLine();

} else { //If we didn't read a H, C, or S, then skip this line
input.nextLine();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote