Start learning free

LESSON 189 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Scala: Single Item Total

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).

Scala'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

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Scala, explained

    Problem overview

    This first version of the shopping cart prints a tiny receipt for one item. The program starts with three values: the item name, the price of one unit and the quantity bought. It multiplies the price by the quantity to get the total and prints two lines. The first is the item and the quantity, written as Coffee Mug x3. The second is the total, written as Total: $36.

    With price = 12 and quantity = 3, the total is 12 * 3 = 36:

    Coffee Mug x3
    Total: $36
    

    The lesson asks for the two lines to be joined into one string called result, which is then printed once.

    Approach: build each piece, then join them

    The receipt is made of small parts, and the clearest way to write it is one named value per part:

    • total is the number, the price times the quantity;
    • line1 is the first row, built from the item name, the text " x" and the quantity;
    • line2 is the second row, built from the text "Total: $" and the total;
    • result is the two rows with a newline between them.

    Naming each part makes the last line of the program trivial and makes mistakes easy to find: if the receipt looks wrong, you can print one part and see which one is off.

    Step by step

    1. Compute total. Write val total = price * quantity. Two Int values multiplied give an Int.
    2. Build line1. Join item, the text " x" and quantity with +. The space belongs inside the quotes, before the x.
    3. Build line2. Join the text "Total: $" and total. The dollar sign is part of the text.
    4. Build result. Put line1, then "\n", then line2. The sequence \n inside a string is a single newline character, and it makes whatever follows start on a new line.
    5. Print result with println.

    The solution in Scala

            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)
    

    Scala details worth knowing:

    • val means a value that never changes. Every name in this program is assigned once, so each is a val. Scala works out the types itself: total is an Int and line1 is a String.
    • + joins text and converts numbers. When one side of + is a String and the other is an Int, Scala turns the number into text. That is why item + " x" + quantity works with no explicit conversion.
    • The $ is an ordinary character here. In "Total: $" it prints as it is. It only has a special meaning inside an interpolated string, one that starts with s before the opening quote, such as s"$item x$quantity". That form gives the same Coffee Mug x3 as the + version, but this lesson uses +.
    • println adds a final newline. The printed result is two lines, and the output ends with a line break after Total: $36.
    • Order matters. The statements run from top to bottom, so a name has to be defined on an earlier line than the line that uses it.

    Testing

    For the starter values, result is the string Coffee Mug x3, a newline and Total: $36. It is 24 characters long, and splitting it at the newline gives two pieces, one per receipt row. Changing the data changes the receipt in the same way: with price = 10 and quantity = 1, the total is 10 and the receipt says Total: $10.

    Edge cases and common mistakes

    • Forgetting the space before the x. item + "x" + quantity prints Coffee Mugx3.
    • Leaving out "\n". The two rows are joined on one line: Coffee Mug x3Total: $36.
    • Putting a space after the newline. The second row would start with a space, and the text would no longer match.
    • Using var everywhere. It works, but val tells readers, and the compiler, that the value never changes.

    Key takeaways

    • Break the output into named parts, then join the parts.
    • + between a String and an Int makes text.
    • "\n" starts a new line, and $ is just a character in a plain string.
    • The next version builds on this one.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free