Build a Simple Inventory Management System in C++
Create a console-based inventory management mini-project using C++. Implement features to add, update, delete, and display items stored in an inventory using vectors and classes.
Challenge prompt
Design a simple inventory management system in C++. You will create an Item class representing an inventory item, including item id, name, quantity, and price. Implement an Inventory class that stores multiple items and supports the following operations: 1. Add a new item to the inventory. 2. Update the quantity or price of an existing item by item id. 3. Remove an item from the inventory by item id. 4. Display all items currently in the inventory. Your program should handle invalid input gracefully and prevent duplicate item ids.
Guidance
- • Use a vector<Item> to store the inventory items inside the Inventory class.
- • Implement member functions in Inventory to perform add, update, delete, and display operations.
- • Use loops and conditionals to search items by id and check for duplicates.
Hints
- • Use std::find_if with a lambda or a simple for loop to look for items by id.
- • Remember to check if an item id already exists before adding a new item.
- • Keep your Item class members private and use public getter and setter functions.
Starter code
class Item {
private:
int id;
std::string name;
int quantity;
double price;
public:
Item(int id, const std::string& name, int quantity, double price);
int getId() const;
void setQuantity(int quantity);
void setPrice(double price);
void display() const;
};
class Inventory {
private:
std::vector<Item> items;
public:
void addItem(const Item& item);
void updateItem(int id, int quantity, double price);
void removeItem(int id);
void displayItems() const;
};Expected output
After adding, updating, deleting items, displaying items should print a list such as: Item ID: 101, Name: Pencil, Quantity: 50, Price: 0.50 Item ID: 102, Name: Notebook, Quantity: 20, Price: 2.00
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.