COMPUTER PROGRAMMING PROJECT EXPLANATION!
•TITLE:
Inventory Management System
• DESCRIPTION:
Create an application to manage inventory for a small business. This
includes adding new items, updating stock quantities, viewing inventory status, and
generating reports.
Group-6 members' name and ID
1. Aisha Adurehman - UGR101072/17
2. Debora Siyum - UGR100469/17
3. Yemisrach Eyasu - UGR99466/17
4. Zelalem Ayele - UGR100799/17
5.Marshet Memhru - UGR99239/17
Here's a detailed explanation of the Inventory Management System C++ code, broken down into its main
components and how they work together.
1. Included Libraries
#include <iostream> // For input/output (cin, cout)
#include <string> // For using std::string
#include <map> // For std::map (key-value container)
#include <iomanip> // For output formatting (setw, fixed, setprecision)
#include <limits> // For clearing input buffer (numeric_limits)
- These libraries provide essential tools:
- iostream for console input/output.
- string for handling text data.
- map to store items as key-value pairs (item ID Item).
- iomanip to format output neatly.
- limits to help with input error handling.
---
2. Item Structure
struct Item {
int id;
std::string name;
int quantity;
double price;
Item(int id_val, std::string name_val, int quantity_val, double price_val)
: id(id_val), name(name_val), quantity(quantity_val), price(price_val) {}
};
- Purpose: Represents a single inventory item.
- Members:
- id: Unique identifier.
- name: Name of the item.
- quantity: How many units are in stock.
- price: Price per unit.
- Constructor: Initializes an Item with given values, allowing easy creation.
---
3. InventoryManager Class
This class manages the entire inventory system.
---
a) Private Members
std::map<int, Item> inventory;
int nextItemId;
- inventory: Stores all items, key is id, value is Item.
- nextItemId: Keeps track of the next unique ID to assign.
---
b) Helper Function
void clearInputBuffer() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
- Clears leftover input (like newline characters) after reading input to prevent input errors, especially when
mixing cin and getline.
---
c) Constructor
InventoryManager() : nextItemId(1) {}
- Initializes nextItemId to 1, so the first item added gets ID 1.
---
4. Adding a New Item
void addItem(const std::string& name, int quantity, double price) {
if (name.empty()) { ... }
if (quantity < 0) { ... }
if (price < 0) { ... }
inventory.emplace(nextItemId, Item(nextItemId, name, quantity, price));
std::cout << "Item '" << name << "' (ID: " << nextItemId << ") added successfully.\n";
nextItemId++;
}
- Validations: Checks for empty name, negative quantity or price.
- Insertion: Uses emplace to efficiently add a new Item with a unique ID.
- Feedback: Confirms addition to the user.
- ID Update: Increments nextItemId for the next item.
---
5. Updating Stock Quantity
void updateStock(int itemId, int newQuantity) {
auto it = inventory.find(itemId);
if (it == inventory.end()) { ... }
if (newQuantity < 0) { ... }
it->second.quantity = newQuantity;
std::cout << "Stock for '" << it->second.name << "' (ID: " << itemId << ") updated to " << newQuantity << ".\n";
}
- Searches for item by itemId.
- Validates existence and non-negative quantity.
- Updates quantity if valid.
- Informs the user about the update.
---
6. Viewing Inventory Status
void viewInventoryStatus() const {
if (inventory.empty()) { ... }
std::cout << "\n--- Current Inventory Status ---\n";
std::cout << std::left << std::setw(5) << "ID" << " | " << std::setw(20) << "Name" << " | " << std::setw(10) <<
"Quantity" << " | " << std::setw(10) << "Price" << " | " << std::setw(15) << "Total Value\n";
std::cout << std::string(67, '-') << "\n";
for (const auto& pair : inventory) {
const Item& item = pair.second;
double totalValue = item.quantity * item.price;
std::cout << std::left << std::setw(5) << item.id << " | "
<< std::setw(20) << item.name << " | "
<< std::setw(10) << item.quantity << " | "
<< std::fixed << std::setprecision(2) << std::setw(10) << item.price << " | "
<< std::fixed << std::setprecision(2) << std::setw(15) << totalValue << "\n";
}
std::cout << std::string(67, '-') << "\n\n";
}
- Checks if inventory is empty.
- Prints a formatted table with:
- ID, Name, Quantity, Price, and Total Value (quantity × price).
- Uses formatting for alignment and decimal precision.
---
7. Generating Inventory Report
void generateReport() const {
if (inventory.empty()) { ... }
int totalUniqueItems = inventory.size();
int totalQuantityInStock = 0;
double totalInventoryValue = 0.0;
for (const auto& pair : inventory) {
const Item& item = pair.second;
totalQuantityInStock += item.quantity;
totalInventoryValue += item.quantity * item.price;
}
std::cout << "\n--- Inventory Summary Report ---\n";
std::cout << "Total Unique Items: " << totalUniqueItems << "\n";
std::cout << "Total Quantity in Stock: " << totalQuantityInStock << "\n";
std::cout << "Total Inventory Value: $" << std::fixed << std::setprecision(2) << totalInventoryValue << "\n";
std::cout << "-------------------------------\n\n";
}
- Summarizes:
- Number of unique items.
- Total quantity of all items.
- Total value of all inventory.
- Prints a neat summary report.
---
8. Interactive Menu Loop
void runMenu() {
int choice;
std::string name;
int quantity;
double price;
int itemId;
while (true) {
std::cout << "--- Inventory Management System ---\n";
std::cout << "1. Add New Item\n2. Update Stock Quantity\n3. View Inventory Status\n4. Generate Inventory
Report\n5. Exit\n";
std::cout << "Enter your choice (1-5): ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a number.\n";
std::cin.clear();
clearInputBuffer();
continue;
}
clearInputBuffer();
switch (choice) {
case 1:
std::cout << "Enter item name: ";
std::getline(std::cin, name);
std::cout << "Enter quantity: ";
std::cin >> quantity;
std::cout << "Enter price per unit: ";
std::cin >> price;
if (std::cin.fail()) {
std::cout << "Invalid input for quantity or price. Please enter numbers.\n";
std::cin.clear();
clearInputBuffer();
break;
}
clearInputBuffer();
addItem(name, quantity, price);
break;
case 2:
std::cout << "Enter item ID to update: ";
std::cin >> itemId;
std::cout << "Enter new quantity: ";
std::cin >> quantity;
if (std::cin.fail()) {
std::cout << "Invalid input for item ID or quantity. Please enter numbers.\n";
std::cin.clear();
clearInputBuffer();
break;
}
clearInputBuffer();
updateStock(itemId, quantity);
break;
case 3:
viewInventoryStatus();
break;
case 4:
generateReport();
break;
case 5:
std::cout << "Exiting Inventory Management System. Goodbye!\n";
return;
default:
std::cout << "Invalid choice. Please enter a number between 1 and 5.\n";
break;
}
std::cout << "\n";
}
}
- Displays options repeatedly until user chooses to exit.
- Handles user input with validation to avoid crashes on invalid input.
- Calls corresponding functions based on user choice.
- Uses getline for item names to allow spaces.
- clearInputBuffer() is used to clean the input stream after reading numbers to avoid input issues.
---
9. Program Entry Point
int main() {
InventoryManager manager;
manager.runMenu();
return 0;
}
- Creates an instance of InventoryManager.
- Starts the interactive menu loop.
- Program runs until user chooses to exit.
---
Summary
- The program manages an inventory with unique IDs.
- Supports adding items, updating stock, viewing inventory, and generating reports.
- Uses standard C++ features: classes, maps, input/output, and error handling.
- User interacts via a console menu with input validation for robustness.