Supermarket Pricing
Level: Advanced 60–90 minConcepts: Business LogicDesign PatternsIncremental DesignEdge Cases
Build a checkout system for a supermarket that handles multiple pricing strategies.
Requirements
Step 1: Simple Pricing
Items have a unit price. Scanning items adds them to the total.
| Item | Unit Price |
|---|---|
| A | $50 |
| B | $30 |
| C | $20 |
| D | $15 |
Scanning A, B, A → total $130.
Step 2: Multi-Buy Discounts
Some items have special pricing when bought in bulk:
| Item | Unit Price | Special |
|---|---|---|
| A | $50 | 3 for $130 |
| B | $30 | 2 for $45 |
| C | $20 | — |
| D | $15 | — |
Examples:
A, A, A→ $130 (not $150)A, A, A, A→ $180 ($130 + $50)B, B, B→ $75 ($45 + $30)A, B, A, B, A→ $175 ($130 + $45) — order doesn’t matter
Step 3: Buy One Get One Free
Add a BOGOF rule: buy one item, get a second of the same item free.
| Item | Rule |
|---|---|
| C | Buy one get one free |
Examples:
C, C→ $20 (second one free)C, C, C→ $40 ($20 + $20 + free)C→ $20 (single item, no discount)
Step 4: Weighted Items
Some items are priced by weight rather than unit:
| Item | Price |
|---|---|
| Bananas | $1.99/kg |
| Apples | $3.49/kg |
Bananas (0.5kg) → $1.00 (rounded)
Step 5: Combo Deals
Buy a combination of items for a special price:
| Combo | Price |
|---|---|
| D + C together | $25 (instead of $35) |
The combo should only apply once per qualifying set.
Test Cases
| Items Scanned | Expected Total |
|---|---|
| (nothing) | $0 |
| A | $50 |
| A, B | $80 |
| A, A, A | $130 |
| A, A, A, A | $180 |
| B, B | $45 |
| C, C | $20 |
| C, C, C | $40 |
| A, B, A, B, A | $175 |
| D, C | $25 (combo) |
| D, C, D | $40 ($25 combo + $15) |
Bonus
- Add percentage discounts (e.g. 10% off item E)
- Add a loyalty card modifier — loyalty members get an additional 5% off the total
- Add receipt printing with itemized lines showing original and discounted prices
- Make pricing rules configurable at runtime (not hardcoded)
Hint
Start with simple unit pricing. Get the checkout working for one item, then multiple items. Only then add the first pricing rule. Each new pricing strategy is a separate concern — don’t try to build a generic rule engine upfront. Let the design emerge from the tests.