-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountryMap.cpp
More file actions
112 lines (81 loc) · 3.45 KB
/
CountryMap.cpp
File metadata and controls
112 lines (81 loc) · 3.45 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
#include "CountryMap.h"
// Default Constructor
CountryMap::CountryMap() {
ifstream fileStream; // file stream buffer
string fileName = "SchengenCountries.txt";
fileStream.open( fileName );
if (!fileStream.is_open()) { // close program if file load fails
cout << "Could not open " << fileName << endl;
exit(0);
}
string countryName;
while ( !getline(fileStream, countryName).eof() ) {
Country newCountry( countryName, 0 );
// conver to lowercase for Map key
for ( int i= 0; i< countryName.length(); i++ ) {
countryName[i] = tolower(countryName[i]);
}
Map.insert( make_pair(countryName, newCountry) ); // insert each country into map with 0 days
}
fileStream.close();
}
// Copy Constructor
CountryMap::CountryMap( const CountryMap& copy) : Map(copy.Map), MAX_DAYS(copy.MAX_DAYS) {
}
// Destructor
CountryMap::~CountryMap() {
}
// List Countries Alphabetically
void CountryMap::showMap() const {
cout << endl << "<<<< Your Current Journey >>>>" << endl << endl;
cout << left << setw(20) << "SCHENGEN" << "DAYS" << endl << endl;
for ( auto it= Map.begin(); it != Map.end(); it++ ) {
cout << left << setw(20) << it->second.getName() << it->second.getDays() << endl;
}
cout << endl << left << setw(20) << "TOTAL DAYS USED: " << getUsedDays() << "/" << MAX_DAYS << endl << endl;
}
// Check if country inquiry is within the Schengen region
bool CountryMap::inSchengen(const string& name) const {
if ( Map.find(name) != Map.end() ) {
return true;
} else {
return false;
}
}
// How many days have been used
unsigned CountryMap::getUsedDays() const {
unsigned usedDays= 0;
for (auto it= Map.begin(); it != Map.end(); it++) {
usedDays += it->second.getDays();
}
return usedDays;
}
// Fetch Country Days
unsigned CountryMap::getCountryDays(const string& name) const {
auto requestedCountry = Map.find(name)->second;
return requestedCountry.getDays();
}
// Get MAX_DAYS value
unsigned CountryMap::getMaxDays() const {
return MAX_DAYS;
}
// Get number of countries in Schengen
unsigned CountryMap::getMapSize() const {
return Map.size();
}
// Edit days per country
void CountryMap::setCountryDays(const string& name, unsigned days) {
// if days entered exceeds the MAX_DAYS threshold
if ( (getUsedDays()+days) > MAX_DAYS ) {
cout << endl << "Oh no! That's " << (getUsedDays()+days) - MAX_DAYS <<
" days(s) over the " << MAX_DAYS << " day maximum." << endl;
cout << "You only have " << MAX_DAYS - getUsedDays() << " left to work with." << endl;
} else {
Map.find(name)->second.setDays( days );
//Map[name].setDays( days ); // does not work?
}
// if update reaches MAX_DAY threshold
if (getUsedDays() == MAX_DAYS) {
cout << endl << "Wunderbar! You've planned for the whole " << MAX_DAYS << " days!" << endl;
}
}