Challenge: Dragoncoin

import kotlin.math.roundToInt

const val TAVERN_NAME = “Taernyl’s Folly”
var playerGold = 10
var playerSilver = 10
var dragonBreathQuantity = 5.0
val onePint = 0.125
val showPintCountThreshhold = 12
var dragonCoins = 5.0
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 remainingGold = remainingBalance.toInt()
val remainingSilver = (remainingBalance % 1 * 100).roundToInt()
val remainingDragonCoins = dragonCoins-(price/1.43)
playerGold = remainingGold
playerSilver = remainingSilver
dragonCoins = remainingDragonCoins
displayBalance()
} else {
println(“You don’t have sufficient balance.”)
}
}
private fun displayBalance() {
println(“Player’s purse balance: DragonCoin: ${”%.4f".format(dragonCoins)} 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 to Dragoncoin Challenge:

Tavern.kt
import kotlin.math.roundToInt

const val TAVERN_NAME = "Taernyl's Folly"

var playerDragonCoins = 5.0

fun main() {
    placeOrder("shandy,Dragon's Breath,5.91")
//    placeOrder("elixir,Shirley's Temple,4.12")
}

fun performPurchase(price: Double) {
    displayBalance()
    val totalPurse = playerDragonCoins * 1.43
    println("Total purse: ${"%.2f".format(totalPurse)}")
    println("Purchasing item for $price")

    val remainingBalance = totalPurse - price
    println("Remaining balance: ${"%.2f".format(remainingBalance)}")

    val remainingDragonCoins = remainingBalance / 1.43
    playerDragonCoins = remainingDragonCoins
    displayBalance()
}

fun displayBalance() {
    println("Player's purse balance: Dragon Coins: ${"%.4f".format(playerDragonCoins)}")
}

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