Submitted at
2025-12-02 23:00:17
python
AI generated puzzle
class InventoryManager:
"""Manages product inventory with automatic reordering"""
def __init__(self):
self.inventory = {}
self.reorder_threshold = 10
self.reorder_quantity = 50
def add_product(self, product_id, quantity):
"""Add or update product quantity in inventory"""
if product_id in self.inventory:
self.inventory[product_id] += quantity
else:
self.inventory[product_id] = quantity
def sell_product(self, product_id, quantity):
"""Sell product and automatically reorder if below threshold"""
if product_id not in self.inventory:
raise ValueError(f"Product {product_id} not in inventory")
if self.inventory[product_id] < quantity:
raise ValueError(f"Insufficient stock for product {product_id}")
self.inventory[product_id] -= quantity
# Auto-reorder if stock falls below threshold
if self.inventory[product_id] < self.reorder_threshold:
self.reorder(product_id)
return self.inventory[product_id]
def reorder(self, product_id):
"""Reorder product to replenish stock"""
self.inventory[product_id] += self.reorder_quantity
print(f"Reordered {self.reorder_quantity} units of {product_id}")
# Test the inventory system
manager = InventoryManager()
manager.add_product("WIDGET-001", 15)
# Sell products in batches
for i in range(3):
remaining = manager.sell_product("WIDGET-001", 3)
print(f"Sale {i+1}: {remaining} units remaining")
print(f"Final inventory: {manager.inventory['WIDGET-001']}")