#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

class ScoreBook {
    string filename;
	map<string, string> db;
public:
    ScoreBook( string filename );
    void printall();
    void insert( string name, string score );
    void remove( string iname );
    void search( string iname );
	//void saveall();
	~ScoreBook();
};

ScoreBook::ScoreBook( string filename ) : filename(filename)
{
	string name, score;
	// read data from file to map data structure
	ifstream file( filename );
	if( !file ) return;

	while( ! file.eof() ) {
		getline( file, name, ',' );
		getline( file, score );
		if( name.size() == 0 ) continue;
		db[ name ] = score;
	}
	file.close();
}

ScoreBook::~ScoreBook( )
//void ScoreBook::saveall()
{
	// read data from file to map data structure
	ofstream file( filename );
	for( auto p = db.begin(); p != db.end(); p++ ) 
	{
		file << p->first << "," << p->second << endl;
	}
	file.close();
}

void ScoreBook::insert(string name, string score)
{
	// insert to map data structure
	// if( db.find( name ) != db.end() ) // SHOUDL WORK! But MS Visual Studio has a bug
	// if( db.empty() || db.find( name ) != db.end() ) // Alternative approach
	//if( db.count(name) == 0 )
		db[name] = score;
	//else
	//	cout << name << " already exists in the database." << endl;
}

void ScoreBook::remove(string iname)
{
	if( db.find( iname ) == db.end() )
		cout << iname << " does not exist in the database." << endl;
	else
		db.erase(iname);
		
}

void ScoreBook::search(string iname)
{
	if( db.find(iname) == db.end() )
		cout << iname << " does not exist in the database." << endl;
	else
		cout << iname << "'s score is " << db[iname] << endl;
}

void ScoreBook::printall()
{
	cout << "The list of scores" << endl;

	for( auto p = db.begin(); p != db.end(); p++ )
		cout << p->first << "'s score is " << p->second << endl;

	cout << endl;
}

int getSelection()
{
	cout << "#### MENU ####" << endl;
    cout << "1. print all" << endl;
    cout << "2. insert data" << endl;
    cout << "3. remove data" << endl;
    cout << "4. search" << endl;
    cout << "5. quit" << endl;
    cout << "Choose >> ";
    
    string line;
    getline( cin, line );
    return stoi( line );
 }

string getName() 
{
	string name;
	cout << "Enter name : ";
	getline( cin, name );
	return name;
}

string getScore() 
{
	string score;
	cout << "Enter score : ";
	getline( cin, score );
	return score;
}

int main()
{
    int selection;
    string name, score;

    ScoreBook sb("scorebook.txt");
	// ScoreBoo* sb = new ScoreBook("scorebook.txt");
    
    while( (selection = getSelection()) ) {
        switch (selection) {
            case 1:
		sb.printall();
                break;
            case 2: 
		name = getName();
		score = getScore();
                sb.insert( name, score );
                break;
            case 3:
		sb.remove( getName() );
                break;
            case 4:
                sb.search( getName() );
                break;
            case 5:
                cout << "Good bye" << endl;
				//sb.saveall();
				//delete sb;
				system("pause");
				return 1;
				break;
            default:
                continue;
        }
    }
}