-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeUtil.java
More file actions
90 lines (66 loc) · 2.37 KB
/
EmployeeUtil.java
File metadata and controls
90 lines (66 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sorting.sortparallel;
import java.util.*;
import java.io.*;
public class EmployeeUtil {
ArrayList<Employee> listOfEmployee = new ArrayList<Employee>();
ArrayList<Employee> listOfEmployeeSorted = new ArrayList<Employee>();
Employee[] empArray;
public void readEmployeeData() throws IOException {
String lastName, firstName;
int id;
FileReader fr = new FileReader("sort-parallel-data.txt");
Scanner sc = new Scanner(fr);
// s.useDelimiter("[, ]+"); wrong! you are reading entire line
while (sc.hasNextLine()) {
String data = sc.nextLine(); // you made mistake here sc.next()
// -->wrong
Scanner s = new Scanner(data);
s.useDelimiter("[, ]+"); // you missed this! Each line should be
// parsed
lastName = s.next();
firstName = s.next();
id = s.nextInt();
Employee empObj = new Employee(lastName, firstName, id);
listOfEmployee.add(empObj);
} // end of while
sc.close();
} // end of readEmployeeData
public Employee[] sortEmpByFirstName() {
empArray = new Employee[listOfEmployee.size()];
empArray = listOfEmployee.toArray(empArray);
for (int i = 0; i < empArray.length; i++) {
for (int j = i + 1; j < empArray.length; j++) {
if (empArray[i].getFirstName().compareToIgnoreCase(empArray[j].getFirstName()) > 0) {
Employee temp = empArray[j];
empArray[j] = empArray[i];
empArray[i] = temp;
} // end of if
} // end of inner for
} // end of outer for
// loading sorted array list
for (int i = 0; i < empArray.length; i++) {
listOfEmployeeSorted.add(empArray[i]);
}
return empArray;
} // end of sortEmpByFirstName
public void printSortedEmployee() {
for (Employee e : listOfEmployeeSorted) {
System.out.println(e.getFirstName() + " " + e.getLastName() + " " + e.getId());
} // end of for
} // end of printSortedEmployee()
// Binary search on employee string
public int searchEmployee(int start, int end, Employee[] empArray, String nameToSearch) {
if (start > end)
return -1;
int midIndex = (start + end) / 2;
if (empArray[midIndex].getFirstName().compareToIgnoreCase(nameToSearch) == 0) {
return midIndex;
} // end of if
else if (empArray[midIndex].getFirstName().compareToIgnoreCase(nameToSearch) > 0) {
return searchEmployee(0, midIndex - 1, empArray, nameToSearch);
}
else {
return searchEmployee(midIndex + 1, end, empArray, nameToSearch);
}
}
}