/
baseClassOfFastFoodProject.h
364 lines (334 loc) · 9.96 KB
/
baseClassOfFastFoodProject.h
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
/******************************************************
* File name: baseClassOfFastFoodProject.h
* Author: Vasily I.Gurianov
* Date: 2017.08.22
* Tested on CodeGear C++ Builder 2007
*
* The fast food restaurant simulation.
*
* When a car enters from the street, the driver decides to get in line or not.
* If the driver decides to leaves the restaurant then he leaves as a dissatisfied customer.
* If the driver decides to get in line, then he waits until the menu board is available.
* At that time, the driver makes the order to the order taker.
* After the order taken, then the driver moves forward and the next customer can order.
* It is a first activity. The order sends to the kitchen. As soon as kitchen is available,
* order takes the cook. It is a second activity.
* As soon as customer arrived to the pickup window, he pays and picks up his food.
* As soon as he picks up his food, then the customer leaves the restaurant as satisfied customer.
*
* Using:
* TreeRoot *pWorld = new TreeRoot;
*
* pWorld->Run();
* Memo1->Lines->Add(pWorld->probe);
*
* ***************************************************/
#ifndef baseClassOfFastFoodProjectH
#define baseClassOfFastFoodProjectH
#include <Classes.hpp>
#include <StdCtrls.hpp>
#include <Dialogs.hpp>
/******************************************************
* Class: LinkedList
* ***************************************************/
class LinkedList {
public:
LinkedList *next;
LinkedList() {
this->next = NULL;
}
}; // class LinkedList
/******************************************************
* Class: QueueNode
*
* This class define node of queue.
*
* ***************************************************/
enum QueueNodeState {PROCESSED, UNPROCESSED};
class QueueNode {
public:
AnsiString _data;
QueueNodeState _state;
QueueNode *next;
QueueNode() {
_data = "Who am I?";
_state = UNPROCESSED;
}
QueueNode(AnsiString data) {
_data = data;
_state = UNPROCESSED;
}
}; // class QueueNode
/******************************************************
* Class: Queue
*
* This class define queue.
*
* ***************************************************/
class Queue {
public:
QueueNode *head;
QueueNode *tail;
int count;
Queue() {
head = NULL;
tail = head;
count = 0;
}
int Count() { return count; }
bool IsEmpty() { return count == 0; }
// push
void enqueue(QueueNode *data) {
if (count == 0) {
head = data;
tail = head; }
else {
tail->next = data;
tail = tail->next; };
count++;
}
// pull
QueueNode *dequeue() {
if (count == 0) return NULL;
else {
QueueNode *out;
out = head;
head = head->next;
count--;
return out; };
}
// get first element
QueueNode* first() {
//if (IsEmpty) return NULL; else
return head;
}
// get last element
QueueNode* last() {
//if (IsEmpty) return NULL; else
return tail;
}
// clear queue
void Clear() {
head = NULL;
tail = NULL;
count = 0;
}
//
bool Contains(AnsiString data) {
QueueNode *current = head;
while (current != NULL) {
if (current->_data == data) {return true;}
else current = current->next;
}
return false;
}
}; // class Queue
/******************************************************
* Class: <<Substance>> Component
* The class is Component role in Composite pattern
* ***
* Concept = Service
* ***************************************************/
enum ComponentState {IDLE, BUSY};
class Component {
public:
ComponentState componentState;
QueueNode *input; //*** Concept = Input of entity
QueueNode *output; //*** Concept = Output of entity
virtual void Run() = 0;
}; // class Component
/******************************************************
* Class: <<Ontology Atom>> TreeLeaf
* The class is Leaf role in Composite pattern
* ***
* Concept = Server
* ***************************************************/
class TreeLeaf : public Component {
private:
LinkedList *headItemOfList;
LinkedList *currentItemOfList;
public:
TreeLeaf() {
headItemOfList = new LinkedList;
currentItemOfList = headItemOfList;
for (int i = 0; i < 0; i++) {
currentItemOfList->next = new LinkedList;
currentItemOfList = currentItemOfList->next;
};
currentItemOfList = headItemOfList;
componentState = IDLE;
}
virtual void Run() { // <<Exist>>
if (currentItemOfList != NULL) {
componentState = BUSY;
output = NULL;
currentItemOfList = currentItemOfList->next;
} else {
currentItemOfList = headItemOfList; componentState = IDLE;
if (input != NULL) input->_state = PROCESSED;
output = input;
};
}
};
/******************************************************
* Class: <<Ontology Space>>ItemOfList
* The class is container for instances 'Component' class
* ***
* Concept = Cell of Space
* ***************************************************/
class ItemOfList {
protected:
public:
Component *_component;
ItemOfList *left;
ItemOfList *right;
ItemOfList() {
_component = NULL;
left = NULL;
right = NULL;
}
}; // class ItemOfList
/******************************************************
* Class: <<Ontology Category>>Composite
* The class is Composite role in Composite pattern
* ***
* Concept = Singleton (single-server queue system category)
* ***************************************************/
class Composite : public Component {
protected:
ItemOfList *head;
ItemOfList *tail;
public:
Composite() {
head = NULL; tail = NULL;
/*head = new ItemOfList;
tail = head;
tail->right = new ItemOfList;
tail = tail->right;
tail->right = new ItemOfList;
tail = tail->right; */
}
}; // class Component
/******************************************************
* Class: <<Ontology System>> TreeNode
* The class is Concrete Class role in Composite pattern
* ***
* Concept = Queuing System
* ***************************************************/
class BottomNode : public Composite {
public:
Queue *queue; // *** Concept = Customer queue
QueueNode *client; // *** Concept = Client served
BottomNode () {
head = new ItemOfList;
tail = head;
head->_component = new TreeLeaf;
queue = new Queue();
input = NULL;
output = NULL;
client = NULL;
componentState == IDLE;
}
void Run() { //<<Exist>>
if (input != NULL) queue->enqueue(input);
if (head->_component->componentState == IDLE) {
output = client;
client = queue->dequeue();
head->_component->input = client;
} else output = NULL;
head->_component->Run();
}
}; // BottomNode
/******************************************************
* Class: <<Ontology System>> TopNode
* The class is Concrete Class role in Composite pattern
* ***
* Concept = Queuing System
* ***************************************************/
class TopNode : public Composite {
private:
BottomNode *bottomNode;
QueueNode *product;
public:
TopNode() {
// build restaurant
head = new ItemOfList;
head->_component = new BottomNode;
tail = head;
tail->right = new ItemOfList;
tail->right->_component = new BottomNode;
//tail = tail->right;
//tail = head;
//tail->right = new ItemOfList;
//tail->right->_component = new BottomNode;
tail = tail->right;
bottomNode = new BottomNode;
// and set inicial condition
input = NULL;
output = NULL;
componentState = IDLE;
}
void Run() { //<<Exist>>
// HACK: The algorithm need to generalization
// -- First server
head->_component->input = input;
if (head->_component->componentState == IDLE) {
tail->_component->input = head->_component->output;
bottomNode->input = new QueueNode("Order");
};
head->_component->Run();
// -- Third server
bottomNode->Run();
// -- Second server
if (bottomNode->output != NULL) {
tail->_component->Run();
};
tail->_component->Run();
if (tail->_component->componentState == IDLE) {
output = tail->_component->output;
} else output = NULL;
}
};
/******************************************************
* Class: <<Ontology Context>> TreeRoot
* The class is Concrete Class role in Composite pattern
* ***
* Concept = Calling population (a boundary and initial condition)
* ***************************************************/
class TreeRoot: public Composite {
private:
LinkedList *headItemOfList;
LinkedList *currentItemOfList;
public:
AnsiString probe;
TreeRoot() {
head = new ItemOfList;
head->_component = new TopNode;
// time
headItemOfList = new LinkedList;
currentItemOfList = headItemOfList;
for (int i = 0; i < 0; i++) { // i < 0 is test
currentItemOfList->next = new LinkedList;
currentItemOfList = currentItemOfList->next;
};
currentItemOfList = headItemOfList;
}
void Run() { //<<Exist>>
int firs_value = 0; int width_value = 100; // for random
currentItemOfList = headItemOfList;
while (currentItemOfList != NULL) {
int random_number = firs_value + rand() % width_value;
if (random_number <= 100) { // random_number <= 100 is test
head->_component->input = new QueueNode("Car");
} else { head->_component->input = NULL; };
head->_component->Run();
if (head->_component->output != NULL) {
probe = head->_component->output->_data;
delete head->_component->output;
} else {probe = "NULL";};
currentItemOfList = currentItemOfList->next;
}; // while (currentItemOfList != NULL)
}
};
//---------------------------------------------------------------------------
#endif