-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
201 lines (177 loc) · 4.93 KB
/
main.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
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include "Office.h"
using namespace std;
enum Command
{
ENTER,
LEAVE,
PRINT,
READ,
SORT_INBOX,
SUBMIT,
WITHDRAW,
INVALID
};
Command identify_next_command(const string& cmd_str)
{
Command command;
if (cmd_str.compare("enter") == 0) {
command = ENTER;
} else if (cmd_str.compare("leave") == 0) {
command = LEAVE;
} else if (cmd_str.compare("print") == 0) {
command = PRINT;
} else if (cmd_str.compare("read") == 0) {
command = READ;
} else if (cmd_str.compare("sort-inbox") == 0) {
command = SORT_INBOX;
} else if (cmd_str.compare("submit") == 0) {
command = SUBMIT;
} else if (cmd_str.compare("withdraw") == 0) {
command = WITHDRAW;
} else {
command = INVALID;
}
return command;
}
int main(int argc, char* argv[])
{
string cmd_str;
Command command;
string doc_name;
string doc_priority;
ifstream fin;
Office *office = new Office();
if (argc < 2) {
cerr << "Usage error: ./office_sim input.txt" << endl;
exit(1);
}
/*
* Open the input file
*/
fin.open(argv[1]);
if ( !fin.good() ) {
/*
* Something is wrong. Complain and die.
*/
cout << "Error: could not open input file:" << argv[1] << endl;
exit(1);
}
/*
* Create an instance of the office class, which is aholder for
* everything else.
*/
office = new Office();
/*
* While there is another command to read from the input file, read
* it into cmd_str, identify the command, act accordingly.
*/
while (fin >> cmd_str) {
command = identify_next_command(cmd_str);
switch (command) {
case ENTER:
/*
* Worker Bee arrives at the office
*/
office->enter();
cout << "ENTERED" << endl;
break;
case INVALID:
/*
* identify_next_command does not know the command read
* in. Possibly, reading an earlier command either did not read
* enough arguments, or read too few, and left the program out
* of sync with the input.
*/
cout << "Error: invalid command string: " << cmd_str << endl;
break;
case LEAVE:
/*
* Worker Bee leaves the office. Read count is supposed to
* reflect how many documents the worker read since arriving at
* the office.
*
* FIXED: See office::leave, I think Leaving should imply that the worker has
* finished the current document
*/
office->leave();
cout << "LEFT" << endl;
cout << *office;
cout << "READCNT: " << office->get_read_count() << endl << endl;
break;
case PRINT:
/*
* Print the state of the office
*/
cout << *office << endl;
break;
case READ:
/*
* Worker Bee reads a document - should be front of highest
* priority non-empty document queue. The read_document routine
* prints out a narrative statement because it is hard to know
* what document is read at this location inthe code.
*
*/
office->read_document();
break;
case SORT_INBOX:
/*
* Worker Bee decides to sort the in-box rather than read the
* next document
*
* FIXED: See office::sort_inbox, I think sorting the in_box should imply the worker has
* finished the current document.
*/
office->sort_inbox();
cout << "In Box Sorted" << endl;
break;
case SUBMIT:
/*
* Document submitted to the office
*
* Two arguments: Name and Priority
*/
fin >> doc_name >> doc_priority;
office->submit_document(doc_name, doc_priority);
cout << "Submit: " << doc_name << " Priority: " << doc_priority << endl;
break;
case WITHDRAW:
/*
* Somebody made a mistake and came to get back a document that
* is already in the office, although we probably have to handle
* confused people who try to wthdraw a document that is not
* actually in the office.
*/
fin >> doc_name;
office->withdraw_document(doc_name);
break;
default:
/*
* Double confusion: identify_next_command managed to return an
* invlaide code. Hould be impossible, but suspenders and belt
* is better than only one.
*/
cout << "Error: invalid return value from identify_next_command: " << command << endl;
cout << "Error: command string causing this: " << cmd_str << endl;
break;
}
}
/*
* All processing of office simulation commands is
* finished. Deallocate the office instance and close the input port
* for neatness' sake.
*/
delete office;
fin.close();
/*
* Zero is the conventional exit code for success on POSIX
* systems. Non-zero return values are generally error
* codes. Sometimes negative values are errors and positive are
* result codes for forms of success.
*/
exit(0);
}