-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.cc
189 lines (157 loc) · 5.25 KB
/
printer.cc
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
#include "printer.h"
#include <iostream>
#include <string>
using namespace std;
int Printer::getPosition(Printer::Kind kind, unsigned int lid = 0){
switch(kind){
case Printer::Kind::Parent:
return 0;
case Printer::Kind::Groupoff:
return 1;
case Printer::Kind::WATCardOffice:
return 2;
case Printer::Kind::NameServer:
return 3;
case Printer::Kind::Truck:
return 4;
case Printer::Kind::BottlingPlant:
return 5;
case Printer::Kind::Student:
return 6 + lid;
case Printer::Kind::Vending:
return 6 + numStudents + lid;
case Printer::Kind::Courier:
return 6 + numStudents + numVendingMachines + lid;
default:
return -1;
}
} // Printer::getPosition
void Printer::update(unsigned int location, Info newString){
if (!buffer[location].filled){
buffer[location] = newString;
} else {
flush(location, newString);
}
} // Printer::update
void Printer::flush(unsigned int location, Info newString){
for(unsigned int i = 0; i < 6 + numStudents + numVendingMachines + numCouriers; i++){
if (i){
cout << "\t";
}
if (buffer[i].filled){
// cout << buffer[i]; // print if there is usefui information
if (buffer[i].numValues == 0){
cout << buffer[i].state;
} else if (buffer[i].numValues == 1){
cout << buffer[i].state << to_string(buffer[i].value1);
} else if (buffer[i].numValues == 2){
cout << buffer[i].state << to_string(buffer[i].value1) << "," << to_string(buffer[i].value2);
}
buffer[i].filled = false; // flush old data
}
if (i == location){ // add new data
buffer[i] = newString;
}
}
cout << endl;
} // Printer::flush
Printer::Printer( unsigned int numStudents, unsigned int numVendingMachines, unsigned int numCouriers ):
numStudents(numStudents), numVendingMachines(numVendingMachines), numCouriers(numCouriers)
{
unsigned int numColumns = 6 + numStudents + numVendingMachines + numCouriers;
buffer = new Info[numColumns];
// parent header
cout << "Parent" << "\t";
// groupoff header
cout << "Gropoff" << "\t";
// watcard office header
cout << "WATOff" << "\t";
// name server header
cout << "Names" << "\t";
// truck header
cout << "Truck" << "\t";
// bottling plant header
cout << "Plant" << "\t";
// students header
for( unsigned int i = 0; i < numStudents; i++){
// print voter ids in the header
cout << "Stud" << i << "\t";
}
// vending machines header
for( unsigned int i = 0; i < numVendingMachines; i++){
// print voter ids in the header
cout << "Mach" << i << "\t";
}
// courier header
for( unsigned int i = 0; i < numCouriers; i++){
// print voter ids in the header
cout << "Cour" << i << "\t";
}
// print sepearator in header
cout << endl;
for( unsigned int i = 0; i < numColumns; i++){
// print separators in the header
if (i){
cout << "\t";
}
cout << "*******";
}
cout << endl;
} // Printer::Printer
Printer::~Printer(){
// final flush
Info newString;
flush(Printer::Kind::Parent, newString);
// print footer
cout << "***********************" << endl;
// delete buffer
delete [] buffer;
} // Printer::~Printer
void Printer::print( Printer::Kind kind, char state ){
Info newString;
newString.filled = true;
newString.state = state;
newString.numValues = 0;
update(getPosition(kind), newString);
} // Printer::print
void Printer::print( Printer::Kind kind, char state, unsigned int value1 ){
Info newString;
newString.filled = true;
newString.state = state;
newString.value1 = value1;
newString.numValues = 1;
update(getPosition(kind), newString);
} // Printer::print
void Printer::print( Printer::Kind kind, char state, unsigned int value1, unsigned int value2 ){
Info newString;
newString.filled = true;
newString.state = state;
newString.value1 = value1;
newString.value2 = value2;
newString.numValues = 2;
update(getPosition(kind), newString);
} // Printer::print
void Printer::print( Printer::Kind kind, unsigned int lid, char state ){
Info newString;
newString.filled = true;
newString.state = state;
newString.numValues = 0;
update(getPosition(kind, lid), newString);
} // Printer::print
void Printer::print( Printer::Kind kind, unsigned int lid, char state, unsigned int value1 ){
Info newString;
newString.filled = true;
newString.state = state;
newString.value1 = value1;
newString.numValues = 1;
update(getPosition(kind, lid), newString);
} // Printer::print
void Printer::print( Printer::Kind kind, unsigned int lid, char state, unsigned int value1, unsigned int value2 ){
Info newString;
newString.filled = true;
newString.state = state;
newString.value1 = value1;
newString.value2 = value2;
newString.numValues = 2;
update(getPosition(kind, lid), newString);
} // Printer::print