A versatile and user-friendly shopping cart implementation in Python, enabling easy management of items in a virtual shopping cart with features like adding/removing items, applying discounts, and saving/loading cart data.
- Item Management: Add and remove items from your shopping cart
- Item Categories: Support for different item types (Books, Watches, etc.)
- Dynamic Discounts: Apply percentage-based or fixed amount discounts
- Cart Operations: View, clear, save, and load shopping carts
- Checkout Process: Complete purchases with receipt generation
- User-Friendly Interface: Interactive command-line interface with clear prompts
-
Clone this repository:
git clone https://github.com/rehan363/shopping_cart_project.git cd shopping_cart_project -
No external dependencies are required - the project uses only Python's standard library.
Run the main script:
python main.pyFollow the interactive menu to:
- Add items to your cart (books, watches)
- Remove items from the cart
- Apply percentage or fixed amount discounts
- View items in your cart with total price
- Save/load your cart to/from a JSON file
- Clear your cart
- Checkout and generate receipts
main.py: Entry point of the application with the user interfacecart.py: Contains the Cart class for managing shopping cart operationsitems.py: Defines the Items base class and specialized item types (Books, Watches)discounts.py: Implements discount strategies (Percentage, Fixed)
The Cart class manages the collection of items and provides methods for cart operations.
Items: Base class for all item typesBooks: Specialized class for book items with author informationWatches: Specialized class for watch items with brand and model attributes
PercentageDiscount: Calculates discounts based on a percentageFixedDiscount: Applies a fixed amount discount to the total
Cart data can be saved to and loaded from JSON files, allowing users to:
- Save their shopping cart for later use
- Load previously saved carts
- Generate receipts after checkout
# Create a new cart
cart = Cart()
# Add some items
book = Books(name="Python Programming", price=29.99, author="John Smith")
watch = Watches(name="Elegant Watch", price=199.99, brand="Timex", model="Explorer")
cart.add_item(book)
cart.add_item(watch)
# Apply a discount
discount = PercentageDiscount(15) # 15% discount
new_total = cart.apply_discount(discount)
print(f"Total after discount: ${new_total}")
# Save the cart
save_cart(cart, "my_cart.json")Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.