0% found this document useful (0 votes)
8 views5 pages

Python 1

Uploaded by

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

Python 1

Uploaded by

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

QUESTION 1

class Product:
def __init__(self, name, price, quantity, category):
self.name = name
self.price = price
self.quantity = quantity
self.category = category

def __str__(self):

return f"Product Name: ${self.price:.2f} (Quantity: {self.quantity},


Category: {self.category})"

if __name__ == "__main__":

product1 = Product("Laptop", 999.99, 10, "Electronics")


print(product1)
class Product:
def __init__(self, name, price, quantity, category):
self.name = name
self.price = price
self.quantity = quantity
self.category = category

def __str__(self):
return f"Name: {self.name}, Price: {self.price}, Quantity:
{self.quantity}, Category: {self.category}"

class Inventory:
def __init__(self):
self.products = []

def add_product(self, product):


self.products.append(product)

def update_product(self, name, price, quantity):


for product in self.products:
if product.name == name:
product.price = price
product.quantity = quantity
return f"Updated {name} to Price: {price}, Quantity:
{quantity}"
return f"Product {name} not found."

def display_products(self):
if not self.products:
print("No products in inventory.")
for product in self.products:
print(product)

def find_product(self, name):


for product in self.products:
if product.name == name:
return product
return f"Product {name} not found."

def total_inventory_value(self):
total_value = sum(product.price * product.quantity for product in
self.products)
return total_value

if __name__ == "__main__":
inventory = Inventory()

inventory.add_product(Product("Laptop", 999.99, 10, "Electronics"))


inventory.add_product(Product("Mouse", 25.99, 50, "Electronics"))

print("Current Inventory:")
inventory.display_products()
print(inventory.update_product("Laptop", 899.99, 8))

print(inventory.find_product("Mouse"))

print(f"Total Inventory Value: ${inventory.total_inventory_value():.2f}")


class Product:
def __init__(self, name, price, quantity, category):
self.name = name
self.price = price
self.quantity = quantity
self.category = category

def update_price(self, new_price):


self.price = new_price

def update_quantity(self, new_quantity):


self.quantity = new_quantity

def get_total_value(self):
return self.price * self.quantity

class Inventory:
def __init__(self):
self.products = []

def add_product(self, product):


self.products.append(product)

def display_inventory(self):
for product in self.products:
print(f"Name: {product.name}, Price: {product.price}, Quantity:
{product.quantity}, Category: {product.category}")

def find_product(self, name):


for product in self.products:
if product.name == name:
return product
return None

def calculate_total_value(self):
total_value = sum(product.get_total_value() for product in
self.products)
return total_value

inventory = Inventory()

inventory.add_product(Product("Laptop", 999.99, 10, "Electronics"))


inventory.add_product(Product("Chair", 49.99, 20, "Furniture"))
inventory.add_product(Product("Notebook", 2.99, 100, "Stationery"))
inventory.add_product(Product("Pen", 1.49, 200, "Stationery"))

print("Current Inventory:")
inventory.display_inventory()

laptop = inventory.find_product("Laptop")
if laptop:
laptop.update_price(899.99)
laptop.update_quantity(8)
updated_laptop = inventory.find_product("Laptop")
if updated_laptop:
print(f"\nUpdated Product Details: Name: {updated_laptop.name}, Price:
{updated_laptop.price}, Quantity: {updated_laptop.quantity}, Category:
{updated_laptop.category}")

total_value = inventory.calculate_total_value()
print(f"\nTotal Value of Inventory: ${total_value:.2f}")

You might also like