Basic Inventory Management System in Python
class InventoryManagementSystem:
def __init__(self):
self.inventory = {}
def add_item(self, item_name, quantity, price):
if item_name in self.inventory:
self.inventory[item_name]['quantity'] += quantity
else:
self.inventory[item_name] = {'quantity': quantity, 'price': price}
print(f"Added {quantity} of {item_name}.")
def update_item(self, item_name, quantity=None, price=None):
if item_name in self.inventory:
if quantity is not None:
self.inventory[item_name]['quantity'] = quantity
if price is not None:
self.inventory[item_name]['price'] = price
print(f"Updated {item_name}.")
else:
print(f"{item_name} not found in inventory.")
def delete_item(self, item_name):
if item_name in self.inventory:
del self.inventory[item_name]
print(f"Deleted {item_name} from inventory.")
else:
print(f"{item_name} not found in inventory.")
def view_inventory(self):
if not self.inventory:
print("Inventory is empty.")
return
for item, details in self.inventory.items():
print(f"Item: {item}, Quantity: {details['quantity']}, Price: ${details['price']:.2f}")
def search_item(self, item_name):
if item_name in self.inventory:
details = self.inventory[item_name]
print(f"Found {item_name}: Quantity: {details['quantity']}, Price: ${details['price']:.2f}")
else:
print(f"{item_name} not found in inventory.")
def main():
ims = InventoryManagementSystem()
while True:
print("\nInventory Management System")
print("1. Add Item")
print("2. Update Item")
print("3. Delete Item")
print("4. View Inventory")
print("5. Search Item")
print("6. Exit")
choice = input("Choose an option: ")
if choice == '1':
name = input("Enter item name: ")
qty = int(input("Enter quantity: "))
price = float(input("Enter price: "))
ims.add_item(name, qty, price)
elif choice == '2':
name = input("Enter item name to update: ")
qty = input("Enter new quantity (leave blank to keep current): ")
price = input("Enter new price (leave blank to keep current): ")
qty = int(qty) if qty else None
price = float(price) if price else None
ims.update_item(name, qty, price)
elif choice == '3':
name = input("Enter item name to delete: ")
ims.delete_item(name)
elif choice == '4':
ims.view_inventory()
elif choice == '5':
name = input("Enter item name to search: ")
ims.search_item(name)
elif choice == '6':
print("Exiting the Inventory Management System.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Features of the System
- Add Item: Allows adding new items to the inventory.
- Update Item: Allows updating the quantity and/or price of existing items.
- Delete Item: Removes items from the inventory.
- View Inventory: Displays all items with their quantities and prices.
- Search Item: Searches for a specific item in the inventory.
How to Run the Code
- Copy the code into a Python file, e.g.,
inventory_management.py
.
- Run the file using Python:
python inventory_management.py
.
- Follow the on-screen menu to interact with the system.
Leave a Reply