Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Design an ECommerce Order System with Cart Items and Payments

Model an e-commerce order using composition: products, cart items, cart aggregation, order calculation, and simple payment processing.

Python practice30 minComposition & Real-World ModelingAdvancedLast updated April 11, 2026

Problem statement

You will design a small e-commerce order system that uses composition to combine Product, CartItem, Cart, Order, and PaymentProcessor classes. The system must: - Represent items with a Product and quantity using CartItem. - Aggregate CartItems in a Cart and compute subtotal, apply discounts, and tax to produce a final amount in cents. - Apply a 10% discount on the subtotal when subtotal exceeds 10000 cents ($100). - Apply tax at 8% to the discounted subtotal. - Use a PaymentProcessor to simulate payments. Supported payment methods are 'card' and 'paypal'. Payment succeeds only when the final amount is greater than zero and the method is supported. - Expose a function process_order(items, payment_method) where items is a list of tuples (name, price_cents, quantity). The function builds the objects using composition and returns a tuple (paid: bool, total_cents: int). Implement missing parts in the starter code to make process_order behave as described. Handle edge cases reasonably: negative quantities should be treated as zero, empty carts result in no payment (paid=False, total=0).

Task

Implement a composed system to calculate order totals (with discounts and tax) and simulate payment processing using multiple collaborating classes.

Examples

Single item, no discount

Input

process_order([('Hat', 2500, 2)], 'card')

Output

(True, 5400)

Subtotal = 2500*2 = 5000. No discount. Tax = 8% of 5000 = 400. Total = 5400. Payment method 'card' is supported and amount > 0, so paid=True.

Input format

A call to process_order(items, payment_method) where items is a Python list of tuples (name: str, price_cents: int, quantity: int), and payment_method is a string ('card' or 'paypal').

Output format

Return a tuple (paid: bool, total_cents: int). The test harness will compare str(return_value) to the expected string.

Constraints

- Prices and quantities are integers (cents for price). Negative quantities should be treated as zero. - Discount threshold: 10000 cents. Discount rate: 10%. - Tax rate: 8%. - Supported payment methods: 'card', 'paypal'. - Use composition (Cart composed of CartItem objects that reference Product objects).

Samples

Sample 1

Input

process_order([('Shoes', 7000, 1), ('Socks', 500, 2)], 'paypal')

Output

(True, 8640)

Subtotal = 7000 + (500*2)=8000. No discount. Tax = 640. Total = 8640. Payment succeeds with 'paypal'.