Hi please suggest me in the below code. It is not working. I am watching a YouTube video on callback program. It is working fine to presenter but not in my pgm. The issue is with delay function. The output expected to finish AMERICANO first even though LATTE is called before because of brew time is greater for Latte. Thank you
import kotlinx.coroutines.*
fun main(){
val fred=Barista(“fred”)
val sam=Barista(“sam”)
class Barista(val name:String):CoffeeMaker.OnCoffeeBrewedListner{
private val coffeeMaker=CoffeeMaker()
fun accptOrder(type:CoffeeType){
coffeeMaker.brewCoffee(type, this)
fun main() {
val fred = Barista(“fred”)
val sam = Barista(“sam”)
runBlocking {
fred.accptOrder(CoffeeType.LATTE)
sam.accptOrder(CoffeeType.AMERICANO)
}
}
class Barista(val name: String) : CoffeeMaker.OnCoffeeBrewedListner {
private val coffeeMaker = CoffeeMaker()
fun accptOrder(type: CoffeeType) {
coffeeMaker.brewCoffee(type, this)
import kotlinx.coroutines.*
fun main(){
val fred=Barista("fred")
val sam=Barista("sam")
runBlocking{
fred.accptOrder(CoffeeType.LATTE)
sam.accptOrder(CoffeeType.AMERICANO)
}
}
//Barista starts here
class Barista(val name:String):CoffeeMaker.OnCoffeeBrewedListner{
private val coffeeMaker=CoffeeMaker()
fun accptOrder(type:CoffeeType){
coffeeMaker.brewCoffee(type, this)
}
override fun onCoffeeBrewed(coffee:Coffee){
println( "$name finshed brewing ${coffee.type}")
}
}
data class Coffee(val type:CoffeeType)
//Coffeemaker starts here
class CoffeeMaker{
fun brewCoffee(type:CoffeeType,callback:OnCoffeeBrewedListner){
CoroutineScope(Dispatchers.IO).launch{
delay(type.brewTime)
val madeCoffee=Coffee(type)
withContext(Dispatchers.Main){
callback.onCoffeeBrewed(madeCoffee)
}
}
}
interface OnCoffeeBrewedListner{
fun onCoffeeBrewed(coffee:Coffee)
}
}
//Coffeetype starts here
enum class CoffeeType(val brewTime:Long){
AMERICANO(brewTime= 300L),
LATTE(brewTime=900L)
}
And in fact I see no output from it, likely because you’re launching coroutines on Dispatchers.IO without waiting for them. Modifying your code lightly, I obtain this:
import kotlinx.coroutines.*
fun main(){
val fred=Barista("fred")
val sam=Barista("sam")
runBlocking{
fred.accptOrder(CoffeeType.LATTE)
sam.accptOrder(CoffeeType.AMERICANO)
delay(1200L)
}
}
//Barista starts here
class Barista(val name:String):CoffeeMaker.OnCoffeeBrewedListner{
private val coffeeMaker=CoffeeMaker()
fun accptOrder(type:CoffeeType){
coffeeMaker.brewCoffee(type, this)
}
override fun onCoffeeBrewed(coffee:Coffee){
println( "$name finshed brewing ${coffee.type}")
}
}
data class Coffee(val type:CoffeeType)
//Coffeemaker starts here
class CoffeeMaker{
fun brewCoffee(type:CoffeeType,callback:OnCoffeeBrewedListner){
CoroutineScope(Dispatchers.IO).launch{
delay(type.brewTime)
val madeCoffee=Coffee(type)
withContext(Dispatchers.Default){
callback.onCoffeeBrewed(madeCoffee)
}
}
}
interface OnCoffeeBrewedListner{
fun onCoffeeBrewed(coffee:Coffee)
}
}
//Coffeetype starts here
enum class CoffeeType(val brewTime:Long){
AMERICANO(brewTime= 300L),
LATTE(brewTime=900L)
}
and the behaviour works as expected.
Likely though this is the code you want, where instead of callbacks, you use suspend
import kotlinx.coroutines.*
fun main(){
val fred=Barista("fred")
val sam=Barista("sam")
runBlocking{
launch { fred.accptOrder(CoffeeType.LATTE) }
launch { sam.accptOrder(CoffeeType.AMERICANO) }
}
}
//Barista starts here
class Barista(val name:String){
private val coffeeMaker=CoffeeMaker()
suspend fun accptOrder(type:CoffeeType){
val coffee = coffeeMaker.brewCoffee(type)
println( "$name finshed brewing ${coffee.type}")
}
}
data class Coffee(val type:CoffeeType)
//Coffeemaker starts here
class CoffeeMaker{
suspend fun brewCoffee(type:CoffeeType): Coffee {
return withContext(Dispatchers.IO) {
delay(type.brewTime)
Coffee(type)
}
}
}
//Coffeetype starts here
enum class CoffeeType(val brewTime:Long){
AMERICANO(brewTime= 300L),
LATTE(brewTime=900L)
}
It’s the icon that says “preformatted text”, should be right next to the quotation mark icon. Alternatively, simply surround your code in triple backticks:
```
code goes here
```