Start learning free

LESSON 187 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in Java: Tax and Discount with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

String item = "Coffee Mug";
int price = 12;
int quantity = 3;
int tax_rate = 8;
int discount = 5;

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • after_discount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = after_discount * tax_rate / 100 (integer division, so the result is rounded down). The tax is charged on the discounted amount.
  • total = after_discount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with System.out.println(result).

Compute the four numbers (subtotal, after_discount, tax, total) before formatting.

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

Java's int / int truncates, so after_discount * tax_rate / 100 already rounds down.

String item = "Coffee Mug";
int price = 12;
int quantity = 3;
int tax_rate = 8;
int discount = 5;

int subtotal = price * quantity;
int after_discount = subtotal - discount;
int tax = after_discount * tax_rate / 100;
int total = after_discount + tax;
String line1 = "Subtotal: $" + subtotal;
String line2 = "Discount: $" + discount;
String line3 = "Tax: $" + tax;
String line4 = "Total: $" + total;
String result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
System.out.println(result);

STEP 1 OF 10
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in Java, explained

    Problem overview

    The receipt now includes a discount and sales tax for a single item. The starter code gives item, price (12), quantity (3), tax_rate (8, meaning 8%) and discount (5, a flat amount in dollars). The program prints four lines:

    Subtotal: $36
    Discount: $5
    Tax: $2
    Total: $33

    The rules, in order: subtotal = price * quantity; after_discount = subtotal - discount; tax = after_discount * tax_rate / 100, which is integer division and rounds down; and total = after_discount + tax. The discount comes off before tax, so the tax is charged on the discounted amount. The four lines are joined with "\n" into result and printed with one System.out.println(result).

    The names tax_rate and after_discount use underscores. Java style normally prefers camelCase, but underscores are legal in variable names, and the lesson's tests use these exact names.

    Approach: compute all four numbers, then format

    Each number depends on the one before it, so the order of the lines of code follows the receipt: subtotal, then the discounted amount, then tax, then total. Only when all four numbers exist does the program turn them into text. Doing the arithmetic first keeps the formatting lines trivial: each is a label followed by a variable.

    For the starter values the calculation goes like this: the subtotal is 12 * 3 = 36, after the discount it is 36 - 5 = 31, the tax is 31 * 8 / 100 = 248 / 100 = 2 (the fraction .48 is dropped), and the total is 31 + 2 = 33.

    Step by step

    1. Create subtotal as price * quantity.
    2. Create after_discount as subtotal - discount.
    3. Create tax: after_discount times tax_rate, divided by 100. Multiply first, then divide, so the percentage is applied before any rounding.
    4. Create total as after_discount + tax. This is what the customer pays.
    5. Build line1 to line4, one per receipt row: a label with its dollar sign, then the number. Java converts the int to text when it joins it to a String.
    6. Join the rows into result with a "\n" between each two. Four rows need three newlines.
    7. Print result.

    The solution in Java

    String item = "Coffee Mug";
    int price = 12;
    int quantity = 3;
    int tax_rate = 8;
    int discount = 5;
    int subtotal = price * quantity;
    int after_discount = subtotal - discount;
    int tax = after_discount * tax_rate / 100;
    int total = after_discount + tax;
    String line1 = "Subtotal: $" + subtotal;
    String line2 = "Discount: $" + discount;
    String line3 = "Tax: $" + tax;
    String line4 = "Total: $" + total;
    String result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
    System.out.println(result);
    

    With two int operands, / performs integer division: it drops the fractional part, so the tax is always a whole number of dollars. The expression after_discount * tax_rate / 100 is evaluated left to right, so the multiplication happens first.

    Edge cases and common mistakes

    Where the division goes

    Dividing too early destroys the result. With after_discount = 31 and tax_rate = 8:

    • after_discount * tax_rate / 100 gives 2;
    • tax_rate * (after_discount / 100) gives 0, because 31 / 100 is already 0;
    • after_discount * (tax_rate / 100) also gives 0, because 8 / 100 is 0.

    Tax before the discount

    The rules charge tax on the discounted amount. Computing the tax from subtotal gives 36 * 8 / 100 = 2 for the starter values, which looks the same by coincidence. For other numbers it differs: with a price of 20, a quantity of 5, a tax rate of 10 and a discount of 30, the subtotal is 100 and the amount after the discount is 70. The correct tax is 7 and the total is 77, while a tax taken from the subtotal would be 10. Follow the rule rather than checking only the example.

    Rounding down

    The tax is cut, not rounded to the nearest dollar. In the example, the exact tax is 2.48 and the receipt shows 2. Because the total is computed from the already-rounded tax, the receipt adds up: 31 plus 2 is 33. Each line is a whole number, so the four lines are always consistent with each other.

    Different inputs

    Changing the starter values shows how each line reacts. The outputs below are from the same code:

    price, quantity, tax_rate, discountReceipt
    12, 3, 8, 0Subtotal $36, Discount $0, Tax $2, Total $38
    10, 1, 8, 5Subtotal $10, Discount $5, Tax $0, Total $5
    5, 1, 8, 10Subtotal $5, Discount $10, Tax $0, Total $-5

    The last row shows that nothing in the code stops a discount from being larger than the subtotal: the total becomes negative. The lesson's rules do not cover that case, so the program leaves it alone.

    Treating the discount as a percentage

    Here discount is a flat number of dollars that is subtracted, not a percentage. Multiplying by it, as you would with tax_rate, would give a very different receipt. The two inputs look alike, both small integers, but the code treats them differently: the rate is multiplied and divided by 100, while the discount is only subtracted.

    Whole dollars only

    All the values are int, so the receipt has no cents. That is why the tax rounds down to whole dollars, and why the lesson spells that rule out.

    Joining the rows

    Each "\n" goes between two rows, not after the last one, because println already ends the final line.

    Key takeaways

    • Compute values in the order the receipt depends on them: subtotal, discount, tax, total.
    • Multiply by the rate first and divide by 100 last: integer division rounds down at the end.
    • Do the arithmetic first, then turn the results into text.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free