-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
feat(other): add grocery store cart model (v2) #14547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| Console-free grocery cart logic. | ||
| """ | ||
|
|
||
|
|
||
| class GroceryStoreCart: | ||
| """ | ||
| Maintain cart item quantities and compute totals. | ||
|
|
||
| >>> cart = GroceryStoreCart({"apple": 1.5, "milk": 2.0}) | ||
| >>> cart.add_item("apple", 2) | ||
| >>> cart.add_item("milk") | ||
| >>> round(cart.total_price(), 2) | ||
| 5.0 | ||
| >>> cart.remove_item("apple") | ||
| >>> round(cart.total_price(), 2) | ||
| 3.5 | ||
| """ | ||
|
|
||
| def __init__(self, price_catalog: dict[str, float]) -> None: | ||
| if not price_catalog: | ||
| raise ValueError("price_catalog cannot be empty") | ||
| self.price_catalog = dict(price_catalog) | ||
| self.quantities: dict[str, int] = {} | ||
|
|
||
| def add_item(self, item: str, quantity: int = 1) -> None: | ||
| if item not in self.price_catalog: | ||
| raise KeyError(f"{item!r} is not in the catalog") | ||
|
Check failure on line 28 in other/grocery_store_cart.py
|
||
| if quantity <= 0: | ||
| raise ValueError("quantity must be positive") | ||
| self.quantities[item] = self.quantities.get(item, 0) + quantity | ||
|
|
||
| def remove_item(self, item: str, quantity: int = 1) -> None: | ||
| if quantity <= 0: | ||
| raise ValueError("quantity must be positive") | ||
| current = self.quantities.get(item, 0) | ||
| if current == 0: | ||
| raise KeyError(f"{item!r} is not present in the cart") | ||
|
Check failure on line 38 in other/grocery_store_cart.py
|
||
| remaining = current - quantity | ||
| if remaining > 0: | ||
| self.quantities[item] = remaining | ||
| else: | ||
| self.quantities.pop(item, None) | ||
|
Comment on lines
+39
to
+43
|
||
|
|
||
| def total_price(self) -> float: | ||
| return sum(self.price_catalog[item] * qty for item, qty in self.quantities.items()) | ||
|
Check failure on line 46 in other/grocery_store_cart.py
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doctest examples only cover the happy path. CONTRIBUTING.md asks for doctests that cover erroneous input values too; please add doctests for cases like empty
price_catalog, unknown item inadd_item(), and non-positive quantities to ensure exception behavior is tested and stays stable.