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
- Create
subtotalasprice * quantity. - Create
after_discountassubtotal - discount. - Create
tax:after_discounttimestax_rate, divided by 100. Multiply first, then divide, so the percentage is applied before any rounding. - Create
totalasafter_discount + tax. This is what the customer pays. - Build
line1toline4, one per receipt row: a label with its dollar sign, then the number. Java converts theintto text when it joins it to aString. - Join the rows into
resultwith a"\n"between each two. Four rows need three newlines. - 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 / 100gives 2;tax_rate * (after_discount / 100)gives 0, because31 / 100is already 0;after_discount * (tax_rate / 100)also gives 0, because8 / 100is 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, discount | Receipt |
|---|---|
| 12, 3, 8, 0 | Subtotal $36, Discount $0, Tax $2, Total $38 |
| 10, 1, 8, 5 | Subtotal $10, Discount $5, Tax $0, Total $5 |
| 5, 1, 8, 10 | Subtotal $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.
