-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryManager.cpp
More file actions
198 lines (177 loc) · 7.29 KB
/
InventoryManager.cpp
File metadata and controls
198 lines (177 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "InventoryManager.h"
#include <iostream> // For std::cout, std::cin
#include <limits> // For std::numeric_limits
#include <iomanip> // For std::fixed, std::setprecision
// Constructor
InventoryManager::InventoryManager() {
// Optionally initialize with some dummy data for testing
// inventory.emplace_back("Laptop", 10, 15000.00);
// inventory.emplace_back("Mouse", 50, 250.00);
// inventory.emplace_back("Keyboard", 20, 750.00);
}
// Helper function to find an item by name
int InventoryManager::findItemIndex(const std::string& itemName) const {
for (size_t i = 0; i < inventory.size(); ++i) {
if (inventory[i].getName() == itemName) {
return static_cast<int>(i); // Found
}
}
return -1; // Not found
}
// Helper function for robust integer input
int InventoryManager::getIntInput(const std::string& prompt) {
int value;
while (true) {
std::cout << prompt;
std::cin >> value;
if (std::cin.fail() || value < 0) { // Check for fail state or negative input
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
std::cout << "Invalid input. Please enter a non-negative whole number.\n";
} else {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard remaining newline
return value;
}
}
}
// Helper function for robust double input
double InventoryManager::getDoubleInput(const std::string& prompt) {
double value;
while (true) {
std::cout << prompt;
std::cin >> value;
if (std::cin.fail() || value < 0.0) { // Check for fail state or negative input
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
std::cout << "Invalid input. Please enter a non-negative number.\n";
} else {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard remaining newline
return value;
}
}
}
void InventoryManager::addItem() {
std::string name;
int quantity;
double price;
std::cout << "\n--- Add New Item ---" << std::endl;
std::cout << "Enter item name: ";
std::getline(std::cin, name); // Use getline to read names with spaces
// Check if item already exists
if (findItemIndex(name) != -1) {
std::cout << "Item with name '" << name << "' already exists. Use 'Update Item Quantity' to change it.\n";
return;
}
quantity = getIntInput("Enter quantity: ");
price = getDoubleInput("Enter price per unit: R");
inventory.emplace_back(name, quantity, price);
std::cout << "Item '" << name << "' added successfully!\n";
}
void InventoryManager::viewInventory() const {
std::cout << "\n--- Current Inventory ---" << std::endl;
if (inventory.empty()) {
std::cout << "Inventory is empty." << std::endl;
return;
}
// Print table header
std::cout << std::left << std::setw(20) << "Name"
<< std::right << std::setw(10) << "Quantity"
<< std::right << std::setw(15) << "Price (R)"
<< std::right << std::setw(15) << "Total (R)" << std::endl;
std::cout << std::string(60, '-') << std::endl;
for (const auto& item : inventory) {
std::cout << std::left << std::setw(20) << item.getName()
<< std::right << std::setw(10) << item.getQuantity()
<< std::right << std::setw(15) << std::fixed << std::setprecision(2) << item.getPrice()
<< std::right << std::setw(15) << std::fixed << std::setprecision(2) << (item.getQuantity() * item.getPrice()) << std::endl;
}
std::cout << std::string(60, '-') << std::endl;
std::cout << "Total Inventory Value: R" << std::fixed << std::setprecision(2) << calculateTotalValue() << std::endl;
}
void InventoryManager::updateItemQuantity() {
std::string name;
int newQuantity;
std::cout << "\n--- Update Item Quantity ---" << std::endl;
if (inventory.empty()) {
std::cout << "Inventory is empty. Nothing to update." << std::endl;
return;
}
std::cout << "Enter name of item to update: ";
std::getline(std::cin, name);
int index = findItemIndex(name);
if (index != -1) {
std::cout << "Current quantity for '" << name << "': " << inventory[index].getQuantity() << std::endl;
newQuantity = getIntInput("Enter new quantity: ");
inventory[index].setQuantity(newQuantity);
std::cout << "Quantity for '" << name << "' updated successfully!\n";
} else {
std::cout << "Item '" << name << "' not found in inventory.\n";
}
}
void InventoryManager::removeItem() {
std::string name;
std::cout << "\n--- Remove Item ---" << std::endl;
if (inventory.empty()) {
std::cout << "Inventory is empty. Nothing to remove." << std::endl;
return;
}
std::cout << "Enter name of item to remove: ";
std::getline(std::cin, name);
int index = findItemIndex(name);
if (index != -1) {
inventory.erase(inventory.begin() + index);
std::cout << "Item '" << name << "' removed successfully!\n";
} else {
std::cout << "Item '" << name << "' not found in inventory.\n";
}
}
double InventoryManager::calculateTotalValue() const {
double totalValue = 0.0;
for (const auto& item : inventory) {
totalValue += (item.getQuantity() * item.getPrice());
}
return totalValue;
}
void InventoryManager::displayMenu() const {
std::cout << "\n--- Inventory System Menu ---" << std::endl;
std::cout << "1. Add Item" << std::endl;
std::cout << "2. View Inventory" << std::endl;
std::cout << "3. Update Item Quantity" << std::endl;
std::cout << "4. Remove Item" << std::endl;
std::cout << "5. Calculate Total Inventory Value" << std::endl;
std::cout << "6. Exit" << std::endl;
std::cout << "Enter your choice: ";
}
void InventoryManager::run() {
int choice;
do {
displayMenu();
choice = getIntInput(""); // Use getIntInput for menu choice
switch (choice) {
case 1:
addItem();
break;
case 2:
viewInventory();
break;
case 3:
updateItemQuantity();
break;
case 4:
removeItem();
break;
case 5:
std::cout << "\nTotal Inventory Value: R" << std::fixed << std::setprecision(2) << calculateTotalValue() << std::endl;
break;
case 6:
std::cout << "Exiting Inventory System. Goodbye!\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
break;
}
std::cout << "\nPress Enter to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Consume any leftover newline
std::cin.get(); // Wait for user to press Enter
} while (choice != 6);
}