Skip to content

Commit 0a39fd3

Browse files
ctruchiCLOVIS-AI
authored andcommitted
tp4 step 4 solution
1 parent e5b6221 commit 0a39fd3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package fmt.kotlin.fundamentals
22

3-
abstract class Container(
3+
sealed class Container(
44
val capacity: Int,
55
possibleCapacities: IntRange
66
) {
77
val valid = (possibleCapacities).contains(capacity)
88

9-
fun containersNeededToPourIn(container: Container) = -1
9+
fun containersNeededToPourIn(container: Container) = when (this) {
10+
is Tank -> when (container) {
11+
is Barrel, is FixedVolumeContainer -> capacity / container.capacity
12+
is Tank -> -1
13+
}
14+
15+
is Barrel -> if (container is FixedVolumeContainer) {
16+
capacity / container.capacity
17+
} else -1
18+
19+
is FixedVolumeContainer -> -1
20+
}
1021
}
1122

1223
class Barrel(
@@ -17,7 +28,7 @@ class Tank(
1728
capacity: Int
1829
) : Container(capacity, 2000000..10000000)
1930

20-
abstract class FixedVolumeContainer(capacity: Int) : Container(capacity, capacity..capacity)
31+
sealed class FixedVolumeContainer(capacity: Int) : Container(capacity, capacity..capacity)
2132

2233
class Magnum : FixedVolumeContainer(150)
2334
class Bottle : FixedVolumeContainer(75)

0 commit comments

Comments
 (0)