/*! \file app.cpp * \brief Test Driver for Polygon abstract base class hierarchy * * This application tests the implementation of an abstract polygon list container class. * The abstract list can be expressed as an array, a vector, or a doubly linked list. The three * containers mentioned inherit the abstract class PolygonList. */ #include #include #include "PolygonList.h" #include "PolygonArrayList.h" /*! * \param fileName File to be searched for. * \param args A check of any command line arguments passed. * \param *comms[] Any command line arguments. * \brief Open data file * * The openFile function checks to see if the file the user specified * at runtime can be found. If no filename was passed during runtime, * the user is prompted to provide one and that file is searched out. * If the file is found, the program continues, else the program exits * with code '1'; * * \return A boolean value to specifiy if the file was found. */ bool openFile(std::string &fileName, int args, char *comms[]); /*! * \param *TheList The abstract list that will be manipulated. * \param *fileName The name of the file that WAS found. * \brief Loop and process user commands * * The mainLoop function will call the display menu to inform the user * of the options available and also call the functions to display * the data in each individual list. This loop terminates when user * enters a '4' at menu selection. */ void mainLoop(PolygonList *TheList, std::string fileName); /*! * \param filled A boolean that states if the abstract class pointer is pointing to a concrete list or not. * \brief Display user menu * * The display menu function will display the options available to the * user based on the status of the abstract class pointer. If no class has * been chosen yet, the menu assists in choosing one. Otherwise it will give * options on how to display the information in the list. */ void displayMenu(bool filled); /*! * \param option The menu selection the user chose. * \param TheList The abstract list that will be manipulated. * \param &filled A boolean that states if the abstract class pointer is pointing to a concrete list or not. * \brief Display or discard list * * Display will take the users choice and carry out one of 4 actions: * '1'- Will display all of the information in the list using iteration. * '2'- Will delete the current instantiation of the list so another can be chosen. * Any other value will result in an error message and the menu will be called again. */ void display(char option, PolygonList *TheList, bool &filled); /*! * \param option The menu selection of the user * \param TheList The absrtact list that will be manipulated * \param fileName The previously checked name of the file to be read into the list * \param filled A Boolean flag that stats if the abstract class has been instantiated or not. * \brief Read data into array. Can be expanded to take advantage of polymorphism * * Get fill with take the users choice and carry out one of 4 actions. * '1'-Creates the list as an PolygonArrayList * any other value will result in an error message and menu will be recalled */ PolygonList* getFill(char option, PolygonList *TheList, std::string fileName, bool &filled); //exit status of '1' for file not found //exit stauts of '0' for good execution int main(int argc,char *argv[]) { PolygonList *TheList = 0; //to be polymorphized std::string fileName; //to open the files when container type is chosen if(!openFile(fileName, argc, argv))//make sure the file exists before continuing { return 1;//file was not found } mainLoop(TheList, fileName);//the loop for user to choose options and view results return 0; } //check if the file exists before proceeding bool openFile(std::string &fileName, int args, char *comms[]) { fstream myFile; //the file to be opened if(args>1) //if there was an input on the command line { fileName = comms[1]; myFile.open(fileName.c_str()); //try and open the file } else//otherwise get the new filename { cout<<"Please enter a file name:"<> userOption; if(userOption == '2') //if user quits { std::cout << "Program Exiting" << std::endl << std::endl; return; } if(!filled) { TheList = getFill(userOption, TheList, fileName, filled); //applying dat polymorphism } else { display(userOption, TheList, filled); } } } //displays a menu to the user for inturpeting commands void displayMenu(bool filled) { if(!filled) { std::cout << std::endl; std::cout << "Select Object List Type" << std::endl; std::cout << "------------------------------------------------"<printIteratively(); break; } case '2': { delete TheList; filled = !filled; std::cout << "The list has been cleared" << std::endl; break; } default: { std::cout << "I cannot understand " << option << ". Try again." << std::endl; return; } } } //polymorphises the abstract class PolygonList* getFill(char option, PolygonList *TheList, std::string fileName, bool &filled) { switch(option) { case '1': { TheList = new PolygonArrayList; break; } default: { std::cout<<"I cannot understand "<< option <<". Try again." << std::endl; return TheList; } } TheList->readIntoList(fileName); std::cout << "Reading into List " << std::endl; //DEBUG filled = !filled; return TheList; }