//------------------------------------------ // Proj3-3.C - second solution to project 3 // // Dennis L. Frey CMSC 202 Fall 2002 // // This program improves on the first solution // by using a switch statement to call a function // for each command rather than having a very // large if/else statement with all the code in-line // This makes the code much easier to read and maintain // // In order to use the switch statement, this program // stores the command strings in an array. The index // into the array of commands is used for the switch. // If a new command is added, just add the string to the // array, add a case to the switch and write the function //------------------------------------------------- // // With this many functions, it's probably a good idea // to create an auxiliary .C and .H file. //----------------------------------------------- #include #include #include #include "library.H" using namespace std; // function prototypes for functions // in this file bool ReadLine (ifstream& in, string& s); void AddBook (ifstream& in, Library& library); void AddPatron (ifstream& in, Library& library); void Books (const Library& lib); void CheckIn (ifstream& in, Library& lib); void CheckOut (ifstream& in, Library& lib); void Patrons (const Library& lib); void PatronsBooks (ifstream& in, const Library& lib); // const ints for each command const int ADDBOOK = 0; const int ADDPATRON = 1; const int BOOKS = 2; const int PATRONS = 3; const int CHECKIN = 4; const int CHECKOUT = 5; const int PATRONSBOOKS = 6; // array of command strings // same order as const ints above const int NRCOMMANDS = 7; const string commands[NRCOMMANDS] = {"ADDBOOK", "ADDPATRON", "BOOKS", "PATRONS", "CHECKIN", "CHECKOUT", "PATRONSBOOKS" }; int main (int argc, char *argv[] ) { // check number of command line args if (argc != 2) { cerr << "Usage: Proj2 " << endl; exit (-1); } // instantiate and open the file ifstream infile (argv[1]); if (!infile) { cerr << "Unable to open file: " << argv[1] << endl; exit (-2); } // all ok - instantiate the library Library library; string command; // Loop until no more commands in the file while (ReadLine (infile, command)) { // switch to function for the command // 'while' uses short-circuit evaluation int i = 0; while ((i < NRCOMMANDS) && (commands[i] != command)) i++; switch (i) // one case for each command { case ADDBOOK: AddBook (infile, library); break; case ADDPATRON: AddPatron (infile, library); break; case BOOKS: Books (library); break; case CHECKIN: CheckIn (infile, library); break; case CHECKOUT: CheckOut (infile, library); break; case PATRONS: Patrons (library); break; case PATRONSBOOKS: PatronsBooks (infile, library); break; default: // else bad command cerr << "Invalid command: " << command << endl; break; } } return 0; } //----------------------------------------------- // AddBook // Reads title and author from file, echos command // and adds the book to the library //----------------------------------------------- void AddBook (ifstream& infile, Library& library) { string title, author; ReadLine (infile, title); ReadLine (infile, author); cout << "ADDBOOK: " << title << ", " << author << endl; library.AddBook (title, author); } //-------------------------------------------- // AddPatron // reads patron info from the file // echos command and adds patron to library //-------------------------------------------- void AddPatron (ifstream& infile, Library& library) { string name; ReadLine (infile, name); cout << "COMMAND:ADDPATRON, "<< name << endl; library.AddPatron (name); } //-------------------------------------------- // Books // Prints all books in the library //------------------------------------------- void Books (const Library& library) { cout << "COMMAND:BOOKS" << endl; library.ListBooks( ); } //---------------------------------------------- // CheckIn // reads title from input file, echos // command and checks in book //---------------------------------------------- void CheckIn (ifstream &infile, Library& library) { string title; ReadLine (infile, title); cout << "COMMAND:CHECKIN, " << title << endl; library.CheckIn (title); } //------------------------------------------------- // CheckOut // Reads title and patron from file, echos command // checks out book if no errors detected //------------------------------------------------- void CheckOut (ifstream& infile, Library& library) { string title, patron; ReadLine (infile, title); ReadLine (infile, patron); cout << "CHECKOUT: " << title << ", " << patron << endl; library.CheckOut (title, patron); } //--------------------------------------- // Patrons // Lists all patrons in the library //----------------------------------------- void Patrons (const Library& library) { cout << "COMMAND:PATRONS" << endl; library.ListPatrons( ); } //------------------------------------------------ // PatronsBooks // Reads patron's name from file and lists // books he has checkedout of library //------------------------------------------------ void PatronsBooks (ifstream& infile, const Library& library) { string name; ReadLine (infile, name); cout << "COMMAND:PATRONSBOOKS, " << name << endl; library.ListPatronsBooks (name); } //------------------------------------------------ // ReadLine // skips blank lines in the file // returns true if non-blank line read // returns false on eof //------------------------------------------------- bool ReadLine (ifstream& in, string& s) { s = ""; // empty string with length==0 // loop until eof or non-blank string read while (!in.eof( ) && s.length() == 0) { getline (in, s); if (s.length() != 0) return true; } // eof return false; }