Real World Example

Calculating a Food Order Bill

Every checkout screen in apps like Zomato or Swiggy combines arithmetic (subtotal, GST, discount), comparison (minimum order for a coupon) and logical operators (is the coupon valid AND the user eligible?). Amounts are kept as whole rupees or paise to avoid decimal errors.

Example

A complete bill calculation:

fun main() {
    val biryani = 299
    val raita = 49
    val qtyBiryani = 2
    val qtyRaita = 1

    val subtotal = biryani * qtyBiryani + raita * qtyRaita
    val couponMin = 500
    val isFirstOrder = true
    val couponApplies = subtotal >= couponMin && isFirstOrder
    val discount = if (couponApplies) subtotal * 20 / 100 else 0

    val afterDiscount = subtotal - discount
    val gst = afterDiscount * 5 / 100
    val deliveryFee = if (afterDiscount > 399) 0 else 35
    val total = afterDiscount + gst + deliveryFee

    println("Subtotal:   ₹$subtotal")
    println("Discount:  -₹$discount (coupon applied: $couponApplies)")
    println("GST (5%):   ₹$gst")
    println("Delivery:   ₹$deliveryFee")
    println("To pay:     ₹$total")
    println("Even split for 3 friends: ₹${total / 3}, remaining ₹${total % 3}")
}

Output

Subtotal:   ₹647
Discount:  -₹129 (coupon applied: true)
GST (5%):   ₹25
Delivery:   ₹0
To pay:     ₹543
Even split for 3 friends: ₹181, remaining ₹0

Practice

Exercise: A cab ride costs ₹50 base fare plus ₹12 per km. Calculate the fare for 17 km, and print whether it is above ₹250 using a comparison.

Show answer
fun main() {
    val km = 17
    val fare = 50 + 12 * km
    println("Fare: ₹$fare")            // Fare: ₹254
    println("Above 250: ${fare > 250}") // Above 250: true
}