Skip to content

Commit 4c5e540

Browse files
committed
Simplify the solution and the asked methods so streams are not used.
1 parent f94749a commit 4c5e540

File tree

7 files changed

+129
-74
lines changed

7 files changed

+129
-74
lines changed

exercises/concept/tim-from-marketing-2/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
</classpathentry>
2929
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
3030
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
31-
<classpathentry kind="output" path="bin"/>
31+
<classpathentry kind="output" path="bin/default"/>
3232
</classpath>

exercises/concept/tim-from-marketing-2/.docs/instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Assume that the ID of the first employee is 0, the ID of the second employee is
66
The class constructor receives a parameter of type List<Optional<Employee>>, which is populated in the tests.
77

88
## 1.- Print the names of all the employees
9+
910
Implement the `printAllEmployeesNames()` method to print the names of all the employees, together with their id. If the employee does not exist, print "[id] - No employee found".
1011

1112
```java
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
# Introduction
22

33
%{concept:optional-types}
4-
5-
%{concept:flatMap-operation}

exercises/concept/tim-from-marketing-2/.meta/design.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ This Concept Exercise's prerequisites Concepts are:
3131
- `custom classes`.
3232
- `lists`.
3333
- `generic-types`.
34-
- `streams`.
3534

3635
## Analyzer
3736

3837
This exercise could benefit from the following rules in the [analyzer]:
39-
- `actionable`: If the solution uses `null` in any method, encourage the student to use `Optional<T>` instead.
38+
- `essential`: If the solution uses `null` in any method, encourage the student to use `Optional<T>` instead.
4039
- `actionable`: If the solution uses the `get` or `isPresent` methods of the Optional<T> API, encourage the student to use `orElse`, `orElseThrow` or `ifPresentOrElse` instead.
4140
- `informative`: TODO.
4241

exercises/concept/tim-from-marketing-2/.meta/src/reference/java/Employee.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import java.util.Optional;
33

44
class Employee {
5-
private final int id;
5+
private final int id;
66
private final String name;
77
private final String department;
88

@@ -12,22 +12,24 @@ public Employee(int id, String name, String department) {
1212
this.department = department;
1313
}
1414

15-
public Optional<Integer> getNullableId() {
16-
return Optional.ofNullable(id);
15+
public int getId() {
16+
return id;
1717
}
1818

19-
public Optional<String> getNullableName() {
20-
return Optional.ofNullable(name);
19+
public String getName() {
20+
return name;
2121
}
2222

23-
public Optional<String> getNullableDepartment() {
24-
return Optional.ofNullable(department);
23+
public String getDepartment() {
24+
return department;
2525
}
2626

2727
@Override
2828
public boolean equals(Object o) {
29-
if (this == o) return true;
30-
if (o == null || getClass() != o.getClass()) return false;
29+
if (this == o)
30+
return true;
31+
if (o == null || getClass() != o.getClass())
32+
return false;
3133
Employee employee = (Employee) o;
3234
return id == employee.id &&
3335
Objects.equals(name, employee.name) && Objects.equals(department, employee.department);

exercises/concept/tim-from-marketing-2/.meta/src/reference/java/EmployeeService.java

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import java.util.List;
2+
import java.util.ArrayList;
3+
import java.util.Optional;
4+
import java.util.Objects;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
List<Employee> employees = new ArrayList<>();
9+
employees.add(new Employee(1, "Tim", "Marketing"));
10+
employees.add(new Employee(2, "Joe", "Sales"));
11+
employees.add(new Employee(3, "Jane", "IT"));
12+
employees.add(new Employee(4, null, null));
13+
14+
StringBuilder stringBuilder = new StringBuilder();
15+
EmployeeService employeeService = new EmployeeService(employees);
16+
17+
int employeeId = 1;
18+
Optional<Employee> employee = employeeService.getEmployeeById(employeeId);
19+
employee.ifPresentOrElse(
20+
employee1 -> {
21+
Optional.ofNullable(employee1.getName())
22+
.ifPresentOrElse(name -> System.out.println(stringBuilder.append(employeeId).append(" - ")
23+
.append(employee1.getName()).append(" - ")
24+
.append(employee1.getDepartment()).toString()), () -> {
25+
throw new RuntimeException("No employee found for id: " + employeeId);
26+
});
27+
},
28+
() -> {
29+
throw new RuntimeException("No employee found for id: " + employeeId);
30+
});
31+
}
32+
33+
}
34+
35+
class Employee {
36+
private final int id;
37+
private final String name;
38+
private final String department;
39+
40+
public Employee(int id, String name, String department) {
41+
this.id = id;
42+
this.name = name;
43+
this.department = department;
44+
}
45+
46+
public int getId() {
47+
return id;
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public String getDepartment() {
55+
return department;
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o)
61+
return true;
62+
if (o == null || getClass() != o.getClass())
63+
return false;
64+
Employee employee = (Employee) o;
65+
return id == employee.id && Objects.equals(name, employee.name)
66+
&& Objects.equals(department, employee.department);
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "Employee{" +
72+
"id=" + id +
73+
", name='" + name + '\'' +
74+
", department='" + department + '\'' +
75+
'}';
76+
}
77+
78+
}
79+
80+
class EmployeeService {
81+
82+
// This list is populated in the tests
83+
private List<Employee> employeesList = new ArrayList<>();
84+
85+
public EmployeeService(List<Employee> listOfEmployees) {
86+
employeesList = listOfEmployees;
87+
}
88+
89+
public Optional<Employee> getEmployeeById(int employeeId) {
90+
for (Employee employee : employeesList) {
91+
if (Objects.equals(employee.getId(), employeeId)) {
92+
return Optional.of(employee);
93+
}
94+
}
95+
return Optional.empty();
96+
}
97+
98+
/*
99+
* public String printEmployeeNameAndDepartmentById(int employeeId) {
100+
* Optional<Employee> employee = getEmployeeById(employeeId);
101+
* StringBuilder stringBuilder = new StringBuilder();
102+
* stringBuilder.append(employeeId).append(" - ");
103+
* // Handle Optional values
104+
* employee.ifPresentOrElse(
105+
* e -> {
106+
* e.getNullableName().ifPresent(name -> e.getNullableDepartment()
107+
* .ifPresent(department ->
108+
* stringBuilder.append(name).append(" - ").append(department)));
109+
* },
110+
* () -> stringBuilder.append("No employee found"));
111+
* return stringBuilder.toString();
112+
* }
113+
*/
114+
115+
}

0 commit comments

Comments
 (0)