0% found this document useful (0 votes)
4 views

Code for inventory management

The document outlines an Inventory Management System implemented in Python, allowing users to add, update, delete, view, and search for items in an inventory. It features a command-line interface where users can interact with the inventory through a menu-driven approach. The system stores items in a dictionary, capturing details such as item ID, name, quantity, and price.

Uploaded by

Hammad
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code for inventory management

The document outlines an Inventory Management System implemented in Python, allowing users to add, update, delete, view, and search for items in an inventory. It features a command-line interface where users can interact with the inventory through a menu-driven approach. The system stores items in a dictionary, capturing details such as item ID, name, quantity, and price.

Uploaded by

Hammad
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class Inventory:

def __init__(self):
# Initialize the inventory as an empty dictionary
self.items = {}

def add_item(self):
# Add a new item to the inventory
item_id = input("Enter item ID: ")
if item_id in self.items:
print("Item already exists in the inventory.")
else:
item_name = input("Enter item name: ")
item_quantity = int(input("Enter item quantity: "))
item_price = float(input("Enter item price: $"))
self.items[item_id] = {
'name': item_name,
'quantity': item_quantity,
'price': item_price
}
print(f"Item {item_name} added successfully!")

def update_item(self):
# Update an existing item in the inventory
item_id = input("Enter item ID to update: ")
if item_id not in self.items:
print("Item not found.")
else:
print("Updating item...")
item_name = input(f"Enter new name for {self.items[item_id]['name']}
(leave blank to keep current): ")
item_quantity = input(f"Enter new quantity (current:
{self.items[item_id]['quantity']}): ")
item_price = input(f"Enter new price (current: ${self.items[item_id]
['price']}): ")

# Update only if the user enters a value


if item_name:
self.items[item_id]['name'] = item_name
if item_quantity:
self.items[item_id]['quantity'] = int(item_quantity)
if item_price:
self.items[item_id]['price'] = float(item_price)

print(f"Item {item_id} updated successfully!")

def delete_item(self):
# Delete an item from the inventory
item_id = input("Enter item ID to delete: ")
if item_id in self.items:
del self.items[item_id]
print("Item deleted successfully!")
else:
print("Item not found.")

def view_inventory(self):
# View all items in the inventory
if not self.items:
print("Inventory is empty.")
else:
print("Inventory:")
for item_id, item_details in self.items.items():
print(f"ID: {item_id}, Name: {item_details['name']}, Quantity:
{item_details['quantity']}, Price: ${item_details['price']:.2f}")

def search_item(self):
# Search for an item by ID or name
search_term = input("Enter item ID or name to search: ")
found = False
for item_id, item_details in self.items.items():
if search_term.lower() in item_id.lower() or search_term.lower() in
item_details['name'].lower():
print(f"ID: {item_id}, Name: {item_details['name']}, Quantity:
{item_details['quantity']}, Price: ${item_details['price']:.2f}")
found = True
if not found:
print("Item not found.")

def run(self):
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("Enter your choice (1-6): ")

if choice == '1':
self.add_item()
elif choice == '2':
self.update_item()
elif choice == '3':
self.delete_item()
elif choice == '4':
self.view_inventory()
elif choice == '5':
self.search_item()
elif choice == '6':
print("Exiting the Inventory Management System.")
break
else:
print("Invalid choice, please try again.")

# Create an instance of Inventory and run the system


inventory_system = Inventory()
inventory_system.run()

You might also like