Skip to content

Commit

Permalink
✨ validate amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
velizartodorov committed Oct 19, 2024
1 parent b94635f commit d066f0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/main/kotlin/coin/Coin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ enum class Coin(val value: Int) {
companion object {
private val descending = values().reversed()

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

fun format(coins: Array<Coin>) = format(get(coins.toList()))

fun format(coins: List<Coin>) = format(get(coins))

fun get(coins: List<Coin>?): Int {
return coins?.sumOf { it.value } ?: 0
}

fun format(cents: Int): String {
val euros = cents / 100
val remainingCents = cents % 100
return String.format("%d.%02d €", euros, remainingCents)
}

fun get(coins: List<Coin>?): Int {
return coins?.sumOf { it.value } ?: 0
}

fun get(input: Int): List<Coin> {
val coins = mutableListOf<Coin>()
var amount = input
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/drinks/impl/Drink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sealed interface Drink {
println("$name ordered successfully! Preparing ...")
prepareDrink(order)
if (change > 0) {
val formatChange = Coin.format(Coin.get(orderResponse.change))
val formatChange = Coin.format(orderResponse.change)
println("Take your change: $formatChange")
}
if (orderResponse.status == DONE) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/vending_machine/intefaces/CoinsInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ object CoinsInterface : UserInterface {
███████╗██║ ╚████║ ██║ ███████╗██║ ██║ ╚██████╗╚██████╔╝██║██║ ╚████║███████║
╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝╚══════╝
====================================================================================
Price: ${Coin.format(Drink.getDrink(order.drink).price)}
""".trimIndent()
)
}
Expand All @@ -33,11 +32,11 @@ object CoinsInterface : UserInterface {
"""

override fun process(order: Order): Order {
val value: List<Int> = inputCoins()
val coins = Coin.getArray(value)
val value: List<Int> = input(order)
val coins = Coin.format(value)
val price = Drink.getDrink(order.drink).price
val amount = Coin.get(coins.toList())
val formatPrice = Coin.format(Coin.get(coins.toList()))
val formatPrice = Coin.format(coins)
if (amount >= price) {
println("Amount inserted: $formatPrice")
order.coins(*coins)
Expand All @@ -49,10 +48,12 @@ object CoinsInterface : UserInterface {
return order
}

private fun inputCoins(): List<Int> {
private fun input(order: Order): List<Int> {
val drink = Drink.getDrink(order.drink)
println("Price: ${Coin.format(drink.price)}")
val value: List<Int>
while (true) {
println("Please enter the coins (only numbers allowed):")
print("Please enter the coins (only numbers allowed): ")
val input = readln()
if (input.all { it.isDigit() || it.isWhitespace() }) {
value = input.split(" ")
Expand Down

0 comments on commit d066f0f

Please sign in to comment.