Start learning free

LESSON 190 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in Scala: Tax and Discount with Int 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 Scala "Total: $" + total converts the number to text for you.

Scala'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

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in Scala, explained

    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:

    1. subtotal = price * quantity.
    2. afterDiscount = subtotal - discount. The discount is a fixed amount taken off before tax.
    3. tax = afterDiscount * taxRate / 100, using integer division, so the result is rounded down. The tax is charged on the discounted amount.
    4. 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

    1. subtotal: the price times the quantity, the cost before any discount or tax.
    2. afterDiscount: subtotal - discount. This is the amount the tax will be charged on.
    3. tax: afterDiscount * taxRate / 100. A percentage is "out of 100", so multiply by the rate first and divide by 100 last.
    4. total: afterDiscount + tax, what the customer pays.
    5. line1 to line4: the four rows. Each joins its label, which ends in $, with the matching number: subtotal, discount, tax and total.
    6. result: the four rows with "\n" between each two. Four rows need three newlines.
    7. Print result with println.

    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 / are Int, the result is an Int and 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 / 100 is 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.0 gives 2.48, because one side is a Double. The lesson uses whole dollars, so everything stays in Int.
    • + turns numbers into text when one side is a String, and $ is an ordinary character in a plain string.
    • The unused item. The starter code defines item, but this version's output doesn't show it. Leaving it unused is fine.

    Testing

    price, quantity, taxRate, discountOutput rows
    12, 3, 8, 5Subtotal $36, Discount $5, Tax $2, Total $33
    12, 3, 8, 0Subtotal $36, Discount $0, Tax $2, Total $38
    10, 1, 8, 0Subtotal $10, Discount $0, Tax $0, Total $10
    12, 3, 8, 36Subtotal $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 on subtotal. Using subtotal would 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 * afterDiscount is 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: afterDiscount would 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 val per 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.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free