Skip to content

Commit 2b63d7f

Browse files
LysounCLOVIS-AI
authored andcommitted
tp7 step 2 solution
1 parent a5f876f commit 2b63d7f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package fmt.kotlin.fundamentals
22

33
data class Cellar(val bottles: List<WineBottle>) {
4-
4+
fun describeBottles(descriptionBuilder: (WineBottle) -> String) {
5+
for(i in bottles.indices) {
6+
println("$i: ${descriptionBuilder(bottles[i])}")
7+
}
8+
}
59
}

tp7/src/main/kotlin/fmt/kotlin/fundamentals/Main.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package fmt.kotlin.fundamentals
22

3-
val buildWineBottleDescription: (WineBottle) -> String = {
4-
val bottleType = when(it.wineType) {
5-
WineType.RED -> "rouge"
6-
WineType.WHITE -> "blanc"
7-
WineType.ROSE -> "rosé"
8-
}
9-
10-
"Vin $bottleType de l'année ${it.year}"
3+
val buildWineBottleShortDescription: (WineBottle) -> String = {
4+
"Vin ${buildWineTypeDescription(it.wineType)} de l'année ${it.year}"
115
}
126

137
fun main() {
@@ -31,4 +25,23 @@ fun main() {
3125
2.0
3226
),
3327
))
34-
}
28+
29+
cellar.describeBottles(buildWineBottleShortDescription)
30+
31+
cellar.describeBottles({
32+
"Vin ${buildWineTypeDescription(it.wineType)} de l'année ${it.year}, " +
33+
"cépage ${it.grapeVariety}, bouteille de ${it.volumeInLiters} litres"
34+
})
35+
cellar.describeBottles {
36+
"Vin ${buildWineTypeDescription(it.wineType)} de l'année ${it.year}, " +
37+
"cépage ${it.grapeVariety}, bouteille de ${it.volumeInLiters} litres"
38+
}
39+
cellar.describeBottles(WineBottle::describe)
40+
}
41+
42+
fun buildWineTypeDescription(wineType: WineType): String =
43+
when (wineType) {
44+
WineType.RED -> "rouge"
45+
WineType.WHITE -> "blanc"
46+
WineType.ROSE -> "rosé"
47+
}

tp7/src/main/kotlin/fmt/kotlin/fundamentals/WineBottle.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ data class WineBottle(
66
val grapeVariety: String,
77
val volumeInLiters: Double
88
) {
9+
fun describe(): String {
10+
return "Vin ${buildWineTypeDescription(wineType)} de l'année $year"
11+
}
912
}
1013

1114
enum class WineType {

0 commit comments

Comments
 (0)