The document describes a supermarket checkout kata problem to write code that calculates the total price of items in a shopping basket according to different pricing rules, including:
- A single item has a constant price
- Buying 3 of the same item ("A") gets a 20p discount
- Buying 2 of the same item ("B") gets a 15p discount
- Buying a starter ("A"), main ("B") and dessert ("C" or "D") is priced at 85
- Test cases are provided to test the pricing logic
1. OCP Supemarket Checkout Kata
Using the OCP rules, write a supermarket checkout controller,
using the following test template:
public class CheckoutTester {
private int price(String... items) {
Checkout checkout = new CheckoutFactory().create();
for (String item : items)
checkout.scan(item);
return checkout.total();
}
@Test
public void emptyBasket() {
assertEquals(0, price(""));
}
}
2. OCP Supemarket Checkout Kata
An empty basket has a zero price:
@Test
public void emptyBasket() {
assertEquals(0, price(""));
}
A single item has a constant price:
@Test
public void singleItem() {
assertEquals(50, price("A"));
assertEquals(30, price("B"));
assertEquals(20, price("C"));
assertEquals(15, price("D"));
}
3. Special offers
3 As get a 20p discount:
AA → 100
AAA → 130
AAAA → 180
etc...
2 Bs get a 15p discount:
BB → 45
ABABA → 175
ACABDB → 180
etc...
A starter (A), a main (B) and either pudding (C,D) for 85:
BDA → 85
AADC → 135
AABCA → 180
etc...
4. OCP Supemarket Checkout Kata
The original problem is here:
http://codekata.pragprog.com/2007/01/kata_nine_back_.html