Exp 16
Exp 16
Date of Performance:
Date of Submission:
PROBLEM DEFINITION:
Online Shopping System: Develop classes for products, customers, and shopping carts.
Include methods for adding items to the cart, calculating total costs, processing orders and
managing inventory.
ALGORITHM:
1. Input: A list of products with their ID, name, price, and stock quantity.
2. Process:
● For each product:
● Store product details in the inventory.
1. Output: Inventory is populated with the available products.
1. Input: Customer’s cart, product ID, and quantity of products to add.
2. Process:
● Retrieve the product from the inventory using the product ID.
● Check if the product is in stock.
● If the stock is sufficient:
● Add the product to the customer’s cart with the specified quantity.
● Update the stock of the product in the inventory.
● If the stock is insufficient:
● Display a message indicating that the stock is insufficient.
2. Output: Updated customer cart with added products or error message if stock is
insufficient.
To develop an Online Shopping System with classes for products, customers, shopping carts, and
inventory management in Python, the code is broken down into a set of classes:
1. Product: This class represents a product with attributes like product name, price, and
stock quantity.
2. Customer: This class represents a customer with attributes like name, email, and their
shopping cart.
3. ShoppingCart: This class represents a shopping cart that can hold products. It includes
methods for adding products, calculating the total price, and displaying the cart contents.
4. Inventory: This class manages the inventory, ensuring that products are available for
purchase and stock levels are adjusted accordingly.
def __str__(self):
return f"{self.name} (ID: {self.product_id}) - Rs.{self.price:.2f} (Stock: {self.stock})"
class ShoppingCart:
def __init__(self):
self.items = {}
def calculate_total(self):
total = 0
for item in self.items.values():
total += item['product'].price * item['quantity']
return total
def display_cart(self):
if self.items:
print("Your Shopping Cart:")
for item in self.items.values():
print(f"{item['product'].name}: {item['quantity']} @ Rs.{item['product'].price:.2f}
each")
else:
print("Your cart is empty.")
class Customer:
def __init__(self, name, email):
self.name = name
self.email = email
self.cart = ShoppingCart()
def view_cart(self):
self.cart.display_cart()
def checkout(self):
total = self.cart.calculate_total()
print(f"Total cost for {self.name}: Rs. {total:.2f}")
# Simulate order processing
self.cart = ShoppingCart() # Empty cart after checkout
print("Order has been placed and cart is now empty.")
class Inventory:
def __init__(self):
self.products = {}
def display_inventory(self):
print("Inventory:")
for product in self.products.values():
print(product)
# Example usage
# Create a customer
customer = Customer("Aditi", "[email protected]")
# View inventory
inventory.display_inventory()
# View cart
customer.view_cart()
# Checkout
customer.checkout()
Output
CONCLUSION:
R1 R2 R3 R4 Total Signature