-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cxx
More file actions
1136 lines (1046 loc) · 35.6 KB
/
main.cxx
File metadata and controls
1136 lines (1046 loc) · 35.6 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <bits/stdc++.h>
using namespace std;
namespace csv
{
/*!
* @brief `csv::Parser` is the type that parse the CSV file and store the data in a structured way.
*/
class Parser
{
private:
/*!
* @brief `csv::Parser::record_type` is a vector of strings that represents the fields in a line in the CSV file.
*/
typedef vector<string> record_type;
/*!
* @brief `csv::Parser::title_type` is a vector of strings that represents the title fields in the CSV file.
*/
typedef vector<string> title_type;
/*!
* @brief `csv::Parser::m_title_fields` is a vector of strings that stores the title fields in the CSV file.
*/
title_type m_title_fields;
/*!
* @brief `csv::Parser::m_data` is a vector of `record_type` that stores the data in the CSV file.
*/
vector<record_type> m_data;
/*!
* @brief `csv::Parser::string_trim_result` is a struct that stores two iterators of the trimmed string.
* @details It stores the `begin` and the `end` of the trimmed string.
*/
struct string_trim_result
{
string::const_iterator begin, end;
};
/*!
* @brief `csv::Parser::string_trim` is a function that trims the string from the beginning and the end.
* @details It trims the string from the beginning and the end by removing the leading and trailing whitespaces.
* @param string_begin The beginning of the string
* @param string_end The end of the string
* @return `csv::Parser::string_trim_result` The trimmed string
*/
static string_trim_result string_trim(string::const_iterator string_begin, string::const_iterator string_end);
/*!
* @brief `csv::Parser::parse_line` is a function that parses a line in the CSV file.
* @details It parses a line in the CSV file and returns a vector of strings that represents the fields in the line.
* @param line_begin The beginning of the line
* @param line_end The end of the line
* @return `csv::Parser::record_type` The fields in the line
* @throws `std::runtime_error` If the field is not enclosed in double quotes
*/
static record_type parse_line(string::const_iterator line_begin, string::const_iterator line_end);
public:
/*!
* @brief `csv::Parser::Parser` is a constructor that initializes the `csv::Parser` object.
* @details It initializes the `csv::Parser` object with the title line in the CSV file.
* @param title_line The title line in the CSV file
*/
Parser(string const &title_line)
: m_title_fields(parse_line(title_line.begin(), title_line.end()))
, m_data()
{}
/*!
* @brief `csv::Parser::add_records` is a function that adds the records in the CSV file.
* @details It adds the records in the CSV file by parsing the data string and storing the data in the structured way.
* It also checks if the title line in the CSV file matches the title fields in the `csv::Parser` object.
* @param data_str The string that contains the data in the CSV file
*/
void add_records(string const &data_str);
/*!
* @brief `csv::Parser::titles` is a function that returns the title fields in the CSV file.
* @return `csv::Parser::title_type const &` The title fields in the CSV file
*/
title_type const &titles() const
{
return m_title_fields;
}
/*!
* @brief `csv::Parser::record_at` is a function that returns the record at the specified index.
* @param index The index of the record
* @return `csv::Parser::record_type const &` The record at the specified index
* @throws `std::out_of_range` If the index is more than `record_count()`
*/
record_type const &record_at(size_t index) const
{
return m_data.at(index);
}
/*!
* @brief `csv::Parser::field_at` is a function that returns the field at the specified index in the record at the specified index.
* @param record_index The index of the record
* @param field_index The index of the field
* @return `std::string const &` The field at the specified index in the record at the specified index
* @throws `std::out_of_range` If the index is more than `record_count()` or `field_count()`
*/
string const &field_at(size_t record_index, size_t field_index) const
{
return record_at(record_index).at(field_index);
}
/*!
* @brief `csv::Parser::record_count` is a function that returns the number of records in the CSV file.
* @return `std::size_t` The number of records in the CSV file
*/
size_t record_count() const
{
return m_data.size();
}
/*!
* @brief `csv::Parser::field_count` is a function that returns the number of fields in the CSV file.
* @return `std::size_t` The number of fields in the CSV file
*/
size_t field_count() const
{
return m_title_fields.size();
}
};
Parser::string_trim_result Parser::string_trim(string::const_iterator string_begin, string::const_iterator string_end)
{
typedef string::const_iterator Iterator;
Iterator new_begin = string_begin, new_last = string_end - 1;
while (new_begin != string_end && !isgraph(*new_begin))
++new_begin;
while (new_last != new_begin && !isgraph(*new_last))
--new_last;
Parser::string_trim_result result;
result.begin = new_begin;
result.end = new_last + 1;
return result;
}
Parser::record_type Parser::parse_line(string::const_iterator line_begin, string::const_iterator line_end)
{
typedef string::const_iterator Iterator;
Parser::string_trim_result const trimmed = string_trim(line_begin, line_end);
// Each field is enclosed in double quotes
Parser::record_type fields;
Iterator field_begin = trimmed.begin, field_end;
do
{
field_begin = find(field_begin, trimmed.end, '\"');
field_end = find(field_begin + 1, trimmed.end, '\"');
if (field_begin == trimmed.end)
break; // No more fields
if (field_begin != trimmed.end && field_end == trimmed.end)
throw runtime_error("Invalid CSV format: missing quote");
fields.push_back(string(field_begin + 1, field_end));
field_begin = field_end + 1;
} while (true);
return fields;
}
void Parser::add_records(string const &data_str)
{
typedef string::const_iterator Iterator;
// Trim the data string
Parser::string_trim_result const trimmed = string_trim(data_str.begin(), data_str.end());
// Parse the title line
Iterator record_begin = trimmed.begin;
Iterator record_end = find(record_begin, trimmed.end, '\n');
Parser::record_type const title_fields = parse_line(record_begin, record_end);
if (title_fields != this->titles())
throw runtime_error("Invalid CSV format: title line mismatch");
// Parse the data lines
while (record_end != trimmed.end)
{
record_begin = record_end + 1;
record_end = find(record_begin, trimmed.end, '\n');
Parser::record_type const fields = parse_line(record_begin, record_end);
if (fields.size() != this->field_count())
throw runtime_error("Invalid CSV format: field count mismatch");
m_data.push_back(fields);
}
}
} // namespace csv
namespace mail
{
/*!
* @brief `mail::Location` is an abstract class that represents a location.
*/
class Location
{
public:
/*!
* @brief `mail::Location::~Location` is a pure virtual destructor of the `mail::Location` class.
* @details This ensures that the `mail::Location` class is an abstract class and won't cause memory leak.
*/
virtual ~Location() = 0;
/*!
* @brief `mail::Location::operator<` is a function that compares two locations.
* @details It compares two locations by comparing their string representations. This is to enable the use of `std::map`.
* @param other The other location
* @return `bool` `true` if the current location is less than the other location, `false` otherwise
*/
bool operator<(Location const &other) const
{
return (string)(*this) < (string)(other);
}
/*!
* @brief `mail::Location::operator==` is a function that compares two locations.
* @details It compares two locations by comparing their string representations. This is to enable the use of `std::map`.
* @param other The other location
* @return `bool` `true` if the current location is equal to the other location, `false` otherwise
*/
bool operator==(Location const &other) const
{
return (string)(*this) == (string)(other);
}
/*!
* @brief `mail::Location::operator string` is a pure virtual function that returns the string representation of the location.
*/
virtual operator string() const = 0;
/*!
* @brief `mail::Location::to_string` is a pure virtual function that returns the string representation of the location with more information that suitable for human reading.
*/
virtual string to_string() const = 0;
};
/*!
* @brief `mail::Location::~Location` is using the default destructor created by the compiler.
* @details This ensures that the use of `mail::Location` won't cause any linker error.
*/
Location::~Location() = default;
/*!
* @brief `mail::FromLocation` is a class that represents a location from a city.
*/
class FromLocation : public Location
{
protected:
/*!
* @brief `mail::FromLocation::city` is a string that represents the city of the location.
*/
string city;
public:
/*!
* @brief `mail::FromLocation::FromLocation` is a constructor that initializes the `mail::FromLocation` object.
*/
FromLocation(string const &city)
: city(city)
{
}
/*!
* @brief `mail::FromLocation::~FromLocation` is using the default destructor created by the compiler.
* @details This ensures that the use of `mail::FromLocation` won't cause any linker error or memory leakage.
*/
virtual ~FromLocation() = default;
/*!
* @brief `mail::FromLocation::operator string` is a function that returns the string representation of the location.
*/
virtual operator string() const override
{
return "{ " + this->city + " }";
}
/*!
* @brief `mail::FromLocation::to_string` is a function that returns the string representation of the location with more information.
*/
virtual string to_string() const override
{
return "From: { " + this->city + " }";
}
};
/*!
* @brief `mail::ToLocation` is a class that represents a location to a city.
*/
class ToLocation : public Location
{
protected:
/*!
* @brief `mail::ToLocation::city` is a string that represents the city of the location.
*/
string city;
public:
/*!
* @brief `mail::ToLocation::ToLocation` is a constructor that initializes the `mail::ToLocation` object.
* @param city The city of the location
*/
ToLocation(string const &city)
: city(city)
{
}
/*!
* @brief `mail::ToLocation::~ToLocation` is using the default destructor created by the compiler.
* @details This ensures that the use of `mail::ToLocation` won't cause any linker error or memory leakage.
*/
virtual ~ToLocation() = default;
/*!
* @brief `mail::ToLocation::operator string` is a function that returns the string representation of the location.
*/
virtual operator string() const override
{
return "{ " + this->city + " }";
}
/*!
* @brief `mail::ToLocation::to_string` is a function that returns the string representation of the location with more information.
*/
virtual string to_string() const override
{
return "To: { " + this->city + " }";
}
};
/*!
* @brief `Route` is a type that represents a route from a location to another location.
*/
typedef pair<FromLocation, ToLocation> Route;
/*!
* @brief `mail::make_route` is a function that creates a route from a location to another location.
* @param from The location from
* @param to The location to
* @return `mail::Route` The route from the location `from` to the location `to`
*/
Route make_route(FromLocation from, ToLocation to)
{
return make_pair(from, to);
}
/*!
* @brief `mail::RouteToDistance` is a class that stores the distance between two locations and converts a route to its distance.
*/
class RouteToDistance
{
public:
typedef Route RouteType;
typedef unsigned int DistanceType;
protected:
/*!
* @brief `mail::RouteToDistance::distance_map_init` is a function that reads the distance from file.
* @return `std::map<mail::RouteToDistance::RouteType, mail::RouteToDistance::DistanceType>` The distance map
*/
static map<RouteType, DistanceType> distance_map_init();
/*!
* @brief `mail::RouteToDistance::distance_map_filename` is a string that represents the filename of the distance map file in CSV format.
*/
static string const distance_map_filename;
/*!
* @brief `mail::RouteToDistance::distance_map` is a map that stores the distance between two locations.
*/
static map<RouteType, DistanceType> const distance_map;
public:
/*!
* @brief `mail::RouteToDistance::exists` is a function that checks if the route exists in the distance map.
* @param route The route
* @return `bool` `true` if the route exists in the distance map, `false` otherwise
*/
bool exists(RouteType const &route) const
{
return distance_map.find(route) != distance_map.end();
}
/*!
* @brief `mail::RouteToDistance::operator()` is a function that converts a route to its distance.
* @param route The route to convert
* @return `mail::RouteToDistance::DistanceType` The distance between the two locations in the route
* @throws `std::out_of_range` If the route does not exist in the distance map
*/
DistanceType operator()(RouteType const &route) const
{
try
{
return distance_map.at(route);
}
catch (out_of_range const &)
{
throw out_of_range("Route not found");
}
}
} const route_to_distance;
map<RouteToDistance::RouteType, RouteToDistance::DistanceType> RouteToDistance::distance_map_init()
{
map<RouteToDistance::RouteType, RouteToDistance::DistanceType> distance_map;
ifstream distance_map_stream(distance_map_filename);
if (!distance_map_stream)
throw runtime_error("Failed to open distance map file");
string distance_map_title_line;
getline(distance_map_stream, distance_map_title_line);
distance_map_stream.seekg(0);
string file_buffer((istreambuf_iterator<char>(distance_map_stream)), istreambuf_iterator<char>());
csv::Parser distance_map_parser(distance_map_title_line);
distance_map_parser.add_records(file_buffer);
for (size_t i = 0; i < distance_map_parser.record_count(); ++i)
{
string const &from_city = distance_map_parser.field_at(i, 0);
string const &to_city = distance_map_parser.field_at(i, 1);
RouteToDistance::RouteType const route = make_pair(FromLocation(from_city), ToLocation(to_city));
istringstream ss(distance_map_parser.field_at(i, 2));
RouteToDistance::DistanceType distance;
ss >> distance;
distance_map[route] = distance;
}
return distance_map;
}
const string RouteToDistance::distance_map_filename = "distance.csv";
const map<RouteToDistance::RouteType, RouteToDistance::DistanceType> RouteToDistance::distance_map =
RouteToDistance::distance_map_init();
/*!
* @brief `mail::Centimeter` is a class that represents a length in centimeters.
*/
class Centimeter
{
public:
typedef long double ValueType;
private:
/*!
* @brief `mail::Centimeter::value` is a value that represents the length in centimeters.
*/
ValueType value;
public:
/*!
* @brief `mail::Centimeter::Centimeter` is a constructor that initializes the `mail::Centimeter` object.
* @param value A value not less than 0 that represents the length in centimeters
* @throws `std::runtime_error` If the value is less than 0
*/
Centimeter(ValueType value = 0) : value(value >= 0 ? value : -1)
{
if (this->value == -1)
throw runtime_error("Invalid centimeter value");
}
/*!
* @brief `mail::Centimeter::unit` is a function that returns the unit of the length.
* @return `string` The unit of the length
*/
static string unit()
{
return "cm";
}
/*!
* @brief `mail::Centimeter::operator ValueType` is a function that converts the `mail::Centimeter` object to its value.
* @return `mail::Centimeter::ValueType` The value of the `mail::Centimeter` object
*/
operator ValueType() const
{
return this->value;
}
};
/*!
* @brief `mail::Freight` is an abstract class that represents a freight.
* @example
* ```cpp
* Freight const *freight = &air_freight;
* long double volumetric_weight = freight->volumetric_weight(32, 24, 1, 1);
* ```
*/
class Freight
{
public:
/*!
* @brief `mail::Freight::~Freight` is a pure virtual destructor of the `mail::Freight` class.
* @details This ensures that the `mail::Freight` class is an abstract class and won't cause memory leak.
*/
virtual ~Freight() = 0;
/*!
* @brief `mail::Freight::volumetric_weight` is a pure virtual function that calculates the volumetric weight of this freight.
* @param l The length of the package
* @param w The width of the package
* @param h The height of the package
* @param packages The number of packages
* @return `long double` The volumetric weight of the freight
*/
virtual long double volumetric_weight(Centimeter l, Centimeter w, Centimeter h, unsigned int packages) const = 0;
/*!
* @brief `mail::Freight::operator string` is a pure virtual function that returns the string representation of the freight.
*/
virtual operator string() const = 0;
};
/*!
* @brief `mail::Freight::~Freight` is using the default destructor created by the compiler.
* @details This ensures that the use of `mail::Freight` won't cause any linker error or memory leakage.
*/
Freight::~Freight() = default;
class AirFreight : public Freight
{
public:
virtual ~AirFreight() = default;
virtual long double volumetric_weight(Centimeter l, Centimeter w, Centimeter h, unsigned int packages) const override
{
return l * w * h / 6000.0 * packages;
}
virtual operator string() const override
{
return "Air transport";
}
} const air_freight;
class OceanFreight : public Freight
{
public:
virtual ~OceanFreight() = default;
virtual long double volumetric_weight(Centimeter l, Centimeter w, Centimeter h, unsigned int packages) const override
{
return l * w * h / 1000.0 * packages;
}
virtual operator string() const override
{
return "Ocean transport";
}
} const ocean_freight;
class RailFreight : public Freight
{
public:
virtual ~RailFreight() = default;
virtual long double volumetric_weight(Centimeter l, Centimeter w, Centimeter h, unsigned int packages) const override
{
return l * w * h / 3000.0 * packages;
}
virtual operator string() const override
{
return "Rail transport";
}
} const rail_freight;
// Corresponding to the CSV file format
class PostalAddress
{
protected:
string m_user_type;
string m_country;
string m_postal_code;
string m_location;
public:
PostalAddress() = default;
PostalAddress(string ut, string c, string pc, string l)
{
m_user_type = ut;
m_country = c;
m_postal_code = pc;
m_location = l;
}
string getUserType() const
{
return m_user_type;
}
void setUserType(string ut)
{
if (ut == "business" || ut == "private")
{
m_user_type = ut;
}
else
{
cout << "Invalid user type! Choose business or private." << endl;
}
}
string getCountry() const
{
return m_country;
}
void setCountry(string c)
{
m_country = c;
}
string getPostalCode() const
{
return m_postal_code;
}
void setPostalCode(string pc)
{
m_postal_code = pc;
}
std::string getLocation() const
{
return m_location;
}
void setLocation(string l)
{
m_location = l;
}
void display()
{
std::cout << "Country: " << m_country << std::endl;
std::cout << "Postal code: " << m_postal_code << std::endl;
std::cout << "location: " << m_location << std::endl;
}
};
// Other information
class UserInfo
{
protected:
std::string m_name;
std::string m_email;
std::string m_phone_number;
public:
UserInfo() = default;
UserInfo(std::string name, std::string email, std::string phone_number)
{
std::string m_name = name;
std::string m_email = email;
std::string m_phone_number = phone_number;
}
std::string getName() const
{
return m_name;
}
void setName(const std::string &name)
{
m_name = name;
}
std::string getEmail() const
{
return m_email;
}
void setEmail(const std::string &email)
{
m_email = email;
}
std::string getPhoneNumber() const
{
return m_phone_number;
}
void setPhoneNumber(const std::string &phone_number)
{
m_phone_number = phone_number;
}
void display()
{
std::cout << "Name: " << m_name << std::endl;
std::cout << "Email: " << m_email << std::endl;
std::cout << "Phone number: " << m_phone_number << std::endl;
}
};
// Pure storage struct, no need for any method
struct Dimension
{
Centimeter length;
Centimeter width;
Centimeter height;
};
class ShipmentMode
{
private:
int choice;
string userType;
Dimension m_dimension;
string ModeOfShipment;
public:
ShipmentMode() = default;
ShipmentMode(int c, string ut)
{
choice = c;
userType = ut;
setshipmentMode(c);
}
void selectShipmentMode()
{
if (userType == "private")
{
std::cout << "Select Mode of Shipment: " << std::endl;
std::cout << "1. Document (32 x 24 x 1 cm)" << std::endl;
std::cout << "2. Moving Box (75 x 35 x 35 cm)" << std::endl;
std::cout << "3. Packages" << std::endl;
std::cout << "Enter choice: " << std::endl;
}
else if (userType == "business")
{
std::cout << "Select Mode of Shipment: " << std::endl;
std::cout << "1. Pallet (110 x 110 cm)" << std::endl;
std::cout << "2. Container" << std::endl;
std::cout << "3. Cargo" << std::endl;
std::cout << "Enter choice: " << std::endl;
}
}
void setshipmentMode(int choice)
{
if (userType == "private")
{
switch (choice)
{
case 1:
m_dimension.length = 32;
m_dimension.width = 24;
m_dimension.height = 1;
ModeOfShipment = "document";
break;
case 2:
m_dimension.length = 75;
m_dimension.width = 35;
m_dimension.height = 35;
ModeOfShipment = "moving box";
case 3:
ModeOfShipment = "packages";
break;
default:
std::cout << "Invalid choice!" << std::endl;
break;
}
}
else if (userType == "business")
{
switch (choice)
{
case 1:
m_dimension.length = 110;
m_dimension.width = 110;
ModeOfShipment = "pallet";
break;
case 2:
ModeOfShipment = "container";
break;
case 3:
ModeOfShipment = "cargo";
break;
default:
std::cout << "Invalid choice \n" << std::endl;
break;
}
if (m_dimension.length)
std::cout << "Suggested length: " << m_dimension.length;
if (m_dimension.width)
std::cout << "Suggested width: " << m_dimension.width;
if (m_dimension.height)
std::cout << "Suggested height: " << m_dimension.height;
}
}
};
class PackageInfo
{
protected:
Dimension m_dimension;
long double m_weight;
unsigned int m_quantity;
public:
PackageInfo() = default;
PackageInfo(long double length, long double width, long double height, long double weight, unsigned int quantity)
{
m_dimension.length = length;
m_dimension.width = width;
m_dimension.height = height;
m_weight = weight;
m_quantity = quantity;
}
long double getLength() const
{
return m_dimension.length;
}
void setLength(long double length)
{
m_dimension.length = length;
}
long double getWidth() const
{
return m_dimension.width;
}
void setWidth(long double width)
{
m_dimension.width = width;
}
long double getHeight() const
{
return m_dimension.height;
}
void setHeight(long double height)
{
m_dimension.height = height;
}
long double getWeight() const
{
return m_weight;
}
void setWeight(long double weight)
{
m_weight = weight;
}
unsigned int getQuantity() const
{
return m_quantity;
}
void setQuantity(unsigned int quantity)
{
m_quantity = quantity;
}
void display() const
{
std::cout << "Weight:" << m_weight << std::endl;
std::cout << "length: " << m_dimension.length << std::endl;
std::cout << "Width: " << m_dimension.width << std::endl;
std::cout << "Height " << m_dimension.height << std::endl;
std::cout << "Quantity " << m_quantity << std::endl;
}
};
class ShipmentInfo
{
protected:
UserInfo m_user;
PostalAddress m_origin;
PostalAddress m_destination;
PackageInfo m_package;
UserInfo m_consignee;
Freight const *m_service_type;
long double m_freight_weight;
long double m_cost;
static Freight const *service_type_to_freight(string const &service_type)
{
if (service_type == "Air transport")
{
return &air_freight;
}
if (service_type == "Ocean transport")
{
return &ocean_freight;
}
if (service_type == "Rail transport")
{
return &rail_freight;
}
throw runtime_error("Invalid service type");
}
public:
ShipmentInfo() = default;
ShipmentInfo(
const PostalAddress& origin,
const PostalAddress& destination,
const PackageInfo& package,
const UserInfo& user,
const UserInfo& consignee,
const std::string& service_type,
long double /* For ABI compatibility */
)
{
m_origin = origin;
m_destination = destination;
m_package = package;
m_user = user;
m_consignee = consignee;
m_service_type = service_type_to_freight(service_type);
m_freight_weight = m_service_type->volumetric_weight(
m_package.getLength(),
m_package.getWidth(),
m_package.getHeight(),
m_package.getQuantity()
);
}
// Getters
PostalAddress getOrigin() const
{
return m_origin;
}
PostalAddress getDestination() const
{
return m_destination;
}
PackageInfo getPackage() const
{
return m_package;
}
UserInfo getUser() const
{
return m_user;
}
UserInfo getconsignee() const
{
return m_consignee;
}
string getServiceType() const
{
return (string)(*m_service_type);
}
long double getCost() const
{
return m_cost;
}
long double getFreightWeight() const
{
return m_freight_weight;
}
// Setters
void setOrigin(const PostalAddress &o)
{
m_origin = o;
}
void setDestination(const PostalAddress &d)
{
m_destination = d;
}
void setPackage(const PackageInfo &p)
{
m_package = p;
}
void setconsignee(const UserInfo &s)
{
m_consignee = s;
}
void setServiceType(const string &st)
{
m_service_type = service_type_to_freight(st);
}
void setFreightWeight(long double /* For ABI compatibility */)
{
m_freight_weight = m_service_type->volumetric_weight(
m_package.getLength(),
m_package.getWidth(),
m_package.getHeight(),
m_package.getQuantity()
);
}
void setCost(long double c)
{
m_cost = c;
}
void display()
{
std::cout << "Account" << std::endl;
m_user.display();
std::cout << std::endl;
std::cout << "Origin" << std::endl;
m_origin.display();
std::cout << std::endl;
std::cout << "Destination" << std::endl;
m_destination.display();
std::cout << std::endl;
std::cout << "consignee information" << std::endl;
m_consignee.display();
std::cout << std::endl;
std::cout << "Shipment information" << std::endl;
m_package.display();
std::cout << std::endl;
std::cout << " Mode of transport: " << (string)(*m_service_type) << std::endl;
std::cout << std::endl;
std::cout << " Freight weight: " << m_freight_weight << std::endl;
std::cout << std::endl;
std::cout << "Cost: " << m_cost << std::endl;
}
};
void interface()
{
std::cout << "********************* Ship now *********************" << std::endl;
std::cout << "*******1.Please enter your account******************" << std::endl;
std::cout << "****** 2.Enter your departure and destination ******" << std::endl;
std::cout << "****** 3.Enter your service type *******************" << std::endl;
std::cout << "****** 4.Describe your shipment ********************" << std::endl;
std::cout << "****** 5.Get shipping prices ***********************" << std::endl;
std::string usertype, username, useremail, usernumber, ocountry, opostalcode, ocity, consigneename,
consigneeemail, consigneenumber, dcountry, dpostalcode, dcity, shipmentid, shipmentdate, servicetype;
char confirm;
int userChoice, type;
unsigned int packages;
while (true)
{
std::cout << "Account" << std::endl;
std::cout << "I am shipping as a.... ( business | private )" << std::endl;