The before code shows manual stock decrement without alerts. The after code adds automatic stock updates and triggers reorder alerts when stock falls below a threshold, improving reliability and responsiveness.
### Before: No inventory management, manual stock update
class Store:
def __init__(self):
self.stock = {}
def sell_item(self, item, quantity):
if item in self.stock and self.stock[item] >= quantity:
self.stock[item] -= quantity
print(f"Sold {quantity} of {item}")
else:
print("Not enough stock")
### After: Inventory management with automatic stock update and reorder alert
class InventoryManager:
def __init__(self, reorder_threshold=5):
self.stock = {}
self.reorder_threshold = reorder_threshold
def add_stock(self, item, quantity):
self.stock[item] = self.stock.get(item, 0) + quantity
def sell_item(self, item, quantity):
if self.stock.get(item, 0) >= quantity:
self.stock[item] -= quantity
print(f"Sold {quantity} of {item}")
self.check_reorder(item)
else:
print("Not enough stock")
def check_reorder(self, item):
if self.stock.get(item, 0) <= self.reorder_threshold:
print(f"Reorder alert: {item} stock is low ({self.stock[item]})")