-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.cpp
328 lines (299 loc) · 12.4 KB
/
platform.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
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
#include "platform.hpp"
#include "carrier.hpp"
#include "destroyer.hpp"
#include "fighter.hpp"
#include "station.hpp"
#include "human.hpp"
#include "alien.hpp"
#include "sale.hpp"
using namespace std;
// -- Destructor --
platform::~platform() {
for (unsigned int i = 0; i < vehicles.size(); i++) {
delete vehicles[i];
}
for (unsigned int i = 0; i < owners.size(); i++) {
delete owners[i];
}
}
// -- List functions --
void platform::lavailable() {
vector<vehicle*> tempvector;
vehicle *temp;
for (unsigned int i = 0; i < vehicles.size(); i++) {
if (checksales(vehicles[i]->rvrn()) == 0) { tempvector.push_back(vehicles[i]); }
}
int a;
for (unsigned int i = 1; i < tempvector.size(); i++) { // Using insertion sorting as we are expecting low amounts of vehicles for testing
temp = tempvector[i];
for (a = i - 1; (a >= 0) && (vehicles[a]->rvrn() < vehicles[i]->rvrn()); a--) /* Overloaded operators at string.h lets us do _StringA < _StringB directly */{
tempvector[a + 1] = tempvector[a];
}
tempvector[a + 1] = temp;
}
for (unsigned int i = 0; i < tempvector.size(); i++) { tempvector[i]->show(); }
}
void platform::lall() {
vector<vehicle*> tempvector = vehicles;
vehicle *temp;
int a;
for (unsigned int i = 1; i < tempvector.size(); i++) { // Using insertion sorting as we are expecting low amounts of vehicles for testing
temp = tempvector[i];
for (a = i - 1; (a >= 0) && (vehicles[a]->rvrn() < vehicles[i]->rvrn()); a--){
tempvector[a + 1] = tempvector[a];
}
tempvector[a + 1] = temp;
}
for (unsigned int i = 0; i < tempvector.size(); i++) {
tempvector[i]->show();
}
}
void platform::lbyocapacity() {
vector<vehicle*> tempvector = vehicles;
vehicle *temp;
int a;
for (unsigned int i = 1; i < tempvector.size(); i++) { // Using insertion sorting as we are expecting low amounts of vehicles for testing
temp = tempvector[i];
for (a = i - 1; (a >= 0) && (tempvector[a]->ocapacity() < tempvector[i]->ocapacity()); a--){
tempvector[a + 1] = tempvector[a];
}
tempvector[a + 1] = temp;
}
for (unsigned int i = 0; i < tempvector.size(); i++) {
tempvector[i]->show();
}
}
void platform::lbyowner(string rn) {
for (unsigned int i = 0; i < sales.size(); i++) {
if (sales[i].rorn() == rn) { sales[i].showsale(); }
}
}
void platform::lfordate(date saledate) {
for (unsigned int i = 0; i < sales.size(); i++) {
if (sales[i].equalthan(saledate) == 1) {
for (unsigned int j = 0; j < vehicles.size(); j++) {
if (sales[i].rvrn() == vehicles[j]->rvrn()) { vehicles[j]->show(); }
}
}
}
}
void platform::listowners() {
vector<owner*> tempvector = owners;
owner *temp;
int type = 0, a;
for (unsigned int i = 1; i < tempvector.size(); i++) { // Using insertion sorting as we are expecting low amounts of owners for testing
temp = tempvector[i];
for (a = i - 1; (a >= 0) && (owners[a]->rrn() < owners[i]->rrn()); a--) {
tempvector[a + 1] = tempvector[a];
}
tempvector[a + 1] = temp;
}
for (unsigned int i = 0; i < tempvector.size(); i++) {
tempvector[i]->show();
}
}
void platform::showweapons(int position) {
int nweapons;
if (vehicles[position]->checktype() == 3) {
nweapons = 2;
cout << "Weapon 1 type:: ";
vehicles[position]->showweapon(1);
cout << "Weapon 2 type: ";
vehicles[position]->showweapon(2);
}
else {
nweapons = vehicles[position]->nweapons(); // function of destroyer
for (int i = 0; i < nweapons; i++) {
cout << "Position " << i << " weapon type:";
vehicles[position]->showweapon(i); // function of weapon in vector of destroyer
}
}
}
void platform::lsales() {
for (unsigned int i = 0; i < sales.size(); i++) {
sales[i].showsale();
}
}
void platform::lsales(date start, date end) {
for (unsigned int i = 0; i < sales.size(); i++){
if ((sales[i].greaterequalthan(start) == 1) && (sales[i].greaterequalthan(end) == 0)) { sales[i].showsale(); }
}
}
// -- Create functions --
void platform::createhuman(string rn, string planet) {
human *a = new human(rn, planet);
owners.push_back(a);
}
void platform::createalien(string rn, string planet){
alien *a = new alien(rn, planet);
owners.push_back(a);
}
void platform::createfighter(int ms, int propulsiont, int pricet, weapon weapon1, weapon weapon2, string ownern, string rn){
owner *b = owners[ownerposition(ownern)];
fighter *a = new fighter(ms, propulsiont, pricet, weapon1, weapon2, b, rn);
vehicles.push_back(a);
}
void platform::createcarrier(int ml, int cs, bool es, int propulsion, int maxcrew, int price, string ownern, string rn) { // creates a carrier with given parameters and includes it in vehicles vector
owner *b = owners[ownerposition(ownern)];
carrier *a = new carrier(ml, cs, es, propulsion, maxcrew, price, b, rn);
vehicles.push_back(a);
}
void platform::createdestroyer(vector<weapon> weapons, int propulsion, int maxcrew, int price, string ownern, string rn) { // creates a destroyer with given parameters and includes it in vehicles vector
owner *b = owners[ownerposition(ownern)];
destroyer *a = new destroyer(weapons, propulsion, maxcrew, price, b, rn);
vehicles.push_back(a);
}
void platform::createstation(int maxp, int hn, bool eshield, int propulsion, int maxcrew, int price, string ownern, string rn) { // creates a station with given parameters and includes it in vehicles vector
owner *b = owners[ownerposition(ownern)];
station *a = new station(maxp, hn, eshield, propulsion, maxcrew, price, b, rn);
vehicles.push_back(a);
}
// -- Modify functions --
void platform::modifyowner(int position, string nrn) {
for (unsigned int i = 0; i < sales.size(); i++) {
if (sales[i].rorn() == owners[position]->rrn()) {
sales[i].changeowner(nrn);
}
}
owners[position]->modify(nrn);
}
void platform::modifyowner(int position, string planet, int a) { owners[position]->modifyplanet(planet); }
void platform::modifyvehicle(int position, string nrn) {
for (unsigned int i = 0; i < sales.size(); i++) {
if (sales[i].rvrn() == vehicles[position]->rvrn()) {
sales[i].changevehicle(nrn);
}
}
vehicles[position]->modifyvrn(nrn);
}
void platform::modifyvehicleowner(int position, int oposition) {
owner *a = owners[oposition];
vehicles[position]->modifyowner(a);
for (unsigned int i = 0; i < sales.size(); i++) {
if (sales[i].rvrn() == vehicles[position]->rvrn()) {
sales[i].changeowner(owners[oposition]->rrn());
}
}
}
void platform::modifyvehicle(int position, int code, int value) { // code-based to avoid creating too many functions
if (code == 1) { vehicles[position]->modifycs(value); }
else if (code == 2) { vehicles[position]->modifyml(value); }
else if (code == 3) { vehicles[position]->addweapon(value); }
else if (code == 4) { vehicles[position]->removeweapon(value); }
else if (code == 5) { vehicles[position]->modifyms(value); }
else if (code == 6) { vehicles[position]->modifymp(value); }
else if (code == 7) { vehicles[position]->modifyhn(value); }
}
void platform::modifyvehicle(int position, int wposition, weapon weapon) { vehicles[position]->modifyweapon(wposition, weapon); }
void platform::modifyvehicle(int position, bool es) { vehicles[position]->modifyes(es); }
void platform::modifypropulsion(int position, int propulsion) { vehicles[position]->modifypropulsion(propulsion); }
void platform::modifymcrew(int position, int maxcrew) { vehicles[position]->modifymaxcrew(maxcrew); }
void platform::modifyprice(int position, int price) { vehicles[position]->modifyprice(price); }
// -- Remove functions --
void platform::removeowner(int position) { delete owners[position]; owners.erase(owners.begin() + position); }
void platform::removevehicle(int position) { delete vehicles[position]; vehicles.erase(vehicles.begin() + position); }
// -- Sell functions --
void platform::sell(string vrn, string rn, date saledate) {
sale newsale(vrn, rn, saledate);
sales.push_back(newsale);
owner *newowner = owners[ownerposition(rn)];
vehicles[vehicleposition(vrn)]->modifyowner(newowner);
}
// -- Check functions --
bool platform::checkowner(string rn) {
for (unsigned int i = 0; i < owners.size(); i++) {
if (owners[i]->rrn() == rn) { return 1; } // checks if the given rn is equal to the owner rn
}
return 0;
}
bool platform::checkvehicle(string rn) {
for (unsigned int i = 0; i < vehicles.size(); i++) {
if (vehicles[i]->rvrn() == rn) { return 1; } // checks if the given rn is equal to the vehicle rn
}
return 0;
}
bool platform::checksales(string vrn) {
for (unsigned int i = 0; i < sales.size(); i++) {
if (sales[i].rvrn() == vrn) { return 1; } // checks if the given rn is equal to the sold vehicle rn
}
return 0;
}
bool platform::checkdate(date d) {
if (d.month <= 0 || d.month > 12 || d.day <= 0 || d.year <= 0) { return 0; }
else if (d.month == 1 || d.month == 3 || d.month == 5 || d.month == 7 || d.month == 9 || d.month == 1) {
if (d.day > 31) { return 0; }
}
else if (d.month == 2){
if (d.year % 4 == 0 && d.year % 100 != 0) { // leap years
if (d.day > 29) { return 0; }
}
else {
if (d.day > 28) { return 0; }
}
}
else {
if (d.day > 30) { return 0; }
}
return 1;
}
int platform::checktype(string rn){
int length = rn.length();
if (length!=10 && length!=9 && length!=8) return 0; // will only check valid lengths
int position = 0, check = checkchar(rn.at(position));
if (check == 1){
for(int i=1;i<8;i++){ // 9 letters
check = checkchar(rn.at(i));
if(check != 1) return 0;
}
check = checkchar(rn.at(8));
if (check == 1 && length == 10){ // + 1 letter = alien
if (checkchar(rn.at(9)) == 1 && length == 10) return 2;
else { return 0; }
}
else if (check == 2 && length == 9) return 1; // or + 1 number = human
else return 0;
} else if (check == 2){ // 1 letter
for(int i=1;i<5;i++){ // + 4 numbers
check = checkchar(rn.at(i));
if(check != 1) return 0;
}
for(int i=5;i<8;i++){ // + 3 letters = vehicle
check = checkchar(rn.at(i));
if(check != 2) return 0;
}
if (length == 8) return 3; else return 0;
} else return 0;
return 0;
}
int platform::checkchar(char position){ // Note: solved like this instead of with header <cctype> because we would be using only two functions and the code isnt enlargered as much
if (position == '0'||position == '1'||position == '2'||position == '3'||position == '4'||position == '5'||position == '6'||position == '7'||position == '8'||position == '9'){
return 1;
} else if (position == 'a'||position == 'b'||position == 'c'||position == 'd'||position == 'e'||position == 'f'||position == 'g'||position == 'h'||position == 'i'||position == 'j'||position == 'k'||position == 'l'||position == 'm'||position == 'n'||position == 'o'||position == 'p'||position == 'q'||position == 'r'||position == 's'||position == 't'||position == 'u'||position == 'v'||position == 'w'||position == 'x'||position == 'y'||position == 'z'||position == 'A'||position == 'B'||position == 'C'||position == 'D'||position == 'E'||position == 'F'||position == 'G'||position == 'H'||position == 'I'||position == 'J'||position == 'K'||position == 'L'||position == 'M'||position == 'N'||position == 'Ñ'||position == 'O'||position == 'P'||position == 'Q'||position == 'R'||position == 'S'||position == 'T'||position == 'U'||position == 'V'||position == 'W'||position == 'X'||position == 'Y'||position == 'Z' ){
return 2;
} else return 0;
}
// -- Auxiliary functions --
int platform::vehicleposition(string rn) { // Note: will return 3301 if its not registered but shouldnt be called without previous checkvehicle
int size = vehicles.size();
for (int i = 0; i < size; i++) {
if (vehicles[i]->rvrn() == rn) { return i; }
}
return 3301;
}
int platform::ownerposition(string rn) { // Note: will return 3301 if its not registered but shouldnt be called without previous checkvehicle
int size = owners.size();
for (int i = 0; i < size; i++) {
if (owners[i]->rrn() == rn) { return i; }
}
return 3301;
}
int platform::vsize() { return vehicles.size(); }
int platform::osize() { return owners.size(); }
int platform::ssize() { return sales.size(); }
int platform::destroyerwsize(int position) { return vehicles[position]->nweapons(); }
int platform::stationhn(int position) { return vehicles[position]->hn(); }
int platform::checkvehicletype(int position) { return vehicles[position]->checktype(); }
// -- Formatting functions --
string platform::vregister(int position) { return vehicles[position]->reg(); }
string platform::oregister(int position) { return owners[position]->reg(); }
string platform::sregister(int position) { return sales[position].reg(); }