Problem No 2
Create a ShoppingCart with Product Items
Easy≈ 8 minute session
Lesson guide
What this Python exercise practices
Create a ShoppingCart with Product Items is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Practice path
Related public exercises
Summary
Learn composition by building Product and ShoppingCart classes to manage items and compute totals.
Problem statement
You will model a simple shopping cart using composition. Implement two classes: - Product: represents a product with a name and a price (stored in cents as an integer). - ShoppingCart: holds Product instances and quantities. The cart should allow adding products with quantities, removing a quantity of a product (not letting counts go negative), querying how many units of a product are in the cart, and computing the total price in cents. Design choices: - Use composition: ShoppingCart stores Product objects (not subclassing Product). - Price handling: work in integer cents to avoid floating-point rounding issues. Provide total_cents() returning an integer total. Required methods (names must match exactly): - Product(name: str, price_cents: int) - ShoppingCart() - add_item(product: Product, quantity: int = 1) -> None - remove_item(product_name: str, quantity: int = 1) -> bool - removes up to quantity of the named product; returns True if any were removed, False if product not present - item_count(product_name: str) -> int - total_cents() -> int Follow standard behaviors: adding increases count, removing reduces but not below zero, querying unknown product returns 0.
Task
Implement Product and ShoppingCart classes where ShoppingCart stores Product instances (composition). Provide methods to add/remove items, query item counts, and compute total price in cents.
Examples
Add two apples (150 cents each) and get total
Input
cart = ShoppingCart(); p = Product('Apple', 150); cart.add_item(p, 2); cart.total_cents()
Output
300
Explanation
Two apples at 150 cents each makes 300 cents total.
Input format
Each test uses an expression that constructs Product and ShoppingCart objects and then calls one of the required methods. Your classes must be available in the global scope.
Output format
Return values are compared by their string representation. total_cents() and item_count() should return integers.
Constraints
- Prices are non-negative integers (cents). - Quantities passed to add_item/remove_item are non-negative integers. - remove_item should not reduce a product's count below zero.
Samples
Sample input 0
cart = ShoppingCart(); cart.add_item(Product('Banana', 99), 3); cart.total_cents()
Sample output 0
297
Explanation 0
Three bananas at 99 cents each total 297 cents.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.