Skip to content

Commit 9b20d73

Browse files
committed
Make more improvements, including a new "about.md" document.
1 parent 145d49b commit 9b20d73

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

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

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

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Instructions
22

33
In this exercise you will be writing code to retrieve the factory employees.
4-
Employees have an ID, a name and a department name, like in the [tim-from-marketing](/exercises/concept/tim-from-marketing) exercise.
4+
Employees have an ID, a name and a department name, like in the [tim-from-marketing](/exercises/concept/tim-from-marketing) exercise.
55
The first field of an employee is always an integer number, but the name and the department name may be empty or null.
66
The class constructor receives a parameter of type List<Employee>, which is populated in the tests.
77
You will be writing two methods: `getEmployeeById(int)` and `getEmployeeDetailsById(int)`.
@@ -14,8 +14,7 @@ If the employee does not exist, returns an empty Optional instance.
1414

1515
## 2. Return the name and department of a given employee in a certain format
1616

17-
Implement the `getEmployeeDetailsById(int)` method to return a string containing the id, the name and
18-
the department of a given employee:
17+
Implement the `getEmployeeDetailsById(int)` method to return a string containing the id, the name and the department of a given employee:
1918

2019
```java
2120
getEmployeeDetailsById(1) => "1 - Tim - Marketing"

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,37 @@ In other words, there is a chance the method returns "no value" at all.
88

99
## Creating an Optional<T> object
1010

11-
Given an object of type Employee, an Optional<Employee> object is created as follows:
11+
Given an object of type Employee, an Optional<Employee> object can be created using the static [of][optional-of-javadoc] method:
1212

1313
```java
1414
Employee employee = new Employee();
1515
Optional<Employee> optionalEmployee = Optional.of(employee);
1616
```
1717

18-
`optionalEmployee` is a wrapper of `employee`.
18+
If the employee _may_ be not present, the static [ofNullable][optional-ofNullable-javadoc] method must be used:
1919

20-
TBD: explain empty, present and get.
20+
```java
21+
Employee nullableEmployee = new Employee();
22+
Optional<Employee> nullableEmployee = Optional.ofNullable(employee);
23+
```
24+
25+
`optionalEmployee` and `nullableEmployee` both are wrappers of an `Employee` object.
26+
27+
## Basic methods
28+
29+
If a value is present, the [isPresent][optional-isPresent-javadoc] method returns true and the [get][optional-get-javadoc] method returns the value.
30+
31+
```java
32+
Employee employee = new Employee("Tim", 45);
33+
Optional<Employee> optionalEmployee = Optional.ofNullable(employee);
34+
boolean isThereAnEmployee = optionalEmployee.isPresent(); // true
35+
Employee employee = optionalEmployee.get();
36+
```
2137

2238
## Usage
2339

40+
In order to throw an exception when the value is not present, the [orElseThrow][optional-orElseThrow-javadoc] method must be used.
41+
2442
```java
2543
public Optional<Employee> getEmployee(String name) {
2644
// Assume that getEmployeeByName returns an Optional<Employee>
@@ -29,7 +47,7 @@ public Optional<Employee> getEmployee(String name) {
2947
}
3048
```
3149

32-
If a default value must be returned, the `orElse` method can be used.
50+
If a default value must be returned, the [orElse][optional-orElse-javadoc] method can be used.
3351

3452
```java
3553
public Optional<Employee> getEmployee(String name) {
@@ -39,7 +57,7 @@ public Optional<Employee> getEmployee(String name) {
3957
}
4058
```
4159

42-
Other commonly used method is `ifPresentOrElse`, which is used to handle the case where the value is present and the case where the value is empty.
60+
Other commonly used method is [ifPresentOrElse][optional-ifPresentOrElse-javadoc], which is used to handle both cases with the same method: the case where the value is present and the case where the value is empty.
4361

4462
```java
4563
public Optional<Employee> getEmployee(String name) {
@@ -62,3 +80,11 @@ public Optional<Integer> getEmployeeAge(String name) {
6280
.orElse("No employee found");
6381
}
6482
```
83+
84+
[optional-of-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#of(java.lang.Object)
85+
[optional-ofNullable-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#ofNullable(java.lang.Object)
86+
[optional-get-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#get()
87+
[optional-isPresent-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#isPresent()
88+
[optional-orElse-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#orElse(T)
89+
[optional-orElseThrow-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#orElseThrow()
90+
[optional-ifPresentOrElse-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#ifPresentOrElse(java.util.function.Consumer,java.lang.Runnable)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This Concepts Exercise's Concepts are:
2727

2828
This Concept Exercise's prerequisites Concepts are:
2929

30-
- `custom classes`.
30+
- `custom-classes`.
3131
- `lists`.
3232
- `generic-types`.
3333

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
connection.project.dir=../tim-from-marketing
1+
connection.project.dir=../..
22
eclipse.preferences.version=1

0 commit comments

Comments
 (0)