Problem overview
The receipt from the first version gets a flat discount and a sales tax. The program starts with the item, the price of one unit, the quantity, a taxRate in percent and a discount in dollars. It prints four lines: Subtotal, Discount, Tax and Total.
The rules, in order:
subtotal = price * quantity.afterDiscount = subtotal - discount. The discount is a fixed amount taken off before tax.tax = afterDiscount * taxRate / 100, using integer division, so the result is rounded down. The tax is charged on the discounted amount.total = afterDiscount + tax.
With a price of 12, a quantity of 3, a tax rate of 8 and a discount of 5, the subtotal is 36, and 36 - 5 gives 31. The tax is 31 * 8 / 100 = 248 / 100, which rounds down to 2, so the total is 31 + 2 = 33:
Subtotal: $36
Discount: $5
Tax: $2
Total: $33
Approach: one named value per line of the calculation
The calculation is a chain: each number depends on the one before it. Writing it as one val per number, in the order of the rules, keeps every step readable and means each printed row can reuse a value that is already computed. Then the four receipt rows are built, and the rows are joined with newlines into result.
The order of the operations is the main thing to get right. The discount comes off first, and the tax is computed on what is left. Taxing the full subtotal and subtracting the discount afterwards gives a different number, as the testing section shows.
Step by step
subtotal: the price times the quantity, the cost before any discount or tax.afterDiscount:subtotal - discount. This is the amount the tax will be charged on.tax:afterDiscount * taxRate / 100. A percentage is "out of 100", so multiply by the rate first and divide by 100 last.total:afterDiscount + tax, what the customer pays.line1toline4: the four rows. Each joins its label, which ends in$, with the matching number:subtotal,discount,taxandtotal.result: the four rows with"\n"between each two. Four rows need three newlines.- Print
resultwithprintln.
The solution in Scala
val item = "Coffee Mug"
val price = 12
val quantity = 3
val taxRate = 8
val discount = 5
val subtotal = price * quantity
val afterDiscount = subtotal - discount
val tax = afterDiscount * taxRate / 100
val total = afterDiscount + tax
val line1 = "Subtotal: $" + subtotal
val line2 = "Discount: $" + discount
val line3 = "Tax: $" + tax
val line4 = "Total: $" + total
val result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
println(result)
Scala details worth knowing:
- Integer division. When both sides of
/areInt, the result is anIntand the fraction is cut off. For these amounts 248 / 100 is 2, never 2.48. It truncates toward zero, which for positive amounts means rounding down. - Multiply before dividing.
afterDiscount * taxRate / 100is evaluated left to right, so the product 248 is divided once. Dividing first (taxRate / 100) would give 0, because 8 / 100 is 0 in integers, and the tax would always be zero. - Doubles behave differently. The expression
(36 - 5) * 8 / 100.0gives2.48, because one side is aDouble. The lesson uses whole dollars, so everything stays inInt. +turns numbers into text when one side is aString, and$is an ordinary character in a plain string.- The unused
item. The starter code definesitem, but this version's output doesn't show it. Leaving it unused is fine.
Testing
| price, quantity, taxRate, discount | Output rows |
|---|---|
| 12, 3, 8, 5 | Subtotal $36, Discount $5, Tax $2, Total $33 |
| 12, 3, 8, 0 | Subtotal $36, Discount $0, Tax $2, Total $38 |
| 10, 1, 8, 0 | Subtotal $10, Discount $0, Tax $0, Total $10 |
| 12, 3, 8, 36 | Subtotal $36, Discount $36, Tax $0, Total $0 |
The third row shows the round-down: 10 * 8 / 100 is 80 / 100, which is 0, so there is no tax at all on a $10 purchase. The last row shows a discount that covers the whole subtotal.
To see why the order matters, take the starter data. Taxing the discounted amount gives (36 - 5) * 8 / 100, which is 2. Taxing the full subtotal and then subtracting the discount, (36 * 8 / 100) - 5, gives -3, which is meaningless as a tax.
Edge cases and common mistakes
- Taxing the wrong amount. The tax is charged on
afterDiscount, not onsubtotal. Usingsubtotalwould print a Tax of 2 in this example too, but the rounding differs for other data, so the tests would catch it. - Dividing before multiplying.
taxRate / 100 * afterDiscountis always 0 for rates below 100. - Counting the discount twice. The total is
afterDiscount + tax. Subtracting the discount again from that would take it off a second time. - Missing newlines. Four rows need three
"\n"separators. With one missing, two rows run together on one line. - A discount larger than the subtotal. This version doesn't guard against it:
afterDiscountwould be negative, and the total would follow. - Expecting cents. Every amount is a whole number of dollars, so the tax is rounded down to a whole dollar.
Key takeaways
- Compute a chain of values with one
valper step, in the order the rules give. - Integer
/drops the fraction, so multiply by the percentage first and divide by 100 last. - The discount comes off before the tax is calculated.
- The next version builds on this one.
