Created by: Aaron Strickland
Date: 16/08/2024
- Project Overview
- Architecture
- Core Components
- Discount System
- Edge Cases
- Logging and Debugging
- Future Extensions
This project models a shopping cart system that allows multiple products to be added to a basket, with support for various discounts. The system is designed for flexibility, maintainability, and extensibility, supporting product categories, applying discounts, and logging actions.
The project is modular and object-oriented, with core components such as Basket
, Product
, Category
, and Discount
. The system is easily extendable to add new discount types.
- Design Pattern: Simplified MVVM, where
Basket
manages state and discounts handle business logic.
- Manages items in the basket: Tracks products and calculates totals.
- Key Methods:
addProduct
: Adds or updates product quantities.calculateTotal
: Returns the total price of items.applyDiscounts
: Applies discounts and returns the final total.
- Represents a product: Includes properties like
id
,name
,price
, andcategory
.
- Organises products: Helps manage and potentially apply category-based discounts.
- Defines discount logic: The
Discount
protocol requires anapply
method to modify basket totals.
- GeneralBasketDiscount: Applies a percentage discount to the entire basket.
- ProductSpecificDiscount: Applies a discount to specific products.
- BuyXGetYFree: Offers promotions like "Buy 2, Get 1 Free."
- CombinationDealDiscount: Applies special pricing for specific product combinations.
- Single Discount Per Product: Tracks applied discounts to prevent overlapping.
- Sequential Application: Discounts are applied in order, prioritising those that reduce the total.
- Zero-Price Products: Skipped in discount calculations.
- Negative Prices: Validation recommended to prevent issues.
- Conflicting Discounts: Only one discount per product is applied.
- Empty Basket: Early exit in
applyDiscounts
to avoid processing. - Invalid Product IDs: Should be handled gracefully.
- Logs key actions: Tracks product additions and discount applications.
- Debugging Tips:
- Adjust log levels for more detailed information during development.
- Review error logs to quickly identify issues.
- Category-Based Discounts: Apply discounts to entire categories.
- Persistent Basket: Save and restore basket state across sessions.
- Multi-Currency Support: Handle different currencies and exchange rates.
- Discount Expiry: Implement expiry dates for discounts.
- Enhanced Logging: Support for logging to files or external services.