Real World Example

Order Status and Payment Results

A Zomato or Swiggy tracking screen shows a different message for each order status, and a UPI app like PhonePe shows a different screen for each payment result code. when is the standard way to map these values to UI text.

Example

Mapping order status and UPI response codes:

fun statusText(status: String, minutesLeft: Int): String = when (status) {
    "PLACED" -> "Order placed. Waiting for restaurant"
    "ACCEPTED", "PREPARING" -> "Your food is being prepared"
    "PICKED_UP" -> if (minutesLeft <= 5) "Almost there!" else "On the way, $minutesLeft min"
    "DELIVERED" -> "Delivered. Enjoy your meal!"
    "CANCELLED" -> "Order cancelled. Refund in 3-5 days"
    else -> "Updating status..."
}

fun upiResult(code: String): String = when (code) {
    "00" -> "Payment successful"
    "Z9", "Z8" -> "Insufficient funds"
    "U16" -> "Daily limit exceeded"
    in listOf("U30", "U68") -> "Bank server busy, try again"
    else -> "Payment failed ($code)"
}

fun main() {
    println(statusText("PREPARING", 25))
    println(statusText("PICKED_UP", 12))
    println(statusText("PICKED_UP", 3))
    println(statusText("DELIVERED", 0))
    println(upiResult("00"))
    println(upiResult("Z9"))
    println(upiResult("U68"))
    println(upiResult("XX"))
}

Output

Your food is being prepared
On the way, 12 min
Almost there!
Delivered. Enjoy your meal!
Payment successful
Insufficient funds
Bank server busy, try again
Payment failed (XX)

Practice

Exercise: Write a function shippingCharge(weightKg: Int): Int using when with ranges: up to 1 kg → 40, 2–5 kg → 80, 6–10 kg → 150, above → 300.

Show answer
fun shippingCharge(weightKg: Int): Int = when (weightKg) {
    in 0..1 -> 40
    in 2..5 -> 80
    in 6..10 -> 150
    else -> 300
}

fun main() {
    println(shippingCharge(1))   // 40
    println(shippingCharge(7))   // 150
    println(shippingCharge(25))  // 300
}