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
- Declare
shippingwithout a value. It is avarof typeInt, and the two branches below will give it its value. - Write the
if. Its condition checkssubtotal >= freeShippingThreshold. The closing brace goes in right away so the block is never left open. - Free case. Inside the
if, setshippingto 0. - Open the
else. It runs for every cart the condition rejected. - Paying case. Inside the
else, setshippingtoshippingCost. - Compute
total. It is aval, the subtotal plus the shipping. For a free cart, shipping is 0, so the total equals the subtotal. - Build
line1,line2andline3. Each is a label followed by a number. - Build
result. Join the three lines with"\n"between each two. Three lines need two separators. - Print
resultwithprintln.
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
varwith a type and no value.var shipping: Intis legal because the type is written out: Kotlin can't infer a type from nothing. The compiler also checks that every path assignsshippingbefore it is read. Both branches assign it, so the lineval total = subtotal + shippingcompiles. 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 textTotal: $. 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 aString. That is why"Subtotal: $" + subtotalworks. The conversion only goes that way:subtotal + "x"with anInton 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
$0for 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.
