-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestaurant.cpp
201 lines (187 loc) · 6.38 KB
/
restaurant.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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <stdexcept>
#include <climits>
#include "table.h"
#include "order.h"
#include "restaurant.h"
using namespace std;
Restaurant::Restaurant(string strName, vector<Table> vctTables) {
this->name = strName;
this->tables = vctTables;
}
void Restaurant::take_table(int intTable) {
if ((intTable-1) >= int(this->tables.size())||intTable<1)
throw runtime_error("Table " + to_string(intTable) + " doesn't exist!");
else
this->tables.at(intTable-1).take();
}
void Restaurant::return_table(int intTable) {
if ((intTable-1) >= int(this->tables.size())||intTable<1) {
throw runtime_error("Table " + to_string(intTable) + " doesn't exist!");
} else if (this->om.find(intTable) != this->om.end()) {
throw runtime_error("Table " + to_string(intTable) + " has open orders!");
} else {
this->tables.at(intTable-1).giveback();
}
}
void Restaurant::get_tables() {
for (size_t i {0}; i < this->tables.size(); i++) {
if (this->tables.at(i).is_free()) {
cout << (i+1) << ' ';
}
}
cout << '\n';
}
void Restaurant::make_order(int intTable, const string& strOrder, bool fast, double dblPrice) {
if ((intTable-1) >= int(this->tables.size())||intTable<1)
throw runtime_error("Table " + to_string(intTable) + " doesn't exist!");
if (this->tables.at(intTable-1).is_free())
throw runtime_error("Table " + to_string(intTable) + " has no customers!");
if (this->om.find(intTable) == this->om.end()) {
this->om.insert({intTable, vector<Order>({Order(strOrder, dblPrice)})});
} else {
if (!fast)
this->om[intTable].push_back(Order(strOrder, dblPrice));
else
this->om[intTable].insert(this->om[intTable].begin(), Order(strOrder, dblPrice));
}
}
void Restaurant::cancel_order(int intTable, const string& strOrder) {
if ((intTable-1) >= int(this->tables.size())||intTable<1)
throw runtime_error("Table " + to_string(intTable) + " doesn't exist!");
if (this->tables.at(intTable-1).is_free())
throw runtime_error("Table " + to_string(intTable) + " has no customers!");
if (this->om.find(intTable) != this->om.end()) {
auto it = this->om[intTable].begin();
while (it->get_name() != strOrder) {
if (++it == this->om[intTable].end())
throw runtime_error("The order doesnt't exist!");
}
this->om[intTable].erase(it);
if (this->om[intTable].empty()) {
this->om.erase(intTable);
}
} else {
throw runtime_error("Table " + to_string(intTable) + " has no orders!");
}
}
string Restaurant::process() {
if (this->om.empty())
throw runtime_error("No orders!");
string strProcess;
auto it = this->om.begin();
strProcess.append("Prepare ");
while (it != this->om.end()) {
for (size_t i {0}; i < it->second.size(); i++) {
strProcess.append(it->second.at(i).get_name());
if ((i+1) < it->second.size()) {
strProcess.append(", ");
}
}
if (++it != this->om.end()) {
strProcess.append(", ");
}
}
strProcess.append(".\n");
it = this->om.begin();
while (it != this->om.end()) {
strProcess.append("Deliver ");
for (size_t i {0}; i < it->second.size(); i++) {
strProcess.append(it->second.at(i).get_name());
if ((i+1) < it->second.size()) {
strProcess.append(", ");
}
}
strProcess.append(" to table " + to_string(it->first) + '.');
if (++it != this->om.end()) {
strProcess.append("\n");
}
}
this->om.clear();
return strProcess;
}
void Restaurant::stats() {
if (this->om.empty()) {
throw runtime_error("No current orders!");
}
double min {INT_MAX}, max {0}; int table_min = 0, table_max = 0; int total = 0;
for (auto& it: this->om) {
if (min > static_cast<int>(it.second.size())) {
min = static_cast<int>(it.second.size());
table_min = it.first;
}
if (max < static_cast<int>(it.second.size())) {
max = static_cast<int>(it.second.size());
table_max = it.first;
}
total += static_cast<int>(it.second.size());
}
cout << "Minimum Orders: " << min << " for Table " << table_min << '\n'
<< "Maximum Orders: " << max << " for Table " << table_max << '\n'
<< "Total Orders: " << total << '\n'
<< "Average Orders: " << (total/static_cast<double>(this->om.size())) << '\n';
}
ostream& Restaurant::print(ostream& o) {
o << '{' << this->name << ", Table {";
for (size_t i {0}; i < this->tables.size(); i++) {
o << int(i+1) << ' ' << this->tables.at(i);
if ((i+1) < this->tables.size()) {
o << ", ";
}
}
o << "} Orders {";
unordered_map<int, vector<Order>>::iterator it = this->om.begin();
while (it != this->om.end()) {
o << "Table " << it->first << ": {";
for (size_t i {0}; i < it->second.size(); i++) {
o << it->second.at(i);
if ((i+1) < it->second.size()) {
o << ", ";
}
}
o << '}';
if (++it != this->om.end()) {
o << ", ";
}
}
o << '}';
return o;
}
Restaurant& Restaurant::operator++() {
this->tables.push_back(Table());
return *this;
}
Restaurant& Restaurant::operator+=(const int& n) {
if (n<1)
throw runtime_error("Invalid number of tables to add!");
for (size_t i {0}; i < static_cast<size_t>(n); i++) {
this->tables.push_back(Table());
}
return *this;
}
string Restaurant::operator[](const int& intTable) {
if ((intTable-1) >= int(this->tables.size())||intTable<1)
throw runtime_error("Table " + to_string(intTable) + " doesn't exist!");
if (this->tables.at(intTable-1).is_free())
throw runtime_error("Table " + to_string(intTable) + " has no customers!");
string helper {};
double sum = 0;
auto it = this->om.find(intTable);
if (it != this->om.end()) {
for (size_t i {0}; i < it->second.size(); i++) {
helper.append(it->second.at(i).get_name() + ": " + to_string(it->second.at(i).get_price()) + '\n');
sum += it->second.at(i).get_price();
}
helper.append("Sum: " + to_string(sum) + '\n');
} else {
throw runtime_error("Table " + to_string(intTable) + " doesn't have any orders!");
}
return helper;
}
ostream& operator<<(ostream& o, Restaurant& r) {
r.print(o);
return o;
}