Did anyone complete the “Remaining Pints” challenge?
Here is how i achieved
const val TAVERN_NAME = “Taernyl’s Folly”
var playerGold = 10
var playerSilver = 10
var dragonBreathQuantity = 5.0
val onePint=0.125
val showPintCountThreshhold = 12
fun main() {
displayRemaningDragonsBreath(0)
placeOrder(“shandy,DRAGON’S BREATH; IT’S GOT WHAT ADVENTURES CRAVE,5.91”)
displayRemaningDragonsBreath(13)
}
fun placeOrder(menuData:String){
try {
val indexOfApostrophe = TAVERN_NAME.indexOf(’’’)
val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
println(“Madrigal speaks with tavernMaster about their order")
/*val data = menuData.split(",")
val type = data[0]
val name = data[1]
val price = data[2]*/
val (type,name,price) = menuData.split(',')
val message = "Madrigal buys a {name.split(’;’)[0].toLowerCase().capitalize()} ($type) for $price”
val phrase = “Ah, delicious $name”
println(message)
performPurchase(price = price.toDouble())
println(toDragonSpeak(phrase))
}catch (e:Exception){
println(e)
println(“Oh No!. Details not fully entered.”)
}
}
fun toDragonSpeak(phrase:String)=phrase.replace(Regex("[aAeEiIoOuU]")){
when(it.value){
“a”->“4”
“e”->“3”
“i”->“1”
“o”->“0”
“u”->"||"
“A”->“4”
“E”->“3”
“I”->“1”
“O”->“0”
“U”->"||"
else->it.value
}
}
fun performPurchase(price: Double){
displayBalance()
val totalPurse = playerGold+(playerSilver/100.0)
println(“Total purse: $totalPurse”)
println(“Purchasing item for price")
val remainingBalance=totalPurse-price
println("Remaning Balance: {”%.2f".format(remainingBalance)}")
val remaningGold = remainingBalance.toInt()
val remainingSilver = (remainingBalance%1*100).roundToInt()
playerGold=remaningGold
playerSilver=remainingSilver
displayBalance()
}
private fun displayBalance(){
println(“Player’s purse balance: Gold: $playerGold, Silver $playerSilver”)
}
private fun displayRemaningDragonsBreath(purchasedquantity:Int){
dragonBreathQuantity -= (purchasedquantity * onePint)
println(“Remaining Breath (Gallons): dragonBreathQuantity")
if(purchasedquantity>=showPintCountThreshhold){
println("Remaining Pints: {(dragonBreathQuantity/onePint).roundToInt()}”)
}
}
Here is my solution:
Tavern.kt
import kotlin.math.roundToInt
const val TAVERN_NAME = "Taernyl's Folly"
var playerGold = 10
var playerSilver = 10
var dragonBreathCaskGallons = 5.0
const val PINT_IN_GALLON = 0.125
var dragonBreathCaskPints = dragonBreathCaskGallons / PINT_IN_GALLON
var dragonBreathOrders = 0
fun main() {
for (i in 1..15) {
println("--- Order number: $i ---")
placeOrder("shandy,Dragon's Breath,5.91")
}
// placeOrder("elixir,Shirley's Temple,4.12")
}
fun performPurchase(price: Double) {
displayBalance()
val totalPurse = playerGold + (playerSilver / 100.0)
println("Total purse: $totalPurse")
println("Purchasing item for $price")
val remainingBalance = totalPurse - price
println("Remaining balance: ${"%.2f".format(remainingBalance)}")
val remainingGold = remainingBalance.toInt()
val remainingSilver = (remainingBalance % 1 * 100).roundToInt()
playerGold = remainingGold
playerSilver = remainingSilver
displayBalance()
}
fun displayBalance() {
println("Player's purse balance: Gold: $playerGold , Silver: $playerSilver")
}
private fun toDragonSpeak(phrase: String) =
phrase.replace(Regex("[aeiou]")) {
when (it.value) {
"a" -> "4"
"e" -> "3"
"i" -> "1"
"o" -> "0"
"u" -> "|_|"
else -> it.value
}
}
private fun placeOrder(menuData: String) {
val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
println("Madrigal speaks with $tavernMaster about their order.")
val (type, name, price) = menuData.split(',')
val message = "Madrigal buys a $name ($type) for $price"
println(message)
performPurchase(price.toDouble())
val phrase = if (name == "Dragon's Breath") {
"Madrigal exclaims: ${toDragonSpeak("Ah, delicious $name!")}"
} else {
"Madrigal says: Thanks for the $name"
}
println(phrase)
if (name == "Dragon's Breath") {
dragonBreathOrders++
dragonBreathCaskPints--
if (dragonBreathOrders >= 12) displayDragonBreathPintsLeft()
}
}
private fun displayDragonBreathPintsLeft() {
println("Dragon Breath pints left: ${dragonBreathCaskPints.toInt()}")
}