-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOffice.cpp
260 lines (233 loc) · 6.82 KB
/
Office.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
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
#include "Office.h"
using namespace std;
/*
* Constructor and destructor. Note that each element created with a
* "new" statement in the constructor has a corresponding "delete"
* statement in the destructor.
*/
Office::Office()
{
inbox_stack = new Stack;
priority_queue_one = new Queue;
priority_queue_two = new Queue;
priority_queue_three = new Queue;
in_office = false;
read_count = 0;
Document* current_document = NULL;
}
/* what all needs to be deconstructed and deallocated?
* anything with new, delete.
*/
Office::~Office()
{
delete inbox_stack;
delete priority_queue_one;
delete priority_queue_two;
delete priority_queue_three;
in_office = false;
read_count = 0;
Document* current_document = NULL;
}
/*
* Overloaded output operator for the Office class. Do not change this
* code, as it will change the output format, making the use of "diff"
* in the makefile to check correctness impossible.
*/
ostream& operator<<(std::ostream &os, Office &office)
{
os << "-------------------------" << endl;
os << "| CURRENT OFFICE STATUS |" << endl;
os << "-------------------------" << endl;
/*
* Worker state
*/
if (office.in_office) {
if (office.current_document != NULL) {
os << "READER : READING ";
os << (*office.current_document).get_name() << endl;
} else {
os << "READER : IDLE" << endl;
}
} else {
os << "READER : ABSENT" << endl;
}
/*
* In-box state
*/
if (!office.inbox_stack->is_empty()) {
os << "INBOX : ";
os << *office.inbox_stack;
os << "<-TOP" << endl;
} else {
os << "INBOX : EMPTY" << endl;
}
/*
* Now, each queue, in priority order
*/
if (!office.priority_queue_one->is_empty()) {
os << "QUEUE 1: ";
os << *office.priority_queue_one;
os << "<-FRONT" << endl;
} else {
os << "QUEUE 1: EMPTY" << endl;
}
if (!office.priority_queue_two->is_empty()) {
os << "QUEUE 2: ";
os << *office.priority_queue_two;
os << "<- FRONT" << endl;
} else {
os << "QUEUE 2: EMPTY" << endl;
}
if (!office.priority_queue_three->is_empty()) {
os << "QUEUE 3: ";
os << *office.priority_queue_three;
os << "<- FRONT" << endl;
} else {
os << "QUEUE 2: EMPTY" << endl;
}
return os;
}
/*
* Worker Bee enters and leaves the office
*/
void Office::enter()
{
/*
* These are the narrative ouput messages you will need for this
* routine, but you have to figure out the controlling code.
*
* 2 scenarios, either in or out of office.
*/
if (in_office == true) {
cout << "Error: someone is already in the office" << endl;
cout << "You cannot enter until he leaves" << endl;
} else if (in_office == false) {
in_office = true;
// cout << "You have entered the office" << endl;
}
}
void Office::leave()
{
/*
* These are the narrative ouput messages you will need for this
* routine, but you have to figure out the controlling code.
*/
if (in_office == true) {
in_office = false;
/* should fix sate variable
*/
current_document = NULL;
} else {
cout << "Error: there is nobody in the office to leave" << endl;
}
}
int Office::get_read_count()
{
return read_count;
}
/* takes the info from fin, parses, shoots here. Then it uses
* the native linked list push function.
*/
void Office::submit_document(string name, string priority)
{
Document *doc = new Document(name, priority);
inbox_stack->push(doc);
}
/*
* Read the highest priority document from the available queue.
* if queue 1 has docs, read them, if not, q2, if not, q3, if not no docs!
*/
void Office::read_document()
{
/*
* These are the narrative ouput messages you will need for this
* routine, but you have to figure out the controlling code.
*
* Go through each of the queues, if one is empty move on to the next.
* also, if no worker is in the office, no one can read.
*
*/
if (in_office) {
if (priority_queue_one->is_empty() &&
priority_queue_two->is_empty() &&
priority_queue_three->is_empty()) {
cout << "No more documents to read" << endl;
} else {
if (!priority_queue_one->is_empty()) {
current_document = priority_queue_one->dequeue();
read_count++;
} else if (!priority_queue_two->is_empty()) {
current_document = priority_queue_two->dequeue();
read_count++;
} else if (!priority_queue_three->is_empty()) {
current_document = priority_queue_three->dequeue();
read_count++;
}
cout << "READER : START " << current_document->get_name()
<< endl << endl;
}
} else {
cout << "Error: cannot read when nobody is in the office" << endl;
}
}
void Office::sort_inbox()
{
/*
* These are the narrative ouput messages you will need for this
* routine, but you have to figure out the controlling code.
*
* for each of the documents in the stack, beginning with the top,
* pop it off of the stack, and enqueue it based on priority
*
* If the inbox is being sorted, that means the current doc should have
* been read.
*/
Document* target;
current_document = NULL;
while (!inbox_stack->is_empty()) {
target = inbox_stack->pop();
if (target->get_priority() == PRIORITY_1) {
priority_queue_one->enqueue(target);
} else if (target->get_priority() == PRIORITY_2) {
priority_queue_two->enqueue(target);
} else if (target->get_priority() == PRIORITY_3) {
priority_queue_three->enqueue(target);
} else {
cout << "Error: bad priority on document:" << target << endl;
}
}
}
void Office::withdraw_document(string name)
{
/*
* These are the narrative ouput messages you will need for this
* routine, but you have to figure out the controlling code.
*
* Each of these statements is meant to try to find the document in the
* stack, if it does, it should remove it from the stack,
* otherwise, check the next structure.
*/
Document* stack_target = inbox_stack->peek(name);
Document* p1_target = priority_queue_one->peek(name);
Document* p2_target = priority_queue_two->peek(name);
Document* p3_target = priority_queue_three->peek(name);
if (stack_target != NULL) {
inbox_stack->withdraw(name);
cout << "Document " << name << " removed from inbox" << endl;
} else if (p1_target != NULL) {
priority_queue_one->withdraw(name);
cout << "Document " << name << " removed from queue 1" << endl;
} else if (p2_target != NULL) {
priority_queue_two->withdraw(name);
cout << "Document " << name << " removed from queue 2" << endl;
} else if (p3_target != NULL) {
priority_queue_three->withdraw(name);
cout << "Document " << name << " removed from queue 3" << endl;
} else {
cout << "Document " << name << " was not found" << endl;
}
stack_target = NULL;
p1_target = NULL;
p2_target = NULL;
p3_target = NULL;
}