You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercise-3.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,14 @@ Open `Basket.java`
10
10
11
11
After the conversion the code is still far from optimal. The Java2kotlin converter still uses the Java Stream API and tries to make the best out of it.
12
12
13
-
We can clean it up a bit by writing the calculation in exactly the same way as we would do with the Java Stream API.
14
-
15
-
Kotlin also has a build in reduce function in the std lib, so we can use that instead of the Java Stream API.
13
+
We could clean it up a bit by writing the calculation in exactly the same way as we would do with the Java Stream API. But Kotlin has a build in reduce function in the std lib, so we can use that instead of the Java Stream API.
16
14
17
15
```kotlin
18
16
val totalPrice:BigDecimal
19
17
get() = items.map(OrderItem::totalPrice).reduce { acc, next -> acc + next }
20
18
```
21
19
22
-
**Exercise:** change the calculation to the snippet above
20
+
**Exercise:** change the `totalPrice`calculation to the snippet above
23
21
24
22
There are a few problems with this code. First, the map operation produces a new list as its result on which the reduce operation is performed while we actually don't even use this list anywhere else.
25
23
Our newly introduced reduce does not work on an empty collection. Even though the code compiles, it is effectively broken.
@@ -47,7 +45,7 @@ val totalPrice: BigDecimal
47
45
get() = items.sumBy { it.totalPrice }
48
46
```
49
47
50
-
Kotlin already has build-in functions to sum types:
48
+
Kotlin already has build-in functions to sum BigDecimal types:
**Exercise**: Write a primary constructor for the Basket class and combine the constructors into one property items: MutableList<OrderItem>. Does the code still work?
95
+
**Exercise**: Write a primary constructor for the Basket class and combine the constructors into one property for the items. Verify that the code still works!
98
96
99
97
<details>
100
98
<summary>Suggested solution:</summary>
@@ -128,7 +126,7 @@ First have a look at the `getBasketById()`, it uses `computeIfAbsent`. See if yo
@@ -220,9 +218,11 @@ While `@PathVariable` is required by default, the id argument is nullable here.
220
218
221
219
**Exercise**: Change the `id` argument to the non-nullable type String and in the expression body remove the non-null assertion. Do the same for `addToBasket()`.
222
220
223
-
In `addToBasket` we can also handle the case where we try to add a non-existing product to our basket a bit better.
221
+
As a bonus we could omit the `"id"` from `@PathVariable("id")` because Spring is able to figure out the name of the argument from the Kotlin generated bytecode.
222
+
223
+
In `addToBasket` we improve on handling the case where we try to add a non-existing productId to our basket.
224
224
225
-
**Exercise**: Add a null check for non-existing products and throw an ResponseStatusException() if not found.
225
+
**Exercise**: Add a null check for a non-existing product and throw an ResponseStatusException() if not found.
226
226
227
227
<details>
228
228
<summary>The resulting code should look like this:</summary>
Last but not least we have the BootiqueApplicationTests class. Once this test class has been converted we could get rid of the Java compiler if needed.
244
244
245
-
**Exercise**: Convert BootiqueApplicationTests to Kotlin. Build the application and verify everything is still working as expected.
245
+
**Exercise**: Convert BootiqueApplicationTests to Kotlin. Build the application and verify everything is still working as expected!
0 commit comments