Skip to content

Commit

Permalink
tp4 step 4 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ctruchi committed Feb 10, 2025
1 parent 88210c8 commit 98a1e57
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions tp4/src/main/kotlin/fmt/kotlin/fundamentals/Container.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fmt.kotlin.fundamentals

abstract class Container(
sealed class Container(
val capacity: Int,
possibleCapacities: IntRange
) {
Expand All @@ -10,7 +10,18 @@ abstract class Container(
}
}

fun containersNeededToPourIn(container: Container) = -1
fun containersNeededToPourIn(container: Container) = when (this) {
is Tank -> when (container) {
is Barrel, is FixedVolumeContainer -> capacity / container.capacity
is Tank -> throw IllegalArgumentException()
}

is Barrel -> if (container is FixedVolumeContainer) {
capacity / container.capacity
} else throw IllegalArgumentException()

is FixedVolumeContainer -> throw IllegalArgumentException()
}
}

class Barrel(
Expand All @@ -21,7 +32,7 @@ class Tank(
capacity: Int
) : Container(capacity, 2000000..10000000)

abstract class FixedVolumeContainer(capacity: Int) : Container(capacity, capacity..capacity)
sealed class FixedVolumeContainer(capacity: Int) : Container(capacity, capacity..capacity)

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

0 comments on commit 98a1e57

Please sign in to comment.