//CatalogBrowser.cxx //Brooke Loftus #include "Catalog.h" #include #include #include void showMenu(); Catalog searchString(Catalog &catalog); Catalog readFile(); void showResults(Catalog &search); void showCatalog(Catalog &catalog); void browseCurrent(Catalog $search, Catalog &catalog); int main() { char ans; bool quit=false; Catalog currentSearch, lastStep, catalog; while (!quit){ showMenu(); cerr << " What would you like to do? " ; cin >> ans; switch(ans) { case 'N': case 'n': lastStep = currentSearch; currentSearch.makeEmpty(); currentSearch = searchString(catalog); break; case 'S': case 's': lastStep = currentSearch; currentSearch = searchString(catalog); break; case 'F': case 'f': catalog = readFile(); break; case 'U': case 'u': currentSearch = lastStep; break; case 'R': case 'r': showResults(currentSearch); break; case 'C': case 'c': showCatalog(catalog); break; case 'B': case 'b': browseCurrent(currentSearch, catalog); break; case 'Q': case 'q': quit=true; break; default: break; };//switch } return 0; }//main void showMenu() { cerr << " case 'n': start a new search " << endl; cerr << " case 's': search for a string in author or title " << endl; cerr << " case 'f': read a catalog from a file " << endl; cerr << " case 'u': undo last change to results " << endl; cerr << " case 'r': show results " << endl; cerr << " case 'c': show catalog " << endl; cerr << " case 'b': 'browse', e.g., if the user types " << endl; cerr << " b 7 " << endl; cerr << " then a list gets displayed of the " << endl; cerr << " 7th book in the current list of " << endl; cerr << " search results plus the five books " << endl; cerr << " that sit on either side of it on " << endl; cerr << " the library shelf " << endl; cerr << " case 'q': quit " << endl; }//showMenu Catalog searchString(Catalog &catalog) { //get string, give to catalog to search, return catalog of length 11 //5 before, 1 current, 5 after //make retrieve entry at place 6 //that will be the found items ... see if can do for multiple finds string answer; Catalog searchResults; char ans; cerr << "Search for (t)itle or (a)uthor? "; cin >> ans; switch(ans){ case 't': case 'T': cerr << "Input? "; cin >> answer; searchResults = catalog.searchTitle(answer); break; case 'a': case 'A': cerr << "Input? "; cin >> answer; searchResults = catalog.searchAuthor(answer); break; case 'Q': case 'q': break; default : cerr << "Proper input not received! Try again." << endl; break; }//switch return searchResults; }//searchString Catalog readFile() { // getting the file name from the user Catalog catalog; string infileName; ifstream infile; cerr << system ("dir") << endl << "File: "; cin >> infileName; // trying to open the file (note the conversion to // a "C string" ... the "open" function wants that) infile.open (infileName.c_str ()) ; // if successful ... if (infile) { catalog.makeEmpty (); // finally, read the file infile >> catalog; catalog.setTitle (infileName); // announcing success cerr << infileName << " read" << endl; // cleaning up ... infile.close (); catalog.showTitleAndSize (); } else { // failed to open (probably wrong name, or file already open) cerr << "Failed to open " << infileName << "." << endl; system ("dir | more"); } return catalog; }//readFile void showResults(Catalog &search) { cout << search; }//showResults void showCatalog(Catalog &catalog) { cout << catalog; }//showCatalog void browseCurrent(Catalog &search, Catalog &catalog) { int number; char letter; bool quit=false; do{ cerr << "Input? " ; cin >> letter >> number; switch(letter) { case 'Q': case 'q': quit=true; break; default : cerr << "Error in input" << endl; break; case 'b': case 'B': cout << catalog.returnEleven(spot, search); break; }//switch }while(!quit); }//browseCurrent