From f6784adff91c216dd47cbb596478e0f799737c6d Mon Sep 17 00:00:00 2001 From: "anastasia.birillo" Date: Tue, 13 Aug 2024 15:20:37 +0200 Subject: [PATCH] Fix task1 and task2 - move functions that need to be implemented into a separated file --- .../course/culinary/game/GameResource.kt | 28 +++++++++++-- .../course/culinary/game/GameService.kt | 20 +++------ .../culinary/game/recipes/TomatoSoup.kt | 42 +++++++++++++++++++ .../implementation/cooking/PotImpl.kt | 11 ++--- .../implementation/cooking/SaladBowlImpl.kt | 2 +- .../implementation/storage/FridgeImpl.kt | 4 +- .../culinaryServerTask1/task-info.yaml | 2 + .../culinaryServerTask1/test/Tests.kt | 13 ++++-- 8 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/recipes/TomatoSoup.kt diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameResource.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameResource.kt index 353dd50..ac555c3 100644 --- a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameResource.kt +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameResource.kt @@ -1,6 +1,10 @@ package org.jetbrains.kotlin.course.culinary.game import culinary.JsAction +import culinary.JsItemType +import org.jetbrains.kotlin.course.culinary.converters.toJsItemType +import org.jetbrains.kotlin.course.culinary.game.recipes.NUMBER_OF_TOMATOES +import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl import org.jetbrains.kotlin.course.culinary.models.food.FruitType import org.jetbrains.kotlin.course.culinary.models.food.SpiceType import org.jetbrains.kotlin.course.culinary.models.food.VegetableType @@ -10,21 +14,37 @@ import org.springframework.web.bind.annotation.* @RequestMapping("/api/functions/") class CookingFunction(val service: CookingService) { @CrossOrigin - @GetMapping("/test-task1") - fun task1(): List { + @GetMapping("/refill-fridge") + fun refillFridge(): List { + FridgeImpl.refill() + return FridgeImpl.vegetables.map{ it.toJsItemType() } + } + + @CrossOrigin + @GetMapping("/tomato-soup") + fun tomatoSoup(): List { + if (FridgeImpl.vegetables.count{ it.type == VegetableType.Tomato && it.isFresh } < NUMBER_OF_TOMATOES) { + // Show an error + return emptyList() + } + service.cookTomatoSoup() clearKitchen() return actions } @CrossOrigin - @GetMapping("/test-task2") - fun task2(): List { + @GetMapping("/soup-spices") + fun soupSpices(): List { service.cookWithSpices() clearKitchen() return actions } + @CrossOrigin + @GetMapping("/check-soup") + fun checkSoup(): Boolean = pot.doesTastePerfect() + @CrossOrigin @GetMapping("/test-task3") fun task3(): List { diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameService.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameService.kt index 315d377..ef6554a 100644 --- a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameService.kt +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameService.kt @@ -1,30 +1,22 @@ package org.jetbrains.kotlin.course.culinary.game +import org.jetbrains.kotlin.course.culinary.game.recipes.* import org.jetbrains.kotlin.course.culinary.models.food.FruitType -import org.jetbrains.kotlin.course.culinary.models.food.SpiceType -import org.jetbrains.kotlin.course.culinary.models.food.VegetableType import org.springframework.stereotype.Service @Service class CookingService { // task#1 fun cookTomatoSoup() { - val vegetables = List(3) { fridge.getVegetable(what = VegetableType.Tomato) } - vegetables - .onEach { kitchen.put(it) } - .map { kitchen.cut(it) } - .forEach { pot.put(kitchen.take(it)) } - pot.simmer() + val tomatoes = getTomatoesForSoup() + prepareTomatoes(tomatoes) + cookSoup() } // task#2 fun cookWithSpices() { - val spices = generateSequence { SpiceType.entries.random() } - spices - .map { shelf.getSpice(it) } - .map { pot.put(it) } - .takeWhile { !pot.doesTastePerfect() } - .toList() // terminate + val spices = generateSpices() + addSpecies(spices) pot.simmer() } diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/recipes/TomatoSoup.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/recipes/TomatoSoup.kt new file mode 100644 index 0000000..12f46e2 --- /dev/null +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/recipes/TomatoSoup.kt @@ -0,0 +1,42 @@ +package org.jetbrains.kotlin.course.culinary.game.recipes + +import org.jetbrains.kotlin.course.culinary.game.fridge +import org.jetbrains.kotlin.course.culinary.game.kitchen +import org.jetbrains.kotlin.course.culinary.game.pot +import org.jetbrains.kotlin.course.culinary.game.shelf +import org.jetbrains.kotlin.course.culinary.models.food.SpiceType +import org.jetbrains.kotlin.course.culinary.models.food.Vegetable +import org.jetbrains.kotlin.course.culinary.models.food.VegetableType +import kotlin.random.Random + +internal const val NUMBER_OF_TOMATOES = 3 + +// task#1 +fun getTomatoesForSoup(): List = + List(NUMBER_OF_TOMATOES) { fridge.getVegetable(what = VegetableType.Tomato) } + +// task#1 +fun prepareTomatoes(tomatoes: List) { + tomatoes + .onEach { kitchen.put(it) } + .map { kitchen.cut(it) } + .forEach { pot.put(kitchen.take(it)) } +} + +// task#1 +fun cookSoup() { + pot.simmer() +} + +// task#2 +fun generateSpices(): Sequence = generateSequence { SpiceType.entries.random() } + +// task#2 +fun addSpecies(spices: Sequence) { + val howMuchToAdd = Random.nextInt(1, 10) + spices + .map { shelf.getSpice(it) } + .map { pot.put(it) } + .take(howMuchToAdd) + .toList() // terminate +} diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/PotImpl.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/PotImpl.kt index 3ba1bc7..6bbafa6 100644 --- a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/PotImpl.kt +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/PotImpl.kt @@ -9,15 +9,11 @@ import org.jetbrains.kotlin.course.culinary.models.food.* data object PotImpl : Pot { val filling: MutableList = mutableListOf() - var simmering = false + var simmering: Boolean = false override fun put(ingredient: T) { - if (filling.add(ingredient)) { - actions.add(buildJsAction(JsActionType.PUT_IN_POT, ingredient)) - } - else { - error("You are trying to put the same $ingredient in the pot twice") - } + filling.add(ingredient) + actions.add(buildJsAction(JsActionType.PUT_IN_POT, ingredient)) } override fun put(vegetable: CutVegetable) { @@ -32,6 +28,7 @@ data object PotImpl : Pot { private fun checkIfAllVegetablesFresh() = filling.filter{ it is Vegetable }.all{ (it as Vegetable).isFresh } + // task#2 override fun doesTastePerfect(): Boolean = checkIfManySpices() && checkIfAllVegetablesFresh() override fun simmer() { diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/SaladBowlImpl.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/SaladBowlImpl.kt index 649259d..835aa47 100644 --- a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/SaladBowlImpl.kt +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/cooking/SaladBowlImpl.kt @@ -11,7 +11,7 @@ import org.jetbrains.kotlin.course.culinary.models.food.VegetableType data object SaladBowlImpl : SaladBowl { val filling: MutableList = mutableListOf() - var mixing = false + var mixing: Boolean = false override fun add(type: VegetableType, cuts: List) { requireNotNull(cuts.all { it.type == type }) { diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/storage/FridgeImpl.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/storage/FridgeImpl.kt index 3578d52..330f1e9 100644 --- a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/storage/FridgeImpl.kt +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/storage/FridgeImpl.kt @@ -11,18 +11,20 @@ data object FridgeImpl : Fridge { private const val RANDOM_VEGETABLES_NUMBER = 7 private const val RANDOM_FRESH_VEGETABLES_NUMBER = 3 - private val vegetables = generateRandomVegetables().toMutableList() + val vegetables: MutableList = generateRandomVegetables().toMutableList() private fun generateRandomVegetables() = buildList { addAll(List(RANDOM_VEGETABLES_NUMBER) { Vegetable(VegetableType.entries.random(), Random.nextBoolean()) }) addAll(List(RANDOM_FRESH_VEGETABLES_NUMBER) { Vegetable(VegetableType.Tomato, true) }) } + // Task#0, student fun refill() { vegetables.clear() vegetables.addAll(generateRandomVegetables()) } + // task#1, student override fun getVegetable(what: VegetableType): Vegetable { val vegetable = checkNotNull(vegetables.find { it.type == what && it.isFresh }) { "Fresh vegetable $what not found." } vegetables.remove(vegetable) diff --git a/culinaryServer/culinaryServerTask1/task-info.yaml b/culinaryServer/culinaryServerTask1/task-info.yaml index f4070e7..ad2a50b 100644 --- a/culinaryServer/culinaryServerTask1/task-info.yaml +++ b/culinaryServer/culinaryServerTask1/task-info.yaml @@ -47,3 +47,5 @@ files: visible: true - name: src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/GameActions.kt visible: true + - name: src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/recipes/TomatoSoup.kt + visible: true diff --git a/culinaryServer/culinaryServerTask1/test/Tests.kt b/culinaryServer/culinaryServerTask1/test/Tests.kt index e0313b7..027933c 100644 --- a/culinaryServer/culinaryServerTask1/test/Tests.kt +++ b/culinaryServer/culinaryServerTask1/test/Tests.kt @@ -1,5 +1,6 @@ import org.jetbrains.kotlin.course.culinary.game.CookingService import org.jetbrains.kotlin.course.culinary.game.clearKitchen +import org.jetbrains.kotlin.course.culinary.game.pot import org.jetbrains.kotlin.course.culinary.implementation.cooking.BlenderImpl import org.jetbrains.kotlin.course.culinary.implementation.cooking.PotImpl import org.jetbrains.kotlin.course.culinary.implementation.cooking.SaladBowlImpl @@ -28,10 +29,14 @@ class Test { @Test fun testTask2() { - clearKitchen() - CookingService().cookWithSpices() - assertTrue(PotImpl.doesTastePerfect(), "The soup in the pot does not taste perfect.") - assertTrue(PotImpl.simmering) + val tastes = List(100) { + clearKitchen() + CookingService().cookWithSpices() + assertTrue(PotImpl.simmering) + pot.doesTastePerfect() + } + assertTrue(tastes.toSet().size == 2, "The spices should be added randomly") + } @Test