An inventory management system is a cornerstone in the domain of logistics and supply chain management. Python’s object-oriented programming capabilities make it an ideal choice for designing such systems. This guide will walk you through the process of creating Python classes for a basic inventory management system.
Understanding object-oriented design in Python
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects” which can contain data and code: data in the form of fields (often known as attributes), and code, in the form of procedures (often known as methods).
Ensure that Python is installed on your system. This implementation doesn’t require any external libraries.
Designing the classes
The inventory management system will consist of two main classes: Item
and Inventory
.
The item class:
This class represents items in the inventory.
class Item:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
def __str__(self):
return f"Item({self.name}, {self.price}, {self.quantity})"
The inventory class:
This class represents the entire inventory
class Inventory:
def __init__(self):
self.items = []
def add_item(self, item):
self.items.append(item)
def total_value(self):
return sum(item.price * item.quantity for item in self.items)
def __str__(self):
return '\n'.join(str(item) for item in self.items)
To test the system, create instances of Item
and add them to an Inventory
instance:
# Test data
inventory = Inventory()
inventory.add_item(Item("Apple", 0.50, 50))
inventory.add_item(Item("Banana", 0.20, 100))
inventory.add_item(Item("Orange", 0.30, 75))
# Display inventory
print(inventory)
# Display total value
print("Total Inventory Value: $", inventory.total_value())