Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. /*
  2. * test.cpp
  3. * project-1
  4. * Eli Maine
  5. *
  6. */
  7.  
  8. #ifndef _Player_
  9. #define _Player_
  10.  
  11. #include <map>
  12. #include <string>
  13. #include <fstream>
  14. #include <iostream>
  15. using namespace std;
  16. //----------------------------
  17. // Class Player:
  18. // Stores each individual players information.
  19. //-----------------------------
  20. class Player
  21. {
  22. public:
  23. Player (){name_ = "UNDEFINED"; reg_stat_ = false; year_born_ = 99999; division_ = "n/a";};
  24. Player (string & a, int y, bool b);
  25. string get_name(){return name_;};
  26. string get_reg_stat(){if (reg_stat_==true) return "paid"; else return "unpaid";};
  27. int get_year_born(){return year_born_;};
  28. string get_division() {return division_;};
  29.  
  30. private:
  31. string name_;
  32. int year_born_;
  33. string division_;
  34. bool reg_stat_;
  35.  
  36. };
  37. #endif
  38.  
  39. //Player constructer.
  40.  
  41. Player::Player(string & name, int year_born, bool reg){
  42. name_ = name;
  43. reg_stat_ = reg;
  44. year_born_ = year_born;
  45. int current_year;
  46. ifstream file;
  47. file.open("current_year.txt");
  48.  
  49. if (file.is_open())
  50. {
  51. file >> current_year;
  52. }
  53. file.close();
  54. int age = current_year - year_born;
  55.  
  56. if (age < 4 || age > 16) {cout << "player is not an within age limit" << endl; name_ = ""; name = "";}
  57. else if (age < 6) division_ = "U6";
  58. else if (age < 8) division_ = "U8";
  59. else if (age < 10) division_ = "U10";
  60. else if (age < 12) division_ = "U12";
  61. else if (age < 14) division_ = "U14";
  62. else if (age < 17) division_ = "U17";
  63. }
  64.  
  65. //--------------------------------
  66. // menu:
  67. // writes the user interface.
  68. //--------------------------------
  69. void menu()
  70. {
  71. cout << endl << "------------------------------ " << endl;
  72. cout << " add look up quit " << endl;
  73. cout << " edit save list" << endl;
  74. cout << "---------------------" << endl;
  75. cout << "first letter command: ";
  76. }
  77.  
  78. //--------------------------------
  79. // add_player:
  80. // Responsibilities: Gather name and registration status of a player and add to map.
  81. //--------------------------------
  82. void add_player(map <string, Player> & all_players)
  83. {
  84. string name;
  85. string fname;
  86. string lname;
  87. int year_born;
  88. bool reg_stat = false;
  89. char is_paid;
  90.  
  91. cout << "You are adding a player." << endl;
  92. cout << "Please enter player first name: ";
  93. cin >> fname;
  94. cout << "Please enter player last name: ";
  95. cin >> lname;
  96.  
  97. name = fname + " " + lname;
  98.  
  99. cout << "Please enter the year the player was born: ";
  100. cin >> year_born;
  101.  
  102. cout << "is player paid? y or n: ";
  103. cin >> is_paid;
  104.  
  105. if (is_paid == 'y') reg_stat = true;
  106.  
  107. Player new_player = Player(name, year_born, reg_stat);
  108.  
  109. all_players[name] = new_player;
  110.  
  111. cout << "Thank you drive around please." << endl;
  112. }
  113. //--------------------------------
  114. // find_player:
  115. // Searches the map for player by name.
  116. //--------------------------------
  117. Player find_player(const string & query, const map <string, Player> & all_players)
  118. {
  119. map<string,Player>::const_iterator iter;
  120. for( iter = all_players.begin(); iter != all_players.end(); iter++ )
  121. {
  122. if (iter->first == query) return iter->second;
  123. }
  124. cout << "YOU MUST TYPE IN A REAL EXISITING PERSONS NAME 100% CORRECTLY HHHHHHGGGGGGGG";
  125. exit(1);
  126. }
  127. //--------------------------------
  128. // look_up_player:
  129. // prompts user for name, uses find_player to search, and prints that players info.
  130. //--------------------------------
  131. void look_up_player(const map <string, Player> & all_players)
  132. {
  133. string name;
  134. string fname;
  135. string lname;
  136. cout << "You are looking up a player." << endl;
  137. cout << "Please enter player first name: ";
  138. cin >> fname;
  139. cout << "Please enter player last name: ";
  140. cin >> lname;
  141.  
  142. name = fname + " " + lname;
  143.  
  144. Player Found_player = find_player(name, all_players);
  145. cout << endl << "Name: " << Found_player.get_name() << endl;
  146. cout << "Year Born: " << Found_player.get_year_born() << endl;
  147. cout << "Division: " << Found_player.get_division() << endl;
  148. cout << "Reg stat: " << Found_player.get_reg_stat() << endl;
  149.  
  150. }
  151.  
  152. //--------------------------------
  153. // edit_player:
  154. // prompts user for name, uses find_player to search, and edits that players info.
  155. //--------------------------------
  156. void edit_player(map <string, Player> & all_players)
  157. {
  158. string name;
  159. string fname;
  160. string lname;
  161. cout << "You are editing a player." << endl;
  162. cout << "Please enter player first name: ";
  163. cin >> fname;
  164. cout << "Please enter player last name: ";
  165. cin >> lname;
  166.  
  167. name = fname + " " + lname;
  168. all_players.erase(name);
  169.  
  170. string new_fname;
  171. string new_lname;
  172. string new_name;
  173. char is_paid;
  174. int new_year_born;
  175. bool new_reg_stat = false;
  176. cout << "New first name: ";
  177. cin >> new_fname;
  178. cout << "New last name: ";
  179. cin >> new_lname;
  180. new_name = new_fname + " " + new_lname;
  181.  
  182. cout << "New birth year: ";
  183. cin >> new_year_born;
  184.  
  185. cout << "is player paid? y or n: ";
  186. cin >> is_paid;
  187.  
  188. if (is_paid == 'y') new_reg_stat = true;
  189.  
  190. all_players.erase(name);
  191. all_players[new_name] = Player(new_name, new_year_born, new_reg_stat);
  192.  
  193.  
  194. }
  195.  
  196. //--------------------------------
  197. // save_all_players:
  198. // opens the output file, iterates through map of players and saves their info.
  199. //--------------------------------
  200. void save_all_players(const map <string, Player> & all_players)
  201. {
  202. ofstream myfile;
  203. myfile.open ("player_data.txt");
  204. map<string,Player>::const_iterator iter;
  205. if (myfile.is_open())
  206. {
  207. for( iter = all_players.begin(); iter != all_players.end(); iter++ )
  208. {
  209. Player Curr = iter->second;
  210. myfile << Curr.get_name() << '\n';
  211. myfile << Curr.get_year_born() << '\n';
  212. myfile << Curr.get_division() << '\n';
  213. myfile << Curr.get_reg_stat() << '\n' << '\n';
  214. }
  215. }
  216. myfile.close();
  217. }
  218. //--------------------------------
  219. // save_list:
  220. // save_list prompts user for what division to save and a filename.
  221. // then cycles through users and saves to file all users in the chosen division.
  222. //--------------------------------
  223. void save_list(map <string, Player> & all_players)
  224. {
  225. string file_name;
  226. string target_division;
  227.  
  228. cout << "please enter a file name to save the data under (remember, no spaces): ";
  229. cin >> file_name;
  230.  
  231. cout << "enter the division (capitalized U please): ";
  232. cin >> target_division;
  233.  
  234. ofstream myfile;
  235. myfile.open(file_name.c_str());
  236.  
  237. map<string,Player>::iterator iter;
  238. for( iter = all_players.begin(); iter != all_players.end(); iter++ )
  239. {
  240. if (iter->second.get_division() == target_division)
  241. {
  242. Player Curr = iter->second;
  243. myfile << Curr.get_name() << '\n';
  244. myfile << Curr.get_year_born() << '\n';
  245. myfile << Curr.get_division() << '\n';
  246. myfile << Curr.get_reg_stat() << '\n' << '\n';
  247. }
  248. }
  249.  
  250. }
  251. //--------------------------------
  252. // user_command:
  253. // reads in users menu command and interprets.
  254. //--------------------------------
  255. bool user_command(map <string, Player> & all_players)
  256. {
  257. char command;
  258. cin >> command;
  259. switch (command)
  260. {
  261. case 'a':
  262. add_player(all_players);
  263. break;
  264. case 'e':
  265. edit_player(all_players);
  266. break;
  267. case 'l':
  268. look_up_player(all_players);
  269. break;
  270. case 's':
  271. save_list(all_players);
  272. break;
  273. case 'q':
  274. save_all_players(all_players);
  275. return false;
  276. break;
  277. default:
  278. cout << "please enter valid command (the first letter of the menu choice)";
  279. }
  280. return true;
  281. }
  282.  
  283.  
  284. //--------------------------------
  285. // read_file_in:
  286. // opens input file and reads the lines into a player object
  287. // and puts that object in map with the players name as key.
  288. //--------------------------------
  289. void read_file_in(map <string, Player> & all_players)
  290. {
  291. ifstream myfile;
  292. myfile.open ("player_data.txt");
  293. if (myfile.is_open())
  294. {
  295. int i = 0;
  296. string name;
  297. int year_born;
  298. bool paid;
  299. string line;
  300. while ( myfile.good() )
  301. {
  302.  
  303. switch (i)
  304. {
  305. case 0:
  306. getline (myfile,line);
  307. name = line;
  308. break;
  309. case 1:
  310. myfile >> year_born;
  311. myfile.get();
  312. break;
  313. case 2:
  314. getline (myfile,line);
  315. break;
  316. case 3:
  317. getline (myfile,line);
  318. if ((line == "Paid") || (line == "paid")) paid = true;
  319. else paid = false;
  320. all_players[name] = Player (name, year_born, paid);
  321. break;
  322. case 4:
  323. getline (myfile,line);
  324. i = -1;
  325. break;
  326. }
  327. ++i;
  328. }
  329. myfile.close();
  330. }
  331.  
  332. else{
  333. cout << "Unable to open file";
  334. exit(1);
  335. }
  336.  
  337. }
  338.  
  339. //--------------------------------
  340. // initialize:
  341. // Creates the map database, reads input file into it.
  342. // infinetly runs menu until user_command turns false (it's been quit)
  343. //--------------------------------
  344. void initialize()
  345. {
  346.  
  347. map <string, Player> all_players;
  348. read_file_in(all_players);
  349. while(true)
  350. {
  351. menu();
  352. if (!user_command(all_players)) break;
  353.  
  354. }
  355.  
  356. }
  357.  
  358.  
  359. int main ()
  360. {
  361. initialize();
  362. return 0;
  363. }
Add Comment
Please, Sign In to add comment