Converting an Int to a Double
Now to take the gold out of the player’s purse. The purse contains whole gold and silver coins, but the price of a menu item is represented in gold as a double.
To do the sale, you first need to convert the player’s gold and silver to a single double so that the item price can be subtracted. Add a new variable to performPurchase to track the player’s total purse. One gold is worth 100 silver, so divide the player’s silver by 100 and add the result to the quantity of gold to get the total value. The totalPurse and price variables are of the same type, Double, so subtract the price from the purse and assign the result to a new variable.
Listing 8.4 Subtracting the price from the player’s purse (Tavern.kt ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access