0% found this document useful (0 votes)
25 views6 pages

Exp 16

The document outlines the development of an Online Shopping System using Object-Oriented Programming principles, detailing classes for products, customers, shopping carts, and inventory management. It includes a step-by-step algorithm for adding products, managing customer carts, processing orders, and updating inventory. The provided source code demonstrates the implementation of these classes and their methods in Python.

Uploaded by

roshanishirage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

Exp 16

The document outlines the development of an Online Shopping System using Object-Oriented Programming principles, detailing classes for products, customers, shopping carts, and inventory management. It includes a step-by-step algorithm for adding products, managing customer carts, processing orders, and updating inventory. The provided source code demonstrates the implementation of these classes and their methods in Python.

Uploaded by

roshanishirage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NUMBER: 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:

Step 1: Add Products to Inventory

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.

Step 2: Create Customer

1.​ Input: Customer details such as name and email.


2.​ Process:
●​ Create a new customer object with the given details.
●​ Initialize a shopping cart for the customer.
2.​ Output: A new customer object with an empty shopping cart.

Step 3: Customer Adds Products to Cart

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.

Step 4: Customer Checks Out

1.​ Input: Customer’s cart.


2.​ Process:
●​ Calculate the total cost of the cart by summing the prices of all products in the
cart.
●​ Display the total cost.
●​ Simulate order processing and mark the order as complete.
2.​ Output: Total cost, order confirmation, and updated cart (now empty).

Step 5: Empty the Cart and Update Inventory

1.​ Input: Customer's cart, inventory.


2.​ Process:
●​ For each product in the cart:
●​ Update the product’s stock in the inventory by subtracting the quantity purchased.
●​ Clear the customer's cart (i.e., empty the cart).
2.​ Output: Updated inventory with reduced stock, and the customer’s cart is now empty.

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.

Description of the classes and their methods:


1.​ Product Class:
●​ __init__: Initializes the product with an ID, name, price, and stock quantity.
●​ update_stock: Decreases stock by a specified quantity if available, otherwise
returns False.
2.​ ShoppingCart Class:
●​ add_product: Adds a product to the cart and updates the stock of that product.
●​ remove_product: Removes a product from the cart and adjusts stock accordingly.
●​ calculate_total: Calculates the total cost of all products in the cart.
●​ display_cart: Displays the current contents of the cart.
3.​ Customer Class:
●​ view_cart: Displays the customer's current shopping cart.
●​ checkout: Calculates the total cost and simulates an order placement, then empties
the cart.
4.​ Inventory Class:
●​ add_product: Adds a product to the inventory.
●​ get_product: Retrieves a product by its ID.
●​ display_inventory: Displays all products in the inventory.
SOURCE CODE:

print('Practical 6- Object-oriented programming (OOP) - Online Shopping System: ')


print('Performed by Name of Student')
print('----------------------------------------------------------------')
class Product:
def __init__(self, product_id, name, price, stock):
self.product_id = product_id
self.name = name
self.price = price
self.stock = stock

def __str__(self):
return f"{self.name} (ID: {self.product_id}) - Rs.{self.price:.2f} (Stock: {self.stock})"

def update_stock(self, quantity):


if self.stock >= quantity:
self.stock -= quantity
return True
else:
return False

class ShoppingCart:
def __init__(self):
self.items = {}

def add_product(self, product, quantity):


if product.update_stock(quantity):
if product.product_id in self.items:
self.items[product.product_id]['quantity'] += quantity
else:
self.items[product.product_id] = {'product': product, 'quantity': quantity}
print(f"Added {quantity} of {product.name} to the cart.")
else:
print(f"Insufficient stock for {product.name}.")

def remove_product(self, product, quantity):


if product.product_id in self.items:
if self.items[product.product_id]['quantity'] >= quantity:
self.items[product.product_id]['quantity'] -= quantity
product.stock += quantity
print(f"Removed {quantity} of {product.name} from the cart.")
else:
print(f"Not enough of {product.name} in the cart to remove.")
else:
print(f"{product.name} is not in the cart.")

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 add_product(self, product):


self.products[product.product_id] = product

def get_product(self, product_id):


return self.products.get(product_id)

def display_inventory(self):
print("Inventory:")
for product in self.products.values():
print(product)

# Example usage

# Create some products


product1 = Product(1, "Laptop", 50000.00, 10)
product2 = Product(2, "Smartphone", 15000.00, 15)
product3 = Product(3, "Headphones", 1500.00, 30)

# Initialize inventory and add products


inventory = Inventory()
inventory.add_product(product1)
inventory.add_product(product2)
inventory.add_product(product3)

# Create a customer
customer = Customer("Aditi", "[email protected]")

# View inventory
inventory.display_inventory()

# Add products to customer's cart


customer.cart.add_product(product1, 2) # Add 2 laptops
customer.cart.add_product(product2, 1) # Add 1 smartphone
customer.cart.add_product(product3, 5) # Add 5 headphones

# View cart
customer.view_cart()

# Checkout
customer.checkout()

# View inventory after purchase


inventory.display_inventory()

Output
CONCLUSION:

This Online Shopping System demonstrates Object-Oriented Programming (OOP) by using


classes to manage products and a shopping cart. It efficiently handles adding, removing items,
and stock management. The code is structured, reusable, and can be expanded with more features
like payment options and a user interface.

MARKS & SIGNATURE:

R1 R2 R3 R4 Total Signature

(3 Marks) (3 Marks) (3 Marks) (1 Mark) (10 Marks)

You might also like