Skip to content

Commit 704dcfa

Browse files
committed
updated docs
1 parent 270fdf6 commit 704dcfa

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

exercise-3.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ Open `Basket.java`
1010

1111
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.
1212

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.
1614

1715
```kotlin
1816
val totalPrice: BigDecimal
1917
get() = items.map(OrderItem::totalPrice).reduce { acc, next -> acc + next }
2018
```
2119

22-
**Exercise:** change the calculation to the snippet above
20+
**Exercise:** change the `totalPrice` calculation to the snippet above
2321

2422
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.
2523
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
4745
get() = items.sumBy { it.totalPrice }
4846
```
4947

50-
Kotlin already has build-in functions to sum types:
48+
Kotlin already has build-in functions to sum BigDecimal types:
5149

5250
```kotlin
5351
public inline fun <T> Iterable<T>.sumOf(selector: (T) -> java.math.BigDecimal): java.math.BigDecimal
@@ -94,7 +92,7 @@ constructor(items: MutableList<OrderItem>) {
9492
}
9593
```
9694

97-
**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!
9896

9997
<details>
10098
<summary>Suggested solution:</summary>
@@ -128,7 +126,7 @@ First have a look at the `getBasketById()`, it uses `computeIfAbsent`. See if yo
128126
```kotlin
129127
fun getBasketById(id: String): Basket = baskets.getOrPut(id) { Basket() }
130128
```
131-
Or
129+
Or without the need for a Lambda:
132130

133131
```kotlin
134132
fun getBasketById(id: String): Basket = baskets.getOrDefault(id, Basket())
@@ -220,9 +218,11 @@ While `@PathVariable` is required by default, the id argument is nullable here.
220218

221219
**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()`.
222220

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.
224224

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.
226226

227227
<details>
228228
<summary>The resulting code should look like this:</summary>
@@ -242,7 +242,7 @@ fun addToBasket(@PathVariable("id") id: String, @RequestBody orderItem: OrderIte
242242

243243
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.
244244

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!
246246

247247
### Next steps
248248

0 commit comments

Comments
 (0)