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

1 #include "ui.

h"
2 using namespace std;
3
4 void UI::display() {
5 //system("clear || cls");
6 cout << season_.year() << " Season: ";
7 if(is_browsing_)
8 cout << "Browsing View" << endl;
9 else
10 cout << "Search View" << endl;
11
12 for (int i = 0; i < 35; ++i) {
13 cout << "-";
14 }
15 cout << endl;
16 if (season_.empty()) {
17 cout << "No players to display" << endl;
18 }
19 else {
20 cout << "Player (" << season_.get_current_pos() << "/" << season_.get_player_count()
<< "):" << endl;
21 cout << "\tName: " << season_.display_name() << endl;
22 cout << "\tBirth Year: " << season_.display_year() << endl;
23 cout << "\tLeague: " << season_.display_league() << endl;
24 cout << "\tPaid: " << season_.display_status() << endl;
25 }
26
27 for (int i = 0; i < 35; ++i) {
28 cout << "-";
29 }
30 cout << endl;
31 cout << "Type 'help' for a list of commands" << endl;
32 }
33
34 bool UI::check_paid(const string & input, bool & paid) {
35 bool good = false;
36 if(input == "y" || input == "yes") {
37 paid = true;
38 good = true;
39 }
40 else if(input == "n" || input == "no") {
41 paid = false;
42 good = true;
43 }
44 return good;
45 }
46 //Helper function to get player details for add and edit to recycle code
47 void UI::get_player_details(string & name, int & year, bool & paid) {
48 string temp;
49 cout << "Name: ";
50 getline(cin, name);
51 bool good = false;
52 //Get a birth year
53 do {
54 cout << "Birth Year: ";
55 getline(cin, temp);
56 stringstream ss(temp);
57 ss >> year;
58 if (ss) {
59 if (season_.year() - year < 4) {
60 cout << "Player must be older than 3" << endl;
61 ss.clear();
62 }
63 else if (season_.year() - year > 17) {
64 cout << "Player must be younger than 17" << endl;
65 ss.clear();
66 }
67 else {
68 good = true;
69 }
70 }
71 else {
72 cout << "Please enter a valid year" << endl;
73 }
74 } while (!good);
75
76 good = false;
77 do {
78 cout << "Paid? (Y/n) ";
79 getline(cin, temp);
80 transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
81 if (check_paid(temp, paid))
82 good = true;
83 else
84 cout << "Please enter yes or no" << endl;
85 } while(!good);
86 }
87
88 //Create a new season
89 void UI::new_season() {
90 int year;
91 string temp;
92 bool good = false;
93 do {
94 cout << "New Season Year: ";
95 getline(cin, temp);
96 stringstream ss(temp);
97 ss >> year;
98 if (ss) {
99 season_.new_season(year);
100 good = true;
101 }
102 else {
103 cout << "Please enter a valid year" << endl;
104 ss.clear();
105 }
106 } while (!good);
107 }
108
109 void UI::search() {
110 string first;
111 string last;
112 //Used to tell if the year is supposed to be blank
113 int year = season_.year();
114 string league;
115 bool paid;
116 bool search_paid = true;
117
118 string temp;
119 stringstream ss;
120 bool good = false;
121
122 cout << "Input any search parameters you wish to fill. Leave them blank to ignore them"
<< endl;
123 cout << "First Name: ";
124 getline(cin, first);
125 cout << "Last Name: ";
126 getline(cin, last);
127 do {
128 cout << "Year: ";
129 getline(cin, temp);
130 if (temp.length() == 0) {
131 year = season_.year();
132 good = true;
133 }
134 else {
135 ss >> year;
136 if (!ss) {
137 cout << "Please enter a valid year" << endl;
138 }
139 else if (season_.year() - year < 4 || season_.year() > 17) {
140 cout << "Please enter a year between " << season_.year() - 4 << " and " <<
season_.year() - 17 << endl;
141 }
142 else {
143 good = true;
144 }
145 }
146 } while (!good);
147
148 do {
149 cout << "League (ex. U6): ";
150 getline(cin, league);
151 if (league.length() == 0) {
152 good = true;
153 }
154 else if (league.length() < 2 || (league[0] != 'U' && league[0] != 'u') ||
!isdigit(league[1]) ) {
155 cout << "League must be in the form 'U6' or u12" << endl;
156 }
157 else {
158 good = true;
159 }
160 } while (!good);
161
162 good = false;
163 do {
164 cout << "Paid? (Y/n) ";
165 getline(cin, temp);
166 if (temp.length() == 0) {
167 good = true;
168 search_paid = false;
169 }
170 else {
171 transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
172 if (check_paid(temp, paid))
173 good = true;
174 else
175 cout << "Please enter yes or no" << endl;
176 }
177 } while(!good);
178 season_.search(first, last, year, search_paid, paid);
179 }
180
181 bool UI::exec_command(const string & command, bool & done) {
182 if (command == "help") {
183 cout << "Commands:" << endl;
184 cout << "\t* next - displays the next player" << endl;
185 cout << "\t* previous - displays the previous player" << endl;
186 cout << "\t* add - adds a player" << endl;
187 cout << "\t* edit - edits the current player" << endl;
188 cout << "\t* delete - deletes the current player" << endl;
189 cout << "\t* search - searches for a player" << endl;
190 if (is_browsing_) {
191 cout << "\t* new - starts a new season" << endl;
192 cout << "\t* stats - display season statistics" << endl;
193 }
194 else {
195 //cout << "\t* print - prints the players to a file" << endl;
196 cout << "\t* exit - exits the search view" << endl;
197 }
198 cout << "\t* save - saves the program" << endl;
199 cout << "\t* stop - stops the program" << endl;
200 }
201 else if (command == "next") {
202 season_.next_player();
203 display();
204 }
205 else if (command == "previous" || command == "prev") {
206 season_.previous_player();
207 display();
208 }
209 else if (command == "add") {
210 string name;
211 int year;
212 bool paid;
213 get_player_details(name, year, paid);
214 season_.add_player(name, year, paid);
215 display();
216 }
217 else if (command == "edit") {
218 string name;
219 int year;
220 bool paid;
221 get_player_details(name, year, paid);
222 season_.edit_player(name, year, paid);
223 display();
224 }
225 else if (command == "delete") {
226 season_.delete_player();
227 display();
228 }
229 else if (command == "new" && is_browsing_) {
230 new_season();
231 display();
232 }
233 else if (command == "stats" && is_browsing_) {
234 season_.update_stats();
235 auto itr = season_.get_stats();
236 auto end_itr = season_.get_end_stat();
237 for(itr; itr != end_itr; ++itr)
238 {
239 cout << itr -> first << " Players: " << itr -> second.players << endl;
240 cout << itr -> first << " Paid: " << itr -> second.paid << endl;
241 cout << itr -> first << " Not Paid: " << itr -> second.not_paid << endl;
242 cout << endl;
243 }
244 //display();
245 }
246 else if (command == "search") {
247 is_browsing_ = false;
248 search();
249 display();
250 }
251 else if (command == "exit" && !is_browsing_) {
252 is_browsing_ = true;
253 display();
254 }
255 else if (command == "save") {
256 season_.save();
257 display();
258 }
259 else if (command == "stop") {
260 done = true;
261 season_.save();
262 }
263 else {
264 return false;
265 }
266 return true;
267 }
268
269 void UI::start() {
270 if (season_.open()) {
271 display();
272 run();
273 }
274 else {
275 //Waiting for input
276 bool waiting = true;
277 string input;
278 cout << "No previous seasons exist." << endl;
279 cout << "Would you like to start a new season? (y/n): ";
280 //Get input, make it all lowercase and then see if the user input yes or no
281 do {
282 getline(cin, input);
283 transform(input.begin(), input.end(), input.begin(), ::tolower);
284 if (input == "y" || input == "yes") {
285 bool temp = true;
286 if (exec_command("new", temp)) {
287 waiting = false;
288 run();
289 }
290 }
291 else if (input == "n" || input == "no") {
292 waiting = false;
293 }
294 else {
295 cout << "Please input either yes or no: ";
296 }
297 } while (waiting);
298 }
299 }
300
301 //Run the UI
302 void UI::run() {
303 string input;
304 bool done = false;
305 cout << ">> ";
306 while (!done && getline(cin, input)) {
307 if (!exec_command(input, done))
308 cout << "Failed to run command: " + input << endl;
309 if (!done)
310 cout << ">> ";
311 }
312 }
313

You might also like