//================================================================================ // Project 2 for CS 1704 Spring 2004 // // Programmer: Elizabeth Liu // OS: Windows XP // Compiler: Microsoft Visual Studio.net 2003 // Last modified: Feb 23, 2004 // // Purpose // This program will be implementing a console, command line based Multimedia // Organizer. The basic idea is that your current Video, CD, DVD, cassette, etc // collection should be able to be organized along with other students' collections // so that we have a huge database of multimedia items that can be borrowed, // traded, etc.(program spec) // // the output files shows if the commands were successful or not, also print out // informations stored in the array // // includes #include #include using namespace std; #include "Controller.h" // prototypes void AddItem(ofstream&, ifstream&, Controller&); void AddStudent(ofstream&, ifstream&, Controller&); string GetNextLine(ifstream&); void main() { ifstream commandIn("commands.data"); // input file assert (commandIn); // assert statement ofstream Out("output.data"); // output file Controller myController; // declare myController string nextLine = ""; // initialize next line // while command input file is valid while (commandIn) { // call the GetNextLine function nextLine = GetNextLine(commandIn); // myComLine set istringstream myComLine(nextLine.c_str()); // initialize command and command info to blank string command = ""; string comInfo = ""; // read in command and command information myComLine >> command; myComLine >> comInfo; if (command == "print") { // if the command is print, then output "print" and the command info Out << command<< " " << comInfo << endl; if (comInfo == "students") { // if the command is print student, // then call the PrintStudents function in controller class myController.PrintStudents(Out); } else if (comInfo == "items") { // if comInfo is print itmes, // then call the printItem funciton in controller class myController.PrintItems(Out); } else { // else print according to individual e-mail addresses // call the IndividualStatus function in the controller class myController.IndividualStatus(Out, comInfo); } } // if command is add else if (command == "add") { // and if comInfo is item if (comInfo == "item") { // output command and comInfo header Out << command << " " << comInfo << endl; // call the AddItem function AddItem(Out, commandIn, myController); } // if the command is Add student else if (comInfo == "student") { // then output command and comInfo header Out << command << " " << comInfo << endl; // call the AddStudent function AddStudent(Out, commandIn, myController); } } // else if the command is delete else if (command == "delete") { // set a boolean variable success to delete function in controller class bool success = myController.Delete(comInfo); // output the command and comInfo header Out << command << " " << comInfo << endl; // if delete is success if (success) { // output success Out << "Success" << endl; } else { // if delete is unsuccess, then output failure Out << "Failure" << endl; } } // else if command is quit, then ouput quit, and close input output files else if (command == "quit") { Out << "quit"; // close everything and quit. commandIn.close(); Out.close(); return; } } } //////////////////////////////////////////////////////////////// //Set a temperary variable to add students, then later add to the controller // class // // Parameters: // ofstream& Out: output stream // ifstream& studentIn: student input file // Controller& myController: class defined type // // Pre: no new add student info were set to tempStudent, no ouputs // // Post: new student info were set to tempStudent, and ready to be added // in the controller class, ouptut success or failure // // Returns: none // // Called by: main // Calls: tempStudent.(SetName(), SetPhoneNumber(), SetEmail(), SetAddL1(), // SetADDL2(),SetCity(), SetState(), SetZipcode(), SetOther), // myController.AddStudent() // void AddStudent(ofstream& Out, ifstream& studentIn, Controller& myController){ // set stillLoading to true bool stillLoading = true; // initiazlie string variables string infoType; string info; // declare tempStudent StudentInfo tempStudent; // while still loading is true while (stillLoading){ // get the info and infotype getline(studentIn, infoType, '\n'); getline(studentIn, info, '\n'); // if infotype is name, then set name if (infoType == "@Name:") { tempStudent.SetName(info); } // if infotype is phone number, then set phonenumber else if (infoType == "@Phone Number:") { // check to make sure the phone number is valid. bool valid = true; unsigned int i; // info.length is unsigned // making sure all the digits in the phone number are valid for (i = 0; i < info.length(); i++) { // include spaces, numbers and ( ) s, obtained from ascii code online if ((int(info[i]) == 32) || ((int(info[i]) <= 57) && (int(info[i]) >= 40)) ) { // do nothing. } else { valid = false; // set valid to false } } // if valid is true if (valid) { // set phone number tempStudent.SetPhoneNumber(info); } } // if info type is e-mail, then set e-mail else if (infoType == "@E-mail:") { tempStudent.SetEmail(info); } // if infotype is address line 1, then set address line1 else if (infoType == "@Address Line 1:") { tempStudent.SetAddL1(info); } // if infotype is address line2, then set address line 2 else if (infoType == "@Address Line 2:") { tempStudent.SetAddL2(info); } // if infotype is city, then set city else if (infoType == "@City:") { tempStudent.SetCity(info); } // if infotype is state, then set state else if (infoType == "@State:") { tempStudent.SetState(info); } // if infotype is zipcode, then set zipcode, and make sure its valid else if (infoType == "@Zipcode:") { bool valid = true;// preset valid to true unsigned int i; // set i as unsigned int for (i = 0; i < info.length(); i++) { // if only contains numbers if (int(info[i]) <= 57 && int(info[i]) >= 48) { // we're ok so do nothing } else { valid = false; // valid to false } } // if valid is true, set zipcode if(valid) { tempStudent.SetZipcode(info); } } // if infotype is other, set other else if (infoType == "@Other:") { tempStudent.SetOther(info); } // if infotype is @@, stop setting temp variables else if (infoType == "@@") { stillLoading = false;// set still loading is false } } bool added;// bool variable added // call the Add student function in controller class added = myController.AddStudent(tempStudent); // if added, output success if (added) { Out << "Success"<< endl; } // else output failure else { Out << "Failure" << endl; } } //////////////////////////////////////////////////////////////// //Set a temperary variable to add items, then later add to the controller // class // // Parameters: // ofstream& Out: output stream // ifstream& studentIn: student input file // Controller& myController: class defined type // // Pre: no new add items info were set to tempItem, no ouputs // // Post: new items info were set to tempItem, and ready to be added // in the controller class, ouptut success or failure // // Returns: none // // Called by: main // Calls: tempItem.( SetItem(), SetType(), SetAuthor(), SetEmail(), // SetOwner(), SetPrice(), SetAvailability()), // myController.AddItem() // void AddItem(ofstream& Out, ifstream& itemIn, Controller& myController) { // set still loading to true bool stillLoading = true; // declare string variables string infoType; string info; // declare tempItem ItemInfo tempItem; // while still loading is true while (stillLoading){ // get the info and infotype getline(itemIn, infoType, '\n'); getline(itemIn, info, '\n'); // info type is item, then set item if (infoType == "@Item:") { tempItem.SetItem(info); } // if infotype is media type, hten set type else if (infoType == "@Media Type:") { tempItem.SetType(info); } // if infotype is by, then set author else if (infoType == "@by:") { tempItem.SetAuthor(info); } // if infotype is owner, then set owner's e-mail else if (infoType == "@Owner:") { tempItem.SetEmail(info); } // if infotype is price, then set price else if (infoType == "@Price:") { // make sure price formate is valid bool valid = true;// set valid to true unsigned int i; // info.length is unsigned // making sure all the digits in the phone number are valid for (i = 0; i < info.length(); i++) { // include spaces, numbers and ( ) s, base on ascii codes obtained online if ((int(info[i]) == 32) || (int (info[i])==36) || (int (info[i]) == 46) || ((int(info[i]) <= 57) && (int(info[i]) >= 40)) ) { // do nothing. } // set valid to false else { valid = false; } } // if valid, then set price if (valid) { tempItem.SetPrice(info); } } // if infotype is availability, then set status else if (infoType == "@Availability:") { tempItem.SetAvailability(info); } // if infotype is @@, then stop setting else if (infoType == "@@") { // and set still loading to false stillLoading = false; } } // bool variable added bool added; // call the addItem function in controller class added = myController.AddItem(tempItem); // if added, then output success if (added) { Out << "Success"<< endl; } // else, output failure else { Out << "Failure" << endl; } } //////////////////////////////////////////////////////////////// // read in the next line // // Parameters: // ifstream& In: input file stream // // Pre: no next line has been read // // Post: get the next line // // Returns: strCommand // // Called by: main // Calls: none // string GetNextLine(ifstream& In) { // set strCommand to blank string strCommand = ""; // get the command 'till the new line character getline(In, strCommand, '\n'); // while the command line length is less than one while (strCommand.length() < 1) { // then get the commnad 'till newline character getline(In, strCommand, '\n'); } // return strCommand return strCommand; } //============================================================================ /* On my honor: - I have not discussed the C++ language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C++ language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C++ language code or documentation used in my program was obtained from another source, such as a textbook or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. Elizabeth Liu */ //=========================================================================================