Here is my main program, I\'m having some trouble with getting the array to read
ID: 3623801 • Letter: H
Question
Here is my main program, I'm having some trouble with getting the array to read from the Employee class, so I need help with that. Also if you could help out with the part under #4 of what the program needs to do(instructions below the code) that would be great. And finally I am sure there are plenty of other errors in the program, so if you could take a look and find them that would be awesome.
import java.util.*;
import java.io.*;
public class Employee_test
{
public static void main(String[]agrs) throws IOException
{
Employee[] emp = new Employee[];
private char type;
private int ID;
private String firstName;
private String lastName;
private String title;
private Float commissionRate;
private Float threshold;
private Float salary;
private Float hourlyRate;
private Boolean overtimeEligible;
Scanner input = new Scanner(new FileReader("Employee.dat"));
ArrayList temp = new ArrayList();
while(input.hasNext())
{
temp.add(input.next());
}
// create array from list
Employee[] emp = new Employee[temp.size()];
for(Employee i = 0; i < emp.length; i++)
{
emp[i] = temp.get(i);
}
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();
String overtimeEligibleString = input.next();
overtimeEligible = overtimeEligibleString.equals("Y");
input.nextLine();
}
else if (type == 'C')
{
ID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
salary = input.nextFloat();
commissionRate = input.nextFloat();
threshold = input.nextFloat();
input.nextLine();
}
else
{
input.nextLine();
}
}
public static void bubbleSort(Employee[] emp)
{
boolean swapped;
do
{
swapped = false;
for(Employee i=1; i < emp.length; i++)
{
if(emp[i-1].getLastName().compareTo(emp[i].getLastName())>0 ||
emp[i-1].getLastName().compareTo(emp[i].getLastName() == 0 &&
emp[i-1].getFirstName().compareTo(emp[i].getFirstName() >0)))
{
Employee temp =emp[i];
emp[i] = emp[i-1];
emp[i-1] = temp;
swapped = true;
}
}
}while (swapped);
}
}
Instructions for Main
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.
//I have the binary search written which is at the bottom I have posted at the bottom of#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.
public static int BinarySearch(int[] list, int value) {
int first = 0;
int last = list.length-1;
int mid = 0;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid] == value)
found = true;
else if (list[mid] > value)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
_______________________________________________________________________________________
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
Once all employees are retrieved print the total pay for salaried employees, and your program is finished.
Explanation / Answer
I fixed some problems with reading the data into the emp array. First of all, the part that reads in each employee's data, branching on the employee type, has to occur within a while loop. The way you had it, it would only happen once, whereas it needs to happen repeatedly, for each employee. Secondly, you need to create a new Employee object for each line that's read in. Here's the code with those problems fixed:
List<Employee> temp = new ArrayList<Employee>();
while(input.hasNext()) {
// Set default values
commissionRate = 0;
threshold = 0;
salary =0;
hourlyRate=0;
overtimeEligible=false;
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();
String overtimeEligibleString = input.next();
overtimeEligible = overtimeEligibleString.equals("Y");
input.nextLine();
}
else if (type == 'C')
{
ID = input.nextInt();
firstName = input.next();
lastName = input.next();
title = input.next();
salary = input.nextFloat();
commissionRate = input.nextFloat();
threshold = input.nextFloat();
input.nextLine();
}
else
{
input.nextLine();
}
// Create a new Employee object according to the data that was read in,
// and add it to our list.
// NOTE: Since you didn't show me your Employee code I didn't know the right order
// for the arguments to the Employee constructor, so you might have to switch them around.
temp.add(new Employee(id,firstName,lastName,title,hourlyRate,overtimeEligible,salary,commissionRate,threshold));
}
Employee[] emp = new Employee[temp.size()];
for(int i = 0; i < emp.length; i++)
{
emp[i] = temp.get(i);
}
-----------------------------------
Here is help with #4. This is a recursive method (it contains a call to itself). The one you had highlighted was iterative not recursive.
public static int searchEmployeesByID(Employee[] emps, int id) {
return searchEmployeesByID(emps, id, 0, emps.length);
}
// The 'begin' and 'end' arguments specify the range of indexes to search in.
// The search includes begin but excludes end.
public static int searchEmployeesByID(Employee[] emps, int id, int begin, int end) {
int mid = (end + begin) / 2;
if (mid == begin) {
if (emps[begin].getID() != id) {
System.out.println("Warning: employee "+id+" not found in array");
}
return mid;
}
if (emps[mid].getID() > id) {
return searchEmployeesByID(emps,id,begin,mid);
} else {
return searchEmployeesByID(emps,id,mid,end);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.