0
0
Kotlinprogramming~30 mins

Map-backed delegated properties in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Map-backed Delegated Properties in Kotlin
📖 Scenario: You are creating a simple user profile system where user details are stored in a map. You want to access these details as properties of a Kotlin class using map-backed delegated properties.
🎯 Goal: Build a Kotlin class UserProfile that uses a Map to store user data and access it through delegated properties.
📋 What You'll Learn
Create a Map with user data including "name", "age", and "email".
Create a UserProfile class that takes the map as a constructor parameter.
Use map-backed delegated properties to access name, age, and email inside UserProfile.
Print the values of name, age, and email from an instance of UserProfile.
💡 Why This Matters
🌍 Real World
Map-backed delegated properties are useful when you want to store data in a flexible map but access it like normal class properties, such as in configuration settings or user profiles.
💼 Career
Understanding delegated properties and map usage is valuable for Kotlin developers working on Android apps or backend services where dynamic data structures are common.
Progress0 / 4 steps
1
Create the user data map
Create a Map called userData with these exact entries: "name" to "Alice", "age" to 30, and "email" to "alice@example.com".
Kotlin
Need a hint?

Use mapOf to create a map with keys and values.

2
Create the UserProfile class with map parameter
Create a class called UserProfile that takes a Map<String, Any> parameter called data in its primary constructor.
Kotlin
Need a hint?

Define a class with a primary constructor that takes a map parameter.

3
Add map-backed delegated properties
Inside the UserProfile class, add three properties: name, age, and email. Use by data to delegate these properties to the data map.
Kotlin
Need a hint?

Use val propertyName: Type by data syntax for map-backed delegation.

4
Create UserProfile instance and print properties
Create an instance of UserProfile called user using userData. Then print user.name, user.age, and user.email each on a new line.
Kotlin
Need a hint?

Create the UserProfile instance and use println to show each property.