Problem overview
Version 2 of the shopping cart adds a flat discount and a sales tax to a single-item purchase, using only integers. The starter code defines item, price (12), quantity (3), taxRate (8, meaning 8%) and discount (5, a flat amount). The program prints a four-line receipt:
Subtotal: $36
Discount: $5
Tax: $2
Total: $33
The rules fix the order of the arithmetic:
subtotal = price * quantity;afterDiscount = subtotal - discount: the discount comes off first;tax = afterDiscount * taxRate / 100: tax is charged on the discounted amount, with integer division rounding it down;total = afterDiscount + tax.
The previous version printed a two-line receipt for the same item. This one adds the discount and the tax.
Approach: calculate everything, then format
Each number depends on the one before it, so the four values are computed in a chain: subtotal, then the amount after the discount, then tax, then total. Only after all four exist does the program build the four text rows and join them.
The order of the operations decides the answer. Charging tax on the subtotal instead of the discounted amount gives a different number. For the starter data, the rule's tax is 2, from 31 * 8 / 100 = 2.48 cut down to 2.
The tax calculation multiplies first and divides second. A percentage is "times the rate, divided by 100", and doing the multiplication first keeps as much precision as integers allow. Integer division then drops the fraction, which rounds the tax down, as the rules ask.
The values for the starter data
| Name | Expression | Value |
|---|---|---|
subtotal | 12 * 3 | 36 |
afterDiscount | 36 - 5 | 31 |
tax | 31 * 8 / 100 | 2 |
total | 31 + 2 | 33 |
The receipt shows the discount and the tax as separate rows, but the total reuses afterDiscount, which already holds the subtracted amount, so nothing is subtracted twice. Naming each intermediate value means every formula in the program is one short expression.
Using whole numbers throughout is a deliberate choice. The program works in whole dollars, and a Double can't store most decimal fractions exactly, so a receipt computed with Double would need rounding rules of its own. With Int the rule is built in: every division drops the fraction. The cost is that amounts below one dollar of tax vanish, which the edge cases below show.
Step by step
- Create
subtotalasprice * quantity, the cost before any discount or tax. - Create
afterDiscountassubtotal - discount. - Create
taxasafterDiscount * taxRate / 100. - Create
totalasafterDiscount + tax, which is what the customer pays. - Create
line1for the Subtotal row: the labelSubtotal: $and the number, using interpolation. - Create
line2for the Discount row in the same way. - Create
line3for the Tax row. - Create
line4for the Total row. - Create
result: the four rows in order, with a newline between each two. Four rows need three newlines. - Print
result.
The solution in Swift
let item = "Coffee Mug"
let price = 12
let quantity = 3
let taxRate = 8
let discount = 5
let subtotal = price * quantity
let afterDiscount = subtotal - discount
let tax = afterDiscount * taxRate / 100
let total = afterDiscount + tax
let line1 = "Subtotal: $\(subtotal)"
let line2 = "Discount: $\(discount)"
let line3 = "Tax: $\(tax)"
let line4 = "Total: $\(total)"
let result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
print(result)
The Swift details worth knowing:
- Integer division truncates toward zero. Both operands of
afterDiscount * taxRate / 100areInt, so the result is anIntwith the fraction dropped. That is rounding down for positive amounts. For negative ones, truncating toward zero rounds up, since -4 * 8 / 100 is 0, not -1. - No automatic mixing of types. Swift doesn't convert between
IntandDoubleon its own. Staying withIntfrom start to finish keeps the code simple and the results whole. - Interpolation and
$."Tax: $\(tax)"contains a plain dollar sign followed by an interpolated value. - Joining with
+. The expressionline1 + "\n" + line2 + "\n" + line3 + "\n" + line4chains string concatenations, all of themString + String, which is why it compiles without conversions. - Unused starter value.
itemis defined but not printed in this version's receipt.
Edge cases and common mistakes
- Tax rounds down. With
discount = 0, the tax is 36 * 8 / 100 = 2, and the total is 38. The fractional part of 2.88 is cut off, not rounded to 3. - Small amounts give zero tax. For a subtotal of 10 with no discount, the tax is 10 * 8 / 100, which is 0, so the total stays 10.
- A discount larger than the subtotal. With
discount = 40, the amount after the discount is -4. The tax is 0, and the receipt showsTotal: $-4. The lesson's rules don't clamp the value at zero. - A zero tax rate or zero discount. Both are ordinary values: with
taxRate = 0the tax row shows$0, and the total equals the amount after the discount. No special case is needed. - Taxing the wrong amount. The tax uses
afterDiscount, notsubtotal. - Dividing first.
afterDiscount / 100 * taxRatetruncates the amount before multiplying and loses most of the tax: for 31 it gives 0 instead of 2. - Missing newlines. Four rows need three
"\n"separators. Forgetting one runs two rows together.
Testing
- The starter data prints
Subtotal: $36,Discount: $5,Tax: $2andTotal: $33. - With
discount = 0, it printsSubtotal: $36,Discount: $0,Tax: $2andTotal: $38. - With
discount = 40, it printsSubtotal: $36,Discount: $40,Tax: $0andTotal: $-4.
