Start learning free

LESSON 189 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Kotlin: A One-Item Receipt

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

You start with:

        val item = "Coffee Mug"
        val price = 12
        val quantity = 3

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total = price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result, then print it with println(result).

Kotlin's + on a String and an Int converts the integer to text for you.

line1 + "\n" + line2 joins both lines into one string.

        val item = "Coffee Mug"
        val price = 12
        val quantity = 3

        val total = price * quantity
        val line1 = item + " x" + quantity
        val line2 = "Total: $" + total
        val result = line1 + "\n" + line2
        println(result)

STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Kotlin, explained

    Problem overview

    This is the first version of the Shopping Cart project. It is a small program that prints a one-item receipt, and it practises variables, arithmetic and string concatenation together. The starter code gives item = "Coffee Mug", price = 12 and quantity = 3.

    The program must work out the total cost, which is price * quantity, build two lines of text, join them with a newline into one string called result, and print it. The expected output is:

    Coffee Mug x3
    Total: $36
    

    The first line is the item name, a space, an x and the quantity. The second line is Total: $ followed by the total.

    Approach

    Work in the order the data flows. First calculate the number you need, total. Then build each output line in its own variable, line1 and line2. Finally join the lines into result and print it once.

    Giving each line a name makes the program easy to read and to change. When a later version adds more rows, the new rows are just more variables, and the part that joins them is the only place that needs to grow.

    Step by step

    1. Create total as price * quantity. Three mugs at 12 each cost 36. It is a val, because it is calculated once.
    2. Create line1. Join item, the text " x" (a space and the letter x) and quantity. The text starts with a space, which is why the output reads Coffee Mug x3 and not Coffee Mugx3.
    3. Create line2. Join the label "Total: $" and total.
    4. Create result. Join line1, "\n" and line2. The \n escape is a single newline character, so the second line begins right after it.
    5. Print result with println.

    The solution in Kotlin

            val item = "Coffee Mug"
            val price = 12
            val quantity = 3
    
            val total = price * quantity
            val line1 = item + " x" + quantity
            val line2 = "Total: $" + total
            val result = line1 + "\n" + line2
            println(result)
    

    Kotlin details worth knowing:

    • Adding a number to a string. When the left side of + is a String, Kotlin turns the number on the right into text and joins them. So item + " x" + quantity works from left to right: the String item plus " x" is a String, and a String plus the Int quantity is a String again. The other order does not compile: quantity + " x" is an error, because Int.plus accepts only numbers.
    • Arithmetic comes first. price * quantity is an Int, with no text involved. Doing the arithmetic in its own variable keeps numbers and text apart.
    • Dollar signs in strings. Kotlin uses $ for string templates, as in "$total". A $ directly before a closing quote has nothing to insert, so "Total: $" prints a plain dollar sign. To put a dollar sign right before a template, escape it: "Total: \$$total" also prints Total: $36.
    • The newline. "\n" is an escape sequence for one character. println then adds its own line ending after the last line.
    • Types. Kotlin infers total as Int and the lines as String. No type annotations are needed.

    Edge cases and common mistakes

    • A missing space. Writing "x" instead of " x" gives Coffee Mugx3. The lesson's tests compare the text exactly.
    • A space after the dollar sign. The second line is Total: $36, with no space between $ and the number.
    • Starting a join with a number. total + " dollars" doesn't compile for the reason above. Start with a string or use a template.
    • Printing the lines separately. Two println calls produce the same two lines, but the lesson asks for one string, result, built with a newline, so follow that.
    • Adding instead of multiplying. The total is price * quantity, not a sum of the two.

    Testing

    Change item, price and quantity in the editor above and check the output:

    item, price, quantityoutput
    "Coffee Mug", 12, 3Coffee Mug x3, then Total: $36
    "Notebook", 4, 10Notebook x10, then Total: $40
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free