Skip to content

Commit

Permalink
🔨 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
velizartodorov committed Dec 13, 2024
1 parent 2e2a23b commit 761c438
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/coin/Coin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ enum class Coin(val value: Int) {
TWO_EUROS(200);

companion object {
private val descending = values().reversed()
private val descending = entries.reversed()

fun format(numbers: List<Int>): Array<Coin> {
return numbers.mapNotNull { coin ->
values().find { it.value == coin }
entries.find { it.value == coin }
}.toTypedArray()
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/drinks/impl/Drink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ sealed interface Drink {
}

private fun getErrorMessage(order: Order) =
"Amount insufficient for $name! " +
"Needed amount: $price " +
"Current amount: ${Coin.get(order.coins)}"
"""
Amount insufficient for ${name}!
Needed amount: ${Coin.format(price)}
Current amount: ${Coin.format(order.coins)}
"""

fun getAmount(strength: Strength?): Amount = when (strength) {
LOW -> Amount.LOW
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/order/Amount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum class Amount {

companion object {
fun get(amount: Int?): Amount {
return amount?.minus(1)?.let { values().getOrNull(it) }
return amount?.minus(1)?.let { entries.getOrNull(it) }
?: throw IllegalArgumentException("Amount unsupported: $amount")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/order/Strength.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum class Strength {

companion object {
fun get(number: Int?): Strength {
return values().find { it.ordinal + 1 == number }
return entries.find { it.ordinal + 1 == number }
?: throw IllegalArgumentException("Strength unsupported: $number")
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/vending_machine/intefaces/CoinsInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ object CoinsInterface : UserInterface {
amount: Int
) = """
Amount insufficient for ${order.drink}!
Needed amount: $price
Current amount: $amount
Needed amount: ${Coin.format(price)}
Current amount: ${Coin.format(amount)}
"""

override fun process(order: Order): Order {
Expand Down

0 comments on commit 761c438

Please sign in to comment.