Start learning free

LESSON 185 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Java: Single Item Total

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

You start with:

String item = "Coffee Mug";
int price = 12;
int 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 System.out.println(result).

Java's + on a String and an int converts the integer to text for you.

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

String item = "Coffee Mug";
int price = 12;
int quantity = 3;

int total = price * quantity;
String line1 = item + " x" + quantity;
String line2 = "Total: $" + total;
String result = line1 + "\n" + line2;
System.out.println(result);

STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Java, explained

    Problem overview

    This project prints a tiny receipt for one item. The starter code gives you item ("Coffee Mug"), price (12) and quantity (3). The program computes the total and prints two lines: the item with its quantity, then the total.

    Coffee Mug x3
    Total: $36

    The lesson practises three basics together: variables, arithmetic and joining strings. The two lines must be joined with a newline into one string called result, which is printed with a single System.out.println(result).

    Approach: numbers first, then text

    First compute the number, total = price * quantity, with int arithmetic. Then build each line as its own String, then join the lines, and print once. Keeping the calculation apart from the formatting makes each variable do one job, and a mistake is easy to find because you can print any piece on its own.

    Step by step

    1. Create total as price * quantity. It is the cost of all the mugs.
    2. Create line1: the item, then the text " x" (with its leading space), then the quantity. Java's + converts the int to text when the other side is a String.
    3. Create line2: the text "Total: $" followed by total.
    4. Create result from line1, "\n" and line2. The newline character starts a new row where it sits.
    5. Print result with System.out.println.

    The solution in Java

    String item = "Coffee Mug";
    int price = 12;
    int quantity = 3;
    int total = price * quantity;
    String line1 = item + " x" + quantity;
    String line2 = "Total: $" + total;
    String result = line1 + "\n" + line2;
    System.out.println(result);
    

    The newline is a single character written as the two symbols \n inside the quotes. The second row starts after it, so println displays two rows even though it was called once.

    Edge cases and common mistakes

    Operator order with +

    The + operator works from left to right, and it adds numbers or joins text depending on what is on its sides. With the starter values:

    • "Total: $" + price * quantity prints Total: $36, because * is evaluated before +.
    • "Total: $" + price + quantity prints Total: $123: after the text, each + joins, so 12 and 3 are glued side by side.
    • "Total: $" + (price + quantity) prints Total: $15, because the parentheses add first.

    Computing total in its own variable, as the lesson does, avoids these surprises.

    Missing spaces

    The first row is Coffee Mug x3, so the text piece is " x" with a space before the x and none after it. The tests compare the text exactly, so a missing space fails.

    Integer arithmetic

    Because price and quantity are int values, the total is a whole number. This is fine for whole-dollar prices. Prices with cents would need another representation, which this lesson does not need.

    Testing

    Change the three starter values and check both lines. The rows below come from the same code:

    item, price, quantityOutput
    "Notebook", 4, 10Notebook x10 then Total: $40
    "Pen", 1, 1Pen x1 then Total: $1
    "Lamp", 25, 0Lamp x0 then Total: $0

    The quantity appears twice in the program's output, once in the first row and once inside the total. Both come from the same variable, so a single change to quantity updates both rows. A quantity of 0 is accepted without any special handling: the total is 0.

    Key takeaways

    • Compute numbers in int variables, then build the text.
    • "\n" inside a string starts a new row.
    • Put arithmetic in its own variable, or in parentheses, before joining it to text.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free