Managing inventory is a crucial task for businesses, whether you’re running a retail shop or a warehouse. In this post, we’ll walk through the architecture and functionality of a Simple Inventory Management System built in C++. This system demonstrates practical use of object-oriented programming, file I/O, and user interaction — all within the command-line interface.

🛠️ Project Overview

This console-based inventory system supports the following:

  • Adding, editing, deleting, and viewing items.
  • Searching by name, quantity range, price range, or item details.
  • Saving and loading inventory to/from a CSV file.
  • Modular, reusable design using object-oriented principles.

The system is split into logical modules:

  • Item, Inventory, and InventoryDisplay for core functionality.
  • ItemCsvFileManager for file handling.
  • UserInput for robust user input with type-safety and validation.

📦 Core Components

1. Item Class (Not shown here but assumed)

Each inventory entry is an Item with:

  • name
  • price
  • quantity
  • details

2. Inventory Class

This class holds and manages a list of Item objects.

🔧 Key Functions:

  • addItem, deleteItem, getItem
  • Setters/Getters for each field
  • Search functions by name, quantity, price, or details (e.g., keyword in description)

💡 It uses a std::vector<Item> internally and searches using C++ lambdas, offering both flexibility and performance.

cppCopyEditstd::vector<Item> Inventory::search(std::function<bool(Item&)> condition) {
    std::vector<Item> matchingItems;
    for (Item& item : this->items) {
        if (condition(item)) {
            matchingItems.push_back(item);
        }
    }
    return matchingItems;
}

3. InventoryDisplay Class

Acts as the UI controller, handling user interactions and displaying formatted output. It ties the Inventory to console operations.

cppCopyEditvoid InventoryDisplay::UIaddItem() {
    // Collects name, quantity, price, and details from user
    // Validates inputs
    // Adds new Item to inventory
}

It also includes a welcome() method that displays the main menu and various UI handlers for each command, making the experience interactive and user-friendly.


4. CSV File Handling with ItemCsvFileManager

The system supports importing and exporting data via CSV files.

cppCopyEditvoid ItemCsvFileManager::save(Inventory& inv, const std::string& fileName) const {
    std::ofstream output(fileName);
    for (const Item &item : inv.getItems()) {
        output << item.name << "," << item.price << "," 
               << item.quantity << "," << item.details << '\n';
    }
}

It handles:

  • Reading lines and parsing them into Item objects.
  • Saving inventory back into a .csv file on exit (optional).

🧑‍💻 Input Handling

To handle user input robustly, a set of templated utility functions are defined:

cppCopyEdittemplate <typename T>
std::optional<T> getField(const std::string& prompt) {
    std::cout << prompt;
    T value = getUserInput<T>();
    if (value == -1 || value == "-1") return std::nullopt;
    return value;
}

This allows graceful exits (user enters -1) and avoids repeated boilerplate for error-checking user input.


🚀 Running the Program

  • If a CSV file is passed as a command-line argument, it will load the inventory from it.
  • Otherwise, the user can start fresh.
  • Upon exiting, the user is prompted to save the data to a CSV file.
cppCopyEditif (argc == 2) {
    fileManager.open(argv[1]);
}

🧩 A Sample Session

plaintextCopyEdit===== Inventory Management System =====
0. Set Item Name
1. Set Item Details
...
13. Search Item by Details
-1. Exit
=======================================
Please select an option above: 8
Enter item name: Mouse
Enter item quantity: 20
Enter item price: 15.99
Enter item details: Wireless mouse with USB receiver
Operation Complete: Item addition.
Item id: 0

🔚 Conclusion

This Inventory Management System showcases real-world problem solving using C++ features like:

  • Lambda expressions for flexible searching
  • Optional types for safe user input
  • Stream handling for file I/O
  • Separation of concerns through modular class design

While simple in scope, it forms a solid foundation for building more complex inventory or resource management systems. You could extend it further with:

  • JSON support (using nlohmann/json)
  • GUI (using Qt or ImGui)
  • SQLite database backend

Code Highlights:

  • Organized OOP architecture
  • Clean separation between UI, logic, and persistence
  • Error handling and validation throughout

Whether you’re learning C++ or building a portfolio project, this is a practical and extensible base to grow from.



Leave a Reply

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