-
-
Notifications
You must be signed in to change notification settings - Fork 756
Expand file tree
/
Copy pathEmployeeService.java
More file actions
60 lines (52 loc) · 2.27 KB
/
EmployeeService.java
File metadata and controls
60 lines (52 loc) · 2.27 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
import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
class EmployeeService {
// This list is populated in the tests
private List<Optional<Employee>> nullableEmployeesList = new ArrayList<>();
public EmployeeService(List<Optional<Employee>> listOfEmployees) {
nullableEmployeesList = listOfEmployees;
}
public Optional<Employee> getEmployeeById(int employeeId) {
return nullableEmployeesList
.stream()
.flatMap(employee -> employee.stream())
.filter(employee -> employee.getNullableId()
.map(id -> id == employeeId)
.orElse(false))
.findFirst();
}
/* I could use IntStream.range(0, nullableEmployeesList.size()) instead of a for loop, but
understanding the Optional API is difficult enough.
I do not use method references for the same reason. */
public String printAllEmployeesNames() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < nullableEmployeesList.size(); i++) {
stringBuilder.append(i).append(" - ");
nullableEmployeesList.get(i)
.flatMap(employee -> employee.getNullableName())
.ifPresentOrElse(
name -> stringBuilder.append(name).append("\n"),
() -> stringBuilder.append("No employee found\n")
);
}
return stringBuilder.toString();
}
public String printEmployeeNameAndDepartmentById(int employeeId) {
Optional<Employee> employee = getEmployeeById(employeeId);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(employeeId).append(" - ");
// Handle Optional values
employee.ifPresentOrElse(
e -> {
e.getNullableName().ifPresent(name ->
e.getNullableDepartment().ifPresent(department ->
stringBuilder.append(name).append(" - ").append(department)
)
);
},
() -> stringBuilder.append("No employee found")
);
return stringBuilder.toString();
}
}