0% found this document useful (0 votes)
3 views3 pages

CODE BAsE

The document outlines a simple e-commerce system with classes for products, cart items, shopping carts, and orders. It includes methods for calculating prices, managing inventory, and processing checkout, along with an example usage demonstrating the creation of products, adding them to a cart, and processing an order. The system also allows for applying discounts and calculating totals with tax and shipping costs.

Uploaded by

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

CODE BAsE

The document outlines a simple e-commerce system with classes for products, cart items, shopping carts, and orders. It includes methods for calculating prices, managing inventory, and processing checkout, along with an example usage demonstrating the creation of products, adding them to a cart, and processing an order. The system also allows for applying discounts and calculating totals with tax and shipping costs.

Uploaded by

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

class Product:

def __init__(self, id, name, price, inventory=0, tax_rate=0.0, discount=0.0):


self.id = id
self.name = name
self.price = price
self.inventory = inventory
self.tax_rate = tax_rate
self.discount = discount

def get_discounted_price(self):
"""Calculate the price after discount."""
return self.price * (1 - self.discount)

def get_final_price(self):
"""Calculate final price including tax."""
discounted_price = self.get_discounted_price()
return discounted_price * (1 + self.tax_rate)

def update_inventory(self, quantity):


"""Update inventory after purchase or restock."""
self.inventory += quantity
return self.inventory

class CartItem:
def __init__(self, product, quantity=1):
self.product = product
self.quantity = quantity

def get_subtotal(self):
"""Calculate subtotal for this item."""
return self.product.get_discounted_price() * self.quantity

def get_total_with_tax(self):
"""Calculate total with tax for this item."""
return self.product.get_final_price() * self.quantity

class ShoppingCart:
def __init__(self):
self.items = []

def add_item(self, product, quantity=1):


"""Add a product to cart."""
# Check if product already exists in cart
for item in self.items:
if item.product.id == product.id:
item.quantity += quantity
return

# If not, add new cart item


self.items.append(CartItem(product, quantity))

def remove_item(self, product_id):


"""Remove a product from cart."""
self.items = [item for item in self.items if item.product.id != product_id]

def update_quantity(self, product_id, quantity):


"""Update quantity of a product."""
for item in self.items:
if item.product.id == product_id:
item.quantity = quantity
break

def get_subtotal(self):
"""Calculate cart subtotal."""
return sum(item.get_subtotal() for item in self.items)

def get_tax_amount(self):
"""Calculate total tax amount."""
return sum(item.get_total_with_tax() - item.get_subtotal() for item in
self.items)

def get_total(self):
"""Calculate cart total with tax."""
return sum(item.get_total_with_tax() for item in self.items)

def checkout(self):
"""Process checkout and update inventory."""
for item in self.items:
if item.quantity > item.product.inventory:
raise ValueError(f"Not enough inventory for {item.product.name}")
item.product.update_inventory(-item.quantity)

total = self.get_total()
self.items = [] # Empty cart after checkout
return total

class Order:
def __init__(self, order_id, customer_id, items, shipping_cost=0.0,
discount_code=None):
self.order_id = order_id
self.customer_id = customer_id
self.items = items # List of CartItems
self.shipping_cost = shipping_cost
self.discount_code = discount_code
self.order_discount = 0.0

def apply_discount_code(self, discount_amount):


"""Apply an order-level discount."""
self.order_discount = discount_amount

def get_subtotal(self):
"""Calculate order subtotal."""
return sum(item.get_subtotal() for item in self.items)

def get_tax_amount(self):
"""Calculate total tax for order."""
return sum(item.get_total_with_tax() - item.get_subtotal() for item in
self.items)

def get_total(self):
"""Calculate grand total for order."""
item_total = sum(item.get_total_with_tax() for item in self.items)
return item_total + self.shipping_cost - self.order_discount
# Example usage of the e-commerce system
def main():
# Create products
laptop = Product(1, "Laptop", 999.99, inventory=10, tax_rate=0.08,
discount=0.1)
headphones = Product(2, "Headphones", 99.99, inventory=20, tax_rate=0.08)
mouse = Product(3, "Mouse", 29.99, inventory=30, tax_rate=0.08, discount=0.05)

# Initialize cart and add items


cart = ShoppingCart()
cart.add_item(laptop)
cart.add_item(headphones, 2)

# Calculate and display cart details


print(f"Cart Subtotal: ${cart.get_subtotal():.2f}")
print(f"Tax Amount: ${cart.get_tax_amount():.2f}")
print(f"Cart Total: ${cart.get_total():.2f}")

# Create and process an order


order_items = cart.items.copy()
order = Order(1001, "CUST001", order_items, shipping_cost=15.00)
order.apply_discount_code(25.00) # Apply a $25 discount

print(f"\nOrder Summary:")
print(f"Subtotal: ${order.get_subtotal():.2f}")
print(f"Tax: ${order.get_tax_amount():.2f}")
print(f"Shipping: ${order.shipping_cost:.2f}")
print(f"Discount: ${order.order_discount:.2f}")
print(f"Order Total: ${order.get_total():.2f}")

# Process checkout
try:
final_amount = cart.checkout()
print(f"\nCheckout successful! Amount charged: ${final_amount:.2f}")
print(f"Updated laptop inventory: {laptop.inventory}")
print(f"Updated headphones inventory: {headphones.inventory}")
except ValueError as e:
print(f"Checkout failed: {e}")

if __name__ == "__main__":
main()

task (m) > base positive {open.inventory} > {close.listbase}


Light weight and Control devices - Hard control pulse -
Pull string - Apply subtotal - Push Details.

Open Folders { grant permissions } - Administrators.


calculate bases and highlight all control points

You might also like