0
0
Kotlinprogramming~15 mins

Var for mutable references in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Using <code>var</code> for Mutable References in Kotlin
📖 Scenario: Imagine you are managing a simple inventory system for a small store. You want to keep track of the number of items available and update this number as items are sold or restocked.
🎯 Goal: You will create a mutable variable using var in Kotlin to hold the inventory count. Then, you will update this count to reflect sales and restocking.
📋 What You'll Learn
Create a mutable variable called inventoryCount with an initial value of 50.
Create a variable called soldItems with the value 5.
Update inventoryCount by subtracting soldItems.
Print the updated inventoryCount.
Create a variable called restockedItems with the value 10.
Update inventoryCount by adding restockedItems.
Print the final inventoryCount.
💡 Why This Matters
🌍 Real World
Managing inventory counts is a common task in stores, warehouses, and online shops. Using mutable variables helps keep track of changing stock levels.
💼 Career
Understanding mutable variables is essential for any Kotlin developer working on apps that handle data changes, such as inventory systems, games, or user settings.
Progress0 / 4 steps
1
Create a mutable variable for inventory count
Create a mutable variable called inventoryCount and set it to 50.
Kotlin
Need a hint?

Use var to create a variable that can change later.

2
Create a variable for sold items
Create a variable called soldItems and set it to 5.
Kotlin
Need a hint?

Use val for values that do not change.

3
Update inventory after sales
Update inventoryCount by subtracting soldItems using inventoryCount = inventoryCount - soldItems.
Kotlin
Need a hint?

Assign the new value back to inventoryCount after subtracting.

4
Print updated inventory and restock
Print the current inventoryCount. Then create a variable called restockedItems with value 10, update inventoryCount by adding restockedItems, and print the final inventoryCount.
Kotlin
Need a hint?

Use println() to show the values. Remember to update inventoryCount after restocking.