Skip to content

Commit 4b7a800

Browse files
committed
Modify many files, including Hints.md, to adapt them to the new exercise without streams.
1 parent 4c5e540 commit 4b7a800

File tree

8 files changed

+174
-201
lines changed

8 files changed

+174
-201
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Hints
22

3+
## 1. Get an employee by ID
34

4-
## 1.- Print the name of all the employees
5-
WIP
5+
- This method returns an `Optional<Employee>` object, not an `Employee` one.
66

7+
## 2. Return the name and department of a given employee in a certain format
78

8-
## 2.- Print the name and department of a given employee
9-
WIP
9+
- You can call the method `getEmployeeById(int)` to get the employee.
10+
- Remember the syntax of the `ifPresentOrElse()` method.
Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
# Instructions
22

3-
In this exercise you will be writing code to print all the names of the factory employees.
3+
In this exercise you will be writing code to retrieve the factory employees.
44
Employees have an ID, a name and a department name, like in the [tim-from-marketing](/exercises/concept/tim-from-marketing) exercise.
5-
Assume that the ID of the first employee is 0, the ID of the second employee is 1, and so on. The three fields of an employee may be empty, that's why they are declared as Optional<T> types.
6-
The class constructor receives a parameter of type List<Optional<Employee>>, which is populated in the tests.
5+
The first field of an employee is always an integer number, but the name and the department name may be empty or null.
6+
The class constructor receives a parameter of type List<Employee>, which is populated in the tests.
7+
You will be writing two methods: `getEmployeeById(int)` and `getEmployeeDetailsById(int)`.
78

8-
## 1.- Print the names of all the employees
9+
## 1. Get an employee by ID
910

10-
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".
11+
Implement the `getEmployeeById(int)` method so that it returns an Optional<Employee> object.
12+
If the employee does not exist, returns an empty Optional instance.
1113

12-
```java
13-
"
14-
1 - Tim
15-
2 - Bill
16-
3 - Steve
17-
4 - No employee found
18-
5 - Charlotte
19-
"
20-
```
14+
## 2. Return the name and department of a given employee in a certain format
2115

22-
## 2.- Print the name and department of a given employee
23-
24-
Implement the `printEmployeeNameAndDepartmentById(id)` method to print the name and department of a given employee, together with their id. If the employee does not exist, print "[id] - No employee found". You will have to call the method `getEmployeeById(int employeeId)`, which returns an Optional<Employee> and it's already defined.
16+
Implement the `getEmployeeDetailsById(int)` method to return a string containing the id, the name and
17+
the department of a given employee:
2518

2619
```java
27-
printEmployeeNameAndDepartmentById(1) => "1 - Tim - Marketing"
28-
printEmployeeNameAndDepartmentById(3) => "3 - Steve - Engineering"
29-
printEmployeeNameAndDepartmentById(4) => "4 - This employee does not exist"
20+
getEmployeeDetailsById(1) => "1 - Tim - Marketing"
21+
getEmployeeDetailsById(3) => "3 - Steve - Engineering"
3022
```
23+
24+
If the employee does not exist or is null, it returns `No employee found for id: [id]`.

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,23 @@ public boolean equals(Object o) {
3131
if (o == null || getClass() != o.getClass())
3232
return false;
3333
Employee employee = (Employee) o;
34-
return id == employee.id &&
35-
Objects.equals(name, employee.name) && Objects.equals(department, employee.department);
34+
return id == employee.id && Objects.equals(name, employee.name)
35+
&& Objects.equals(department, employee.department);
3636
}
3737

3838
@Override
3939
public int hashCode() {
4040
return Objects.hash(id, name, department);
4141
}
42+
43+
@Override
44+
public String toString() {
45+
return "Employee{" +
46+
"id=" + id +
47+
", name='" + name + '\'' +
48+
", department='" + department + '\'' +
49+
'}';
50+
}
51+
4252
}
53+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.List;
2+
import java.util.Optional;
3+
import java.util.Objects;
4+
5+
class EmployeeService {
6+
7+
// This list is populated in the tests
8+
private List<Employee> employeesList;
9+
10+
public EmployeeService(List<Employee> listOfEmployees) {
11+
employeesList = listOfEmployees;
12+
}
13+
14+
public Optional<Employee> getEmployeeById(int employeeId) {
15+
for (Employee employee : employeesList) {
16+
if (Objects.equals(employee.getId(), employeeId)) {
17+
return Optional.of(employee);
18+
}
19+
}
20+
return Optional.empty();
21+
}
22+
23+
public String getEmployeeDetailsById(int employeeId) {
24+
Optional<Employee> nullableEmployee = getEmployeeById(employeeId);
25+
StringBuilder stringBuilder = new StringBuilder();
26+
nullableEmployee.ifPresentOrElse(
27+
employee1 -> {
28+
Optional.ofNullable(employee1.getName())
29+
.ifPresentOrElse(name -> stringBuilder.append(employeeId).append(" - ")
30+
.append(employee1.getName()).append(" - ")
31+
.append(employee1.getDepartment()),
32+
() -> {
33+
stringBuilder.append("No employee found for id: ").append(employeeId);
34+
});
35+
},
36+
() -> {
37+
stringBuilder.append("That id does not exist: ").append(employeeId);
38+
});
39+
return stringBuilder.toString();
40+
}
41+
}
42+
43+
44+
45+
46+
47+

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

Lines changed: 0 additions & 115 deletions
This file was deleted.

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import java.util.Objects;
2-
import java.util.Optional;
32

43
class Employee {
54
private final int id;
@@ -12,16 +11,16 @@ public Employee(int id, String name, String department) {
1211
this.department = department;
1312
}
1413

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

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

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

2726
@Override
@@ -37,4 +36,13 @@ public boolean equals(Object o) {
3736
public int hashCode() {
3837
return Objects.hash(id, name, department);
3938
}
39+
40+
@Override
41+
public String toString() {
42+
return "Employee{" +
43+
"id=" + id +
44+
", name='" + name + '\'' +
45+
", department='" + department + '\'' +
46+
'}';
47+
}
4048
}
Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
import java.util.ArrayList;
22
import java.util.List;
3+
import java.util.Objects;
34
import java.util.Optional;
45

56
class EmployeeService {
67

7-
// This list is populated in the tests
8-
private List<Optional<Employee>> nullableEmployeesList = new ArrayList<>();
8+
private List<Employee> employeesList;
99

10-
public EmployeeService(List<Optional<Employee>> listOfEmployees) {
11-
nullableEmployeesList = listOfEmployees;
10+
public EmployeeService(List<Employee> listOfEmployees) {
11+
// This list is populated in the tests
12+
employeesList = listOfEmployees;
1213
}
1314

1415
public Optional<Employee> getEmployeeById(int employeeId) {
15-
return nullableEmployeesList
16-
.stream()
17-
.flatMap(employee -> employee.stream())
18-
.filter(employee -> employee.getNullableId()
19-
.map(id -> id == employeeId)
20-
.orElse(false))
21-
.findFirst();
16+
for (Employee employee : employeesList) {
17+
if (Objects.equals(employee.getId(), employeeId)) {
18+
return Optional.of(employee);
19+
}
20+
}
21+
return Optional.empty();
2222
}
2323

24-
public String printAllEmployeesNames() {
25-
throw new UnsupportedOperationException(
26-
"Please implement the EmployeeService.printAllEmployeesNames() method");
24+
public String getEmployeeDetailsById(int employeeId) {
25+
Optional<Employee> nullableEmployee = getEmployeeById(employeeId);
26+
StringBuilder stringBuilder = new StringBuilder();
27+
nullableEmployee.ifPresentOrElse(
28+
employee1 -> {
29+
Optional.ofNullable(employee1.getName())
30+
.ifPresentOrElse(name -> stringBuilder.append(employeeId).append(" - ")
31+
.append(employee1.getName()).append(" - ")
32+
.append(employee1.getDepartment()),
33+
() -> {
34+
stringBuilder.append("No employee found for id: ").append(employeeId);
35+
});
36+
},
37+
() -> {
38+
stringBuilder.append("No employee found for id: ").append(employeeId);
39+
});
40+
return stringBuilder.toString();
2741
}
2842

29-
public String printEmployeeNameAndDepartmentById(int employeeId) {
30-
throw new UnsupportedOperationException(
31-
"Please implement the EmployeeService.printEmployeeNameAndDepartmentById(id) method");
32-
}
43+
// public Optional<Employee> getEmployeeById(int employeeId) {
44+
// throw new UnsupportedOperationException("Please implement this method");
45+
// }
46+
//
47+
// public String getEmployeeDetailsById(int employeeId) {
48+
// throw new UnsupportedOperationException("Please implement this method");
49+
// }
3350

3451
}

0 commit comments

Comments
 (0)