-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreserve.hpp
438 lines (379 loc) · 11.7 KB
/
reserve.hpp
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
#include <algorithm>
void set_reservation(System &system, map<string,string> inputedinformation, User *user)
{
string restaurant_name = inputedinformation["restaurant_name"];
Resturant * resturant = get_resturant_ptr_filter_name(system, restaurant_name);
string table_id = inputedinformation["table_id"];
string inputed_start_time = inputedinformation["start_time"];
string inputed_end_time = inputedinformation["end_time"];
string resturant_start_time = resturant->get_oppening();
string resturant_end_time = resturant->get_closing();
// Added
string food = inputedinformation["food_reserve"];
double estimated_price = 0;
map<string, string> foods_in_restaurant = resturant->get_foods();
map<int, string> restaurant_reserved_chairs = resturant->get_reserve_chairs();
string times_of_occupied_of_table = find_value_by_key2(restaurant_reserved_chairs, stoi(table_id));
vector<string> intervals_of_occupied_of_table = splitStringByDelimiter2(times_of_occupied_of_table, ',');
vector<string> start_and_end;
vector<string> user_reservation_ids_in_this_restaurant;
map<string, string> user_reservations = user->get_reservation();
string the_chair_reservation;
vector<string> the_chair_reservation_intervals;
vector<string> foods_to_reserve = splitStringByDelimiter2(food, ',');
if(!has_foods(foods_to_reserve, foods_in_restaurant)){
cout<<"Not Found"<<endl;
return;
}
if(!has_restaurant(system, restaurant_name)){
cout<<"Not Found"<<endl;
return;
}
if(restaurant_reserved_chairs.find(stoi(table_id)) == restaurant_reserved_chairs.end()){
cout<<"Not Found"<<endl;
return;
}
for(auto interval : intervals_of_occupied_of_table)
{
start_and_end = splitStringByDelimiter2(interval, '-');
if(!(inputed_start_time >= start_and_end[1] || inputed_end_time <= start_and_end[0]))
{
cout<<"Permission Denied"<<endl;
return;
}
}
if(!(stoi(resturant_start_time) <= stoi(inputed_start_time) &&
stoi(resturant_end_time) >= stoi(inputed_end_time)) ||
((stoi(inputed_start_time) == 1 && stoi(inputed_end_time) == 24)))
{
cout<<"Permission Denied"<<endl;
return;
}
for(const auto& pair : user_reservations){
if (pair.first == restaurant_name)
continue;
user_reservation_ids_in_this_restaurant = splitStringByDelimiter2(pair.second, ',');
for (auto id : user_reservation_ids_in_this_restaurant){
the_chair_reservation = find_value_by_key2((get_resturant_ptr_filter_name(system, restaurant_name))->get_reserve_chairs()
, stoi(id));
the_chair_reservation_intervals = splitStringByDelimiter2(the_chair_reservation, ',');
for(auto interval : the_chair_reservation_intervals){
start_and_end = splitStringByDelimiter2(interval, '-');
if(!(inputed_start_time >= start_and_end[1] || inputed_end_time <= start_and_end[0]))
{
cout<<"Permission Denied"<<endl;
return;
}
}
}
}
user->set_reservation(restaurant_name, table_id+"-"+inputed_start_time+"-"+inputed_end_time);
resturant->set_chair_reserve(stoi(table_id), inputed_start_time+"-"+inputed_end_time);
estimated_price = calc_price(foods_in_restaurant, food);
if(estimated_price == -1){
cout<<"Not Found"<<endl;
return;
}
cout << "done reservation" << endl;
}
string find_value_by_key(const map<string, string>& map, const string& x) {
auto it = map.find(x);
if (it != map.end()) {
return it->second;
}
else{
return "";
}
}
string find_value_by_key2(const map<int, string>& map, const int& x) {
auto it = map.find(x);
if (it != map.end()) {
return it->second;
}
else{
return "";
}
}
double calc_price(const map<string, string>& foods_in_restaurant, const string& food)
{
double estimated_price = 0;
for(auto food_name : food)
{
try
{
if (find_value_by_key(foods_in_restaurant, &food_name) == "")
{
return -1;
}
estimated_price += stoi(find_value_by_key(foods_in_restaurant, &food_name));
}
catch(...)
{
return -1;
}
}
return estimated_price;
}
bool has_foods(const vector<string>& foods_to_reserve, map<string, string> foods_in_restaurant){
for (const auto& food_name : foods_to_reserve){
if(foods_in_restaurant.find(food_name) == foods_in_restaurant.end()){
return true;
}
}
return falsel;
}
bool has_restaurant(System &system, const string& restaurant_name){
for(auto restaurant : system.get_resturants()){
if(restaurant_name == restaurant.get_name()){
return true;
}
}
return false;
}
Resturant * get_resturant_ptr_filter_name(System &system,string name){
for (int i = 0 ; i < system.get_resturants().size();i++)
{
if (system.get_resturants()[i].get_name() == name)
{
return &system.get_resturants()[i];
}
}
}
map<string,string> make_command_readble(vector<string> commands){
map<string,string> z ;
string c = "" ;
for (auto i : commands)
{
c += " " + i;
}
bool name = 0;
bool chair_num = 0;
bool come = 0;
bool go = 0;
bool food = 0;
for (auto i : commands)
{
if (i == "restaurant_name")
{
name = 1;
}
if (i == "table_id")
{
chair_num = 1;
}
if (i == "start_time")
{
come = 1;
}
if (i == "end_time")
{
go = 1;
}
if (i == "foods")
{
food = 1;
}
}
if(!(name && chair_num && come && go))
{
z["valid"] = "0";
return z;
}else{
z["valid"] = "1";
}
if(!(food))
{
z["food_valid"] = "0";
return z;
}else{
z["food_valid"] = "1";
}
string d = "";
size_t pos1 = 0;
size_t pos2 = 0;
pos1 = c.find("restaurant_name");
pos1 = c.find('"',pos1);
pos2 = c.find('"',pos1+1);
d = c.substr(pos1+1,pos2-pos1-1);
z["restaurant_name"] = d;
pos1 = c.find("table_id");
pos1 = c.find('"',pos1);
pos2 = c.find('"',pos1+1);
d = c.substr(pos1+1,pos2-pos1-1);
z["table_id"] = d;
pos1 = c.find("start_time");
pos1 = c.find('"',pos1);
pos2 = c.find('"',pos1+1);
d = c.substr(pos1+1,pos2-pos1-1);
z["start_time"] = d;
pos1 = c.find("end_time");
pos1 = c.find('"',pos1);
pos2 = c.find('"',pos1+1);
d = c.substr(pos1+1,pos2-pos1-1);
z["end_time"] = d;
if (food == 1)
{
pos1 = c.find("foods");
pos1 = c.find('"',pos1);
pos2 = c.find('"',pos1+1);
d = c.substr(pos1+1,pos2-pos1-1);
z["foods"] = d;
}else{
z["foods"] = "";
}
return z;
}
string get_string_like_normal(string x){
x.erase(0,1);
x.erase(x.size()-1,1);
return x;
}
bool compareByName(Resturant& a, Resturant& b) {
return a.get_name() < b.get_name();
}
vector<Resturant> get_resturent_filter_district(System &system,string district){
vector<Resturant> x ;
for (auto i : system.get_resturants())
{
if (i.get_distric() == district)
{
x.push_back(i);
}
}
sort(x.begin(), x.end(), compareByName);
return x;
}
vector<string> splitStringByDelimiter2(const string input, char delimiter) {
vector<std::string> result;
stringstream system(input);
string item;
while (std::getline(system, item, delimiter)) {
result.push_back(item);
}
return result;
}
vector<string> show_resturant_in_district(System &system,string district){
vector<string> row;
vector<Resturant> resturants = system.get_resturants();
for(auto &resturant : resturants){
if (district == resturant.get_distric())
{
row.push_back(resturant.get_name());
}
}
return row;
}
bool inside_out(vector<string> x,string y){
bool z = 0;
for (auto i : x)
{
if(i == y){
z = 1;
}
}
return z;
}
Resturant get_restuarant_filter_name(System system,string name){
for (auto i : system.get_resturants())
{
if (i.get_name() == name)
{
return i;
}
}
return system.get_resturants()[0];
}
map<string,string> get_resturant_give_foods(Resturant r){
map<string,string> x;
for (auto i : splitStringByDelimiter2(r.get_foods(),';'))
{
x[splitStringByDelimiter2(i,':')[0]] = splitStringByDelimiter2(i,':')[1];
}
return x;
}
void get_resturant_give_foods_print(Resturant *r){
for (auto i : splitStringByDelimiter2(r->get_foods(),';'))
{
cout << r->get_name();
cout << i<<endl;
// cout << splitStringByDelimiter2(i,':')[0];
}
}
bool resturant_has_food(Resturant r,string food){
map<string,string> foods = get_resturant_give_foods(r);
for (auto [key,value] : foods)
{
if (key == food)
{
return 1;
}
}
return 0;
}
void resturant_list(System &system,User *user,vector<string> &future_search,vector<string> &gone_districts,int n = 0){
if (n == 0){
vector<string> user_neighbor_districts = splitStringByDelimiter2(system.get_districts()[user->get_district()],';');
future_search.insert(future_search.end(),user_neighbor_districts.begin(),user_neighbor_districts.end());
gone_districts.push_back(user->get_district());
n++;
resturant_list(system,user,future_search,gone_districts,n);
}else{
for(auto i : future_search){
if (!inside_out(gone_districts,i))
{
gone_districts.push_back(i);
}
}
if (gone_districts.size() >= system.get_districts().size())
{
return;
}
vector<string> copy_from_future;
for(auto i : future_search){
copy_from_future.push_back(i);
}
future_search.clear();
for (auto i : copy_from_future)
{
for(auto z : splitStringByDelimiter2(system.get_districts()[i],';')){
future_search.push_back(z);
}
}
if (future_search.empty()) {
return;
}
resturant_list(system,user,future_search,gone_districts,n);
}
}
void restaurant_detail(System &system,string name){
int i = -1;
for (int j = 0; j<system.get_resturants().size();j++)
{
if (system.get_resturants()[j].get_name() == name)
{
i = j;
break;
}
}
if (i != -1)
{
Resturant x = system.get_resturants()[i];
cout << "Name: " << x.get_name() << endl;
cout << "District: " << x.get_distric() << endl;
cout << "Time: " << x.get_oppening() << "-" << x.get_closing() << endl;
cout << "Menu: ";
map<string,string> foods = get_resturant_give_foods(x);
for (auto [key,value] : foods)
{
cout << key << "(" << value << ")" << ", " ;
}
cout << endl;
for (auto [key,value] : x.get_reserve_chairs())
{
cout << key <<": "<<endl;
}
}else{
cout << "Not Foundm" << endl;
return;
}
}