Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 03 / Topic 8 of 8

Putting it together: a safe user-profile lookup

Everything above, in one program: a small support-desk tool that looks up a user's profile by username. Three maps stand in for three independent database columns — name, email, phone, city — each one allowed to be missing a given username on its own, because that's how real user data actually looks: some people never give a phone number, some never give an email, and a username that doesn't exist at all needs its own answer, not a crash.

val names: Map<String, String> = mapOf(
    "asha.rao" to "Asha Rao", "vikram.singh" to "Vikram Singh",
    "deepa.iyer" to "Deepa Iyer", "neha.kapoor" to "Neha Kapoor"
)
val emails: Map<String, String> = mapOf("asha.rao" to "[email protected]", /* ... */)
val phones: Map<String, String> = mapOf("vikram.singh" to "98765 43210")

// Set once, at startup, before any lookup runs — exactly the case lateinit is for.
lateinit var supportRegion: String

fun printProfileCard(username: String) {
    val fullName = names[username]
        ?: run { println("[$username] no such user on file"); return }   // Elvis + early return

    println("---- Profile: $fullName ($username) ----")
    println("Region       : $supportRegion")                              // lateinit, no ?. needed
    println("City         : ${cities[username] ?: "not on file"}")       // Elvis default

    emails[username]?.let { address -> println("Email        : $address") }   // ?.let
    phones[username]?.let { number -> println("Phone        : $number") }    // ?.let
}

Every nullable touchpoint in this program — a missing username, a missing email, a missing phone, a missing city — is handled with ?., ?: or ?.let { }. lateinit var supportRegion covers the one value that's genuinely always there by the time a lookup runs, set once in startUp() before the loop begins. There is no !! anywhere in this file, and no path through it throws an uncaught exception.

$ kotlinc 08_user_profile_lookup.kt -include-runtime -d 08.jar
$ java -jar 08.jar
Support desk booted for region: South Asia
---- Profile: Asha Rao (asha.rao) ----
Region       : South Asia
City         : Pune
Email        : [email protected]

---- Profile: Vikram Singh (vikram.singh) ----
Region       : South Asia
City         : Jaipur
Phone        : 98765 43210

---- Profile: Deepa Iyer (deepa.iyer) ----
Region       : South Asia
City         : Chennai
Email        : [email protected]

---- Profile: Neha Kapoor (neha.kapoor) ----
Region       : South Asia
City         : not on file
Contact      : no email or phone on file for Neha Kapoor

[rahul.verma] no such user on file

Five lookups, four real users and one that doesn't exist, every missing field handled on its own terms — Neha Kapoor has neither email nor phone and still gets a clear, specific line instead of a crash or a silent blank.

Full program: examples/08_user_profile_lookup.kt.

Done here? The practice set for Part 3 — including the solutions — is on the part page.

Search the guide