-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadingdock.cpp
More file actions
449 lines (390 loc) · 14.4 KB
/
loadingdock.cpp
File metadata and controls
449 lines (390 loc) · 14.4 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "loadingdock.hpp"
LoadingDock::LoadingDock(const LoadingDock & rhs){
this->ship_fleet = rhs.ship_fleet;
this->all_types_ship_fleet = rhs.all_types_ship_fleet;
this->routes = rhs.routes;
this->all_cargo = rhs.all_cargo;
}
LoadingDock & LoadingDock::operator=(const LoadingDock & rhs){
if(this != &rhs){
LoadingDock temp(rhs);
std::swap(ship_fleet, temp.ship_fleet);
std::swap(all_types_ship_fleet, temp.all_types_ship_fleet);
std::swap(routes, temp.routes);
std::swap(all_cargo, temp.all_cargo);
}
return *this;
}
LoadingDock::~LoadingDock() {
for (size_t i = 0; i < all_types_ship_fleet.size(); i++) {
delete all_types_ship_fleet[i];
}
}
LoadingDock::LoadingDock(char * file_ship, char * file_cargo) {
parseFileShip(file_ship);
parseFileCargo(file_cargo);
}
LoadingDock::LoadingDock(char * file_ship) {
parseFileShip(file_ship);
printRoutes();
}
void LoadingDock::parseFileShip(char * file_ship) {
std::ifstream file(file_ship);
if (file.is_open()) {
std::string line;
while (getline(file, line)) {
Ship temp_ship(line);
if (!checkName(temp_ship.getName(), ship_fleet)) {
this->ship_fleet.push_back(temp_ship);
}
else {
std::cerr << "Ship has the same name of ship in the fleet" << std::endl;
exit(EXIT_FAILURE);
}
Route temp_route(temp_ship.getSourceShip(),
temp_ship.getDestinationShip(),
temp_ship.getTotalCapacityShip());
int route_index = checkRoute(temp_route, routes);
if (route_index == -1) {
routes.push_back(temp_route);
}
else {
routes[route_index].addCapacity(temp_ship.getTotalCapacityShip());
}
}
}
else {
std::cerr << "Error opening file." << std::endl;
exit(EXIT_FAILURE);
}
file.close();
std::sort(routes.begin(), routes.end());
}
void LoadingDock::printRoutes() {
for (size_t i = 0; i < routes.size(); i++) {
std::cout << routes[i];
}
}
void LoadingDock::parseFileCargo(char * file_cargo) {
std::ifstream file(file_cargo);
if (file.is_open()) {
std::string line;
while (getline(file, line)) {
Cargo temp_cargo(line);
this->all_cargo.push_back(temp_cargo);
}
}
else {
std::cerr << "Error opening file." << std::endl;
exit(EXIT_FAILURE);
}
file.close();
}
std::vector<std::string> LoadingDock::seperateElements(std::string line) {
std::string delim = ",";
std::vector<std::string> elements;
size_t index = 0;
std::string token;
std::string line_edit = line;
while ((index = line_edit.find(delim)) != std::string::npos) {
token = line_edit.substr(0, index);
elements.push_back(token);
line_edit.erase(0, index + delim.length());
}
if (!line_edit.empty()) {
elements.push_back(line_edit);
}
return elements;
}
//creates ship objects with the type Container, Tanker, or Animal for dynamic dispatch
void LoadingDock::findShipType() {
for (size_t i = 0; i < ship_fleet.size(); i++) {
std::string type_info = ship_fleet[i].getTypeInfo();
std::string ship_type = ship_fleet[i].checkTypeInfo(type_info);
std::vector<std::string> elem = seperateElements(type_info);
std::string name = ship_fleet[i].getName();
std::string source = ship_fleet[i].getSourceShip();
std::string dest = ship_fleet[i].getDestinationShip();
unsigned long tc = ship_fleet[i].getTotalCapacityShip();
std::vector<Cargo> cos = ship_fleet[i].getCargoOnShip();
std::vector<std::string> hm = ship_fleet[i].getHazards();
if (ship_type.compare("Container") == 0) {
unsigned long slot = 0;
if(elem.size() < 2){
std::cerr << "not enough input for container ship" << std::endl;
exit(EXIT_FAILURE);
}
char * endptr;
const char * slots_string = elem[1].c_str();
slot = strtoul(slots_string, &endptr, 10);
if(slots_string == endptr) {
std::cerr << "Invalid argument for slot size, conversion failed" << std::endl;
exit(EXIT_FAILURE);
}
Ship * temp_c = new Container(name, type_info, source, dest, tc, cos, hm, slot);
temp_c->setShipType("Container");
if (elem.size() >= 3) {
for (size_t i = 2; i < elem.size(); i++) {
temp_c->addHazards(elem[i]);
}
}
all_types_ship_fleet.push_back(temp_c);
}
else if (ship_type.compare("Tanker") == 0) {
signed int min = 0;
signed int max = 0;
unsigned int numTanks = 0;
if(elem.size() < 4){
std::cerr << "not enough input for tanker ship" << std::endl;
exit(EXIT_FAILURE);
}
min = atoi(elem[1].c_str());
max = atoi(elem[2].c_str());
try {
char * endptr;
numTanks = strtoul(elem[3].c_str(), &endptr, 10);
}
catch (const std::exception & e) {
std::cerr << "Invalid argument for number of tanks, for ship: " << std::endl;
exit(EXIT_FAILURE);
}
unsigned int cap_per_tank = 0;
if (tc % numTanks != 0) {
std::cerr << "Total capacity is not a multiple of number of tanks, for ship: "
<< name << " total capacity = " << tc << " and num tanks = " << numTanks
<< std::endl;
exit(EXIT_FAILURE);
}
else {
cap_per_tank = tc / numTanks;
}
Ship * temp_tank = new Tanker(
name, type_info, source, dest, tc, cos, hm, min, max, numTanks, cap_per_tank);
temp_tank->initCargoInTankVector();
temp_tank->setShipType("Tanker");
if (elem.size() >= 5) {
for (size_t i = 4; i < elem.size(); i++) {
temp_tank->addHazards(elem[i]);
}
}
all_types_ship_fleet.push_back(temp_tank);
}
else if (ship_type.compare("Animals") == 0) {
if (elem.size() != 2) {
std::cerr << "incorrect number of inputs for animal ship" << std::endl;
exit(EXIT_FAILURE);
}
unsigned int smallEnough = 0;
try {
char * endptr;
smallEnough = strtoul(elem[1].c_str(), &endptr, 10);
}
catch (const std::exception & e) {
std::cerr << "Invalid argument for small enough in animal ship" << std::endl;
exit(EXIT_FAILURE);
}
Ship * temp_animal =
new Animal(name, type_info, source, dest, tc, cos, hm, smallEnough);
temp_animal->setShipType("Animals");
all_types_ship_fleet.push_back(temp_animal);
}
}
}
bool LoadingDock::checkName(std::string name, std::vector<Ship> ships) {
if (ships.size() == 0) {
return false;
}
else {
for (size_t i = 0; i < ships.size(); i++) {
if (name == ships[i].getName()) {
return true;
}
}
}
return false;
}
int LoadingDock::checkRoute(const Route & temp_route, std::vector<Route> routes) {
if (routes.size() > 0) {
for (size_t i = 0; i < routes.size(); i++) {
if (temp_route.getSource() == routes[i].getSource()) {
if (temp_route.getDestination() == routes[i].getDestination()) {
return i;
}
}
}
}
return -1;
}
void LoadingDock::loadCargo() {
findShipType();
std::vector<Ship *> fleet_alphabetical = all_types_ship_fleet;
std::sort(fleet_alphabetical.begin(), fleet_alphabetical.end(), customOp);
for (size_t i = 0; i < all_cargo.size(); i++) {
std::vector<Ship *> possible_ships;
for (size_t j = 0; j < fleet_alphabetical.size(); j++) {
if (fleet_alphabetical[j]->matchCargo(all_cargo[i]) == true) {
possible_ships.push_back(fleet_alphabetical[j]);
}
}
if (possible_ships.size() == 0) {
}
printPossibleCargo(possible_ships, all_cargo[i]);
}
std::cout << "---Done Loading---Here are the ships---" << std::endl;
printAllShips(this->all_types_ship_fleet);
}
void LoadingDock::printAllShips(std::vector<Ship *> all_types_fleet) {
for (size_t j = 0; j < all_types_fleet.size(); j++) {
std::cout << "The " << (*all_types_fleet[j]).getShipType() << " Ship "
<< (*all_types_fleet[j]).getName() << "("
<< (*all_types_fleet[j]).getUsedCapacityShip() << "/"
<< (*all_types_fleet[j]).getTotalCapacityShip()
<< ") is carrying : " << std::endl;
if ((*all_types_fleet[j]).getCargoOnShip().size() != 0) {
std::vector<Cargo> cargo_s = (*all_types_fleet[j]).getCargoOnShip();
printCargo(cargo_s);
}
if ((*all_types_fleet[j]).getShipType().compare("Container") == 0) {
std::cout << " (" << (*all_types_fleet[j]).getSlots() << ") slots remain"
<< std::endl;
}
else if ((*all_types_fleet[j]).getShipType().compare("Tanker") == 0) {
std::cout << " " << (*all_types_fleet[j]).getNumUsedTank() << " / "
<< (*all_types_fleet[j]).getNumTanks() << " tanks used" << std::endl;
}
else if ((*all_types_fleet[j]).getShipType().compare("Animals") == 0) {
if ((*all_types_fleet[j]).hasRoamer() == true) {
std::cout << " has a roamer" << std::endl;
}
else {
std::cout << " does not have a roamer" << std::endl;
}
}
}
}
void LoadingDock::printCargo(std::vector<Cargo> & cargo_ship) {
for (size_t i = 0; i < cargo_ship.size(); i++) {
std::cout << " " << cargo_ship[i].getName() << "(" << cargo_ship[i].getWeight()
<< ")" << std::endl;
}
}
void LoadingDock::printPossibleCargo(std::vector<Ship *> cargo_ship_match,
const Cargo & cargo_obj) {
if (cargo_ship_match.size() == 0) {
std::cout << "No ships can carry the " << cargo_obj.getName() << " from "
<< cargo_obj.getSource() << " to " << cargo_obj.getDestination()
<< std::endl;
}
else {
std::cout << cargo_ship_match.size() << " ships can carry the " << cargo_obj.getName()
<< " from " << cargo_obj.getSource() << " to " << cargo_obj.getDestination()
<< std::endl;
for (size_t i = 0; i < cargo_ship_match.size(); i++) {
std::cout << " " << (*cargo_ship_match[i]).getName() << std::endl;
}
std::cout << " **Loading the cargo onto " << (*cargo_ship_match[0]).getName() << "**"
<< std::endl;
(*cargo_ship_match[0]).addCargo(cargo_obj);
}
}
/* loops through the set passed in and checks if the cargo can be loaded on to any of the ships
in the set. If 2 ships can take the cargo, the names are compared for alphabetical order
if no ship can take the cargo, the function returns NULL
*/
Ship * LoadingDock::setProcessing(std::set<Ship *, std::less<Ship *> > * potential_ship,
const Cargo & cargo_obj) {
Ship * curr_ans = NULL;
for (std::set<Ship *>::iterator it = potential_ship->begin();
it != potential_ship->end();
++it) {
Ship * temp = const_cast<Ship *>(*it);
if (temp->matchCargo(cargo_obj) == true) {
if (curr_ans == NULL) {
curr_ans = *it;
}
else {
std::string ship1_name = curr_ans->getName();
std::string ship2_name = temp->getName();
int result = ship1_name.compare(ship2_name);
if (result > 0) {
curr_ans = *it;
}
}
}
}
if (curr_ans != NULL) {
return const_cast<Ship *>(curr_ans);
}
return curr_ans;
}
/* this function stable sorts the cargo by weight, then adds all of the Ships into an AVL Tree
It then loops through the cargo and find the node in the AVL Tree that has a reamining capacity
closest (>=) to the weight of the cargo. If NULL is returned then no match is found
if a set is returned, we then process the set to run cargo ship match rules. If that returns NULL
then we look for the next largest value until we get an answer or NULL (cargo does not have a match)
we then print in the formatted output whether there was a match or not
*/
void LoadingDock::bestFit() {
findShipType();
std::vector<Cargo> cargo_sorted_largest_smallest = this->all_cargo;
std::stable_sort(cargo_sorted_largest_smallest.begin(),
cargo_sorted_largest_smallest.end(),
customOp_cargo);
AVLMultiMap<unsigned long, Ship *> avl_tree_ship_fleet;
for (size_t i = 0; i < all_types_ship_fleet.size(); i++) {
avl_tree_ship_fleet.add(all_types_ship_fleet[i]->getRemainingCapacityShip(),
all_types_ship_fleet[i]);
}
bool match_found = false;
std::set<Ship *, std::less<Ship *> > * ans_best;
Ship * match_ship_cargo;
for (size_t i = 0; i < cargo_sorted_largest_smallest.size(); i++) {
ans_best = avl_tree_ship_fleet.findBest(cargo_sorted_largest_smallest[i].getWeight());
if (ans_best != NULL) {
match_ship_cargo = setProcessing(ans_best, cargo_sorted_largest_smallest[i]);
while (match_ship_cargo == NULL) {
if (ans_best != NULL) {
unsigned long newWeight =
const_cast<Ship *>(*ans_best->begin())->getRemainingCapacityShip();
ans_best = avl_tree_ship_fleet.nextLargest(newWeight);
if (ans_best == NULL) {
match_found = false;
break;
}
else {
match_ship_cargo = setProcessing(ans_best, cargo_sorted_largest_smallest[i]);
}
}
}
if (match_ship_cargo != NULL) {
match_found = true;
}
}
else if (ans_best == NULL) {
match_found = false;
}
if (match_found == true) {
avl_tree_ship_fleet.remove(match_ship_cargo->getRemainingCapacityShip(),
match_ship_cargo);
(*match_ship_cargo).addCargo(cargo_sorted_largest_smallest[i]);
avlMatchPrint(match_ship_cargo, cargo_sorted_largest_smallest[i]);
avl_tree_ship_fleet.add(match_ship_cargo->getRemainingCapacityShip(),
match_ship_cargo);
}
else {
avlNoMatchPrint(cargo_sorted_largest_smallest[i]);
}
}
std::cout << "---Done Loading---Here are the ships---" << std::endl;
printAllShips(this->all_types_ship_fleet);
}
void LoadingDock::avlMatchPrint(Ship * ship_obj, const Cargo & cargo_obj) {
std::cout << "Loading " << cargo_obj.getName() << " onto " << ship_obj->getName()
<< " from " << cargo_obj.getSource() << " to " << cargo_obj.getDestination()
<< " " << ship_obj->getRemainingCapacityShip() << " capacity remains"
<< std::endl;
}
void LoadingDock::avlNoMatchPrint(const Cargo & cargo_obj) {
std::cout << "No ships can carry the " << cargo_obj.getName() << " from "
<< cargo_obj.getSource() << " to " << cargo_obj.getDestination() << std::endl;
}