Problem overview
This version of the receipt adds a flat discount and a percentage sales tax to a one-item purchase. The starter has item, price, quantity, taxRate := 8 (a percentage) and discount := 5 (a flat amount). The program prints four lines, Subtotal, Discount, Tax and Total:
Subtotal: $36
Discount: $5
Tax: $2
Total: $33
The rules, in order: the subtotal is the price times the quantity; the discount is taken off the subtotal first; the tax is charged on the discounted amount; the total is the discounted amount plus the tax. All values are integers, so the tax is rounded down.
Approach: one named value per rule
Each rule becomes a variable: subtotal, afterDiscount, tax and total. Then each output row becomes a string variable, line1 to line4, and the rows are joined with newlines into result.
The order of the arithmetic matters. The discount comes off before the tax, so the tax is computed on afterDiscount, not on subtotal. With the starting data that is 31, and 31 * 8 / 100 is 2 after the fraction is cut off.
The percentage uses integer math. afterDiscount * taxRate / 100 multiplies first and divides by 100 last. Dividing two int values in Go yields an int, which drops the fraction, so no floating-point numbers are needed.
It helps to read the starting data through the chain: 12 * 3 is 36, minus 5 is 31, 31 * 8 / 100 is 2, and 31 + 2 is 33. Each result feeds the next line, so the variables must be created in this order.
The item name is never printed in this version. That is why the starter data keeps it alive with a blank assignment.
Step by step
subtotalisprice * quantity: the cost before the discount and the tax.afterDiscountissubtotal - discount.taxisafterDiscount * taxRate / 100.totalisafterDiscount + tax: the amount the customer pays.line1is"Subtotal: $"followed bystrconv.Itoa(subtotal).line2is the Discount row, built the same way fromdiscount.line3is the Tax row, fromtax.line4is the Total row, fromtotal.resultjoins the four rows with"\n"between each two. Four rows need three newlines.- Print
resultwithfmt.Println.
The solution in Go
item := "Coffee Mug"
price := 12
quantity := 3
taxRate := 8
discount := 5
_ = item
subtotal := price * quantity
afterDiscount := subtotal - discount
tax := afterDiscount * taxRate / 100
total := afterDiscount + tax
line1 := "Subtotal: $" + strconv.Itoa(subtotal)
line2 := "Discount: $" + strconv.Itoa(discount)
line3 := "Tax: $" + strconv.Itoa(tax)
line4 := "Total: $" + strconv.Itoa(total)
result := line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
fmt.Println(result)
The code uses the fmt and strconv packages.
The details worth knowing
_ = item. This version never prints the item name, and Go refuses a variable that is declared and never used. Assigning it to the blank identifier_counts as a use and discards the value.- Integer division truncates toward zero.
31 * 8 / 100is248 / 100, which is 2. The 0.48 is dropped, not rounded to the nearest. - Multiply, then divide. Writing
afterDiscount / 100 * taxRatewould divide first and lose the remainder, so for 31 the tax would be 0. - Converting each number. Every
+joins strings, so each of the four numbers goes throughstrconv.Itoa."Tax: $" + taxwould not compile. - Checking the division. With the starting data,
afterDiscount * taxRate / 100evaluates to 2, whileafterDiscount / 100 * taxRateevaluates to 0: the second form divides 31 by 100 first, which is 0. - Naming the steps. The lesson spells the rules out as separate variables on purpose: each line of code maps to one rule in the statement, which makes it easy to check.
An alternative: one formatted call
Once the four numbers exist, fmt.Printf("Subtotal: $%d\nDiscount: $%d\nTax: $%d\nTotal: $%d\n", subtotal, discount, tax, total) prints the same four rows without any strconv.Itoa call or +. Each %d takes the next argument, in order. The lesson builds the rows as strings instead, so every row is its own variable and the joining with "\n" is explicit.
Complexity
The program runs a fixed number of arithmetic operations and string joins, whatever the values are. Time and extra space are O(1) apart from the length of the output text, which is a few dozen characters.
Edge cases and common mistakes
- Taxing the wrong amount. The tax is charged on the discounted amount. With
price := 10,quantity := 5,discount := 10andtaxRate := 10, the correct output isSubtotal: $50,Discount: $10,Tax: $4andTotal: $44. Usingsubtotalin the tax line would charge tax on the amount before the discount, which the rules do not allow. - No discount. With
discount := 0the output isSubtotal: $36,Discount: $0,Tax: $2,Total: $38. - Tax rounding down. With
price := 10,quantity := 1anddiscount := 0, the tax is$0and the total is$10: 10 * 8 / 100 is 0.8, cut to 0. - No tax. With
taxRate := 0the tax row shows$0and the total is 31. - A discount bigger than the subtotal. The code does not guard against it. With
quantity := 1anddiscount := 15, the output ends withTotal: $-3. When the discount equals the subtotal, the total is 0. The lesson's rules do not ask for a guard. - The wrong number of newlines. Joining with fewer than three newlines puts two rows on one line.
Testing
- The starting data prints
Subtotal: $36,Discount: $5,Tax: $2andTotal: $33. discount := 0printsSubtotal: $36,Discount: $0,Tax: $2andTotal: $38.taxRate := 0printsSubtotal: $36,Discount: $5,Tax: $0andTotal: $31.quantity := 1withdiscount := 12printsSubtotal: $12,Discount: $12,Tax: $0andTotal: $0.
