
Python OOP Project: Inventory Management System
Welcome, fellow Pythonista! Today, we’re going to build an Inventory Management System using object-oriented programming (OOP). This project will help you practice key OOP concepts like classes, inheritance, encapsulation, and polymorphism in a practical way. By the end, you’ll have a functional system that can add, update, search, and track inventory items. Let’s dive in!
Understanding the Core Classes
We’ll start by defining the main classes for our system. The Item class will represent individual products in the inventory. Each item will have attributes like name, price, quantity, and a unique ID. We’ll use encapsulation to protect these attributes and provide methods to update them.
class Item:
def __init__(self, item_id, name, price, quantity):
self._item_id = item_id
self._name = name
self._price = price
self._quantity = quantity
def get_id(self):
return self._item_id
def get_name(self):
return self._name
def get_price(self):
return self._price
def get_quantity(self):
return self._quantity
def update_quantity(self, new_quantity):
if new_quantity >= 0:
self._quantity = new_quantity
else:
print("Quantity cannot be negative.")
def update_price(self, new_price):
if new_price > 0:
self._price = new_price
else:
print("Price must be positive.")
def __str__(self):
return f"ID: {self._item_id}, Name: {self._name}, Price: ${self._price}, Quantity: {self._quantity}"
Next, we’ll create the Inventory class, which will manage a collection of Item objects. This class will handle adding, removing, updating, and searching for items. We’ll use a dictionary to store items for efficient lookup by ID.
class Inventory:
def __init__(self):
self._items = {}
def add_item(self, item):
if item.get_id() in self._items:
print("Item ID already exists.")
else:
self._items[item.get_id()] = item
print("Item added successfully.")
def remove_item(self, item_id):
if item_id in self._items:
del self._items[item_id]
print("Item removed successfully.")
else:
print("Item not found.")
def update_item_quantity(self, item_id, new_quantity):
if item_id in self._items:
self._items[item_id].update_quantity(new_quantity)
print("Quantity updated successfully.")
else:
print("Item not found.")
def search_by_id(self, item_id):
return self._items.get(item_id, "Item not found.")
def search_by_name(self, name):
results = [item for item in self._items.values() if item.get_name().lower() == name.lower()]
return results if results else "No items found."
def list_all_items(self):
for item in self._items.values():
print(item)
To make our system more robust, let’s add a Category class and use inheritance to create specialized item types. For example, we can have an Electronics class that inherits from Item and adds attributes like warranty period.
class Electronics(Item):
def __init__(self, item_id, name, price, quantity, warranty_period):
super().__init__(item_id, name, price, quantity)
self._warranty_period = warranty_period
def get_warranty(self):
return self._warranty_period
def __str__(self):
return f"{super().__str__()}, Warranty: {self._warranty_period} months"
Now, let’s integrate these classes into a simple menu-driven program so users can interact with the system.
def main():
inventory = Inventory()
while True:
print("\nInventory Management System")
print("1. Add Item")
print("2. Remove Item")
print("3. Update Quantity")
print("4. Search by ID")
print("5. Search by Name")
print("6. List All Items")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == '1':
item_id = input("Enter item ID: ")
name = input("Enter item name: ")
price = float(input("Enter price: "))
quantity = int(input("Enter quantity: "))
item_type = input("Enter item type (regular/electronics): ").lower()
if item_type == "electronics":
warranty = int(input("Enter warranty period in months: "))
new_item = Electronics(item_id, name, price, quantity, warranty)
else:
new_item = Item(item_id, name, price, quantity)
inventory.add_item(new_item)
elif choice == '2':
item_id = input("Enter item ID to remove: ")
inventory.remove_item(item_id)
elif choice == '3':
item_id = input("Enter item ID: ")
new_quantity = int(input("Enter new quantity: "))
inventory.update_item_quantity(item_id, new_quantity)
elif choice == '4':
item_id = input("Enter item ID to search: ")
print(inventory.search_by_id(item_id))
elif choice == '5':
name = input("Enter item name to search: ")
results = inventory.search_by_name(name)
for item in results:
print(item)
elif choice == '6':
inventory.list_all_items()
elif choice == '7':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
This basic system allows you to manage an inventory, but there’s room for improvement. You could add features like saving data to a file, generating reports, or implementing a GUI. Practice extending this project to strengthen your OOP skills.
Let’s look at a sample inventory to understand the structure better:
Item ID | Name | Price | Quantity | Warranty (if applicable) |
---|---|---|---|---|
101 | Laptop | 899.99 | 10 | 24 months |
102 | Coffee Mug | 12.50 | 50 | N/A |
103 | Smartphone | 499.99 | 15 | 12 months |
Here are some key takeaways from building this system: - Encapsulation helps protect data integrity by controlling access to attributes. - Inheritance allows you to create specialized classes without duplicating code. - Polymorphism enables different item types to be managed uniformly.
To further enhance your project, consider these additions: - Implement data persistence using JSON or CSV files. - Add input validation to prevent errors. - Create a Report class to generate inventory summaries.
Keep coding and experimenting! This project is a great foundation for understanding how OOP can be applied to real-world problems.