CODE BAsE
CODE BAsE
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)
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 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 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)
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()