-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarehouse.cpp
461 lines (391 loc) · 9.94 KB
/
warehouse.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <iomanip>
/**
* TODO
* 1. add description header
* 2. fix add item x/y position check/add dont allow 0/0 (x, y > 0)
* 3. add warehouse positions representation print into file
* 4. add functions description
*/
using namespace std;
int state = 0;
struct Item {
string primary;
string secondary;
int x;
int y;
Item(string name = "", string desc = "", int x = -1, int y = -1) {
Item::primary = name;
Item::secondary = desc;
Item::x = x;
Item::y = y;
}
string get(int key = 0) {
if(key == 1) {
return Item::primary;
}
if(key == 2) {
return Item::secondary;
}
return "";
}
};
bool isInited = false;
int width;
int height;
int size;
vector<vector<int> > storage;
//delete this code for a release version
vector<Item> data = {
Item ("apples", "a box of fresh apples", 0, 0),
Item ("oranges", "picked by mama in the garden", 0, 1),
Item ("apples & oranges", "srsly why can't we compare those two fruits", 3, 6),
};
void showState();
void showMenu();
void addItem();
void findItem(int);
void listItems();
void deleteItem();
void writeData();
void readData();
void showStorage();
bool setDimensions();
bool getSizeLeft();
int main() {
isInited = setDimensions();
while(!isInited) {
isInited = setDimensions();
}
showState();
return 0;
}
/**
* basic state logic
*/
void showState() {
switch(state) {
case 9:
readData();
case 8:
cout << "Good by!";
exit(1);
case 7:
showStorage();
break;
case 6:
writeData();
break;
case 5:
listItems();
break;
case 4:
deleteItem();
break;
case 3:
findItem(1);
break;
case 2:
findItem(0);
break;
case 1:
addItem();
case 0:
default:
showMenu();
break;
}
}
/**
* shows menu
*/
void showMenu() {
cout << "1. Add Item\n";
cout << "2. Find by name\n";
cout << "3. Find by description\n";
cout << "4. Delete Item\n";
cout << "5. List Items\n";
cout << "6. Write data into file\n";
cout << "7. Show storage\n";
cout << "8. Exit\n";
cout << "9. Read file\n\n";
cout << "What will we do? ";
cin >> state;
cout << "\n\n";
showState();
}
/**
* sets initial dimensions of the storage
* @return
*/
bool setDimensions() {
cout << "Enter dimensions (width height): ";
cin >> width >> height;
if(width > 0 && height > 0) {
//fill the storage variable with empty slots
for(int row=0;row<height;row++) {
vector<int> rowArr;
for (int col=0;col<width; col++) {
rowArr.push_back(-1);
}
storage.push_back(rowArr);
}
//delete this code for a release version
storage[0][0] = 1;
storage[0][1] = 2;
storage[3][6] = 3;
return true;
}
return false;
}
/**
* displays storage state to the screen;
*/
void showStorage() {
cout << left << setw(5) << " ";
for (int col=0;col<width; col++) {
cout << "_";
}
cout << endl;
for(int row=0;row<height;row++) {
cout << left << setw(4) << (row+1) << "|";
for (int col=0;col<width; col++) {
if(storage[row][col] != -1) {
cout << storage[row][col];
} else {
cout << " ";
}
}
cout << "|\n";
}
cout << left << setw(5) << " ";
for (int col=0;col<width; col++) {
cout << "_";
}
cout << endl << endl;
state = 0;
showState();
}
/**
* deletes item
*/
void deleteItem() {
int index;
cout << "Enter delete index: ";
cin >> index;
//clear storage array
storage[data[index-1].x][data[index-1].y] = -1;
data.erase (data.begin()+(index-1));
state = 0;
showState();
}
/**
* gets newItem data from input, returns false if had errors
* @param newItem
* @return
*/
bool getItemProps(Item & newItem) {
int x, y;
string tempStr;
//if name||descrition were not specified(first time add)
if(newItem.primary == "") {
cout << "Enter name: ";
cin.ignore();
getline(cin, newItem.primary);
}
if(newItem.secondary == "") {
cout << "Enter description: ";
cin.ignore();
getline(cin, tempStr);
newItem.secondary = tempStr;
}
cout << "Enter x position: ";
cin >> x;
cout << "Enter y position: ";
cin >> y;
newItem.x = x-1;
newItem.y = y-1;
if(newItem.x < 0 || newItem.y < 0 || storage[newItem.x][newItem.y] != -1) {
return false;
}
return true;
}
/**
* upper level function of addItem, generates x, y, interacts with user;
*/
void addItem() {
Item newItem;
bool itemCreated = getItemProps(newItem);
//if the spot is taken then....
while(!itemCreated) {
char yn;
int isYes = -1;
//ask if user wants to auto generate new coordinates
do {
cout << "This position is taken, would you like to auto set position?(y/n)" << endl;
cin >> yn;
if(yn != 'y' || yn != 'Y') {
isYes = 1;
}
if(yn != 'n' || yn != 'N') {
isYes = 0;
}
} while (isYes < 0);
if(isYes > -1) {
//if he does
for(int row=0;row<width && !itemCreated;row++) {
for (int col=0;col<height && !itemCreated; col++) {
if(storage[row][col] == -1) {
newItem.x = row;
newItem.y = col;
itemCreated = true;
cout << "Generated position is: " << newItem.x+1 << ":" << newItem.y+1 << endl;
}
}
}
} else {
itemCreated = getItemProps(newItem);
}
}
//Insert item to data array
data.push_back(newItem);
//dont forget to update the dimensions array
storage[newItem.x][newItem.y] = (int) data.size();
cout << "Created element index is " << data.size() << endl << endl;
state = 0;
showState();
}
/**
* shows items on the screen
*/
void listItems() {
cout << "\n\n";
cout << left << setw(10)
<< "Index" << setw(20)
<< "Primary" << setw(60)
<< "Secondary" << setw(10)
<< "X" << setw(10)
<< "Y" << endl;
for (int i = 0; i < data.size(); i++) {
cout << left << setw(10)
<< i+1 << setw(20)
<< data[i].primary << setw(60)
<< data[i].secondary << setw(10)
<< data[i].x+1 << setw(10)
<< data[i].y+1 << endl;
}
cout << "\n\n";
state = 0;
showState();
}
/**
* serches for an item by name/desc, prints result into the screen
* @param key
*/
void findItem(int key) {
int index = key + 1;
//we need the original position of the element so we create additional structure for that
struct IR {
string primary;
string secondary;
int x;
int y;
int pos;
};
vector<IR> result;
string searchStr;
cout << "Enter search value: ";
cin >> searchStr;
for(int i=0;i<data.size();i++) {
string value = data[i].get(index);
if(value.find(searchStr) != -1) {
//i wonder id we could do this more efficient
IR _tmp = {
data[i].primary,
data[i].secondary,
data[i].x,
data[i].y,
i+1
};
result.push_back(_tmp);
}
}
cout << "\n\n";
cout << left << setw(10)
<< "Index" << setw(20)
<< "Primary" << setw(60)
<< "Secondary" << setw(10)
<< "X" << setw(10)
<< "Y" << endl;
for (int i = 0; i < result.size(); i++) {
cout << left << setw(10)
<< result[i].pos << setw(20)
<< result[i].primary << setw(60)
<< result[i].secondary << setw(10)
<< result[i].x+1 << setw(10)
<< result[i].y+1 << endl;
}
cout << "\n\n";
state = 0;
showState();
}
void readData() {
ifstream infile;
string fileName;
cout << "Enter file name: ";
cin >> fileName;
while (fileName.length() < 5 || fileName.substr(fileName.length()-4, 4) != ".txt") {
cout << "File should be in .txt: ";
cin >> fileName;
}
infile.open(fileName);
if (!infile.is_open()) {
cout << "Error. Couldn't create file " << fileName << endl;
state = 0;
showState();
}
infile.close();
state = 0;
showState();
}
/**
* write data into file
*/
void writeData() {
ofstream outfile;
string fileName;
cout << "Enter file name: ";
cin >> fileName;
while (fileName.length() < 5 || fileName.substr(fileName.length()-4, 4) != ".txt") {
cout << "File should be in .txt: ";
cin >> fileName;
}
outfile.open(fileName);
if (!outfile.is_open()) {
cout << "Error. Couldn't create file " << fileName << endl;
state = 0;
showState();
}
outfile << left << setw(10)
<< "Index" << setw(20)
<< "Primary" << setw(60)
<< "Secondary" << setw(10)
<< "X" << setw(10)
<< "Y" << endl;
for (int i = 0; i < data.size(); i++) {
outfile << left << setw(10)
<< i+1 << setw(20)
<< data[i].primary << setw(60)
<< data[i].secondary << setw(10)
<< data[i].x+1 << setw(10)
<< data[i].y+1 << endl;
}
outfile.close();
state = 0;
showState();
}