Problem overview
This version of the shopping cart program decides whether an order pays for shipping. It starts from three int variables: subtotal (the price of the goods), shipping_cost (the regular shipping fee) and free_shipping_threshold (the subtotal from which shipping is free). The program prints a three-line summary: Subtotal, Shipping and Total.
With a subtotal of 75, a shipping cost of 10 and a threshold of 100, the cart is under the threshold, so it pays the 10 and the total is 85:
Subtotal: $75
Shipping: $10
Total: $85
With a subtotal of 120 the order reaches the threshold, shipping is 0, and the total equals the subtotal. The Shipping line is still printed, showing $0. The new idea in this version is the decision; the next version builds on it.
Approach: decide first, then build the text
The value that changes between orders is shipping. It is either 0 or shipping_cost, and which one depends on a comparison. That is exactly what if/else is for: one branch runs when the condition is true, the other when it is false.
Do the decision first and the formatting afterwards. Once shipping has a value, the total is plain arithmetic, and the three output lines are the same for every order, so they need no special cases. This keeps the free-shipping rule in one place.
The condition is subtotal >= free_shipping_threshold. The comparison is "greater than or equal" because a cart exactly on the threshold ships free. With a threshold of 100, a subtotal of 100 gives Shipping: $0 and a subtotal of 99 gives Shipping: $10.
Step by step
- Declare
shippingwithout a value.int shipping;reserves the variable. Declaring it before theifmatters: a variable declared inside a block is gone when the block ends, and the code after theif/elseneeds it. - Write the
if. Its condition comparessubtotalwithfree_shipping_thresholdusing>=. - Free case. Inside the
ifblock, assignshipping = 0. - Open the
else. It runs for every cart that did not meet the condition. - Paid case. Inside the
else, assignshipping = shipping_cost. - Compute
total. The subtotal plus the shipping. When shipping is 0, the total equals the subtotal without any extra code. - Build the three rows.
line1,line2andline3each glue a label such as"Subtotal: $"to a number with+. - Join the rows into
result. Three rows need two newline characters, one between each pair, and none after the last row. - Print
resultwithSystem.out.println. The newlines inside the string put each row on its own line.
The solution in Java
int subtotal = 75;
int shipping_cost = 10;
int free_shipping_threshold = 100;
int shipping;
if (subtotal >= free_shipping_threshold) {
shipping = 0;
} else {
shipping = shipping_cost;
}
int total = subtotal + shipping;
String line1 = "Subtotal: $" + subtotal;
String line2 = "Shipping: $" + shipping;
String line3 = "Total: $" + total;
String result = line1 + "\n" + line2 + "\n" + line3;
System.out.println(result);
The Java details worth a closer look:
- Definite assignment.
int shipping;has no value yet, and Java refuses to compile code that might read a variable before it is assigned. Here both branches assign it, so the compiler knowsshippinghas a value on every path and accepts the line that uses it afterwards. If you delete theelsebranch, the compiler reports that the variable might not have been initialized. +on aStringand anint. When one operand is aString, Java converts the other to text and joins them. That is how"Total: $" + totalbecomesTotal: $85. The$is an ordinary character in a Java string, with no special meaning.- The escape
\n. Inside a string literal, backslash followed bynis one newline character.printlnthen adds a final line break after the whole string. - Naming. Variables such as
shipping_costcome from the starter code. Java convention would beshippingCost, but the lesson keeps the starter's names, so use them as they are.
Edge cases and common mistakes
- Using
>instead of>=. A subtotal of exactly 100 would pay 10 for shipping. With the correct comparison the output isSubtotal: $100,Shipping: $0,Total: $100. - Skipping the shipping line when it is free. The output must always have all three lines, so do not wrap the
Shippingrow in anif. - Declaring
shippinginside the branches.int shipping = 0;inside theifcreates a variable that exists only in that block, so the later lineint total = subtotal + shipping;does not compile. - Adding a number before the string.
subtotal + shipping + " total"adds the numbers first. In"Total: $" + subtotal + shipping, the numbers are joined as text, so 75 and 10 would appear as7510. Computetotalin its own variable, as the lesson does. - A trailing newline in
result. Putting"\n"after the last row adds an empty line at the end of the output.
Testing
These subtotals, with a shipping cost of 10 and a threshold of 100, give these Shipping and Total lines:
- Subtotal 75:
Shipping: $10,Total: $85. - Subtotal 120:
Shipping: $0,Total: $120. - Subtotal 100 (exactly on the threshold):
Shipping: $0,Total: $100. - Subtotal 99 (just under):
Shipping: $10,Total: $109. - Subtotal 0:
Shipping: $10,Total: $10.
