Challenge: Handling a negative Balance

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))
performPurchase(price = price.toDouble())
} 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 if (remainingBalance > 0) { println("Remaning Balance: {”%.2f".format(remainingBalance)}")
val remaningGold = remainingBalance.toInt()
val remainingSilver = (remainingBalance % 1 * 100).roundToInt()
playerGold = remaningGold
playerSilver = remainingSilver
displayBalance()
} else {
println(“You don’t have sufficient balance.”)
}
}
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 isPurchasePerformed = false

fun main() {
    for (i in 1..3) {
        placeOrder("shandy,Dragon's Breath,5.91")
        print("\n")
    }
}

fun performPurchase(price: Double) {
    displayBalance()
    val totalPurse = playerGold + (playerSilver / 100.0)
    println("Total purse: $totalPurse")
    println("Purchasing item for $price")

    if (isMoneyEnough(totalPurse, 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
        isPurchasePerformed = true
        displayBalance()
    } else {
        isPurchasePerformed = false
        println("Sorry, you are short in gold!")
    }
}

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())

    if (isPurchasePerformed) {
        val phrase = if (name == "Dragon's Breath") {
            "Madrigal exclaims: ${toDragonSpeak("Ah, delicious $name!")}"
        } else {
            "Madrigal says: Thanks for the $name"
        }
        println(phrase)
    }
}

private fun isMoneyEnough(totalPurse: Double, price: Double) = totalPurse >= price