-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroceryController.h
More file actions
137 lines (116 loc) · 3.1 KB
/
GroceryController.h
File metadata and controls
137 lines (116 loc) · 3.1 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
#pragma once
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "BST.h"
#include "CQ.h"
#include "DLL.h"
#include "Item.h"
#include "Queue.h"
using namespace std;
class GroceryController {
private:
DLL<Item, string>* GroceryList;
BST<double, Item>* GL_Price;
BST<double, Item>* GL_Exp;
CQ<string>* Customer_History;
Item* createItem(string name, string cat, double exp, double price) {
Item* newItem = new Item;
newItem->setName(name);
newItem->setCategory(cat);
newItem->setExpiryDate(exp);
newItem->setPrice(price);
return newItem;
}
void PrintItem(Queue<Item>* q) {
while (!q->isEmpty()) {
Item* n = q->deQueue();
cout << n->getName() << ", ";
cout << n->getCategory() << ", ";
cout << n->getPrice() << ", ";
cout << setprecision(9);
cout << n->getExpiryDate() << ", ";
cout << n->getStock();
cout << endl;
}
}
public:
GroceryController() {
GroceryList = new DLL<Item, string>;
GL_Price = new BST<double, Item>;
GL_Exp = new BST<double, Item>;
Customer_History = new CQ<string>(3);
}
void addItem(string name, string cat, double exp, double price) {
Item* item = createItem(name, cat, exp, price);
Item* dup = GroceryList->searchDuplicate(name);
if (dup != NULL) {
dup->incStock();
return;
}
NodeDLL<Item>* ptr = GroceryList->pushDLL(item);
GL_Price->insert(item->getPrice(), ptr);
GL_Exp->insert(item->getExpiryDate(), ptr);
}
void removeItem(string name) {
Item* dup = GroceryList->searchDuplicate(name);
if (dup != NULL && dup->getStock() > 1) {
dup->decStock();
return;
}
// index 0 = price
// index 1 = expiry
double* priceAndExpiry = GroceryList->deletevalueDLL(name);
if (priceAndExpiry != NULL) {
GL_Price->deleteNode(priceAndExpiry[0]);
GL_Exp->deleteNode(priceAndExpiry[1]);
}
}
void addCustomer(string customer) {
Customer_History->enQueue(customer);
}
void displayList() {
cout << "List:" << endl;
GroceryList->printDLL();
}
Queue<Item>* getList() {
return GroceryList->printDLL();
}
void displayByPrice() {
cout << "PRICE:" << endl;
PrintItem(GL_Price->display());
}
void displayByExpiry() {
cout << "Expiry:" << endl;
PrintItem(GL_Exp->display());
}
void displayByCategory(string cat) {
cout << "Category: " << endl;
PrintItem(GroceryList->displayCat(cat));
}
void displayCustomerHistory() {
cout << "Customer History:" << endl;
Customer_History->displayQueue();
}
void writeFile() {
ofstream out_file("../output.json");
if (!out_file) {
cerr << "Error with file" << endl;
return;
}
Queue<Item>* q = getList();
while (!q->isEmpty()) {
Item* i = q->deQueue();
out_file << "{" << endl;
out_file << " \"name\""<<":"<< "\""<< i->getName() << "\"," << endl;
out_file << " \"cat\"" << ":" << "\"" << i->getCategory() << "\"," << endl;
out_file<< setprecision(9);
out_file << " \"exp\"" << ":" << i->getExpiryDate()<<"," << endl;
out_file << " \"price\"" << ":"<< i->getPrice() << "," << endl;
out_file << " \"stock\"" << ":"<< i->getStock() << endl;
out_file << "}" << endl;
}
out_file.close();
}
};