// Project 2 for CS 1704 Spring 2004 // // Programmer: Robert Houtman // OS: Windows XP Professional // Last modified: 24 FEB 2004 // // Purpose // This program reads a list of students and multimedia collections that // each student has. // // The program then gets commands from an input file to manage the // database. //This .cpp file holds all the implementations for the Info class #include "Info.h" #include #include #include #include using namespace std; //////////////////////////////////////////////////////////////// Info() // Constructs two default arrays with 100 members // // Parameters: // Students[100] an array that holds 100 student records // Items[100] an array that holds 100 item records // // Pre: none // // Post: There will two 100 member arrays in the Info class, one with default student info // and the other with default item info // // Returns: none // // Called by: main() // Calls: StudInfo::StudInfo(), ItemInfo::ItemInfo() // Info::Info() { int i=0; //variable used to control initialization //This while loop makes an array of 100 empty StudInfo classes and an array //of 100 ItemInfo classes while(i<100) { Students[i]; Items[i]; i++; } } //////////////////////////////////////////////////////////////// Info(ofstream &oFile) // Constructs two arrays with 100 members, and fills as much of them in as possible based // on the input file // // Parameters: // Students[100] an array that holds 100 student records // Items[100] an array that holds 100 item records // // Pre: none // // Post: There will two 100 member arrays in the Info class, one with student info // and the other with item info // // Returns: none // // Called by: main() // Calls: StudInfo::StudInfo(iFile, oFile) // ItemInfo::ItemInfo(iFile, oFile) // void Info::FirstAddS(ofstream &oFile) { ifstream iFile("students.data"); assert(iFile); int i=0;//variable to control while statement bool complete=false; //variable to test if action was completed bool Ownertest=false; //variable to see if there is an owner field stringstream testOwner(stringstream::in|stringstream::out); string Owner;//variable to hold owner email string category; //variable to hold what information is included in the item description stringstream Input(stringstream::in | stringstream::out); //variable to hold all the input //This while loop gets all the information for the item and inserts it into a strinstream //so that it may be passed into the function to add it to the database getline(iFile, category, '\n'); while(iFile) { while((category != "@@" || category != "@@ ") && category != "") { if(category == "@Name:" || category == "@Name: ") { string Name; getline(iFile, Name, '\n'); Input << "N" << Name << "!"; } else if(category == "@Phone Number:"||category == "@Phone Number: ") { string Phone; getline(iFile, Phone, '\n'); Input << "P" << Phone << "!"; } else if(category == "@E-mail:"||category == "@E-mail: ") { string Email; getline(iFile, Email, '\n'); Input << "E" << Email << "!"; Ownertest = true; } else if(category == "@Address Line 1:"||category == "@Address Line 1: ") { string Address1; getline(iFile, Address1, '\n'); Input << "A" << Address1 << "!"; } else if(category == "@Address Line 2:"||category == "@Address Line 2: ") { string Address2; getline(iFile, Address2, '\n'); Input << "D" << Address2 << "!"; } else if(category == "@City:"||category == "@City: ") { string City; getline(iFile, City, '\n'); Input << "C" << City << "!"; } else if(category == "@State:"||category == "@State: ") { string State; getline(iFile, State, '\n'); Input << "S" << State << "!"; } else if(category == "@Zipcode:" ||category == "@Zipcode: ") { string Zip; getline(iFile, Zip, '\n'); Input << "Z" << Zip << " "; } else if(category == "@Other:"||category == "@Other: ") { string Other; getline(iFile, Other, '\n'); Input << "O" << Other << "!"; } getline(iFile, category, '\n'); } Input << "@"; //this while statement searches for an empty Student to add the information to //and then adds it if(Ownertest == true) { if(Students[i].AddStudent(Input)) { i++; } } Ownertest = false; getline(iFile, category, '\n'); } iFile.close(); ifstream inFile("items.data"); assert(inFile); i=0; Ownertest=false; //variable to see if there is an owner field Input << ""; //This while loop gets all the information for the item and inserts it into a stringstream //so that it may be passed into the function to add it to the database getline(inFile, category, '\n'); while(inFile) { while((category != "@@"||category != "@@ ")&& category != "") { if(category == "@Item:"||category == "@Item: ") { string Item; getline(inFile, Item, '\n'); Input << "I" << Item << "!"; } else if(category == "@Media Type:"||category == "@Media Type: ") { string Media; getline(inFile, Media, '\n'); Input << "M" << Media << "!"; } else if(category == "@by:"||category == "@by: ") { string Artist; getline(inFile, Artist, '\n'); Input << "A" << Artist << "!"; } else if(category == "@Owner:"||category == "@Owner: ") { getline(inFile, Owner, '\n'); Input << "O" << Owner << "!"; Ownertest = true; } else if(category == "@Price:"||category == "@Price: ") { string Price; getline(inFile, Price, '\n'); Input << "P" << Price << "!"; } else if(category == "@Availability:"||category == "@Availability: ") { string Avail; getline(inFile, Avail, '\n'); Input << "V" << Avail << "!"; } getline(inFile, category, '\n'); } Input << "E"; //This if statement makes sure there is an Owner field and if there is it calls the //AddanItem function if(Ownertest == true) { bool complete = false; for(int j = 0; j<100; j++) { if (Students[j].Emailtest(Owner) && complete == false) { if(Items[i].AddanItem(Input)) { complete = true; i++; } } } } Ownertest = false; getline(inFile, category, '\n'); } inFile.close(); } //////////////////////////////////////////////////////////////// AddStud(ifstream &iFile, ofstream &oFile) // Adds a student to the next empty array member // // Parameters: // iFile - input stream to get student information from the input file // oFile - output stream to output whether the addition was a success or a failure // // Pre: There is an empty Students array member // There is an email address in the student being added // // Post: A student will be added to the Students array // // Returns: none // // Called by: main() // Calls: StudInfo::AddStudent(iFile) // void Info::AddStud(ifstream &iFile, ofstream &oFile) { int i=0;//variable to control while statement bool complete=false; //variable to test if action was completed bool Ownertest=false; //variable to see if there is an owner field string Owner;//variable to hold owner email string category; //variable to hold what information is included in the item description stringstream Input(stringstream::in | stringstream::out); //variable to hold all the input //This while loop gets all the information for the item and inserts it into a strinstream //so that it may be passed into the function to add it to the database getline(iFile, category, '\n'); while((category != "@@" ||category != "@@ ") && category != "") { if(category == "@Name:"||category == "@Name: ") { string Name; getline(iFile, Name, '\n'); Input << "N" << Name << "!"; } else if(category == "@Phone Number:"||category == "@Phone Number: ") { string Phone; getline(iFile, Phone, '\n'); Input << "P" << Phone << "!"; } else if(category == "@E-mail:"||category == "@E-mail: ") { string Email; getline(iFile, Email, '\n'); Input << "E" << Email << "!"; Ownertest = true; } else if(category == "@Address Line 1:"||category == "@Address Line 1: ") { string Address1; getline(iFile, Address1, '\n'); Input << "A" << Address1 << "!"; } else if(category == "@Address Line 2:"||category == "@Address Line 2: ") { string Address2; getline(iFile, Address2, '\n'); Input << "D" << Address2 << "!"; } else if(category == "@City:"||category == "@City: ") { string City; getline(iFile, City, '\n'); Input << "C" << City << "!"; } else if(category == "@State:"||category == "@State: ") { string State; getline(iFile, State, '\n'); Input << "S" << State << "!"; } else if(category == "@Zipcode:"||category == "@Zipcode: ") { string Zip; getline(iFile, Zip, '\n'); Input << "Z" << Zip << " "; } else if(category == "@Other:"||category == "@Other: ") { string Other; getline(iFile, Other, '\n'); Input << "O" << Other << "!"; } getline(iFile, category, '\n'); } Input << "@"; //this while statement searches for an empty Student to add the information to //and then adds it if(Ownertest == false) { oFile << "Failure\n"; } else { i = 0; while(i<100 && complete == false) { if (!Students[i].fulloremptytest()) { if(Students[i].AddStudent(Input)) { oFile << "Success\n"; complete = true; } } else { i++; } } if(complete == false) { oFile << "Failure\n"; } } } //////////////////////////////////////////////////////////////// AddItem(ifstream &iFile, ofstream &oFile) // Adds an item to the next empty array member // // Parameters: // iFile - input stream to get item information from the input file // oFile - output stream to output whether the addition was a success or a failure // // Pre: There is an empty Items array member // The email address exists in the Students array // // Post: An item will be added to the Items array // // Returns: none // // Called by: main() // Calls: StudInfo::AddanItem(iFile) // void Info::AddItem(ifstream &iFile, ofstream &oFile) { int i=0, j=0;//variables to control while statements bool complete=false; //variable to test if action was completed bool Ownertest=false; //variable to see if there is an owner field string Owner;//variable to hold owner email string category; //variable to hold what information is included in the item description stringstream Input(stringstream::in | stringstream::out); //variable to hold all the input //This while loop gets all the information for the item and inserts it into a stringstream //so that it may be passed into the function to add it to the database getline(iFile, category, '\n'); while((category != "@@" ||category != "@@ ")&& category != "") { if(category == "@Item:"||category == "@Item: ") { string Item; getline(iFile, Item, '\n'); Input << "I" << Item << "!"; } else if(category == "@Media Type:"||category == "@Media Type: ") { string Media; getline(iFile, Media, '\n'); Input << "M" << Media << "!"; } else if(category == "@by:"||category == "@by: ") { string Artist; getline(iFile, Artist, '\n'); Input << "A" << Artist << "!"; } else if(category == "@Owner:"||category == "@Owner: ") { getline(iFile, Owner, '\n'); Input << "O" << Owner << "!"; Ownertest = true; } else if(category == "@Price:"||category == "@Price: ") { string Price; getline(iFile, Price, '\n'); Input << "P" << Price << "!"; } else if(category == "@Availability:"||category == "@Availability: ") { string Avail; getline(iFile, Avail, '\n'); Input << "V" << Avail << "!"; } getline(iFile, category, '\n'); } Input << "E"; //This if statement makes sure there is an Owner field and if there is it calls the //AddanItem function if(Ownertest == false) { oFile << "Failure\n"; } else { i = 0; for(i = 0; i < 100; i++) { if (Students[i].Emailtest(Owner) && complete == false) { j = 0; while(j<100 && complete == false) { if(!Items[j].fulloremptytest()) { if(Items[j].AddanItem(Input)) { oFile << "Success\n"; complete = true; } } j++; } } } if(complete == false) { oFile << "Failure\n"; } } } //////////////////////////////////////////////////////////////// Delete(string email, ofstream &oFile) // Removes the student, and his/her item(s), with the given email address // // Parameters: // email - email address of student to be deleted // oFile - output stream to output whether the addition was a success or a failure // // Pre: The email address exists in the Students and Items arrays // // Post: The student's information and his/her item(s) will be removed from the database // // Returns: none // // Called by: main() // Calls: StudInfo::RemoveStudent(email) // ItemInfo::RemoveItem(email) // void Info::Delete(string email, ofstream& oFile) { int i=0, //controls student searching while loop j=0; //controls item searching while loop bool complete = false; //variable to test if action was completed while(i<100 && complete == false) { if(Students[i].Emailtest(email)) { Students[i].RemoveStudent(); while(j<100) { if(Items[j].Email(email)) { Items[j].RemoveItem(); } j++; } complete = true; } i++; } if(complete == false) { oFile << "Failure\n"; } else { oFile << "Success\n"; } } //////////////////////////////////////////////////////////////// Print(ofstream &oFile, string command) // Prints information requested in the command.data input file // // Parameters: // command - determines what is to be printed // oFile - output stream to output whether the addition was a success or a failure // // Pre: The email address exists in the Students and Items arrays(for specific printing) // // Post: The student's information and his/her item(s) will be printed to the output file // Or all the students/items will be printed to the output file // // Returns: none // // Called by: main() // Calls: StudInfo::PrintStudent(email) // ItemInfo::PrintItem(email) // void Info::Print(ofstream& oFile, string command) { int i=0; //controls while loops bool complete=false; //control to test if print was successful //This if else if statement calls the print function for the requested class if(command == "students") { while(i<100) { if(Students[i].fulloremptytest()) { Students[i].PrintStudent(oFile); i++; } else { i++; } } } else if(command == "items") { while(i<100) { if(Items[i].fulloremptytest()) { bool All = true; Items[i].PrintItem(oFile, All); } i++; } } else { string email = command; //allows email string to be passed into the email checking function //for both the StudInfo and ItemInfo classes //This while loop prints the information of the student with the given email address i = 0; while(i < 100) { if(complete == false) { if(Students[i].Emailtest(email)) { Students[i].PrintStudent(oFile); complete = true; } } i++; } //This while loop prints all items that are owned by the student with the given email //address for(i = 0; i < 100; i++) { if(Items[i].Email(email)) { bool All = false; Items[i].PrintItem(oFile, All); } } if(complete == false) { oFile << email << " not found.\n"; } } }