generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeeFilter.java
71 lines (62 loc) · 2.42 KB
/
EmployeeFilter.java
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class EmployeeFilter {
public static void main(String[] args) {
String inputFile = "employees.csv";
String outputManagerFile = "managers.csv";
String outputSingleNameFile = "single_name_employees.csv";
List<String[]> employees = readEmployees(inputFile);
List<String[]> managers = filterManagersInRnD(employees);
writeEmployees(managers, outputManagerFile);
List<String[]> singleNameEmployees = filterSingleNameEmployees(employees);
writeEmployees(singleNameEmployees, outputSingleNameFile);
}
private static List<String[]> readEmployees(String inputFile) {
List<String[]> employees = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] employee = line.split(",");
employees.add(employee);
}
} catch (IOException e) {
e.printStackTrace();
}
return employees;
}
private static List<String[]> filterManagersInRnD(List<String[]> employees) {
List<String[]> managersInRnD = new ArrayList<>();
for (String[] employee : employees) {
String role = employee[1];
String division = employee[2];
if (role.equals("Manager") && division.equals("R&D")) {
managersInRnD.add(employee);
}
}
return managersInRnD;
}
private static List<String[]> filterSingleNameEmployees(List<String[]> employees) {
List<String[]> singleNameEmployees = new ArrayList<>();
for (String[] employee : employees) {
String name = employee[0];
if (!name.contains(" ")) {
singleNameEmployees.add(employee);
}
}
return singleNameEmployees;
}
private static void writeEmployees(List<String[]> employees, String outputFile) {
try (FileWriter writer = new FileWriter(outputFile)) {
for (String[] employee : employees) {
String line = String.join(",", employee) + "\n";
writer.write(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}