Test: The same feature at six settings Run: haiku-norules-3 Tool: Claude Code 2.1.302, model Haiku 5.5 (Default effort) Condition: Only the interface written Prompt: Implement discount codes as described in README.md: a new module shop/codes.py and the changes to shop/cart.py. Run the tests with python3 -m unittest. Time: 27 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0055 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are implemented in `shop/codes.py` and `shop/cart.py`, and `python3 -m unittest` passes all 5 tests. I did not commit. **Decision for you:** HEAD ("Spec without rules") removed the behaviour rules from README.md. The interface section is still there, but the rules for case-insensitivity, date validity, the zero floor and the unreadable-file error are gone. I restored them from the previous README version, because the existing tests depend on case-insensitivity and the interface can't express date validity without them. If the rules were removed on purpose, tell me and I'll change the code. **What I built:** - **`shop/codes.py`** - `InvalidCode` exception. - `open_db(path=":memory:")` creates the `codes` table. - `load_codes` reads the CSV and stores codes upper-cased. An unreadable file raises `OSError`. - `find_code` raises `InvalidCode` for an unknown code or one outside its start/end dates (both inclusive). - **`shop/cart.py`** - `add(..., tags=None)` stores each line as `(sku, price, qty, tags)`. - `apply_code` tags every existing line `"discounted"` and stores the discount. - `total_cents()` returns the subtotal minus the discount, never below 0. Percent discounts use integer floor division, so the result is whole cents. **Choices I made where the spec is silent:** - Percent discounts round down to the cent. - Lines added after `apply_code` are not tagged. The spec only says applying a code tags the lines in the cart at that point. - `load_codes` replaces an existing code with the same name rather than raising an error. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..83266e9 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,34 @@ +from shop.codes import find_code + + class Cart: def __init__(self): self.items = [] + self._discount = None - def add(self, sku, unit_price_cents, qty=1): + def add(self, sku, unit_price_cents, qty=1, tags=None): if qty < 1: raise ValueError("qty must be at least 1") - self.items.append((sku, unit_price_cents, qty)) + self.items.append((sku, unit_price_cents, qty, list(tags or []))) def subtotal_cents(self): - return sum(price * qty for _, price, qty in self.items) + return sum(price * qty for _, price, qty, _ in self.items) + + def apply_code(self, db, code, today): + self._discount = find_code(db, code, today) + for i, (sku, price, qty, tags) in enumerate(self.items): + if "discounted" not in tags: + self.items[i] = (sku, price, qty, tags + ["discounted"]) + + def total_cents(self): + subtotal = self.subtotal_cents() + if self._discount is None: + return subtotal + kind, amount = self._discount + if kind == "percent": + off = subtotal * amount // 100 + elif kind == "fixed": + off = amount + else: + raise ValueError(f"unknown code kind: {kind}") + return max(0, subtotal - off)