article banner

Exercise: Cafeteria simulation

Your task is to simulate a cafeteria. Your cafeteria should have four baristas: Alice, Bob, Celine and Dave. All the baristas can work at the same time. They can prepare latte or espresso. Preparing an espresso requires grinding coffee beans and making espresso. Preparing a latte requires grinding coffee beans, making espresso, and adding milk. Each of these processes takes 3 seconds. You have only one cashier, who can announce that a coffee is ready but cannot announce it more than once per second. Each barista can prepare only one coffee at a time. If a customer orders a coffee and all baristas are busy, they have to wait until one of them is free. You should communicate coffee requests using the terminal. As a result, you should be able to see when each coffee is ready and what each barista is doing at a given moment. Here is what an example simulation should look like:

Welcome to Dream Coffee!
Press E to get espresso, L to get latte.
E
Order for ESPRESSO sent
Alice: Grinding coffee...
(3 seconds later)
Alice: Making espresso...
(3 seconds later)
Coffee Espresso is ready
L
Order for LATTE sent
Bob: Grinding coffee...
L
Order for LATTE sent
Celine: Grinding coffee...
(3 seconds later)
Bob: Making espresso...
Celine: Making espresso...
(3 seconds later)
Bob: Brewing milk...
Celine: Brewing milk...
(3 seconds later)
Coffee Latte is ready
(1 second later)
Coffee Latte is ready

This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in coroutines/channel/Cafeteria.kt. You can find there starting code.

Once you are done with the exercise, you can check your solution here.

Playground

import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch enum class CoffeeType { ESPRESSO, LATTE } class Milk class GroundCoffee sealed class Coffee class Espresso(ground: GroundCoffee) : Coffee() { override fun toString(): String = "Espresso" } class Latte(milk: Milk, espresso: Espresso) : Coffee() { override fun toString(): String = "Latte" } suspend fun main(): Unit = coroutineScope { // TODO println("Welcome to Dream Coffee!") println("Press E to get espresso, L to get latte.") while (true) { val type = when (readlnOrNull()) { "E" -> CoffeeType.ESPRESSO "L" -> CoffeeType.LATTE else -> continue } // TODO println("Order for $type sent") } } private suspend fun makeCoffee(order: CoffeeType, baristaName: String): Coffee { val groundCoffee = groundCoffee(baristaName) val espresso = makeEspresso(groundCoffee, baristaName) return when (order) { CoffeeType.ESPRESSO -> espresso CoffeeType.LATTE -> { val milk = brewMilk(baristaName) Latte(milk, espresso) } } } suspend fun groundCoffee(baristaName: String): GroundCoffee { println("$baristaName: Grinding coffee...") delay(3000) return GroundCoffee() } suspend fun brewMilk(baristaName: String): Milk { println("$baristaName: Brewing milk...") delay(3000) return Milk() } suspend fun makeEspresso(ground: GroundCoffee, baristaName: String): Espresso { println("$baristaName: Making espresso...") delay(3000) return Espresso(ground) }