Skip to content

Commit

Permalink
🐛 validate input
Browse files Browse the repository at this point in the history
  • Loading branch information
velizartodorov committed Oct 19, 2024
1 parent c5bc739 commit 5b534e9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
21 changes: 18 additions & 3 deletions src/main/kotlin/vending_machine/intefaces/CoinsInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ object CoinsInterface : UserInterface {
"""

override fun process(order: Order): Order {
val value = readln()
.split(" ")
.map(String::toInt)
val value: List<Int> = inputCoins()
val coins = Coin.getArray(value)
val price = Drink.getDrink(order.drink).price
val amount = Drink.getAmount(coins.toList())
Expand All @@ -50,4 +48,21 @@ object CoinsInterface : UserInterface {
return order
}

private fun inputCoins(): List<Int> {
val value: List<Int>
while (true) {
println("Please enter the coins (only numbers allowed):")
val input = readln()
if (input.all { it.isDigit() || it.isWhitespace() }) {
value = input.split(" ")
.filter { it.isNotBlank() }
.map(String::toInt)
break
} else {
println("Invalid input. Please enter only numbers separated by spaces.")
}
}
return value
}

}
19 changes: 17 additions & 2 deletions src/main/kotlin/vending_machine/intefaces/DrinkInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ object DrinkInterface : UserInterface {
Latte: 10 Macchiato: 11 Mocha: 12
Tea: 13 Water: 14
===================================================================
PICK A DRINK (1 - 14):
""".trimIndent()
)
}

override fun process(order: Order): Order {
val inputDrink = readln().toInt()
val inputDrink: Int = input()

try {
val drink = Drink.get(inputDrink)
println("Drink selected: $drink")
Expand All @@ -39,4 +39,19 @@ object DrinkInterface : UserInterface {
return order
}

private fun input(): Int {
val inputDrink: Int
while (true) {
print("Please enter a drink number (1 - 14): ")
val input = readln()
if (input.toIntOrNull()?.let { it in 1..14 } == true) {
inputDrink = input.toInt()
break
} else {
println("Invalid input. Please enter a number between 1 and 14.")
}
}
return inputDrink
}

}
21 changes: 17 additions & 4 deletions src/main/kotlin/vending_machine/intefaces/MilkInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ object MilkInterface : UserInterface {
==========================================================
NONE: 1 LOW: 2 MEDIUM: 3 HIGH: 4 MAX: 5
==========================================================
Pick amount (1-5):
""".trimIndent()
""".trimIndent()
)
}

override fun process(order: Order): Order {
val inputAmount = readln().toInt()
try {
val milk = Amount.get(inputAmount)
val milk = Amount.get(input())
println("Milk amount selected: $milk")
order.milk(milk)
} catch (e: IllegalArgumentException) {
Expand All @@ -35,4 +33,19 @@ object MilkInterface : UserInterface {
return order
}

private fun input(): Int {
val inputDrink: Int
while (true) {
print("Pick amount (1-5): ")
val input = readln()
if (input.toIntOrNull()?.let { it in 1..5 } == true) {
inputDrink = input.toInt()
break
} else {
println("Invalid input. Please enter a number between 1 and 5.")
}
}
return inputDrink
}

}
21 changes: 17 additions & 4 deletions src/main/kotlin/vending_machine/intefaces/StrengthInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ object StrengthInterface : UserInterface {
=====================================================================
LOW: 1 MEDIUM: 2 HIGH: 3 MAX: 4
=====================================================================
Pick strength (1-4):
""".trimIndent()
""".trimIndent()
)
}

override fun process(order: Order): Order {
val inputStrength = readln().toInt()
try {
val selectedStrength = Strength.get(inputStrength)
val selectedStrength = Strength.get(input())
println("Strength selected: $selectedStrength")
order.strength(selectedStrength)
} catch (e: IllegalArgumentException) {
Expand All @@ -35,4 +33,19 @@ object StrengthInterface : UserInterface {
return order
}

private fun input(): Int {
val strength: Int
while (true) {
print("Pick strength (1-4): ")
val input = readln()
if (input.toIntOrNull()?.let { it in 1..4 } == true) {
strength = input.toInt()
break
} else {
println("Invalid input. Please enter a number between 1 and 4.")
}
}
return strength
}

}
21 changes: 17 additions & 4 deletions src/main/kotlin/vending_machine/intefaces/SugarInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,33 @@ object SugarInterface : UserInterface {
==========================================================
NONE: 1 LOW: 2 MEDIUM: 3 HIGH: 4 MAX: 5
==========================================================
Pick amount (1-5):
""".trimIndent()
""".trimIndent()
)
}

override fun process(order: Order): Order {
val inputAmount = readln().toInt()
try {
val sugar = Amount.get(inputAmount)
val sugar = Amount.get(input())
println("Sugar amount selected: $sugar")
order.sugar(sugar)
} catch (e: IllegalArgumentException) {
reprocess(e, order)
}
return order
}

private fun input(): Int {
val amount: Int
while (true) {
print("Pick amount (1-5): ")
val input = readln()
if (input.toIntOrNull()?.let { it in 1..5 } == true) {
amount = input.toInt()
break
} else {
println("Invalid input. Please enter a number between 1 and 5.")
}
}
return amount
}
}

0 comments on commit 5b534e9

Please sign in to comment.