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

Product Management Class in Python

The document contains a Python code implementation of a product management system with classes for Product, CategoryProduct, and DiscountProduct. Each class has methods for managing stock, selling products, calculating stock value, and displaying product information. The code includes examples of creating products and performing stock operations.
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)
13 views3 pages

Product Management Class in Python

The document contains a Python code implementation of a product management system with classes for Product, CategoryProduct, and DiscountProduct. Each class has methods for managing stock, selling products, calculating stock value, and displaying product information. The code includes examples of creating products and performing stock operations.
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

Code:

class Product:
def __init__(self, product_name, price, quantity):
self.product_name = product_name
[Link] = price
[Link] = quantity

def add_stock(self, amount):


[Link] += amount
print(f"{amount} units added. New stock for {self.product_name}:
{[Link]} units.")

def sell(self, amount):

if amount <= [Link]:


[Link] -= amount
print(f"{amount} units of {self.product_name} sold. Remaining stock:
{[Link]} units.")
else:
print(f"Insufficient stock to sell {amount} units of {self.product_name}. Only
{[Link]} units available.")

def get_stock_value(self):
return [Link] * [Link]

def __str__(self):
return f"Product: {self.product_name}, Price: ${[Link]}, Stock:
{[Link]} units"
class CategoryProduct(Product):
def __init__(self, product_name, price, quantity, category):
super().__init__(product_name, price, quantity)

[Link] = category
def __str__(self):
return f"Category: {[Link]}, {super().__str__()}"

class DiscountProduct(Product):
def __init__(self, product_name, price, quantity, discount_percentage):
super().__init__(product_name, price, quantity)

self.discount_percentage = discount_percentage

def get_discounted_price(self):
return [Link] * (1 - self.discount_percentage / 100)

def get_stock_value(self):
discounted_price = self.get_discounted_price()
return discounted_price * [Link]

def __str__(self):
return f"{super().__str__()}, Discount: {self.discount_percentage}% off,
Discounted Price: ${self.get_discounted_price():.2f}"

product1 = Product("Laptop", 1000, 50)

product2 = CategoryProduct("Smartphone", 700, 30, "Electronics")


product3 = DiscountProduct("Headphones", 150, 100, 10)
print(product1)
product1.add_stock(20)
[Link](15)
print(f"Total stock value for {product1.product_name}:
${product1.get_stock_value()}")

print("\n")
print(product2)
product2.add_stock(10)
[Link](5)
print(f"Total stock value for {product2.product_name}:
${product2.get_stock_value()}")

print("\n")

print(product3)
product3.add_stock(50)
[Link](30)
print(f"Total stock value for {product3.product_name} after discount:
${product3.get_stock_value()}")

Output:

Common questions

Powered by AI

A plausible approach would be to add a `manufacturer` attribute to the base `Product` class, as it applies universally. This ensures that adding this feature doesn't violate the open/closed principle, as existing subclasses would inherit this attribute without requiring modification .

The `add_stock` method allows dynamic modification of a product's quantity, enhancing the class functionality by enabling stock management directly within the class . This exemplifies the encapsulation principle, as it provides a controlled interface for modifying the object's state, thereby preventing arbitrary alterations .

The `super().__str__()` call in both `CategoryProduct` and `DiscountProduct` classes allows these subclasses to extend the string representation of the `Product` class. It ensures that the inherited attributes are included in the string output alongside the subclass-specific information, which maintains a unified display format .

Polymorphism is applied via overriding methods in subclasses such as `get_stock_value` in `DiscountProduct`. This allows objects to be treated as instances of their superclass while specific behavior is dictated by the subclass, facilitating flexible code where different product types can be managed uniformly . The advantage is improved code flexibility and reuse, reducing conditionals and enhancing maintenance .

Implementing the `sell` method provides a clear, standardized way to handle stock deductions, improving maintainability and readability by encapsulating the logic . However, potential issues include the method not handling financial transactions or logging sales, which could complicate auditing and financial accuracy if used in a real-world application .

The design employs inheritance, allowing the `CategoryProduct` and `DiscountProduct` classes to reuse properties and methods from the `Product` base class, fostering code reuse . Extensibility is achieved by enabling additional attributes such as `category` and `discount_percentage` to be included in subclasses without altering the base class, which shows flexibility .

Drawbacks include limited handling of complex financial transactions, lack of features for audit trails, and potential concurrency issues during stock modifications in a multi-user environment . These can be mitigated by integrating transactions with a database system, implementing logging mechanisms, and ensuring thread-safe operations, thereby enhancing reliability and scalability for real-world applications .

Implementing a tracking feature for stock additions could utilize the Observer Design Pattern, where an observer monitors inventory changes . This would allow for decoupled systems to be notified of changes, facilitating robust update mechanisms without tightly coupling the logic into the `Product` class .

Making `price` a private variable would enhance encapsulation by restricting direct access from outside the class. While the code would need additional getter/setter methods to manage access, this change would prevent unauthorized or accidental modifications, promoting data integrity and security . There might be initial disruptions if existing code relies on direct access, but it provides a more robust structure .

The `DiscountProduct` class overrides the `get_stock_value` method from the `Product` class to calculate the stock value using the discounted price instead of the original price . This override is significant as it allows for accurate financial calculations for discounted items while maintaining code organization and reducing redundancy, since the overriding is done only where necessary .

You might also like