Start learning free

LESSON 190 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Swift: 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:

        let item = "Coffee Mug"
        let price = 12
        let 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 print(result).

Swift's string interpolation \(...) embeds any value directly in a string literal.

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

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

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

STEP 1 OF 5
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Swift, explained

    Problem overview

    The first version of the shopping cart project prints a tiny receipt for a single item. The starter code defines item ("Coffee Mug"), price (12) and quantity (3). The program computes the total, then builds one string, result, with two lines and prints it:

    Coffee Mug x3
    Total: $36
    

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

    Approach: compute, format each line, then join

    The work splits into three small pieces, and each one gets a named constant, so every line of the program does one thing:

    • the arithmetic, total, which is the price of one item repeated once per item bought;
    • the two text lines, line1 and line2, each a string literal with interpolated values;
    • the joined text, result, which is line1, a newline and line2.

    Doing the arithmetic before the formatting means the text-building lines contain no calculations, and each row can be read straight off the output it produces.

    Step by step

    1. Create total as a let equal to price * quantity. This is the cost of every mug.
    2. Create line1: the item name, a space, x, then the quantity. Interpolation puts both values into one literal, and the space before the x is part of the literal.
    3. Create line2: the label Total: $ followed by the total, again as one literal with interpolation.
    4. Create result by joining line1, a newline and line2 with +. The newline character starts a new row where it sits.
    5. Print result with print.

    The solution in Swift

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

    The Swift details worth knowing:

    • Interpolation converts for you. In "\(item) x\(quantity)", item is a string and quantity an Int, and both go in without a conversion call. The x sits directly after the first hole and directly before the second, with no extra characters.
    • Numbers don't join to text with +. Writing item + " x" + quantity doesn't compile: the compiler reports binary operator '+' cannot be applied to operands of type 'String' and 'Int'. Use interpolation, or String(quantity).
    • The $ is a normal character. In "Total: $\(total)", the dollar sign is plain text and the interpolation follows it.
    • "\n" is a newline. The two characters in the source, a backslash and an n, become one newline character in the string, so the single print shows two rows.
    • Type inference. price and quantity are Ints, so total is an Int too. Arithmetic between two different numeric types, such as an Int and a Double, needs an explicit conversion in Swift.
    • Overflow. An Int is 64 bits in Swift, so this multiplication has plenty of room. If a product ever did exceed that range, Swift would stop the program with a run-time error, not wrap around.

    Edge cases and common mistakes

    • Quantity of zero. With quantity = 0, the receipt reads Coffee Mug x0 and Total: $0. The code needs no special case.
    • Missing spaces. The row is Coffee Mug x3: a space between the name and the x, and no space between the x and the number. Check the literal character by character.
    • Forgetting the newline. Joining line1 + line2 without "\n" puts both rows on one line.
    • Hard-coding the numbers. Typing 36 into the string matches today's data only. Compute total from price and quantity, and interpolate it.
    • Printing the rows with two separate calls. That also shows two rows, but the lesson builds a single result string first, and the exercise is about composing it.

    Testing

    • The starter data prints Coffee Mug x3 and Total: $36.
    • With quantity = 0, it prints Coffee Mug x0 and Total: $0.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free