Example #1
0
/*
**NOTE**
For all class member functions, check their respective header files
for the DETAILED (Post/Pre condition) description of the function.
*/
int main(int argc,char* argv[])//argv[1] will be the csv inputfile name.
{
 
 //If statement to check if an inputfile command line arguement is passed.
 if(argc<2)
 {
   cout << "\nError usage, please run the program in the following manner:" << endl;
   cout << argv[0] << " CSV_INPUT_FILE_NAME\n" << endl;
   exit(1); //Display usage and exit program if no command line arguement.
 }

//////////////////////////
//TOKEN EXTRACTION BLOCK//
//////////////////////////
 AddressBook bookOne;
 //Create AddressBook object bookOne
 bookOne.pushTitle();
 //This function pushes the title line into the contact vector at position 0.(i.e "First Name", "Last Name" etc...)
 /*Could probably leave this out by reading the first line of the given input file which also contains
 the title line.*/

 vector<string> myString;
 //myString will store text lines from csv inputfile argv[1]
 string fileline, currentline, substring;
 //fileline=line of file from text to be push back into myString
 //currentline=current line that is being looked at by token extraction algorithm
 //substring=token extracted to be pushed into the actual Addressbook Contact


    	//File extration is done here
	ifstream myfile(argv[1]);
	
	//Start extraction line by line if able to open file.
	if(myfile.is_open())
	{
		while(myfile)
		{
			getline(myfile, fileline);
			if(fileline.length()!=0)
			{
				myString.push_back(fileline);
			}
		}
    		myfile.close();
	}

	//Else if the file does not exist, asks user to double check that the files is in the directory.
	else
	{
		cout << "\n*WARNING* CSV File not found in directory. Please check for file:\"" << argv[1] << "\"\n" << endl;
		exit(1);		
	}

    //Token extraction loop/algorithm starts here.
    //Took me a while but I figured it out =)!
    for(int i=1; i<myString.size(); i++) //this for loop will run for the length of myString
    {
        bookOne.iPush();
        //iPush will pushback the vector to make room for the new incomming line.

        currentline=myString.at(i);
        //currentline will get each line from myString from first to end

        //x is left "
        //y is right "
        //token will count token # 1-11 for each field.
        int x=0, y=0, token=0;
        while(token<11)//The algorithm should keep looking for tokens until counter hits 11 tokens
        {
            //x starts looking for the first " from y (0 initially at the start of the string/loop)
            x=currentline.find_first_of("\"", y);
            //y will start looking for the next " starting from x+1
            y=currentline.find_first_of("\"",x+1);

            //This if check is not really needed but to be save, x" will always be less than y"
            if(x<y)
            {
                token++;
            }
            substring=currentline.substr(x+1,(y-x)-1);
            //substring is the token which will always be found at x+1 to y-1
            //to get the length, starting from x+1, it is (y-x)-1...(there are also other ways to calculate)
            bookOne.buildData(i,substring,token);
            //After token is extracted to substring, it is pushed to the address book with it's respective
            //token index counter "token"

            //This was the tricky part...took me a while to figure it out =P
            //y needs to get +1 to prevent x from finding y's " in the next loop. DUHHH!!
            //Also this can be done with a third variable to look for commas but this is more efficient/easier.
            y++;

        }

    }//for loop ends here bringing the extraction process to an end.

    //This loop will validate the newly built vector of contacts. (Start at 1 as 0 is the title line)
    for(int i=1; i<bookOne.vectorSize()-1; i++) //vectorsize returns the size the vector Contact data.
    {
        if(bookOne.contactValidate(i)==0)//this boolean function validates and DELETES invalid contacts.
        {
            bookOne.contactValidate(i);//calling the actual function DELETES invalid contacts (more detail in header file)
            i=1; //Thus bringing i back to 1 is needed to check for all invalid contacts in the new vector size.
        }
    }



/////////////////////
//MAIN OPTION BLOCK//
/////////////////////
 string option;
 //option is the variable for the menu option input from the user
 do{ //this do loop will run wile the option is not Q or q for quit.
    do{ //this do loop will run until a valid option is received from the user.
        displayMenu();
        cout << "Select an option:";
        cin >> option;
        //if the option is longer than 1 character. (can probably leave out >1 as function checks for length also)
        if(checkOption(option)==false || option.length()>1)
        {
            cout << "Invalid choice please try again.." << endl; //output invalid choice statement.
        }
    }while(checkOption(option)==false);

    //Program will get here only if option is valid.
    //Options are listed in order shown on project 3 description

        //////////////////////////////
        //OPTION [A] - Add a contact//
        //////////////////////////////
    if(option=="A" || option=="a")
    {
        //Function A uses push_back to add the new contact
        //Check AddressBook header/source file for more detail.
        bookOne.functionA();

        //if the new added push_back vector is valid (NOTE* if valid this function will NOT delete)
        if(bookOne.contactValidate(bookOne.vectorSize()-1)==1)
        {
            //Thus print detail should print the last entry added which is vector size-1;
            bookOne.printDetail(bookOne.vectorSize()-1);
            cout << "\n**New contact added to the end of the list at position " << bookOne.vectorSize()-1 << endl;
        }

        else //no need to call function again as the first if statement already called it and will delete if it's invalid.
        {    //will enter this else if it's deleted, in that case output the error message below.
            cout <<"\nInvalid contact..."
                 <<"\nPlease input valid/non space leading values for the first four fields." << endl;
        }
    }

        //////////////////////////////////////////
        //OPTION [D] - Display by sorted contact//
        //////////////////////////////////////////
    else if(option=="D" || option=="d")
    {
        //sort is the token # which it needs to be sorted by. (1-4 only)
        int sort=0;

        //do-while loop will run until a valid token choice is given.
        do{
            cout << "\nWhich field would you like to sort by?"
                << "\n1)Last Name"
                << "\n2)First Name"
                << "\n3)Nickname"
                << "\n4)Email 1"
                << "\n--------------"
                << "\nEnter Choice:";
            cin  >> sort;

            //if statement to reprompt user for valid choice
            if(sort<=0 || sort > 4)
            {
                cout << "**Error choice input, please enter choice 1-4." << endl;
            }
        }while(sort<=0 || sort > 4);

        //functionD sorts the contacts accordint to token sort
        //I used the same sorting method as my project 2!! =)
        //More in detail in AddressBook header/source file
        bookOne.functionD(sort);

        //Print preview the newly sorted contacts
        bookOne.printPreview();
    }

        ///////////////////////////////////////////
        //OPTION [C] - Display details of contact//
        ///////////////////////////////////////////
    else if(option=="C" || option=="c")
    {
        //Simply print out the details of the desired contact entry.
        int entry=0;
        bookOne.printPreview();
        do{//do-while loop to keep the entry selection within bounds of the valid vector.
            cout << "Which entry # would you like to view in detail?:";
            cin >> entry;
            if(entry<0 || entry>bookOne.vectorSize()-1)
            {
                cout << "\n**Error entry #, please try again.." << endl;
            }
        }while(entry<0 || entry>bookOne.vectorSize()-1);
        //printDetail will print the details of the desired entry.
        bookOne.printDetail(entry);

    }