-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_manager.cpp
288 lines (231 loc) · 6.8 KB
/
inventory_manager.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
#include "src/item_class.h"
#include "src/save_load.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <list>
using namespace std; // Using the standard namespace
//This function will get an item from an inventory, by being given the name of the item. it will then return the item
const Item* GetItemByName(const vector<Item>& items, const string& name) {
auto it = lower_bound(items.begin(), items.end(), name, [](const Item& item, const string& other) {
return item.getName() < other;
});
if (it != items.end() && it->getName() == name) {
return &*it;
}
return nullptr;
}
vector<Item> LoadScreen(vector<Item> items, string saveFilePath) {
int choice = -1;
while (choice != 0) {
cout << "--------------------------------------\n";
cout << "What would you like to do?\n";
cout << "--------------------------------------\n";
cout << "1. New Inventory\n";
cout << "2. Load Inventory\n";
cout << "3. Settings\n";
cout << "0. Exit\n";
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch (choice) {
case 0:
cout << "Exiting.\n";
return items;
case 1:
items.emplace_back("Sample Sign", "A sample sign, simple but effective", 1, "images/sample.jpg");
return items;
case 2:
loadItemsFromFile(items, saveFilePath);
return items;
case 3:
int subchoice;
while (true) {
cout << "--------------------------------------\n";
cout << "Setting: What would you like to change?\n";
cout << "--------------------------------------\n";
cout << "1. Export CSV seperator\n";
cout << "\t- Default = (,)\n";
cout << "0. Back\n";
cin >> subchoice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string newseperator;
if (subchoice == 1) {
char seperator = loadSettings().front()[0];
cout << "Current seperator = " << seperator << "\n";
cout << "New seperator: ";
cin >> newseperator;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (newseperator == "\"" || newseperator == "\'") {
cout << "Invalid seperator. Try again.\n";
}
else {
seperator = newseperator[0];
SaveSettings(list<string>{newseperator});
break;
}
}
else if (subchoice == 0) {
cout << "Going back.\n";
break;
}
else {
cout << "Invalid choice. Please try again.\n";
}
}
break;
default:
cout << "Invalid choice. Try again.\n";
break;
}
}
//This return should never be used, just here to prevent warnings
return items;
}
// Main function
int main() {
char seperator = loadSettings().front()[0];
// Save file path
const string saveFilePath = "save/inventory.csv";
vector<Item> items;
items = LoadScreen(items, saveFilePath);
if (items.empty()) {
return 0;
}
while (true) {
// Displays item list
if (items.empty()) {
cout << "No items to display.\n";
} else {
for (const auto &item : items) {
item.display();
}
}
cout << "--------------------------------------\n";
cout << "What would you like to do?\n";
cout << "--------------------------------------\n";
cout << "1. Interact with an item\n";
cout << "2. Add an item\n";
cout << "3. Save inventory\n";
cout << "4. Export inventory\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
int choice;
cin >> choice;
//handle invalid input
if (cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, please enter a number.\n";
continue;
}
if (choice == 1)
{
string item_name;
Item* current_item;
cout << "Name of item: ";
cin.ignore(); // Clear leftover newline
getline(cin, item_name); // Safely read the entire line
item_name = toLowerCase(item_name);
auto it = find_if(items.begin(), items.end(), [&item_name](const Item& item) {
return item.getName() == item_name;
});
if (it != items.end()) {
current_item = &*it;
while (true) {
cout << "--------------------------------------\n";
current_item->display();
cout << "What would you like to do with this item?\n";
cout << "--------------------------------------\n";
cout << "1. Change name\n";
cout << "2. Change description\n";
cout << "3. Change image path\n";
cout << "4. Increment amount\n";
cout << "5. Decrement amount\n";
cout << "6. Delete item\n";
cout << "0. Back\n";
int subchoice;
cin >> subchoice;
string input;
if (subchoice == 1) {
cout << "Current name: " << current_item->getName() << endl;
cout << "New name: ";
cin.ignore();
getline(cin, input);
current_item->setName(input);
} else if (subchoice == 2) {
cout << "Current description: " << current_item->getDescription() << endl;
cout << "New description: ";
cin.ignore();
getline(cin, input);
current_item->setDescription(input);
} else if (subchoice == 3) {
cout << "Current image path: " << current_item->getImage() << endl;
cout << "New image path: ";
cin.ignore();
getline(cin, input);
current_item->setImage(input);
} else if (subchoice == 4) {
cout << "Current count: " << current_item->getAmount() << endl;
current_item->increment();
} else if (subchoice == 5) {
cout << "Current count: " << current_item->getAmount() << endl;
current_item->decrement();
} else if (subchoice == 6) {
auto it = find(items.begin(), items.end(), *current_item);
items.erase(it);
cout << "Item deleted.\n";
break;
} else if (subchoice == 0) {
cout << "Going back.\n";
break;
} else {
cout << "Invalid choice. Please try again.\n";
}
}
}
else {
cout << "Item not found.\n";
}
}
// Adds an item
else if (choice == 2) {
string name, description, image;
int amount;
cout << "Enter item name: ";
cin.ignore();
getline(cin, name);
cout << "Enter item description: ";
cin.ignore();
getline(cin, description);
cout << "Enter item amount: ";
cin >> amount;
cout << "Enter item image path: ";
cin.ignore();
getline(cin, image);
items.emplace_back(name, description, amount, image);
cout << "Item added successfully!\n";
}
// Saves the items to file
else if (choice == 3) {
saveItemsToFile(items, saveFilePath);
}
else if (choice == 4) {
cout << "Name of export file: ";
cin.ignore();
string exportname;
getline(cin, exportname);
saveItemsToFile(items, "export/" + exportname + ".csv", seperator);
cout << "Items saved successfully exported to export/" << exportname << ".csv\n";
}
// Exits the program
else if (choice == 0) {
cout << "Exiting program. Goodbye!\n";
break;
}
// Invalid choice
else {
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}