Start learning free

LESSON 188 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in Java: 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:

int subtotal = 75;
int shipping_cost = 10;
int free_shipping_threshold = 100;

Build:

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

Rules:

  • If subtotal >= free_shipping_threshold, set shipping to 0; otherwise set it to shipping_cost.
  • 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 System.out.println(result).

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

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

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

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

STEP 1 OF 11
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in Java, explained

    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

    1. Declare shipping without a value. int shipping; reserves the variable. Declaring it before the if matters: a variable declared inside a block is gone when the block ends, and the code after the if/else needs it.
    2. Write the if. Its condition compares subtotal with free_shipping_threshold using >=.
    3. Free case. Inside the if block, assign shipping = 0.
    4. Open the else. It runs for every cart that did not meet the condition.
    5. Paid case. Inside the else, assign shipping = shipping_cost.
    6. Compute total. The subtotal plus the shipping. When shipping is 0, the total equals the subtotal without any extra code.
    7. Build the three rows. line1, line2 and line3 each glue a label such as "Subtotal: $" to a number with +.
    8. Join the rows into result. Three rows need two newline characters, one between each pair, and none after the last row.
    9. Print result with System.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 knows shipping has a value on every path and accepts the line that uses it afterwards. If you delete the else branch, the compiler reports that the variable might not have been initialized.
    • + on a String and an int. When one operand is a String, Java converts the other to text and joins them. That is how "Total: $" + total becomes Total: $85. The $ is an ordinary character in a Java string, with no special meaning.
    • The escape \n. Inside a string literal, backslash followed by n is one newline character. println then adds a final line break after the whole string.
    • Naming. Variables such as shipping_cost come from the starter code. Java convention would be shippingCost, 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 is Subtotal: $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 Shipping row in an if.
    • Declaring shipping inside the branches. int shipping = 0; inside the if creates a variable that exists only in that block, so the later line int 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 as 7510. Compute total in 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.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free