0
0
Kotlinprogramming~15 mins

Map transformation in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Map transformation
📖 Scenario: You work in a small bookstore. You have a map of book titles and their prices. You want to update the prices by adding a fixed tax to each book.
🎯 Goal: Build a Kotlin program that creates a map of books and prices, sets a tax rate, applies the tax to each price using map transformation, and prints the updated prices.
📋 What You'll Learn
Create a map called bookPrices with these exact entries: "Kotlin Basics" to 30, "Advanced Kotlin" to 45, "Coroutines Guide" to 40
Create a variable called taxRate and set it to 0.1 (which means 10% tax)
Use mapValues on bookPrices to create a new map called updatedPrices where each price is increased by the tax rate
Print the updatedPrices map
💡 Why This Matters
🌍 Real World
Bookstores and shops often need to update prices by adding taxes or discounts to their product lists.
💼 Career
Understanding map transformations is useful for data processing, pricing algorithms, and working with collections in Kotlin.
Progress0 / 4 steps
1
Create the initial map of book prices
Create a map called bookPrices with these exact entries: "Kotlin Basics" to 30, "Advanced Kotlin" to 45, "Coroutines Guide" to 40
Kotlin
Need a hint?

Use mapOf to create a map with the given key-value pairs.

2
Set the tax rate
Create a variable called taxRate and set it to 0.1 (which means 10% tax)
Kotlin
Need a hint?

Use val taxRate = 0.1 to set the tax rate.

3
Apply the tax to each price using mapValues
Use mapValues on bookPrices to create a new map called updatedPrices where each price is increased by the tax rate
Kotlin
Need a hint?

Use mapValues with a lambda that multiplies each price by 1 + taxRate and converts to Int.

4
Print the updated prices
Write println(updatedPrices) to display the updated prices map
Kotlin
Need a hint?

Use println(updatedPrices) to show the final map with updated prices.