Problem overview
Version 2 of the Shopping Cart extends the receipt with a flat discount and a sales tax, still for a single item. The starter code gives item = "Coffee Mug", price = 12, quantity = 3, taxRate = 8 (a percentage) and discount = 5 (a flat amount).
The rules are:
subtotal = price * quantityafterDiscount = subtotal - discount: the discount comes off before taxtax = afterDiscount * taxRate / 100: integer division, so the result is rounded down, and the tax is charged on the discounted amounttotal = afterDiscount + tax
The program prints four lines joined by newlines:
Subtotal: $36
Discount: $5
Tax: $2
Total: $33
Check the numbers: 36 minus 5 is 31, 31 times 8 divided by 100 is 2 after dropping the fraction, and 31 plus 2 is 33.
Approach
As in version 1, the program separates calculation from formatting. It first computes every number the receipt needs, from the subtotal to the total, each in its own val. Then it builds one row per number and joins the rows into result.
Order matters in the calculation, because each value depends on the one before it: the discount needs the subtotal, the tax needs the discounted amount, and the total needs both the discounted amount and the tax. Writing them in that order means every value is ready when it is used.
Money is kept as whole numbers here, so the tax uses integer division rather than decimals. That avoids fractions altogether and fixes the rounding rule: always down.
Step by step
- Create
subtotalasprice * quantity. - Create
afterDiscountassubtotal - discount. The tax will be charged on this amount. - Create
taxasafterDiscount * taxRate / 100. Multiply by the rate first and divide by 100 second, because a percentage is "out of 100". - Create
totalasafterDiscount + tax: what the customer pays. - Build the four rows,
line1toline4. Each is a label ending in$joined to its number. - Join the rows into
result, with a newline between each two. Four rows need three newlines. - Print
resultwithprintln.
The solution in Kotlin
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)
Kotlin details worth knowing:
- Integer division truncates. Dividing an
Intby anIntgives anIntand discards the fraction. With the lesson's numbers, 31 * 8 is 248, and 248 / 100 is 2, not 2.48. With a subtotal of 3, a discount of 1 and a rate of 8, the tax is 0, because 2 * 8 / 100 is 0 after truncation, and the total is 2. Dividing by100.0instead would give aDouble, such as2.48. - Multiply before dividing.
afterDiscount * (taxRate / 100)evaluates8 / 100first, which is 0 in integer math, so the tax would always be 0. KeepafterDiscount * taxRate / 100, which Kotlin evaluates from left to right. - Left to right with text. In
"Subtotal: $" + subtotalthe left side is aString, so theIntbecomes text. Starting a join with a number, as insubtotal + " dollars", doesn't compile. - The dollar sign. A
$before a closing quote is an ordinary character. Before a name it would start a string template, so"Tax: $tax"would print the tax without a dollar sign, and"Tax: \$$tax"prints it with one. - Immutable values. Every variable is a
val. Each is assigned once, in the order its inputs become available.
What each variable holds
On the lesson's data, subtotal is 36, afterDiscount is 31, tax is 2 and total is 33. The four row variables then hold Subtotal: $36, Discount: $5, Tax: $2 and Total: $33, and result is those four strings with a newline between each pair. Because println prints the newline characters as line breaks, the receipt appears on four lines. The names line1 to line4 match the order of the rows, which makes it easy to spot a row that is out of place.
Edge cases and common mistakes
- Tax on the wrong amount. The tax is charged on
afterDiscount. Withdiscount = 20, the amount after the discount is 16, the tax is 1 and the total is 17. Charging tax on the subtotal, 36, would give a tax of 2. - Rounding. Tax is rounded down, never to the nearest value. A tax that works out to 2.88 is 2, which is why the rule says to use integer division.
- Zero discount. With
discount = 0the receipt showsDiscount: $0and the tax is 36 * 8 / 100, which is 2. The total is 38. - A discount larger than the subtotal. The amount after the discount would go negative. The lesson's data doesn't test this, and nothing in the rules clamps it at zero.
- Wrong newline count. Four rows need three
"\n"separators. An extra one leaves a blank line. - Row order. The rows must appear as Subtotal, Discount, Tax, Total.
Testing
Change the starter values in the editor above:
| price, quantity, taxRate, discount | output |
|---|---|
| 12, 3, 8, 5 | Subtotal $36, Discount $5, Tax $2, Total $33 |
| 12, 3, 8, 0 | Subtotal $36, Discount $0, Tax $2, Total $38 |
| 3, 1, 8, 1 | Subtotal $3, Discount $1, Tax $0, Total $2 |
