Skip to content
Katabench
Try free

Tame the Discount Engine

Medium About 25 min Free, no account needed

Untangle a copy-pasted discount calculator until every quality gate passes - without changing a single answer.

The brief

Discount computes a percentage for an order: a base rate per customer tier (bronze 0, silver 5, gold 10), +10 for orders over 1000 (or +5 over 500), +15 with a coupon, +5 during the seasonal sale, capped at 40. Invalid input (non-positive total, unknown tier) returns -1.

It works. Every branch was pasted from the one above it.

Refactor without changing behavior: all tests must stay green AND every code-quality gate must pass (complexity, nesting, method length, duplication). Hint: the tiers differ only by their base rate, a small lookup plus one shared add-ups path replaces all three copies.

Starter code

C#
using System;

public class Solution
{
    // Discount percent for an order. It works - nobody has dared touch it since 2019.
    public int Discount(int total, string tier, bool coupon, bool seasonal)
    {
        if (total <= 0)
        {
            return -1;
        }

        if (tier == "bronze")
        {
            var percent = 0;
            if (total > 500)
            {
                if (total > 1000)
                {
                    percent = percent + 10;
                }
                else
                {
                    percent = percent + 5;
                }
            }
            if (coupon)
            {
                percent = percent + 15;
            }
            if (seasonal)
            {
                percent = percent + 5;
            }
            if (percent > 40)
            {
                percent = 40;
            }
            return percent;
        }
        else if (tier == "silver")
        {
            var percent = 5;
            if (total > 500)
            {
                if (total > 1000)
                {
                    percent = percent + 10;
                }
                else
                {
                    percent = percent + 5;
                }
            }
            if (coupon)
            {
                percent = percent + 15;
            }
            if (seasonal)
            {
                percent = percent + 5;
            }
            if (percent > 40)
            {
                percent = 40;
            }
            return percent;
        }
        else if (tier == "gold")
        {
            var percent = 10;
            if (total > 500)
            {
                if (total > 1000)
                {
                    percent = percent + 10;
                }
                else
                {
                    percent = percent + 5;
                }
            }
            if (coupon)
            {
                percent = percent + 15;
            }
            if (seasonal)
            {
                percent = percent + 5;
            }
            if (percent > 40)
            {
                percent = 40;
            }
            return percent;
        }
        else
        {
            return -1;
        }
    }
}

How it is graded

Behavioral tests have to stay green while the grader measures complexity, nesting, method length, and duplication from your source.

C# refactoring exercises →

Related guides

What it practices

Table-driven logic
Replace branch ladders with data such as a rate table or lookup map.
Remove duplication
Extract the shared shape of pasted blocks and keep the real differences visible.

Related katas