forked from jilmaji247/Inventory_Management_System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
322 lines (290 loc) · 9.79 KB
/
main.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
using namespace std;
class Product
{
private:
int id;
string name;
string category;
double price;
int quantity;
public:
Product(int id, string name, string category, double price, int quantity)
{
this->id=id;
this->name=name;
this->category=category;
this->price=price;
this->quantity=quantity;
}
int getId() const{
return id;
}
void setId(int id){
this->id=id;
}
string getName() const{
return name;
}
void setName(string name){
this->name=name;
}
string getCategory() const{
return category;
}
void setCategory(string category){
this->category=category;
}
double getPrice() const{
return price;
}
void setPrice(double price){
this->price=price;
}
int getQuantity() const{
return quantity;
}
void setQuantity(int quantity){
this->quantity=quantity;
}
};
class Inventory
{
private:
vector<Product> products;
public:
void addProduct(Product product)
{
bool found = false;
for (auto& p : products)
{
if (p.getId() == product.getId())
{
cout << "Id already Exist."<< endl;
found = true;
break;
}
}
if (!found)
{
products.push_back(product);
cout << "Product added successfully." << endl;
cout << "-----------------------------------------------------------" <<endl;
}
}
void removeProduct(int id)
{
bool found = false;
for (auto i = products.begin(); i != products.end(); i++)
{
if (i->getId() == id)
{
products.erase(i);
found = true;
cout << "Product removed successfully." << endl;
cout << "-----------------------------------------------------------" <<endl;
break;
}
}
if (!found)
{
cout << "Id does not exist" << endl;
}
}
Product* findProduct(int id)
{
for(auto i=products.begin();i!=products.end();i++)
{
if(i->getId()==id)
{
return &(*i);
}
}
return nullptr;
}
void updateProduct(int id, string name, string category, double price, int quantity)
{
bool found = false;
for (auto i = products.begin(); i != products.end(); i++)
{
if (i->getId() == id)
{
i->setName(name);
i->setCategory(category);
i->setPrice(price);
i->setQuantity(quantity);
found = true;
break;
}
}
if (!found)
{
cout << "ID does not exist." << endl;
}
}
void printProduct() const{
for(auto i=products.begin();i!=products.end();i++)
{
cout<<"ID : "<<i->getId()<<endl;
cout<<"Name : "<<i->getName()<<endl;
cout<<"Category : "<<i->getCategory()<<endl;
cout<<"Price : $"<<i->getPrice()<<endl;
cout<<"Quantity : "<<i->getQuantity()<<endl;
}
}
void saveInventoryToFile(string filename)
{
ofstream file;
file.open(filename, ios::out | ios::app);
for (int i = 0; i < products.size(); i++)
{
Product p = products[i];
file << p.getId() << "," << p.getName() << "," << p.getCategory() << "," << p.getPrice() << "," << p.getQuantity() << endl;
}
file.close();
}
void loadInventoryFromFile(string filename)
{
ifstream file;
file.open(filename);
if (file.is_open())
{
string line;
while (getline(file, line))
{
stringstream ss(line);
string idStr, name, category, priceStr, quantityStr;
getline(ss, idStr, ',');
getline(ss, name, ',');
getline(ss, category, ',');
getline(ss, priceStr, ',');
getline(ss, quantityStr, ',');
int id = stoi(idStr);
double price = stod(priceStr);
int quantity = stoi(quantityStr);
Product p(id, name, category, price, quantity);
products.push_back(p);
}
file.close();
} else {
cout << "Error: Could not open file " << filename << endl;
}
}
};
int main() {
Inventory inventory;
cout << "-----------------------------------------------------------" <<endl;
cout << "---------------Inventory Management System ----------------" <<endl;
cout << "-----------------------------------------------------------" <<endl;
cout << "------------------------- Welcome! ------------------------" <<endl;
cout << "-----------------------------------------------------------" <<endl;
char choice;
do {
cout << "Please choose an option:" << endl;
cout << "1. Add a product" << endl;
cout << "2. Remove a product" << endl;
cout << "3. Find a product" << endl;
cout << "4. Update a product" << endl;
cout << "5. View all products" << endl;
cout << "6. Save inventory to file" << endl;
cout << "7. Load Inventory from file" << endl;
cout << "Q. Quit" << endl;
cin >> choice;
switch (choice) {
case '1': {
int id;
string name, category;
double price;
int quantity;
cout << "Enter ID: ";
cin >> id;
cout << "Enter product name: ";
cin >> name;
cout << "Enter product category: ";
cin >> category;
cout << "Enter product price: $ ";
cin >> price;
cout << "Enter product quantity: ";
cin >> quantity;
Product product(id, name, category, price, quantity);
inventory.addProduct(product);
break;
}
case '2': {
int id;
cout << "Enter product id: ";
cin >> id;
inventory.removeProduct(id);
break;
}
case '3': {
int id;
cout << "Enter product id: ";
cin >> id;
Product* product = inventory.findProduct(id);
if (product) {
cout << "Name: " << product->getName() << endl;
cout << "Category: " << product->getCategory() << endl;
cout << "Price: $ " << product->getPrice() << endl;
cout << "Quantity: " << product->getQuantity() << endl;
cout << "-----------------------------------------------------------" <<endl;
}
else {
cout << "Product not found." << endl;
cout << "-----------------------------------------------------------" <<endl;
}
break;
}
case '4': {
int id;
string name, category;
double price;
int quantity;
cout << "Enter the product id: ";
cin >> id;
cout << "Enter new product name: ";
cin >> name;
cout << "Enter new product category: ";
cin >> category;
cout << "Enter new product price: $ ";
cin >> price;
cout << "Enter new product quantity: ";
cin >> quantity;
inventory.updateProduct(id, name, category, price, quantity);
cout << "Product updated successfully." << endl;
cout << "-----------------------------------------------------------" <<endl;
break;
}
case '5': {
inventory.printProduct();
break;
}
case '6': {
inventory.saveInventoryToFile("inventory.csv");
cout << "Inventory saved to file." << endl;
cout << "-----------------------------------------------------------" <<endl;
break;
}
case '7': {
inventory.loadInventoryFromFile("inventory.csv");
cout << "Inventory loaded from file." << endl;
cout << "-----------------------------------------------------------" <<endl;
break;
}
case 'q':
case 'Q':
cout << "Goodbye!" << endl;
cout << "-----------------------------------------------------------" <<endl;
return 0;
default:
cout << "Invalid Choice. Please Try again" << endl;
cout << "-----------------------------------------------------------" <<endl;
break;
}
} while (true);
return 0;
}