Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * test.cpp
- * project-1
- * Eli Maine
- *
- */
- #ifndef _Player_
- #define _Player_
- #include <map>
- #include <string>
- #include <fstream>
- #include <iostream>
- using namespace std;
- //----------------------------
- // Class Player:
- // Stores each individual players information.
- //-----------------------------
- class Player
- {
- public:
- Player (){name_ = "UNDEFINED"; reg_stat_ = false; year_born_ = 99999; division_ = "n/a";};
- Player (string & a, int y, bool b);
- string get_name(){return name_;};
- string get_reg_stat(){if (reg_stat_==true) return "paid"; else return "unpaid";};
- int get_year_born(){return year_born_;};
- string get_division() {return division_;};
- private:
- string name_;
- int year_born_;
- string division_;
- bool reg_stat_;
- };
- #endif
- //Player constructer.
- Player::Player(string & name, int year_born, bool reg){
- name_ = name;
- reg_stat_ = reg;
- year_born_ = year_born;
- int current_year;
- ifstream file;
- file.open("current_year.txt");
- if (file.is_open())
- {
- file >> current_year;
- }
- file.close();
- int age = current_year - year_born;
- if (age < 4 || age > 16) {cout << "player is not an within age limit" << endl; name_ = ""; name = "";}
- else if (age < 6) division_ = "U6";
- else if (age < 8) division_ = "U8";
- else if (age < 10) division_ = "U10";
- else if (age < 12) division_ = "U12";
- else if (age < 14) division_ = "U14";
- else if (age < 17) division_ = "U17";
- }
- //--------------------------------
- // menu:
- // writes the user interface.
- //--------------------------------
- void menu()
- {
- cout << endl << "------------------------------ " << endl;
- cout << " add look up quit " << endl;
- cout << " edit save list" << endl;
- cout << "---------------------" << endl;
- cout << "first letter command: ";
- }
- //--------------------------------
- // add_player:
- // Responsibilities: Gather name and registration status of a player and add to map.
- //--------------------------------
- void add_player(map <string, Player> & all_players)
- {
- string name;
- string fname;
- string lname;
- int year_born;
- bool reg_stat = false;
- char is_paid;
- cout << "You are adding a player." << endl;
- cout << "Please enter player first name: ";
- cin >> fname;
- cout << "Please enter player last name: ";
- cin >> lname;
- name = fname + " " + lname;
- cout << "Please enter the year the player was born: ";
- cin >> year_born;
- cout << "is player paid? y or n: ";
- cin >> is_paid;
- if (is_paid == 'y') reg_stat = true;
- Player new_player = Player(name, year_born, reg_stat);
- all_players[name] = new_player;
- cout << "Thank you drive around please." << endl;
- }
- //--------------------------------
- // find_player:
- // Searches the map for player by name.
- //--------------------------------
- Player find_player(const string & query, const map <string, Player> & all_players)
- {
- map<string,Player>::const_iterator iter;
- for( iter = all_players.begin(); iter != all_players.end(); iter++ )
- {
- if (iter->first == query) return iter->second;
- }
- cout << "YOU MUST TYPE IN A REAL EXISITING PERSONS NAME 100% CORRECTLY HHHHHHGGGGGGGG";
- exit(1);
- }
- //--------------------------------
- // look_up_player:
- // prompts user for name, uses find_player to search, and prints that players info.
- //--------------------------------
- void look_up_player(const map <string, Player> & all_players)
- {
- string name;
- string fname;
- string lname;
- cout << "You are looking up a player." << endl;
- cout << "Please enter player first name: ";
- cin >> fname;
- cout << "Please enter player last name: ";
- cin >> lname;
- name = fname + " " + lname;
- Player Found_player = find_player(name, all_players);
- cout << endl << "Name: " << Found_player.get_name() << endl;
- cout << "Year Born: " << Found_player.get_year_born() << endl;
- cout << "Division: " << Found_player.get_division() << endl;
- cout << "Reg stat: " << Found_player.get_reg_stat() << endl;
- }
- //--------------------------------
- // edit_player:
- // prompts user for name, uses find_player to search, and edits that players info.
- //--------------------------------
- void edit_player(map <string, Player> & all_players)
- {
- string name;
- string fname;
- string lname;
- cout << "You are editing a player." << endl;
- cout << "Please enter player first name: ";
- cin >> fname;
- cout << "Please enter player last name: ";
- cin >> lname;
- name = fname + " " + lname;
- all_players.erase(name);
- string new_fname;
- string new_lname;
- string new_name;
- char is_paid;
- int new_year_born;
- bool new_reg_stat = false;
- cout << "New first name: ";
- cin >> new_fname;
- cout << "New last name: ";
- cin >> new_lname;
- new_name = new_fname + " " + new_lname;
- cout << "New birth year: ";
- cin >> new_year_born;
- cout << "is player paid? y or n: ";
- cin >> is_paid;
- if (is_paid == 'y') new_reg_stat = true;
- all_players.erase(name);
- all_players[new_name] = Player(new_name, new_year_born, new_reg_stat);
- }
- //--------------------------------
- // save_all_players:
- // opens the output file, iterates through map of players and saves their info.
- //--------------------------------
- void save_all_players(const map <string, Player> & all_players)
- {
- ofstream myfile;
- myfile.open ("player_data.txt");
- map<string,Player>::const_iterator iter;
- if (myfile.is_open())
- {
- for( iter = all_players.begin(); iter != all_players.end(); iter++ )
- {
- Player Curr = iter->second;
- myfile << Curr.get_name() << '\n';
- myfile << Curr.get_year_born() << '\n';
- myfile << Curr.get_division() << '\n';
- myfile << Curr.get_reg_stat() << '\n' << '\n';
- }
- }
- myfile.close();
- }
- //--------------------------------
- // save_list:
- // save_list prompts user for what division to save and a filename.
- // then cycles through users and saves to file all users in the chosen division.
- //--------------------------------
- void save_list(map <string, Player> & all_players)
- {
- string file_name;
- string target_division;
- cout << "please enter a file name to save the data under (remember, no spaces): ";
- cin >> file_name;
- cout << "enter the division (capitalized U please): ";
- cin >> target_division;
- ofstream myfile;
- myfile.open(file_name.c_str());
- map<string,Player>::iterator iter;
- for( iter = all_players.begin(); iter != all_players.end(); iter++ )
- {
- if (iter->second.get_division() == target_division)
- {
- Player Curr = iter->second;
- myfile << Curr.get_name() << '\n';
- myfile << Curr.get_year_born() << '\n';
- myfile << Curr.get_division() << '\n';
- myfile << Curr.get_reg_stat() << '\n' << '\n';
- }
- }
- }
- //--------------------------------
- // user_command:
- // reads in users menu command and interprets.
- //--------------------------------
- bool user_command(map <string, Player> & all_players)
- {
- char command;
- cin >> command;
- switch (command)
- {
- case 'a':
- add_player(all_players);
- break;
- case 'e':
- edit_player(all_players);
- break;
- case 'l':
- look_up_player(all_players);
- break;
- case 's':
- save_list(all_players);
- break;
- case 'q':
- save_all_players(all_players);
- return false;
- break;
- default:
- cout << "please enter valid command (the first letter of the menu choice)";
- }
- return true;
- }
- //--------------------------------
- // read_file_in:
- // opens input file and reads the lines into a player object
- // and puts that object in map with the players name as key.
- //--------------------------------
- void read_file_in(map <string, Player> & all_players)
- {
- ifstream myfile;
- myfile.open ("player_data.txt");
- if (myfile.is_open())
- {
- int i = 0;
- string name;
- int year_born;
- bool paid;
- string line;
- while ( myfile.good() )
- {
- switch (i)
- {
- case 0:
- getline (myfile,line);
- name = line;
- break;
- case 1:
- myfile >> year_born;
- myfile.get();
- break;
- case 2:
- getline (myfile,line);
- break;
- case 3:
- getline (myfile,line);
- if ((line == "Paid") || (line == "paid")) paid = true;
- else paid = false;
- all_players[name] = Player (name, year_born, paid);
- break;
- case 4:
- getline (myfile,line);
- i = -1;
- break;
- }
- ++i;
- }
- myfile.close();
- }
- else{
- cout << "Unable to open file";
- exit(1);
- }
- }
- //--------------------------------
- // initialize:
- // Creates the map database, reads input file into it.
- // infinetly runs menu until user_command turns false (it's been quit)
- //--------------------------------
- void initialize()
- {
- map <string, Player> all_players;
- read_file_in(all_players);
- while(true)
- {
- menu();
- if (!user_command(all_players)) break;
- }
- }
- int main ()
- {
- initialize();
- return 0;
- }
Add Comment
Please, Sign In to add comment