Code Kata One as Code – Supermarket Pricing

The code kata on supermarket pricing is one we wanted to do because we have some interest in pricing rules. However, it's written as a design exercise (which is a good thing), whereas we still wanted to do some coding.
So :

The Checkout Pricing Kata

Some things in supermarkets have simple prices: this can of beans costs £0.20. Other things have more complex prices. For example:
• three for a £ (so what’s the price if I buy 4, or 5?)
• £1.99/pound (so what does 4 ounces cost?)
• buy two, get one free (so does the third item have a price?)
Here's an example stock list with pricing and rules

Stock Pricing
Baked Beans: 20p per can, with a three-for-two offer.
Bananas: £1 per Kg
Bagged Bananas: £1.20 per bag
Beer: £1.50 per bottle, with a three-for-£4 offer
Bagels: £3 per dozen or £2.00 per half-dozen or 50p each
Kitchen Roll: 50p each
Beer 'n' Beans Cleanup Offer: Buy 3 cans of beans, 3 bottles of beer and get one free kitchen roll (not combinable with any other offer).
Beans 'n' Bagel Breakfast Offer: Get 6 cans of beans and a dozen bagels for £3.50 (not combinable with any other offer).

Checkout Pricing

Write something which, given a list of items purchased, will print out a priced and itemised receipt, with weights shown where relevant and all discount rules correctly applied. The customer should not be able to get a better price by re-organising or splitting up the shopping basket. The receipt should help the uncertain customer to see this.
In TDD style, do it by writing code to pass tests for a list of increasingly complex requirements.

Example shopping baskets

• 1 can of beans, and 1 bottle of beer
• 6 can of beans, and 3 bottles of beer
• 5 cans of beans and 1.4 kg of loose bananas
• 3kg of bananas and 7 bagels
• 4 cans of beans, 3 bottles of beer and a kitchen roll
• 10 cans of beans, 15 bagels, 4 bottles of beer, 2 kitchen rolls, 3.5 kg loose plus 1 bag of bananas.

I leave to the retail stategists amongst you the question, "Should it always be impossible for the customer to get a cheaper price by adding something to their basket?"

Code Kata One as Code – Supermarke…

by Chris F Carroll read it in 2 min
0