From c5a7fcbf7490e574e6e881fc09e88b73bd720a0c Mon Sep 17 00:00:00 2001 From: "anastasia.birillo" Date: Thu, 29 Aug 2024 15:36:00 +0200 Subject: [PATCH] FSmall fixes regarding cooking --- .../course/culinary/game/GameResource.kt | 30 +++++++++--- .../course/culinary/game/GameService.kt | 47 +++++++++++++++---- .../culinary/game/recipes/TomatoSoup.kt | 2 +- .../culinary/implementation/KitchenImpl.kt | 1 - .../implementation/cooking/PotImpl.kt | 2 +- .../culinaryServerTask1/test/Tests.kt | 4 +- 6 files changed, 67 insertions(+), 19 deletions(-) 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 5731c88..f7699b6 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 @@ -46,19 +46,37 @@ class CookingFunction(val service: CookingService) { fun checkSoup(): Boolean = pot.doesTastePerfect() @CrossOrigin - @GetMapping("/test-task3") - fun task3(): List { + @GetMapping("/salad-list") + fun cookSaladAsList(): List { clearActions() - service.cookSalad() + service.cookSaladAsList() clearKitchen() return actions } @CrossOrigin - @GetMapping("/test-task4") - fun task4(): List { + @GetMapping("/salad-sequence") + fun cookSaladAsSequence(): List { clearActions() - service.cookSmoothie() + service.cookSaladAsSequence() + clearKitchen() + return actions + } + + @CrossOrigin + @GetMapping("/smoothie-list") + fun cookSmoothieAsList(): List { + clearActions() + service.cookSmoothieAsList() + clearKitchen() + return actions + } + + @CrossOrigin + @GetMapping("/smoothie-sequence") + fun cookSmoothieAsSequence(): List { + clearActions() + service.cookSmoothieAsSequence() clearKitchen() return actions } 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 ef6554a..8da7da8 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,11 +1,16 @@ package org.jetbrains.kotlin.course.culinary.game import org.jetbrains.kotlin.course.culinary.game.recipes.* +import org.jetbrains.kotlin.course.culinary.models.food.CutVegetable import org.jetbrains.kotlin.course.culinary.models.food.FruitType import org.springframework.stereotype.Service @Service class CookingService { + companion object { + private const val NUM_VEGETABLES_FOR_SALAD = 5 + } + // task#1 fun cookTomatoSoup() { val tomatoes = getTomatoesForSoup() @@ -21,22 +26,37 @@ class CookingService { } // task#3 - fun cookSalad() { - fridge.getAllVegetables() - .map { kitchen.put(it) } + fun cookSaladAsList() { + val listOfVegetables = fridge.getAllVegetables() + val cutVegetables = listOfVegetables.map { kitchen.put(it) } .filter { kitchen.checkFresh(it) } .map { kitchen.cut(it) } - .take(5) + .take(NUM_VEGETABLES_FOR_SALAD) .map { kitchen.take(it) } - .groupBy { it.type } + mixVegetablesForSalad(cutVegetables) + } + + // task#3 + fun cookSaladAsSequence() { + val listOfVegetables = fridge.getAllVegetables().asSequence() + val cutVegetables = listOfVegetables.map { kitchen.put(it) } + .filter { kitchen.checkFresh(it) } + .map { kitchen.cut(it) } + .take(NUM_VEGETABLES_FOR_SALAD) + .map { kitchen.take(it) } + .toList() + mixVegetablesForSalad(cutVegetables) + } + + private fun mixVegetablesForSalad(cutVegetables: List) { + cutVegetables.groupBy { it.type } .forEach { (type, cuts) -> saladBowl.add(type, cuts) } saladBowl.mix() } // task#4 - fun cookSmoothie(){ - val fruits = listOf(FruitType.Citrus, FruitType.Berry) - fruits.map { type -> fridge.getBasketOf(type) } + fun cookSmoothieAsList(){ + FruitType.entries.map { type -> fridge.getBasketOf(type) } .onEach { basket -> kitchen.put(basket) } .flatMap { basket -> List(basket.capacity) { kitchen.takeFromBasket(basket) } } .distinctBy { it.type } @@ -44,4 +64,15 @@ class CookingService { .forEach { blender.add(it) } blender.blend() } + + // task#4 + fun cookSmoothieAsSequence(){ + FruitType.entries.asSequence().map { type -> fridge.getBasketOf(type) } + .onEach { basket -> kitchen.put(basket) } + .flatMap { basket -> List(basket.capacity) { kitchen.takeFromBasket(basket) } } + .distinctBy { it.type } + .sortedBy { it.type.sugarContent }.toList() + .forEach { blender.add(it) } + blender.blend() + } } \ No newline at end of file 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 index 0fdddf0..a77fc37 100644 --- 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 @@ -36,7 +36,7 @@ fun generateSpices(): Sequence = generateSequence { SpiceType.entries // task#2 fun addSpecies(spices: Sequence) { - val howMuchToAdd = Random.nextInt(1, 10) + val howMuchToAdd = Random.nextInt(1, 5) spices .map { shelf.getSpice(it) } .map { pot.put(it) } diff --git a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/KitchenImpl.kt b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/KitchenImpl.kt index 6563c6e..0a708cb 100644 --- a/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/KitchenImpl.kt +++ b/culinaryServer/culinaryServerTask1/src/main/kotlin/org/jetbrains/kotlin/course/culinary/implementation/KitchenImpl.kt @@ -23,7 +23,6 @@ data object KitchenImpl : Kitchen { require(vegetable in filling) { "Vegetable $vegetable is not on in the kitchen, so can't be cut" } require(vegetable.isFresh) { "One can't cut rot vegetable $vegetable" } filling -= vegetable - actions += buildAction(ActionType.REMOVE_FROM_COUNTER, vegetable) val cut = CutVegetable(vegetable.type) filling += cut actions += buildAction(ActionType.SHOW_ON_COUNTER, cut) 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 e81bd80..58b8898 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 @@ -23,7 +23,7 @@ data object PotImpl : Pot { private fun checkIfManySpices() = filling.filter{ it is Spice } .groupingBy{ it } .eachCount() - .filter{ (s, n) -> n > 2 } + .filter{ (_, n) -> n > 2 } .isNotEmpty() private fun checkIfAllVegetablesFresh() = filling.filter{ it is Vegetable }.all{ (it as Vegetable).isFresh } diff --git a/culinaryServer/culinaryServerTask1/test/Tests.kt b/culinaryServer/culinaryServerTask1/test/Tests.kt index 3faf546..f44ab3c 100644 --- a/culinaryServer/culinaryServerTask1/test/Tests.kt +++ b/culinaryServer/culinaryServerTask1/test/Tests.kt @@ -54,7 +54,7 @@ class Test { fun testTask3() { clearKitchen() FridgeImpl.refill() - CookingService().cookSalad() + CookingService().cookSaladAsList() assertTrue(SaladBowlImpl.filling.size in 1..5) { "The salad bowl should contain between 1 and 5 cut vegetables, now it is ${SaladBowlImpl.filling.size}." } @@ -65,7 +65,7 @@ class Test { fun testTask4() { clearKitchen() FridgeImpl.refill() - CookingService().cookSmoothie() + CookingService().cookSmoothieAsList() val hasCitrus = BlenderImpl.filling.any { it.type == FruitType.Citrus } val hasBerry = BlenderImpl.filling.any { it.type == FruitType.Berry } println(BlenderImpl.filling)