Iterating Through a Map

You have updated the patrons’ gold balances, and only one task remains – formatting the results in a more suitable manner. You are currently showing these balances, but you likely want to use a less technical format that omits any brackets or equals signs and that rounds balances for patrons like Sophie Fernsworth, whose balance above is being reported as 0.08999999999999986 due to floating point precision loss. You will improve your output formatting by iterating through your map with forEach.

Add a new function called displayPatronBalances at the end of Tavern.kt that iterates through the patronGold map, printing the final gold balance (formatted to the second decimal place, as you did in Chapter 5) for each ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.