Part 06 / Topic 7 of 7
Putting it together: a shopping cart
Every tool from this part, in one program: a read-only catalog (mapOf), a mutable cart
(mutableMapOf), and a menu loop reusing Part 4's while (true) + when + break shape.
fun printCatalog(catalog: Map<String, Double>) {
println("--- Catalog ---")
for ((item, price) in catalog) {
println("$item: ₹%.2f".format(price))
}
}
fun printCart(cart: Map<String, Int>, catalog: Map<String, Double>) {
if (cart.isEmpty()) {
println("Cart is empty.")
return
}
println("--- Your cart ---")
for ((item, qty) in cart) {
val price = catalog[item] ?: 0.0
println("$item x $qty = ₹%.2f".format(price * qty))
}
val total = cart.entries.sumOf { (item, qty) -> (catalog[item] ?: 0.0) * qty }
println("Subtotal: ₹%.2f".format(total))
}
fun main() {
val catalog: Map<String, Double> = mapOf(
"Rice (kg)" to 68.0,
"Toor dal (kg)" to 145.0,
"Atta (kg)" to 52.0,
"Milk (litre)" to 58.0,
"Sugar (kg)" to 45.0,
"Tea (250g)" to 120.0
)
val cart: MutableMap<String, Int> = mutableMapOf()
while (true) {
println()
println("1. View catalog")
println("2. Add item to cart")
println("3. Remove item from cart")
println("4. View cart")
println("5. Checkout")
println("6. Exit")
print("Choose an option: ")
val choice = readlnOrNull()?.trim()
when (choice) {
"1" -> printCatalog(catalog)
"2" -> {
print("Item name (exact catalog spelling): ")
val item = readlnOrNull()?.trim() ?: ""
if (item !in catalog) {
println("\"$item\" is not in the catalog.")
} else {
print("Quantity: ")
val qty = readlnOrNull()?.trim()?.toIntOrNull()
if (qty == null || qty <= 0) {
println("Enter a valid positive quantity.")
} else {
cart[item] = (cart[item] ?: 0) + qty
println("Added $qty x $item. Cart now has ${cart[item]} of it.")
}
}
}
"3" -> {
print("Item name to remove: ")
val item = readlnOrNull()?.trim() ?: ""
if (item in cart) {
cart.remove(item)
println("Removed $item from the cart.")
} else {
println("\"$item\" is not in your cart.")
}
}
"4" -> printCart(cart, catalog)
"5" -> {
if (cart.isEmpty()) {
println("Cart is empty -- nothing to check out.")
} else {
printCart(cart, catalog)
cart.clear()
println("Checked out. Cart is now empty.")
}
}
"6" -> {
println("Thanks for shopping with us.")
break
}
else -> println("Invalid option, choose 1-6.")
}
}
}
A few design choices worth noticing:
catalogis typedMap<String, Double>on purpose — nothing in this program should ever be able to change the price list, and the type itself enforces that, the same point section 2 made.cart[item] = (cart[item] ?: 0) + qtyis the "update or insert" pattern from section 4: read what's there (or0if it's a new item), add the new quantity, write it back.printCartreturns early (return) for an empty cart instead of printing an empty--- Your cart ---header followed by a₹0.00subtotal — a small readability choice, same spirit as Part 5's early-return style.
Three runs, each piped through printf for a reproducible transcript, exercise different paths:
$ kotlinc 07_shopping_cart.kt -include-runtime -d cart.jar
$ printf '1\n2\nRice (kg)\n5\n2\nMilk (litre)\n3\n4\n6\n' | java -jar cart.jar
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: --- Catalog ---
Rice (kg): ₹68.00
Toor dal (kg): ₹145.00
Atta (kg): ₹52.00
Milk (litre): ₹58.00
Sugar (kg): ₹45.00
Tea (250g): ₹120.00
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Added 5 x Rice (kg). Cart now has 5 of it.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Added 3 x Milk (litre). Cart now has 3 of it.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: --- Your cart ---
Rice (kg) x 5 = ₹340.00
Milk (litre) x 3 = ₹174.00
Subtotal: ₹514.00
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Thanks for shopping with us.
A second run exercises an unknown item, an invalid menu choice, viewing an empty cart, removing an item that was never added, and a full checkout:
$ printf '2\nGhee (kg)\n2\n2\nAtta (kg)\n4\n2\nSugar (kg)\n2\n3\nAtta (kg)\n4\n5\n6\n' | java -jar cart.jar
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): "Ghee (kg)" is not in the catalog.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): "2" is not in the catalog.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Invalid option, choose 1-6.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Cart is empty.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Added 2 x Sugar (kg). Cart now has 2 of it.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name to remove: "Atta (kg)" is not in your cart.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: --- Your cart ---
Sugar (kg) x 2 = ₹90.00
Subtotal: ₹90.00
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: --- Your cart ---
Sugar (kg) x 2 = ₹90.00
Subtotal: ₹90.00
Checked out. Cart is now empty.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Thanks for shopping with us.
Notice the second line of input, "2" is typed where an item name was expected (right after the
first invalid-item message) — it's rejected the same way any other unknown item would be, which is
exactly the point of checking item !in catalog rather than assuming the next line is well-formed.
A third run shows cart[item] = (cart[item] ?: 0) + qty accumulating the same item across two separate
Add calls, plus two different invalid-quantity rejections:
$ printf '2\nRice (kg)\n3\n2\nRice (kg)\n2\n4\n2\nMilk (litre)\nabc\n2\nMilk (litre)\n0\n4\n6\n' | java -jar cart.jar
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Added 3 x Rice (kg). Cart now has 3 of it.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Added 2 x Rice (kg). Cart now has 5 of it.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: --- Your cart ---
Rice (kg) x 5 = ₹340.00
Subtotal: ₹340.00
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Enter a valid positive quantity.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Item name (exact catalog spelling): Quantity: Enter a valid positive quantity.
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: --- Your cart ---
Rice (kg) x 5 = ₹340.00
Subtotal: ₹340.00
1. View catalog
2. Add item to cart
3. Remove item from cart
4. View cart
5. Checkout
6. Exit
Choose an option: Thanks for shopping with us.
Full program: examples/07_shopping_cart.kt.
Done here? The practice set for Part 6 — including the solutions — is on the part page.