Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1

2 #include "soccer.h"
3
4 using namespace std;
5
6 typedef map<string, Player> PlayerMap;
7 typedef map<string, Stat> StatMap;
8
9
10 void Season::print_players(const string & FileName) {
11
12 update_stats();
13 vector<Player> players_U6;
14 vector<Player> players_U8;
15 vector<Player> players_U10;
16 vector<Player> players_U12;
17 vector<Player> players_U14;
18 vector<Player> players_U17;
19 //cout << "GOT HERE" << endl;
20 auto league_itr = stats_.begin();
21 auto itr = players_.begin();
22 for(itr; league_itr != stats_.end(); ++itr)
23 {
24 if(("U" + to_string(get_league_(itr -> second.year))) == league_itr -> first)
25 {
26 if(league_itr -> first == "U6")
27 players_U6.push_back(itr -> second);
28 else if(league_itr -> first == "U8")
29 players_U8.push_back(itr -> second);
30 else if(league_itr -> first == "U10")
31 players_U10.push_back(itr -> second);
32 else if(league_itr -> first == "U12")
33 players_U12.push_back(itr -> second);
34 else if(league_itr -> first == "U14")
35 players_U14.push_back(itr -> second);
36 else if(league_itr -> first == "U17")
37 players_U17.push_back(itr -> second);
38 }
39 if(itr == players_.end())
40 {
41 itr = players_.begin();
42 ++league_itr;
43 }
44 }
45 //cout << "And now Here" << endl;
46 league_itr = stats_.begin();
47 ++league_itr;
48 ofstream OutData;
49 OutData.open(FileName);
50 OutData << "Format: Name, Year of Birth, Paid Status" << endl;
51 while(league_itr != stats_.end()){
52 if(league_itr -> first == "U6") {
53 OutData << "League: U6" << endl;
54 auto itr = players_U6.begin();
55 while(itr != players_U6.end()) {
56 OutData << itr -> first << itr -> last << " " << itr -> year << " " <<
itr -> paid << endl;
57 ++itr;
58 }
59 ++league_itr;
60 }
61 else if(league_itr -> first == "U8"){
62 OutData << "League: U8" << endl;
63 auto itr = players_U8.begin();
64 while(itr != players_U8.end()) {
65 OutData << itr -> first << itr -> last << " " << itr -> year << " " <<
itr -> paid << endl;
66 ++itr;
67 }
68 ++league_itr;
69 }
70 else if(league_itr -> first == "U10") {
71 OutData << "League: U10" << endl;
72 auto itr = players_U10.begin();
73 while(itr != players_U10.end()) {
74 OutData << itr -> first << itr -> last << " " << itr -> year << " " <<
itr -> paid << endl;
75 ++itr;
76 }
77 ++league_itr;
78 }
79 else if(league_itr -> first == "U12") {
80 OutData << "League: U12" << endl;
81 auto itr = players_U12.begin();
82 while(itr != players_U12.end()) {
83 OutData << itr -> first << itr -> last << " " << itr -> year << " " <<
itr -> paid << endl;
84 ++itr;
85 }
86 ++league_itr;
87 }
88 else if(league_itr -> first == "U14") {
89 OutData << "League: U14" << endl;
90 auto itr = players_U14.begin();
91 while(itr != players_U14.end()) {
92 OutData << itr -> first << itr -> last << " " << itr -> year << " " <<
itr -> paid << endl;
93 ++itr;
94 }
95 ++league_itr;
96 }
97 else if(league_itr -> first == "U17") {
98 OutData << "League: U17" << endl;
99 auto itr = players_U17.begin();
100 while(itr != players_U17.end()) {
101 OutData << itr -> first << itr -> last << " " << itr -> year << " " <<
itr -> paid << endl;
102 ++itr;
103 }
104 ++league_itr;
105 }
106 }
107 cout << "Finished Printing" << endl;
108 }
109
110 string Season::display_name() {
111 return (current_player_ -> second.first) + (current_player_ -> second.last);
112
113 }
114 string Season::display_year() {
115 return to_string(current_player_ -> second.year);
116 }
117 string Season::display_league() {
118 return ("U" + to_string(get_league_(current_player_ -> second.year)));
119 }
120 string Season::display_status() {
121 string status;
122 if(current_player_ -> second.paid)
123 status = "Paid";
124 else
125 status = "Not Paid";
126 return status;
127 }
128
129
130 void Season::update_stats() {
131
132 stats_.clear();
133 stats_.insert({"Total", {"Total"}});
134 for(auto itr = players_.begin(); itr != players_.end(); ++itr)
135 {
136 string league = "U" + to_string(get_league_(itr -> second.year));
137 if(stats_.find(league) != stats_.end())
138 {
139 add_stat(league, (itr -> second.paid));
140 }
141 else
142 {
143 stats_.insert({league, {league}});
144 add_stat(league, (itr -> second.paid));
145 }
146 }
147 }
148
149 void Season::add_stat(std::string key, bool status) {
150
151 auto itr = stats_.find(key);
152 auto total = stats_.find("Total");
153 if(status == true)
154 {
155 ++(itr -> second.players);
156 ++(itr -> second.paid);
157 ++(total -> second.players);
158 ++(total -> second.paid);
159 }
160 else
161 {
162 ++(itr -> second.players);
163 ++(itr -> second.not_paid);
164 ++(total -> second.players);
165 ++(total -> second.not_paid);
166 }
167 }
168
169 //starts a new season
170 void Season::new_season(const int new_year)
171 {
172 current_year_ = new_year;
173 players_.clear();
174 current_player_ = players_.end();
175 current_player_pos_ = 0;
176 }
177
178 //Adds a player to the player map
179 void Season::add_player(const string & name, const int birth_year, const bool & status) {
180
181 if (get_league_(birth_year) != -1) {
182 auto entry = players_.insert({get_key_(name), {get_first_(name), get_last_(name),
birth_year, status}});
183 current_player_ = entry.first;
184 }
185 }
186
187 //Edits a player by deleting that player and adds a new player with the edited info
188
189 void Season::edit_player(string new_name, int new_year, bool new_paid) {
190 delete_player();
191 add_player(new_name, new_year, new_paid);
192 }
193
194 void Season::delete_player() {
195 players_.erase(current_player_->first);
196 next_player();
197 }
198
199 //Returns a key to the player in the map
200 string Season::get_key_(const string & name) {
201 string key = "";
202 for (int i = 0; i < name.length(); ++i) {
203 if (name[i] == ' ') {
204 ++i;
205 while (i < name.length()) {
206 key = key + name[i];
207 ++i;
208 }
209 key = key + ", ";
210 i = 0;
211 while (name[i] != ' ') {
212 key = key + name[i];
213 ++i;
214 }
215 i = name.length();
216 }
217 }
218 return key;
219 }
220
221 //Gets a player's first name
222 string Season::get_first_(const string & name) {
223 std::string first = "";
224 for (int i = 0; name[i] != ' '; ++i)
225 first = first + name[i];
226 return first;
227 }
228
229 //Gets a player's last name
230 string Season::get_last_(const string & name) {
231 string last = "";
232 int space = 0;
233 for (int i = 0; i < name.length(); ++i) {
234 if (name[i] == ' ')
235 space++;
236 if (space == 1)
237 last = last + name[i];
238 }
239 return last;
240 }
241
242 //Gets the league the player is in
243 int Season::get_league_(const int birth_year) {
244 const int age = (current_year_ - birth_year);
245 if (age >= 4 && age < 6)
246 return 6;
247 else if (age >= 6 && age < 8)
248 return 8;
249 else if (age >= 8 && age < 10)
250 return 10;
251 else if (age >= 10 && age < 12)
252 return 12;
253 else if (age >= 12 && age < 14)
254 return 14;
255 else if (age >= 14 && age < 17)
256 return 17;
257 return -1;
258 }
259
260 //Opens the season file
261 bool Season::open() {
262 ifstream in(file_);
263 if (!in)
264 return false;
265
266 string first_name;
267 string last_name;
268 int year;
269 bool status;
270 stringstream year_stream;
271 string temp;
272
273 //Use a string stream to santize input
274 getline(in, temp);
275 year_stream.str(temp);
276 temp = "";
277
278 //If stream state is invalid fail to open the file
279 if (!year_stream)
280 return false;
281
282 year_stream >> current_year_;
283 while (getline(in, temp)) {
284 stringstream ss(temp);
285 ss >> first_name >> last_name >> year >> status;
286 if (ss) {
287 //Could be a bit more efficient
288 string name = first_name + " " + last_name;
289 add_player(name, year, status);
290 }
291 }
292 //Initialize the current player
293 current_player_ = players_.begin();
294 current_player_pos_ = 1;
295 return true;
296 }
297
298 bool Season::save() {
299 ofstream out(file_);
300 if (!out)
301 return false;
302
303 out << current_year_ << endl;
304
305 for (auto e: players_) {
306 out << e.second.first << ' ' << e.second.last << ' ';
307 out << e.second.year << ' ' << e.second.paid << endl;
308 }
309 out.close();
310
311 return true;
312 }
313
314 void Season::search(const string & first, const string & last, const int year,
315 const bool search_paid, const bool paid) {
316 }
317

You might also like