In this project, you will develop a comprehensive inventory management system for a retail business by applying your knowledge in object-oriented programming (OOP) in Python. Imagine you are working for an e-commerce company called ShopSmart, a rapidly growing online retailer that sells a wide range of products, including electronics, clothing, and home goods. As the company expands, efficiently managing inventory becomes crucial to ensure smooth operations and customer satisfaction.
Object-oriented programming (OOP) is a programming paradigm that organizes software design around data or objects rather than functions and logic. OOP allows for modular, reusable, and maintainable code, which is particularly beneficial for complex systems like inventory management systems.
You will define two classes Product and Order, using the implementation requirements detailed below:
Product
Product- Constructor parameter(s):
self,product_id,name,category,quantity,price, andsupplier. - Class-level variable(s):
inventory.
Product class method(s)
Product class method(s)add_product()
add_product()- Parameter(s):
cls,name,category,quantity,price, andsupplier. - Behavior:
- Define the
product_idassuming it's auto-generated incrementally, without any duplicateproduct_idvalues. - Define a
new_productvariable that will call the constructor of the Product class. - Return the message
"Product added successfully"to know that the product was added successfully.
- Define the
update_product()
update_product()- Parameter(s):
cls,product_id,quantity,price, andsupplier.quantity,price, andsuppliershould have default values ofNone.
- Behavior:
- Check if the
product_idalready exists in theinventory. - If
product_idexists, check for the given parameters in the method if they have a value and update accordingly the product. - Return either one of these messages:
"Product information updated successfully"or"Product not found".
- Check if the
delete_product()
delete_product()- Parameter(s):
cls,product_id. - Behavior:
- Check in the inventory list if the given
product_idwas passed as a parameter. - If
product_idexists then remove the product from the list. - Return either one of these messages:
"Product deleted successfully"or"Product not found".
- Check in the inventory list if the given
Order
Order- Constructor parameter(s):
self,order_id,products, andcustomer_info.customer_infoshould have a default value ofNone.
Order method(s)
Order method(s)place_order()
place_order()- Parameter(s):
self,product_id,quantity, andcustomer_info.customer_infoshould have a default value ofNone.
- Behavior:
- Append to the
productslist a tuple containingproduct_idandquantity. - Assume that each order can only take one product.
- Return the message:
"Order placed successfully. Order ID: {self.order_id}".
- Append to the
As an example, your code must be able to create products like this:
p1 = Product.add_product("Laptop", "Electronics", 50, 1000, "Supplier A")
Update them like this:
update_p1 = Product.update_product(1, quantity=45, price=950)
Delete them like this:
delete_p1 = Product.delete_product(1)
And, create and place orders like this:
order = Order(order_id=1, products=[])
order_placement = order.place_order(1, 2, customer_info="John Doe")
Complete the following code so that the classes perform the same behavior as the examples provided.
class Product:
# Class-level inventory variable to hold all products
inventory = []
# A class-level variable for auto-generating product IDs
_product_counter = 1
def __init__(self, product_id, name, category, quantity, price, supplier):
self.product_id = product_id
self.name = name
self.category = category
self.quantity = quantity
self.price = price
self.supplier = supplier
@classmethod
def add_product(cls, name, category, quantity, price, supplier):
# Create a new product with an auto-generated product_id
product_id = cls._product_counter
cls._product_counter += 1
new_product = Product(product_id, name, category, quantity, price, supplier)
cls.inventory.append(new_product)
return "Product added successfully"
@classmethod
def update_product(cls, product_id, quantity=None, price=None, supplier=None):
# Find the product by product_id
product = next((p for p in cls.inventory if p.product_id == product_id), None)
if product:
# Only update the attributes that were passed in
if quantity is not None:
product.quantity = quantity
if price is not None:
product.price = price
if supplier is not None:
product.supplier = supplier
return "Product information updated successfully"
else:
return "Product not found"
@classmethod
def delete_product(cls, product_id):
# Find and remove the product by product_id
product = next((p for p in cls.inventory if p.product_id == product_id), None)
if product:
cls.inventory.remove(product)
return "Product deleted successfully"
else:
return "Product not found"
class Order:
def __init__(self, order_id, products, customer_info=None):
self.order_id = order_id
self.products = products
self.customer_info = customer_info
def place_order(self, product_id, quantity, customer_info=None):
# Add the product and quantity to the order
self.products.append((product_id, quantity))
# If customer info is provided, update the order
if customer_info:
self.customer_info = customer_info
return f"Order placed successfully. Order ID: {self.order_id}"
# Example usage
# Create products
p1 = Product.add_product("Laptop", "Electronics", 50, 1000, "Supplier A")
p2 = Product.add_product("Smartphone", "Electronics", 100, 500, "Supplier B")
# Update product details
update_p1 = Product.update_product(1, quantity=45, price=950) # Only update quantity and price
update_p2 = Product.update_product(2, supplier="Supplier C") # Only update supplier
# Try updating a non-existent product
update_non_existent = Product.update_product(3, quantity=30)
# Delete product
delete_p1 = Product.delete_product(2)
# Create an order and place it
order = Order(order_id=1, products=[])
order_placement = order.place_order(1, 2, customer_info="John Doe")
# Printing outputs
print(update_p1) # Expected output: "Product information updated successfully"
print(update_p2) # Expected output: "Product information updated successfully"
print(update_non_existent) # Expected output: "Product not found"
print(delete_p1) # Expected output: "Product deleted successfully"
print(order_placement) # Expected output: "Order placed successfully. Order ID: 1"