Start learning free

LESSON 190 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in Kotlin: Discount and Tax with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

        val item = "Coffee Mug"
        val price = 12
        val quantity = 3
        val taxRate = 8
        val discount = 5

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • afterDiscount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = afterDiscount * taxRate / 100 (integer division, so the result is rounded down). The tax is charged on the discounted amount.
  • total = afterDiscount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with println(result).

Compute the four numbers (subtotal, afterDiscount, tax, total) before formatting.

In Kotlin "Total: $" + total converts the number to text for you.

Kotlin's Int / Int truncates, so afterDiscount * taxRate / 100 already rounds down.

        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)

STEP 1 OF 10
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in Kotlin, explained

    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 * quantity
    • afterDiscount = subtotal - discount: the discount comes off before tax
    • tax = afterDiscount * taxRate / 100: integer division, so the result is rounded down, and the tax is charged on the discounted amount
    • total = 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

    1. Create subtotal as price * quantity.
    2. Create afterDiscount as subtotal - discount. The tax will be charged on this amount.
    3. Create tax as afterDiscount * taxRate / 100. Multiply by the rate first and divide by 100 second, because a percentage is "out of 100".
    4. Create total as afterDiscount + tax: what the customer pays.
    5. Build the four rows, line1 to line4. Each is a label ending in $ joined to its number.
    6. Join the rows into result, with a newline between each two. Four rows need three newlines.
    7. Print result with println.

    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 Int by an Int gives an Int and 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 by 100.0 instead would give a Double, such as 2.48.
    • Multiply before dividing. afterDiscount * (taxRate / 100) evaluates 8 / 100 first, which is 0 in integer math, so the tax would always be 0. Keep afterDiscount * taxRate / 100, which Kotlin evaluates from left to right.
    • Left to right with text. In "Subtotal: $" + subtotal the left side is a String, so the Int becomes text. Starting a join with a number, as in subtotal + " 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. With discount = 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 = 0 the receipt shows Discount: $0 and 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, discountoutput
    12, 3, 8, 5Subtotal $36, Discount $5, Tax $2, Total $33
    12, 3, 8, 0Subtotal $36, Discount $0, Tax $2, Total $38
    3, 1, 8, 1Subtotal $3, Discount $1, Tax $0, Total $2
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free