-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkitchenFunctions.cpp
More file actions
279 lines (234 loc) · 5.94 KB
/
kitchenFunctions.cpp
File metadata and controls
279 lines (234 loc) · 5.94 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
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
/*
* Function definitions for kitchen.h
*
*/
#include "kitchen.h"
/*************************
** general functions **
*************************/
/*********************
** Hub functions **
*********************/
// constructor
Hub::Hub(){ return; }
// prints all orders of all stations in Hub.stations[]
void Hub::printOrders()
{
cout << "\n\n";
for (int stat_i = 0; stat_i < stations.size(); ++stat_i) {
cout << stat_i << ". " << stations[stat_i]->name << ": "
<< stations[stat_i]->attendant->name << endl;
for (int comp_i = 0; comp_i < stations[0]->components.size(); ++comp_i) {
cout << "\t" << comp_i << ". "
<< stations[stat_i]->components[comp_i]->name << endl;
}
}
return;
}
// adds a station to hub of name stationName
int Hub::addStation(char* stationName)
{
Station *station = new Station;
station->name = stationName;
stations.push_back(station);
return 0;
}
// sorts waiting orders, pop top order off waitingOrders, and push_back onto orders
// then sends components to stations
int Hub::pushOrder()
{
sortWaitingOrders();
if (orders.size() >= MAX_ORDERS_SIZE)
return 0;
Dish *top = waitingOrders.front();
orders.push_back(top);
waitingOrders.erase(waitingOrders.begin());
sendOrders();
return 0;
}
// sends components to stations
int Hub::sendOrders()
{
Dish::Component* comp;
Dish* dish;
for (int i = 0; i < orders.size(); ++i) { // for each order
dish = orders[i];
if (dish->sentToStations == 0){ // if dish was not sent
dish->sentToStations = 1;
for (int j = 0; j < dish->components.size(); ++j) { // for each component
comp = orders[i]->components[j];
for (int k = 0; k < stations.size(); ++k) { // find the relevent station
if (strcmp(orders[i]->components[j]->relevantStation, stations[k]->name) == 0) {
stations[k]->addComponent(dish, comp);
}
}
}
}
}
return 0;
}
// notify waiters that dish is done (client-server?)
int Hub::notifyDone(Dish *d)
{
printf("Notify waiters that %s is complete.\n", d->name);
return 0;
}
// adds Dish* order to waitingOrders queue
int Hub::addOrder(Dish *order)
{
waitingOrders.push_back(order);
return 0;
}
// marks the component complete
int Hub::completeComponent(int stations_it, int components_it)
{
stations[stations_it]->components[components_it]->status = 1;
return 0;
}
// checks if all orders of dish are complete
// components_it is used for dishReference
// returns 1 if dish complete, 0 if not
int Hub::checkOrder(int stations_it, int components_it)
{
Dish* dish = stations[stations_it]->dishReferences[components_it];
cout << "check order: " << dish->name << " ";
for (int i = 0; i < dish->components.size(); ++i) {
if (dish->components[i]->status == 0) {
return 0;
}
}
dish->status = 1;
return 1;
}
// removes all completed orders:Dish from all stations and from orders[]
int Hub::removeOrders()
{
//remove from all stations:
Dish::Component* comp;
Dish* dish;
for (int i = 0; i < orders.size(); ++i) { // for each order
dish = orders[i];
if (dish->status == 1) { // if dish is complete
for (int j = 0; j < dish->components.size(); ++j) { // for each component
comp = orders[i]->components[j];
for (int k = 0; k < stations.size(); ++k) { // find the relevent station
if (strcmp(orders[i]->components[j]->relevantStation, stations[k]->name) == 0) {
// remove from components[] and dishReferences[]:
stations[k]->removeComponent(dish, comp);
return 0;
}
}
}
}
}
cout << "removeOrders: not found!\n";
return -1;
}
int Hub::removeOrder(int dish_it)
{
for (int i = 0; i < stations.size(); ++i) {
stations[i]->components.erase(stations[i]->components.begin() + dish_it);
stations[i]->dishReferences.erase(stations[i]->dishReferences.begin() + dish_it);
}
orders.erase(orders.begin() + dish_it);
return 0;
}
int Hub::sortWaitingOrders()
{
Dish* temp;
for (int i = 0; i < (waitingOrders.size()-1); ++i) {
for (int j = 1; j < waitingOrders.size(); ++j) {
if (waitingOrders[i]->pvalue < waitingOrders[j]->pvalue) {
temp = waitingOrders[i];
waitingOrders[i] = waitingOrders[j];
waitingOrders[j] = temp;
}
}
}
return 0;
}
/*************************
** Station functions **
*************************/
// constructors
Hub::Station::Station() { ; }
Hub::Station::Station(Chef *att, char *n)
{
name = n;
attendant = att;
return;
}
// set Chef chef as station's attendant
// takes Chef*, return 0 if success, -1 otherwise
int Hub::Station::selectChef(Chef *chef)
{
attendant = chef;
if(chef->name == NULL){
printf("selectChef: Invalid chef");
return -1;
}
return 0;
}
int Hub::Station::addComponent(Dish* dish, Dish::Component* comp)
{
components.push_back(comp);
dishReferences.push_back(dish);
return 0;
}
/*********************
** Chef functions **
*********************/
// create chef with name cname
Chef::Chef(char* cname)
{
name = cname;
return;
}
// sets station.attendant to this chef pointer
int Chef::selectStation(Hub::Station *station)
{
station->attendant = this;
return 0;
}
// sets the start time or end time for a chef
// 0 sets start time, 1 sets end time to current time
int Chef::punchHours(int selector)
{
if (selector == 0) {
// set start time
}
else if (selector == 1) {
// set end time
}
else{
return -1;
}
return 0;
}
/*********************
** Dish functions **
*********************/
// constructor: Name, componentA, componentB
Dish::Dish(char* dishName, char* compA, char* compB, double coefficient)
{
sentToStations = 0;
receiveTime = time(NULL);
pcoeff = coefficient;
pvalue = time(NULL)*pcoeff;
status = 0;
name = dishName;
Component *comp = new Component(compA,"FRY");
components.push_back(comp);
comp = new Component(compB, "OVEN");
components.push_back(comp);
return;
}
/*************************
** Component functions **
*************************/
// constructor
Dish::Component::Component(char* str, char* station)
{
name = str;
relevantStation = station;
}