-
Notifications
You must be signed in to change notification settings - Fork 0
/
Building.kt
52 lines (40 loc) · 1.68 KB
/
Building.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.coroutines
import kotlinx.coroutines.*
import java.util.*
import kotlin.coroutines.EmptyCoroutineContext
class Building(val name: String, var floors: Int = 0, private val scope: CoroutineScope) {
val random = Random()
suspend fun makeFoundation() = scope.launch {
delay(300)
speakThroughBullHorn("[${Thread.currentThread().name}] The foundation is ready")
}
suspend fun buildFloor(floor: Int) = withContext(scope.coroutineContext) {
delay(100)
if (random.nextBoolean()) {
throw Exception("[${Thread.currentThread().name} Something went wrong on $floor'th floor")
}
speakThroughBullHorn("[${Thread.currentThread().name}] The $floor'th floor is raised")
++floors
}
suspend fun placeWindows(floor: Int) = scope.launch {
delay(100)
speakThroughBullHorn("[${Thread.currentThread().name}] Windows are place on $floor'th floor")
}
suspend fun installDoors(floor: Int) = scope.launch {
delay(100)
speakThroughBullHorn("[${Thread.currentThread().name}] doors install on $floor'th floor")
}
suspend fun provideElectric(floor: Int) = scope.launch {
delay(100)
speakThroughBullHorn("[${Thread.currentThread().name}] Electric provided on floor num $floor")
}
suspend fun buildRoof() = scope.launch {
delay(200)
speakThroughBullHorn("[${Thread.currentThread().name}] The roof is ready")
}
suspend fun fitOut(floor: Int) = scope.launch {
delay(200)
speakThroughBullHorn("[${Thread.currentThread().name}] floor num $floor is furnished")
}
fun speakThroughBullHorn(message: String) = println(message)
}