Start learning free

LESSON 191 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in Kotlin: Free Shipping with if/else

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

        val subtotal = 75
        val shippingCost = 10
        val freeShippingThreshold = 100

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= freeShippingThreshold, set shipping to 0; otherwise set it to shippingCost.
  • total = subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result, then print it with println(result).

Decide shipping in an if/else first, then build the three lines.

Declare var shipping: Int before the if, so it is still visible after the if/else, then assign it in both branches.

In Kotlin "Total: $" + total converts the number to text for you.

        val subtotal = 75
        val shippingCost = 10
        val freeShippingThreshold = 100

        var shipping: Int
        if (subtotal >= freeShippingThreshold) {
            shipping = 0
        } else {
            shipping = shippingCost
        }
        val total = subtotal + shipping
        val line1 = "Subtotal: $" + subtotal
        val line2 = "Shipping: $" + shipping
        val line3 = "Total: $" + total
        val result = line1 + "\n" + line2 + "\n" + line3
        println(result)

STEP 1 OF 11
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in Kotlin, explained

    Problem overview

    This version of the shopping cart decides whether an order pays for shipping. The program starts with three values: a subtotal of 75, a shippingCost of 10 and a freeShippingThreshold of 100. A cart whose subtotal reaches the threshold ships free. Any other cart pays the regular shipping cost.

    The output is three lines, always all three, in this order: Subtotal, Shipping and Total. For the starting values the cart is under the threshold, so it pays shipping:

    Subtotal: $75
    Shipping: $10
    Total: $85

    When shipping is free, its line still appears and shows $0. The total is the subtotal plus whatever shipping turned out to be. The previous version already printed a receipt from fixed values; the new part here is the decision.

    Approach: decide first, then print

    The only thing that varies between a free cart and a paying cart is the value of shipping. Everything after it, the total and the three lines, is the same in both cases. So the program first settles the shipping amount with an if/else, then builds the output from it. That keeps the printing code written once instead of once per branch.

    The comparison is subtotal >= freeShippingThreshold. It uses "greater than or equal to" because a cart exactly on the threshold counts as free.

    There are other ways to write the decision. One is to print inside each branch, but that repeats the three println calls, and a later change to the receipt format would need to be made twice. Another is to compute the shipping in a single expression, which Kotlin allows because if can produce a value. This lesson uses the plain statement form because it shows the branching clearly, and the declared-then-assigned var is a pattern worth knowing.

    Step by step

    1. Declare shipping without a value. It is a var of type Int, and the two branches below will give it its value.
    2. Write the if. Its condition checks subtotal >= freeShippingThreshold. The closing brace goes in right away so the block is never left open.
    3. Free case. Inside the if, set shipping to 0.
    4. Open the else. It runs for every cart the condition rejected.
    5. Paying case. Inside the else, set shipping to shippingCost.
    6. Compute total. It is a val, the subtotal plus the shipping. For a free cart, shipping is 0, so the total equals the subtotal.
    7. Build line1, line2 and line3. Each is a label followed by a number.
    8. Build result. Join the three lines with "\n" between each two. Three lines need two separators.
    9. Print result with println.

    The solution in Kotlin

            val subtotal = 75
            val shippingCost = 10
            val freeShippingThreshold = 100
    
            var shipping: Int
            if (subtotal >= freeShippingThreshold) {
                shipping = 0
            } else {
                shipping = shippingCost
            }
            val total = subtotal + shipping
            val line1 = "Subtotal: $" + subtotal
            val line2 = "Shipping: $" + shipping
            val line3 = "Total: $" + total
            val result = line1 + "\n" + line2 + "\n" + line3
            println(result)
    

    Kotlin details worth a closer look:

    • A var with a type and no value. var shipping: Int is legal because the type is written out: Kotlin can't infer a type from nothing. The compiler also checks that every path assigns shipping before it is read. Both branches assign it, so the line val total = subtotal + shipping compiles. If one branch were missing, that line would be a compile error.
    • The dollar sign. In a string, $ followed by a name starts a template. A $ followed by a quote is just a dollar sign, so "Total: $" is the text Total: $. Writing "Total: $total" instead would insert the number but leave out the dollar sign, because the $ is consumed by the template. To keep a literal $ in front of a template, write \$.
    • String + Int. + converts the number to text when the left side is a String. That is why "Subtotal: $" + subtotal works. The conversion only goes that way: subtotal + "x" with an Int on the left does not compile.
    • The "\n" escape is a single newline character, so the three lines end up on separate lines when printed.

    The same if/else structure works for any rule with two outcomes: one condition, one branch for when it holds, one for when it doesn't. The skill in this lesson is picking the condition and the boundary correctly, not the syntax.

    Edge cases and common mistakes

    • The boundary. Using > instead of >= makes a cart of exactly 100 pay shipping. The condition must include equality.
    • Skipping the shipping line when it is free. The rules want all three lines every time, with $0 for free shipping.
    • Forgetting the separators. Without the "\n" pieces, the three lines run together on one line.

    Testing

    Change the starting subtotal and compare the outputs. A subtotal of 99, one under the threshold, prints:

    Subtotal: $99
    Shipping: $10
    Total: $109

    A subtotal of 100, exactly on the threshold, prints:

    Subtotal: $100
    Shipping: $0
    Total: $100

    A subtotal of 120 prints:

    Subtotal: $120
    Shipping: $0
    Total: $120

    Notice that a customer with 99 pays more in total than one with 100. That is how a threshold behaves, and it is the reason the boundary test matters.

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