Inventory Management System

Inventory Management System (IMS)

Requirements

  • Python 3.x
  • Basic knowledge of Python

Code

class Item:
    def __init__(self, name, quantity, price):
        self.name = name
        self.quantity = quantity
        self.price = price

    def __str__(self):
        return f"Item(name={self.name}, quantity={self.quantity}, price={self.price})"


class Inventory:
    def __init__(self):
        self.items = {}

    def add_item(self, name, quantity, price):
        if name in self.items:
            self.items[name].quantity += quantity
        else:
            self.items[name] = Item(name, quantity, price)
        print(f"Added {quantity} of {name}.")

    def update_item(self, name, quantity=None, price=None):
        if name in self.items:
            if quantity is not None:
                self.items[name].quantity = quantity
            if price is not None:
                self.items[name].price = price
            print(f"Updated {name}.")
        else:
            print(f"Item {name} not found.")

    def delete_item(self, name):
        if name in self.items:
            del self.items[name]
            print(f"Deleted {name}.")
        else:
            print(f"Item {name} not found.")

    def view_inventory(self):
        if not self.items:
            print("Inventory is empty.")
        else:
            for item in self.items.values():
                print(item)

def main():
    inventory = Inventory()

    while True:
        print("\nInventory Management System")
        print("1. Add Item")
        print("2. Update Item")
        print("3. Delete Item")
        print("4. View Inventory")
        print("5. Exit")

        choice = input("Choose an option: ")

        if choice == '1':
            name = input("Enter item name: ")
            quantity = int(input("Enter quantity: "))
            price = float(input("Enter price: "))
            inventory.add_item(name, quantity, price)
        elif choice == '2':
            name = input("Enter item name: ")
            quantity = input("Enter new quantity (leave blank to skip): ")
            price = input("Enter new price (leave blank to skip): ")
            inventory.update_item(name, quantity=int(quantity) if quantity else None,
                                  price=float(price) if price else None)
        elif choice == '3':
            name = input("Enter item name: ")
            inventory.delete_item(name)
        elif choice == '4':
            inventory.view_inventory()
        elif choice == '5':
            print("Exiting...")
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

How It Works

  1. Classes:
    • Item: Represents an individual item with attributes for name, quantity, and price.
    • Inventory: Manages a collection of items, allowing you to add, update, delete, and view items.
  2. Functions:
    • add_item(): Adds a new item or updates the quantity if it already exists.
    • update_item(): Updates the quantity and/or price of an existing item.
    • delete_item(): Deletes an item from the inventory.
    • view_inventory(): Displays all items in the inventory.
  3. Main Loop: The main() function provides a simple console menu for user interaction.

Running the Code

  1. Save the code to a file named inventory_management.py.
  2. Run the script using Python:
python inventory_management.py

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *