-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.cpp
246 lines (243 loc) · 11.1 KB
/
init.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
#include "init.hpp"
init::init(platform *_platforme) { platforme = _platforme; }
void init::initialize() { // Note: any non-valid parameter will make the program to ignore the following text in the file
cout << "Initializing registered values..." << endl << endl;
ifstream owner("OwnerRegister.txt");
if (!owner) { cerr << "Error: No owner register files encountered" << endl; }
else {
string rn, planet;
int i = 0;
owner.seekg(0, ios_base::end);
streamoff len = owner.tellg(); // get the lenght of the file to check if it is 0
owner.seekg(0, ios_base::beg); // returns to the beginning of the file before reading values
if (len == 0) { cerr << "Error: empty file" << endl; owner.ignore(); }
while (!owner.eof()) { // duplication of the last line due to this practise will be ignored because no two owners can have the same rn
owner >> rn >> planet;
if (platforme->checktype(rn) == 1 && platforme->checkowner(rn) == 0) { platforme->createhuman(rn, planet); i++; }
else if (platforme->checktype(rn) == 2 && platforme->checkowner(rn) == 0) { platforme->createalien(rn, planet); i++; }
else if (platforme->checktype(rn) != 2 && platforme->checktype(rn) != 1) cerr << "Error: corrupted data encountered" << endl;
}
cout << i << " owners initialized" << endl;
}
owner.close();
ifstream vehicle("VehicleRegister.txt");
if (!vehicle) { cerr << "Error: No vehicle register files encountered" << endl; }
else {
string vrn, owner;
int ii = 0, type, price, maxcrew, propulsion, maxload, maxp, hn, cs, size, wtype, maxspeed, wt1, wt2;
bool es;
vector<weapon> weapons;
weapon w1(1), w2(2);
vehicle.seekg(0, ios_base::end);
streamoff len = vehicle.tellg(); // get the lenght of the file to check if it is 0
vehicle.seekg(0, ios_base::beg); // returns to the beginning of the file before reading values
if (len == 0) { cerr << "Error: empty file" << endl; vehicle.ignore(); } // ignore the first read of vehicle so that eof flag can be set to 1 and the program doesnt enter in the while loop
while (!vehicle.eof()) { // duplication of the last line due to this practise will be ignored because no two vehicles can have the same rn
vehicle >> vrn >> owner >> type >> propulsion >> maxcrew >> price;
if (platforme->checktype(vrn) != 3 && !vehicle.fail()) { cerr << "Error: corrupted data encountered" << endl; type = 5; }
if (vehicle.fail()) type = 5;
if (platforme->checkvehicle(vrn) == 1) { type = 5; }
if (platforme->checktype(owner) != 1 && platforme->checktype(owner) != 2) {
cerr << "Error: corrupted data encountered" << endl;
type = 5;
}
if (platforme->checkowner(owner) == 0) {
cerr << "Error: vehicle with unregistered owner" << endl;
type = 5;
}
switch (type) {
case 1:
vehicle >> maxload >> es >> cs;
if (!vehicle.fail()) { platforme->createcarrier(maxload, cs, es, propulsion, maxcrew, price, owner, vrn); ii++; }
break;
case 2:
vehicle >> size;
weapons.clear(); // Important so that if there is more than one destroyer the weapons dont stack each loop
for (int i = 0; i < size; i++) {
vehicle >> wtype;
weapon a(wtype);
weapons.push_back(a);
}
if (!vehicle.fail()) {
platforme->createdestroyer(weapons, propulsion, maxcrew, price, owner, vrn);
ii++;
}
break;
case 3:
vehicle >> maxspeed >> wt1 >> wt2;
w1.modify(wt1);
w2.modify(wt2);
if (!vehicle.fail()) { platforme->createfighter(maxspeed, propulsion, price, w1, w2, owner, vrn); ii++; }
break;
case 4:
vehicle >> maxp >> hn >> es;
if (!vehicle.fail()) { platforme->createstation(maxp, hn, es, propulsion, maxcrew, price, owner, vrn); ii++; }
break;
case 5:
break;
}
}
cout << ii << " vehicles initialized" << endl;
}
vehicle.close();
ifstream sale("SaleRegister.txt");
if (!sale) { cerr << "Error: No sale register files encountered" << endl; }
else {
string rn, vrn;
int i = 0, rntype, vrntype;
bool checksold, checkrn, checkvrn, checkdate;
date date;
sale.seekg(0, ios_base::end);
streamoff len = sale.tellg(); // get the lenght of the file to check if it is 0
sale.seekg(0, ios_base::beg); // returns to the beginning of the file before reading values
if (len == 0) { cerr << "Error: empty file" << endl; sale.ignore(); }
while(!sale.eof()) { // duplication of the last line due to this practise will be ignored because no two sales can have the same vehicle rn
sale >> vrn >> rn >> date.day >> date.month >> date.year;
rntype = platforme->checktype(rn);
vrntype = platforme->checktype(vrn);
checkdate = platforme->checkdate(date);
checkrn = platforme->checkowner(rn);
checkvrn = platforme->checkvehicle(vrn);
checksold = platforme->checksales(vrn);
if (checkrn == 1 && checkvrn == 1 && checksold == 0 && vrntype == 3 && checkdate == 1 && (rntype == 1 || rntype == 2) && !sale.fail()) {
platforme->sell(vrn, rn, date);
i++;
}
else if (checkrn == 0 && !sale.fail()) { cerr << "Error: Sale with unregistered owner" << endl; }
else if (checkvrn == 0 && !sale.fail()) { cerr << "Error: Sale with unregistered vehicle" << endl; }
else if (checksold == 1 && !sale.fail()) { cerr << "Error: Repeated sale" << endl; }
else if (checkdate == 0 || (rntype != 1 && rntype != 2) || vrntype != 3) cerr << "Error: corrupted data encountered" << endl;
}
cout << i << " sales initialized" << endl;
}
sale.close();
}
void init::initialize(string VehicleFile, string OwnerFile, string SaleFile) {
cout << "Initializing registered values..." << endl << endl;
ifstream owner(OwnerFile.c_str());
if (!owner) { cerr << "Error: No owner register files encountered" << endl; }
else {
string rn, planet;
int i = 0;
owner.seekg(0, ios_base::end);
streamoff len = owner.tellg(); // get the lenght of the file to check if it is 0
owner.seekg(0, ios_base::beg); // returns to the beginning of the file before reading values
if (len == 0) { cerr << "Error: empty file" << endl; owner.ignore(); }
while (!owner.eof()) { // duplication of the last line due to this practise will be ignored because no two owners can have the same rn
owner >> rn >> planet;
if (platforme->checktype(rn) == 1 && platforme->checkowner(rn) == 0) { platforme->createhuman(rn, planet); i++; }
else if (platforme->checktype(rn) == 2 && platforme->checkowner(rn) == 0) { platforme->createalien(rn, planet); i++; }
else if (platforme->checktype(rn) != 2 && platforme->checktype(rn) != 1) cerr << "Error: corrupted data encountered" << endl;
}
cout << i << " owners initialized" << endl;
}
owner.close();
ifstream vehicle(VehicleFile.c_str());
if (!vehicle) { cerr << "Error: No vehicle register files encountered" << endl; }
else {
string vrn, owner;
int ii = 0, type, price, maxcrew, propulsion, maxload, maxp, hn, cs, size, wtype, maxspeed, wt1, wt2;
bool es;
vector<weapon> weapons;
weapon w1(1), w2(2);
vehicle.seekg(0, ios_base::end);
streamoff len = vehicle.tellg(); // get the lenght of the file to check if it is 0
vehicle.seekg(0, ios_base::beg); // returns to the beginning of the file before reading values
if (len == 0) { cerr << "Error: empty file" << endl; vehicle.ignore(); } // ignore the first read of vehicle so that eof flag can be set to 1 and the program doesnt enter in the while loop
while (!vehicle.eof()) { // duplication of the last line due to this practise will be ignored because no two vehicles can have the same rn
vehicle >> vrn >> owner >> type >> propulsion >> maxcrew >> price;
if (platforme->checktype(vrn) != 3 && !vehicle.fail()) { cerr << "Error: corrupted data encountered" << endl; type = 5; }
if (vehicle.fail()) type = 5;
if (platforme->checkvehicle(vrn) == 1) { type = 5; }
if (platforme->checktype(owner) != 1 && platforme->checktype(owner) != 2) {
cerr << "Error: corrupted data encountered" << endl;
type = 5;
}
if (platforme->checkowner(owner) == 0) {
cerr << "Error: vehicle with unregistered owner" << endl;
type = 5;
}
switch (type) {
case 1:
vehicle >> maxload >> es >> cs;
if (!vehicle.fail()) { platforme->createcarrier(maxload, cs, es, propulsion, maxcrew, price, owner, vrn); ii++; }
break;
case 2:
vehicle >> size;
weapons.clear(); // Important so that if there is more than one destroyer the weapons dont stack each loop
for (int i = 0; i < size; i++) {
vehicle >> wtype;
weapon a(wtype);
weapons.push_back(a);
}
if (!vehicle.fail()) {
platforme->createdestroyer(weapons, propulsion, maxcrew, price, owner, vrn);
ii++;
}
break;
case 3:
vehicle >> maxspeed >> wt1 >> wt2;
w1.modify(wt1);
w2.modify(wt2);
if (!vehicle.fail()) { platforme->createfighter(maxspeed, propulsion, price, w1, w2, owner, vrn); ii++; }
break;
case 4:
vehicle >> maxp >> hn >> es;
if (!vehicle.fail()) { platforme->createstation(maxp, hn, es, propulsion, maxcrew, price, owner, vrn); ii++; }
break;
case 5:
break;
}
}
cout << ii << " vehicles initialized" << endl;
}
vehicle.close();
ifstream sale(SaleFile.c_str());
if (!sale) { cerr << "Error: No sale register files encountered" << endl; }
else {
string rn, vrn;
int i = 0, rntype, vrntype;
bool checksold, checkrn, checkvrn, checkdate;
date date;
sale.seekg(0, ios_base::end);
streamoff len = sale.tellg(); // get the lenght of the file to check if it is 0
sale.seekg(0, ios_base::beg); // returns to the beginning of the file before reading values
if (len == 0) { cerr << "Error: empty file" << endl; sale.ignore(); }
while (!sale.eof()) { // duplication of the last line due to this practise will be ignored because no two sales can have the same vehicle rn
sale >> vrn >> rn >> date.day >> date.month >> date.year;
rntype = platforme->checktype(rn);
vrntype = platforme->checktype(vrn);
checkdate = platforme->checkdate(date);
checkrn = platforme->checkowner(rn);
checkvrn = platforme->checkvehicle(vrn);
checksold = platforme->checksales(vrn);
if (checkrn == 1 && checkvrn == 1 && checksold == 0 && vrntype == 3 && checkdate == 1 && (rntype == 1 || rntype == 2) && !sale.fail()) {
platforme->sell(vrn, rn, date);
i++;
}
else if (checkrn == 0 && !sale.fail()) { cerr << "Error: Sale with unregistered owner" << endl; }
else if (checkvrn == 0 && !sale.fail()) { cerr << "Error: Sale with unregistered vehicle" << endl; }
else if (checksold == 1 && !sale.fail()) { cerr << "Error: Repeated sale" << endl; }
else if (checkdate == 0 || (rntype != 1 && rntype != 2) || vrntype != 3) cerr << "Error: corrupted data encountered" << endl;
}
cout << i << " sales initialized" << endl;
}
sale.close();
}
void init::registry() { // Note: files are overwritten with the new status each complete program execution
ofstream vehicle("VehicleRegister.txt");
for (int i = 0; i < platforme->vsize(); i++) {
vehicle << platforme->vregister(i);
}
vehicle.close();
ofstream owner("OwnerRegister.txt");
for (int i = 0; i < platforme->osize(); i++) {
owner << platforme->oregister(i);
}
owner.close();
ofstream sale("SaleRegister.txt");
for (int i = 0; i < platforme->ssize(); i++) {
sale << platforme->sregister(i);
}
sale.close();
}