This is a simple yet robust Inventory Management System implemented in C++ for managing items in an inventory. The program allows users to add items, sell items, and list all items in the inventory. It has been designed with modern C++ best practices to ensure memory safety, scalability, and ease of use.
- Add Items: Add new items to the inventory with their name, quantity, and price.
- Sell Items: Sell items by specifying their name and the quantity to be sold. The system ensures that the quantity does not exceed the current stock.
- Automatic Item Removal: When the quantity of an item reaches zero, it is automatically removed from the inventory.
- List Items: Display all items in the inventory with their details, including name, quantity, and price.
- Dynamic Storage: Uses
std::vectorfor dynamic resizing of the inventory to handle an arbitrary number of items efficiently. - Memory Safety: Implements
std::unique_ptrto manage items, ensuring no memory leaks and automatic cleanup of resources.
-
Data Structure Upgrade:
- Replaced the fixed-size raw array (
Item *items[20]) with astd::vector<std::unique_ptr<Item>>for improved flexibility, memory safety, and modern C++ standards compliance. - This allows the inventory to grow or shrink dynamically as items are added or removed.
- Replaced the fixed-size raw array (
-
Item Removal Logic:
- Enhanced the
remove_item()function to fully remove items from the inventory when their quantity reaches zero. This prevents unnecessary clutter and improves system efficiency.
- Enhanced the
-
Code Cleanup:
- Improved the readability and maintainability of the code by removing redundant variables, simplifying logic, and adding meaningful comments.
-
Modern C++ Practices:
- Leveraged
std::unique_ptrfor automatic memory management, eliminating the need for explicit deletion and reducing the risk of memory leaks. - Used
std::vectorfor dynamic resizing and bounds checking, replacing manual array size tracking.
- Leveraged
-
Add New Item:
- Enter the item's name, quantity, and price.
- The system stores the item dynamically in the inventory.
-
Sell Item:
- Enter the name of the item to sell.
- Specify the quantity to sell. If the quantity after selling becomes zero, the item is automatically removed from the inventory.
-
List Items:
- Display all the items in the inventory along with their details.
-
Exit:
- Exit the program.
Using std::unique_ptr ensures that item objects are automatically deallocated when removed from the inventory or when the program exits:
std::vector<std::unique_ptr<Item>> items;std::vector provides the flexibility to handle an arbitrary number of items:
items.push_back(std::make_unique<Item>(name, quantity, price));Automatically removes items with zero quantity to maintain a clean and accurate inventory:
if (item.get_quantity() == 0) {
items.erase(items.begin() + item_index);
}- Clone this repository:
git clone https://github.com/your-username/inventory-management-system.git
- Navigate to the project directory:
cd inventory-management-system - Compile the program using a C++ compiler (e.g.,
g++):g++ -o inventory main.cpp
- Run the program:
./inventory
- Case-Insensitive Matching: Improve user experience by implementing case-insensitive string matching for item names.
- Input Validation: Add robust validation for numeric inputs to handle invalid data gracefully.
- Persistent Storage: Enable saving and loading the inventory data to/from a file for data persistence.
Contributions, bug reports, and feature suggestions are welcome! Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as per the license terms.
Thank you for checking out this project! If you find it useful, feel free to star the repository ⭐ on GitHub.