Skip to content

Commit

Permalink
Merge pull request #75 from jetbrains-academy/fixes-for-cooking
Browse files Browse the repository at this point in the history
FSmall fixes regarding cooking
  • Loading branch information
nbirillo authored Aug 29, 2024
2 parents 0870661 + c5a7fcb commit 4077fca
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,37 @@ class CookingFunction(val service: CookingService) {
fun checkSoup(): Boolean = pot.doesTastePerfect()

@CrossOrigin
@GetMapping("/test-task3")
fun task3(): List<Action> {
@GetMapping("/salad-list")
fun cookSaladAsList(): List<Action> {
clearActions()
service.cookSalad()
service.cookSaladAsList()
clearKitchen()
return actions
}

@CrossOrigin
@GetMapping("/test-task4")
fun task4(): List<Action> {
@GetMapping("/salad-sequence")
fun cookSaladAsSequence(): List<Action> {
clearActions()
service.cookSmoothie()
service.cookSaladAsSequence()
clearKitchen()
return actions
}

@CrossOrigin
@GetMapping("/smoothie-list")
fun cookSmoothieAsList(): List<Action> {
clearActions()
service.cookSmoothieAsList()
clearKitchen()
return actions
}

@CrossOrigin
@GetMapping("/smoothie-sequence")
fun cookSmoothieAsSequence(): List<Action> {
clearActions()
service.cookSmoothieAsSequence()
clearKitchen()
return actions
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -21,27 +26,53 @@ 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<CutVegetable>) {
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 }
.sortedBy { it.type.sugarContent }
.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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun generateSpices(): Sequence<SpiceType> = generateSequence { SpiceType.entries

// task#2
fun addSpecies(spices: Sequence<SpiceType>) {
val howMuchToAdd = Random.nextInt(1, 10)
val howMuchToAdd = Random.nextInt(1, 5)
spices
.map { shelf.getSpice(it) }
.map { pot.put(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions culinaryServer/culinaryServerTask1/test/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}."
}
Expand All @@ -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)
Expand Down

0 comments on commit 4077fca

Please sign in to comment.