Start learning free

LESSON 412 OF 414 · PROJECTS: OBJECT-ORIENTED

Shopping Cart in Kotlin in Kotlin

Practice

A shop keeps its orders in a cart. Build two classes that work together: a LineItem is one row of the cart (a product, its price and how many were bought), and a Cart holds many rows and adds them up. The cart has line items (composition), it is not one.

LineItem

  • class LineItem(val name: String, val price: Double, val quantity: Int): the primary constructor declares the three fields.
  • fun subtotal(): Double returns price * quantity.

Cart

  • It keeps its items in private val items = mutableListOf<LineItem>().
  • fun add(item: LineItem) appends the item to the list.
  • fun total(): Double returns the sum of the subtotals of all items: start var sum = 0.0, loop with for (item in items), return sum.

Rules:

  • The total is worked out every time it is asked for, from the items in the cart at that moment. Adding an item later changes the next total.
  • An empty cart has a total of 0.
  • An item with price 5 and quantity 3 has a subtotal of 15.
  • Prices in the driver are whole numbers.

The driver lives in Solution.mainFunction() and uses .toInt() to print whole numbers.

The driver (already in the starter code, don't change it) creates an empty cart, checks one subtotal, then fills a cart and prints the total twice:

class Solution {
    fun mainFunction() {
        val empty = Cart()
        println("Empty: " + empty.total().toInt())
        val apple = LineItem("Apple", 5.0, 3)
        println("Subtotal: " + apple.subtotal().toInt())
        val cart = Cart()
        cart.add(apple)
        cart.add(LineItem("Bread", 20.0, 3))
        println("Total: " + cart.total().toInt())
        cart.add(LineItem("Milk", 10.0, 2))
        println("Total: " + cart.total().toInt())
    }
}

Expected output:

Empty: 0
Subtotal: 15
Total: 75
Total: 95

Write LineItem with its primary constructor first, then Cart.

for (item in items) { sum += item.subtotal() }

class LineItem(val name: String, val price: Double, val quantity: Int) {
    fun subtotal(): Double {
        return price * quantity
    }
}

class Cart {
    private val items = mutableListOf<LineItem>()

    fun add(item: LineItem) {
        items.add(item)
    }

    fun total(): Double {
        var sum = 0.0
        for (item in items) {
            sum += item.subtotal()
        }
        return sum
    }
}

class Solution {
    fun mainFunction() {
        val empty = Cart()
        println("Empty: " + empty.total().toInt())
        val apple = LineItem("Apple", 5.0, 3)
        println("Subtotal: " + apple.subtotal().toInt())
        val cart = Cart()
        cart.add(apple)
        cart.add(LineItem("Bread", 20.0, 3))
        println("Total: " + cart.total().toInt())
        cart.add(LineItem("Milk", 10.0, 2))
        println("Total: " + cart.total().toInt())
    }
}

STEP 1 OF 12
    Ctrl + Enter to check

    Shopping Cart in Kotlin

    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free