This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
forked from hadisfr/ratatouille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.cpp
120 lines (103 loc) · 3.2 KB
/
system.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
#include "system.h"
#include <string>
#include <vector>
#define ERRSTR "ERR\n"
using namespace std;
void System::addCustomer(string name, long id, string location){
customers.push_back(Customer(name, id, location));
}
void System::addRestaurant(string name, int id, string location, vector <Food> foods){
vector <Restaurant>::iterator i;
for(i = restaurants.begin(); i < restaurants.end(); i++)
if(i->getId() > id)
break;
restaurants.insert(i, Restaurant(name, id, location, foods));
}
void System::addFood(int restaurantId, int foodId, string name, Food::Type type, int cost){
Restaurant *restaurant = findRestaurant(restaurantId);
if(!restaurant)
return;
restaurant->addFood(foodId, name, type, cost);
}
void System::newOrder(long customerId){
Customer *customer = findCustomer(customerId);
if(!customer)
return;
return customer->newOrder();
}
void System::addToOrder(long customerId, long foodId, int num, string personalizations){
Customer *customer = findCustomer(customerId);
Restaurant *restaurant = findRestaurant(foodId / 1000);
Food *food = restaurant->findFood(foodId % 1000);
if(!customer || !restaurant)
return;
return customer->addToOrder(food, restaurant, num, personalizations);
}
string System::getOrderReport(long customerId){
Customer *customer = findCustomer(customerId);
if(!customer)
return ERRSTR;
return customer->getOrderReport();
}
string System::getLastBill(long customerId){
Customer *customer = findCustomer(customerId);
if(!customer)
return ERRSTR;
return customer->getLastBill();
}
string System::getAllBills(long customerId){
Customer *customer = findCustomer(customerId);
if(!customer)
return ERRSTR;
return customer->getAllBills();
}
bool System::isNear(const Customer &customer, const Restaurant &restaurant){
return customer.getLocation() == restaurant.getLocation();
}
Customer * System::findCustomer(long id){
for(int i = 0; i < customers.size(); i++)
if(customers[i].getId() == id)
return &customers[i];
return NULL;
}
Restaurant * System::findRestaurant(int id){
for(int i = 0; i < restaurants.size(); i++)
if(restaurants[i].getId() == id)
return &restaurants[i];
return NULL;
}
string System::getMenu(int restaurantId){
Restaurant *restaurant = findRestaurant(restaurantId);
if(!restaurant)
return "";
return restaurant->getMenu();
}
string System::getRestaurantsDetail(long customerId){
string result = "";
Customer *customer;
if(customerId){
customer = findCustomer(customerId);
}
if(customerId && !customer)
return "";
for(int i = 0; i < restaurants.size(); i++)
if(!customerId || isNear(*customer, restaurants[i]))
result += restaurants[i].getName() + string(" ") + restaurants[i].getLocation() + string(" ") + to_string(restaurants[i].getId()) + string("\n");
return result;
}
string System::listFoods(long customerId){
string result = "";
Customer *customer = findCustomer(customerId);
if(!customer)
return "";
for(int i = 0; i < restaurants.size(); i++)
if(isNear(*customer, restaurants[i]))
result += restaurants[i].getListFoods();
return result;
}
string System::listFoods(Food::Type type){
string result = "";
for(int i = 0; i < restaurants.size(); i++)
result += restaurants[i].getListFoods(type);
return result;
}