Skip to content

Commit 79c51fe

Browse files
committed
tp7 step 2 solution
1 parent 75e7548 commit 79c51fe

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package fmt.kotlin.fundamentals
2+
3+
fun cellar(init: CellarDsl.() -> Unit): Cellar {
4+
val dsl = CellarDsl()
5+
dsl.init()
6+
val cellar = Cellar(
7+
barrels = dsl.barrels,
8+
bottles = dsl.bottles
9+
)
10+
return cellar
11+
}
12+
13+
class CellarDsl {
14+
val barrels = mutableListOf<Barrel>()
15+
val bottles = mutableListOf<Bottle>()
16+
17+
fun barrel(init: BarrelDsl.() -> Unit): Barrel {
18+
val dsl = BarrelDsl()
19+
dsl.init()
20+
val barrel = Barrel(
21+
capacity = dsl.capacity
22+
)
23+
barrels.add(barrel)
24+
return barrel
25+
}
26+
27+
fun bottle(init: BottleDsl.() -> Unit): Bottle {
28+
val dsl = BottleDsl()
29+
dsl.init()
30+
val bottle = Bottle(
31+
name = dsl.name,
32+
year = dsl.year
33+
)
34+
bottles.add(bottle)
35+
return bottle
36+
}
37+
}
38+
39+
class BarrelDsl {
40+
var capacity: Int = 0
41+
}
42+
43+
class BottleDsl {
44+
var name: String = ""
45+
var year: Int = 0
46+
}

0 commit comments

Comments
 (0)