-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.cpp
729 lines (715 loc) · 22 KB
/
Project.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
using namespace std;
#include<iostream>
#include <math.h>
#include<ctime>
#include<time.h>
#include<fstream>
#include<string>
///size of the 3d array.
const int sizeOfShelf = 10;
///3d array that shows us which places are free.
bool mirrorTable[sizeOfShelf][sizeOfShelf][sizeOfShelf];
class Product
{
private:
string name;
int expDate;
time_t dateOfEntry;
string supplier;
string unit;
int amount;
unsigned int location;
string note;
public:
Product()
{
}
///constructor with note.
Product(string name, int expDate, time_t dateOfEntry, string supplier, string unit, int amount, string note)
{
this->name = name;
this->expDate = expDate;
this->dateOfEntry = dateOfEntry;
this->supplier = supplier;
this->unit = unit;
this->amount = amount;
this->note = note;
}
///constructor.
Product(string name, int expDate, time_t dateOfEntry, string supplier, string unit, int amount)
{
this->name = name;
this->expDate = expDate;
this->dateOfEntry = dateOfEntry;
this->supplier = supplier;
this->unit = unit;
this->amount = amount;
this->note = "No note set";
}
///setter for Location.
void setLocation(unsigned int location)
{
this->location=location;
}
///setter for Amount.
void setAmount(int amount)
{
this->amount = amount;
}
///setter for DateOfEntry.
void setDateOfEntry(time_t dateOfEntry)
{
this->dateOfEntry = dateOfEntry;
}
///getter for name.
string getName()
{
return name;
}
///getter for supplier.
string getSupplier()
{
return supplier;
}
///getter for unit.
string getUnit()
{
return unit;
}
///getter for amount.
unsigned int getAmount()
{
return amount;
}
///getter for expDate.
int getExpDate()
{
return expDate;
}
///getter for location.
int getlocation()
{
return location;
}
///Prints all the data for **this** Product.
void Print()
{
struct tm dateOfEntry;
dateOfEntry = *localtime(&(this->dateOfEntry));
cout<<"name:"<<name<<" expDate:"<<expDate<<" day:"<<dateOfEntry.tm_mday<<" supplier:"<<supplier<<" unit:"<<unit<<" amount:"<<amount<<" note:"<<note<<endl;
}
///calculates expDay from expDate.
int ExpDay()
{
return expDate%100;
}
///calculates expMonth from expDate.
int ExpMonth()
{
return (expDate/100)%100;
}
///calculates expYear from expDate.
int ExpYear()
{
return expDate/10000;
}
///checks if **this** Product is expired.
bool IsItExpired()
{
struct tm dateOfEntry;
dateOfEntry = *localtime(&(this->dateOfEntry));
if(ExpYear()>dateOfEntry.tm_year - 100)
{
return false;
}
if(ExpYear()==dateOfEntry.tm_year - 100&&ExpMonth()>dateOfEntry.tm_mon)
{
return false;
}
if(ExpYear()==dateOfEntry.tm_year - 100&&ExpMonth()==dateOfEntry.tm_mon&&ExpDay()>dateOfEntry.tm_mday)
{
return false;
}
else return true;
}
///calculates how many days are left until **this** Product expires.
int HowLongTillExp()
{
int daysLeft;
struct tm dateOfEntry;
dateOfEntry = *localtime(&(this->dateOfEntry));
daysLeft = (ExpYear() - dateOfEntry.tm_year - 100)*365;
daysLeft = daysLeft + (ExpMonth() - dateOfEntry.tm_mon)*30;
daysLeft = daysLeft + (ExpDay() - dateOfEntry.tm_mday);
return daysLeft;
}
///Logs the adding of **this** Product(from the storage) onto the log file.
void logAddingThisItem()
{
struct tm dateOfLog;
dateOfLog = *localtime(&(this->dateOfEntry));
ofstream log;
log.open("log.txt",fstream::app);
if (log.is_open())
{
log<<dateOfLog.tm_mday<<" "<<dateOfLog.tm_mon<<" "<<dateOfLog.tm_year - 100<<" Adding ";
log<<name<<" "<<expDate<<" "<<supplier<<" "<<unit<<" "<<amount<<" "<<location<<endl;
log.close();
}
}
///Logs adding more amount of **this** Product(from the storage) onto the log file.
void logAddingThisItem(int amount)
{
//Adding to the log file (just the new part)
struct tm dateOfLog;
dateOfLog = *localtime(&(this->dateOfEntry));
ofstream log;
log.open("log.txt",fstream::app);
if (log.is_open())
{
log<<dateOfLog.tm_mday<<" "<<dateOfLog.tm_mon<<" "<<dateOfLog.tm_year - 100<<" Adding ";
log<<name<<" "<<expDate<<" "<<supplier<<" "<<unit<<" "<<amount<<" "<<location<<endl;
log.close();
}
}
///Logs the removing of **this** Product(from the storage) onto the log file.
void logRemovingThisItem(double removeAmount)
{
if(removeAmount>=this->amount)
{
for(int p=0;p<this->amount/50+1;p++)
{
mirrorTable[this->location/sizeOfShelf*sizeOfShelf][this->location/sizeOfShelf][this->location%sizeOfShelf+p]=false;
}
}
struct tm dateOfLog;
dateOfLog = *localtime(&(this->dateOfEntry));
ofstream log;
log.open("log.txt",fstream::app);
if (log.is_open())
{
log<<dateOfLog.tm_mday<<" "<<dateOfLog.tm_mon<<" "<<dateOfLog.tm_year - 100;
if(removeAmount>=amount)
{log<<" RemovingAll ";
}
else log<<" Removing("<<(amount - removeAmount)<<"-left) ";
log<<name<<" "<<expDate<<" "<<supplier<<" "<<unit<<" "<<removeAmount<<" "<<location<<endl;
log.close();
}
}
};
///3D array that we use as our storage.
Product* storage[sizeOfShelf][sizeOfShelf][sizeOfShelf];
///Clears and makes both of the tables (3d arrays) easy to use.
void FillBothTables()
{
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
{
if(storage[i][j][k]!=nullptr)
{
delete storage[i][j][k];
}
}
mirrorTable[i][j][k] = false;
storage[i][j][k] = nullptr;
}
}
}
}
///Finds place for product.
///
///Finds the first place in the storage where we can put a new product with a given size.
unsigned int SearchForFreeSpace(unsigned int spaceNeeded)
{
unsigned int currentSpace;
for(int i=0;i<sizeOfShelf;i++)
{
currentSpace = 0;
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(mirrorTable[i][j][k] == false)
{
currentSpace++;
if(currentSpace==spaceNeeded)
{
return i*pow(sizeOfShelf,2)+j*sizeOfShelf+(k-spaceNeeded+1);
}
}
else currentSpace = 0;
}
}
}
}
///Creating a new Product.
///
///Creating a new Product, checking if we can put it next to a Product with the same name and expDate.
///If that is not possible we use SearchForFreeSpace(int) to find the first place where it will fit.
///Log that action.
void AddNewProduct(string name, int expDate, string supplier, string unit, int amount)
{
time_t now;
now=time(nullptr);
int combinedAmount;
bool flag = true;
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k]!=nullptr)
{
if(storage[i][j][k]->getName()==name&&storage[i][j][k]->getExpDate()==expDate)
{
combinedAmount = storage[i][j][k]->getAmount() + amount;
for(int m=storage[i][j][k]->getAmount()/50 + 1 ; m < combinedAmount/50 + 1; m++)
{
if(k+m<sizeOfShelf)
{
if(mirrorTable[i][j][k+m])
{
flag = false;
}
}
else flag = false;
}
if(flag)
{
storage[i][j][k]->setAmount(combinedAmount);
for(int m=0; m < combinedAmount/50 + 1; m++)
{
mirrorTable[i][j][k+m] = true;
}
storage[i][j][k]->setDateOfEntry(now);
storage[i][j][k]->logAddingThisItem(amount);
return;
}
}
}
}
}
}
Product* newProduct = new Product(name,expDate,now,supplier,unit,amount);
unsigned int spaceNeeded = newProduct->getAmount()/50+1;
unsigned int positionToPlaceOn = SearchForFreeSpace(spaceNeeded);
newProduct->setLocation(positionToPlaceOn);
storage[positionToPlaceOn/sizeOfShelf*sizeOfShelf][positionToPlaceOn/sizeOfShelf][positionToPlaceOn%sizeOfShelf] = newProduct;
for(int i=0;i<spaceNeeded;i++)
{
mirrorTable[positionToPlaceOn/sizeOfShelf*sizeOfShelf][positionToPlaceOn/sizeOfShelf][positionToPlaceOn%sizeOfShelf + i] = true;
}
newProduct->logAddingThisItem();
}
///Remove a set amount of a Product.
///
///Remove a set amount of a Product from the storage by name and amount.
///Log that action.
void RemoveSetAmountOfProduct(string removeName, int removeAmount)
{
int shortestTimeToExp=2000000;
unsigned int positionOfShortest;
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k]!=nullptr)
{
if(storage[i][j][k]->getName()==removeName&&storage[i][j][k]->getExpDate()<shortestTimeToExp)
{
positionOfShortest = storage[i][j][k]->getlocation();
shortestTimeToExp = storage[i][j][k]->getExpDate();
}
}
}
}
}
if(shortestTimeToExp!=2000000)
{
storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf]->logRemovingThisItem(removeAmount);
if(storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf]->getAmount()>removeAmount)
{
storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf]->setAmount(storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf]->getAmount() - removeAmount);
}
else
{
unsigned int howBigIsThis = storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf]->getAmount()/50+1;
double leftToGet =removeAmount - storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf]->getAmount();
delete storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf];
storage[positionOfShortest/sizeOfShelf*sizeOfShelf][positionOfShortest/sizeOfShelf][positionOfShortest%sizeOfShelf] = nullptr;
RemoveSetAmountOfProduct(removeName,leftToGet);
}
}
else {cout<<"Amount too low!"<<endl;}
}
///Prints all info for all Products in the storage.
void PrintAllInfoOnAllProducts()
{
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k]!=nullptr)
{
storage[i][j][k]->Print();
}
}
}
}
}
///Full amount of Product.
///
///By a given Product name returns the combined amount of all Product objects with that name in the storage.
int AmountOfThisProductInTheStorage(string name)
{
int amount = 0;
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k]!=nullptr)
{
if(storage[i][j][k]->getName()==name)
{
amount = amount + storage[i][j][k]->getAmount();
}
}
}
}
}
return amount;
}
///Prints amounts of the products in the storage.
///
///using AmountOfThisProductInTheStorage(string) finds and prints to the console the names and amounts of all Products in the storage.
void PrintAmountsOfAllProducts()
{
string alreadyCheckedProducts[100];
int numberOfCheckedProducts = 0;
bool currentProductIsNew = true;
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k]!=nullptr)
{
for(int m=0;m<numberOfCheckedProducts;m++)
{
if(storage[i][j][k]->getName()==alreadyCheckedProducts[m])
{
currentProductIsNew = false;
}
}
if(currentProductIsNew)
{
alreadyCheckedProducts[numberOfCheckedProducts + 1] = storage[i][j][k]->getName();
numberOfCheckedProducts++;
cout<<"Product: "<<storage[i][j][k]->getName()<<" amount: "<<AmountOfThisProductInTheStorage(storage[i][j][k]->getName())<<endl;
}
currentProductIsNew = true;
}
}
}
}
}
///Prints the log from a chosen period.
void PrintLogForPeriod(int fromDate,int toDate)
{
ifstream readStream;
readStream.open("log.txt");
if(readStream.fail())
{
cerr<<"Error opening file!"<<endl;
exit(1);
}
int day;
int month;
int year;
string action;
string name;
int expDate;
string supplier;
string unit;
int amount;
unsigned int position;
while(!readStream.eof())
{
readStream>>day>>month>>year>>action>>name>>expDate>>supplier>>unit>>amount>>position;
if(year>fromDate/10000||(year==fromDate/10000&&month>fromDate/100%100)||(year==fromDate/10000&&month==fromDate/100%100&&day>=fromDate%100))
{
if(year<toDate/10000||(year==toDate/10000&&month<toDate/100%100)||(year==toDate/10000&&month==toDate/100%100&&day<=toDate%100))
{
cout<<day<<" "<<month<<" "<<year<<" "<<action<<" "<<name<<" "<<expDate<<" "<<supplier<<" "<<unit<<" "<<amount<<" "<<position<<endl;
}
}
}
}
///Saves the current storage to a file with a given name.
void SaveFileAs(string fileName)
{
ofstream save;
save.open(fileName);
if (save.is_open())
{
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k] != nullptr)
{
save<<storage[i][j][k]->getName()<<" "<<storage[i][j][k]->getExpDate()<<" "<<storage[i][j][k]->getSupplier();
save<<" "<<storage[i][j][k]->getUnit()<<" "<<storage[i][j][k]->getAmount()<<endl;
}
}
}
}
save.close();
}
}
///Clears all the Products that have expired or are about to.
///
///Clears all the Products from the storage that have expired or are about in the next 10 days.
///Logs that action.
///Prints the deleted Products.
void Clean()
{
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k] != nullptr)
{
if(storage[i][j][k]->HowLongTillExp()<=10)
{
storage[i][j][k]->logRemovingThisItem(storage[i][j][k]->getAmount());
storage[i][j][k]->Print();
delete storage[i][j][k];
}
}
}
}
}
}
///Opens a storage file.
///
///Clears the tables from all of the current data and loads the data from a file.
string OpenFile()
{
FillBothTables();
string fileName;
cout<<"Write the name of the file that you want to open:";
cin>>fileName;
ifstream readStream;
readStream.open(fileName);
if(readStream.fail())
{
cerr<<"Error opening file!"<<endl;
exit(1);
}
string name;
int expDate;
string supplier;
string unit;
double amount;
while(!readStream.eof())
{
readStream>>name>>expDate>>supplier>>unit>>amount;
cout<<name<<expDate<<supplier<<unit<<amount<<endl;
AddNewProduct(name,expDate,supplier,unit,amount);
}
return fileName;
}
///Calculates the money that will be lost if a Product were to expire.
///
///Calculates the money that will be lost if (given number of days) were to pass from a Product with a given name and price.
void MoneyLoss()
{
string name;
cout<<"name:";
cin>>name;
cout<<endl;
float price;
cout<<"price:";
cin>>price;
cout<<endl;
int days;
cout<<"days:";
cin>>days;
int amount;
for(int i=0;i<sizeOfShelf;i++)
{
for(int j=0;j<sizeOfShelf;j++)
{
for(int k=0;k<sizeOfShelf;k++)
{
if(storage[i][j][k] != nullptr)
{
if(storage[i][j][k]->getName()==name&&storage[i][j][k]->HowLongTillExp()<=days)
{
amount=amount+storage[i][j][k]->getAmount();
}
}
}
}
}
cout<<"The loss will be:"<<amount*price<<endl;
}
int main()
{
FillBothTables();
string fileName;
cout<<"Hello, welcome to StorageProject"<<endl<<"Write the name of the file that you want to open:";
cin>>fileName;
ifstream readStream;
readStream.open(fileName);
if(readStream.fail())
{
cerr<<"Error opening file!"<<endl;
exit(1);
}
string name;
int expDate;
string supplier;
string unit;
double amount;
while(!readStream.eof())
{
readStream>>name>>expDate>>supplier>>unit>>amount;
cout<<name<<expDate<<supplier<<unit<<amount<<endl;
AddNewProduct(name,expDate,supplier,unit,amount);
}
cout<<"open, close, save, save as, help, exit, print, ad, remove, log, clean, bonus"<<endl;
string nextAction;
while(nextAction != "exit")
{
cout<<"Next action:";
cin>>nextAction;
if(nextAction == "open") {
fileName = OpenFile();
}
else if(nextAction == "close")
{
string command;
cout<<"open, exit"<<endl;
cin>>command;
if(command=="open")
{
fileName = OpenFile();
}
else return 0;
}
else if(nextAction == "save")
{
SaveFileAs(fileName);
}
else if(nextAction == "save as")
{
cout<<"name of file:";
cin>>fileName;
cout<<endl;
SaveFileAs(fileName);
}
else if(nextAction == "help")
{
cout<<"open <file> opens <file>"<<endl;
cout<<"close closes currently opened file"<<endl;
cout<<"save saves the currently open file"<<endl;
cout<<"saveas <file> saves the currently open file in <file>"<<endl;
cout<<"help prints this information"<<endl;
cout<<"exit exists the program"<<endl;
cout<<"print prints what is currently in the storage"<<endl;
cout<<"add add a new product to the storage"<<endl;
cout<<"remove remove an amount of a product from the storage"<<endl;
cout<<"log print the log of product added or removed from the storage"<<endl;
cout<<"clean remove all the products that have expired or are about to expire"<<endl;
cout<<"bonus by product's price see what the loss will be for a period if it were to expire"<<endl;
}
else if(nextAction == "print")
{
PrintAllInfoOnAllProducts();
break;
}
else if(nextAction == "add")
{
cout<<"name:";
cin>>name;
cout<<endl;
cout<<"expDate:";
cin>>expDate;
cout<<endl;
cout<<"supplier:";
cin>>supplier;
cout<<endl;
cout<<"unit:";
cin>>unit;
cout<<endl;
cout<<"amount:";
cin>>amount;
cout<<endl;
AddNewProduct(name,expDate,supplier,unit,amount);
}
if(nextAction == "remove")
{
cout<<name;
cin>>name;
cout<<endl;
cout<<"amount:";
cin>>amount;
cout<<endl;
RemoveSetAmountOfProduct(name,amount);
}
if(nextAction == "log")
{
int Day;
int Month;
int Year;
cout<<"fromDay:";
cin>>Day;
cout<<endl;
cout<<"fromMonth";
cin>>Month;
cout<<endl;
cout<<"fromYear";
cin>>Year;
cout<<endl;
int fromDate = Day + 100*Month + 10000*Year;
cout<<"toDay:";
cin>>Day;
cout<<endl;
cout<<"toMonth";
cin>>Month;
cout<<endl;
cout<<"toYear";
cin>>Year;
cout<<endl;
PrintLogForPeriod(fromDate,Day + 100*Month + 10000*Year);
}
else if(nextAction == "clean")
{
Clean();
}
else if(nextAction == "open")
{
MoneyLoss();
}
}
return 0;
};