// Project 2 for CS 1704 Spring 2004 // // Programmer: Deborah A Thompson // OS: Windows XP Professional // System: Pentium III 500, 256 MB Memory // Compiler: Visual C++ 6.0, Service Pack 4 // Last modified: Febraury 23, 2004 // // Purpose: The first file will be a list of students with accompanying // information named "students.data". The second file will // be a list of multimedia items named "items.data". This class will // provide methods that implement the commands: print, add, delete and quit. #include #include "Organizer.h" #include #include #define NDEBUG //////////////////////////////////////////////////////////////// Organizer // opens and reads 2 data files, items and students // // Parameters: // None // // Pre: None // // Post: Data files are read in and student & items are set to arrays // // Returns: None // // Called by: MultimediaOrganizer ( RdCommand ) // Calls: SetAddress, Student.Update, SetDefault, Item.IUpDate, SetDefaultItem // //constructor //opens & read students.data and items.data Organizer::Organizer() { SCounter = 0; //sets student counter to zero ICounter = 0; //sets item counter to zero int j = 0; //sets j to zero char Check[16]; //check read character of phone and price bool correct; //format is correct bool inEntry = false; //whether address was read in string Command = DEFAULT; //command name string inStreet, //read in street inApt, //read in apt inCity, //read in city inState, //read in state inZip; //read in zipcode bool Checker = false; //set checker to false //initialize Check[] for (int a = 0; a < 16; a++) { Check[a] = ' '; } //sets defaults students SetDefault(); //sets defaults items SetDefaultItem(); //open students.data ifstream ReadS; ReadS.open("students.data"); //read students.data do { //set defaults inApt = DEFAULT; inCity = DEFAULT; inState = DEFAULT; inStreet = DEFAULT; inZip = DEFAULT; inEntry = false; //loop with student do { //read in '@' ReadS >> Check[1]; // error checking assert #2 assert(Check[1] == '@'); //reads in command getline(ReadS, Command, '\n'); //cout << "Command name: " << Command << endl; //if command is name if (Command == "Name:") { //read in student name getline (ReadS, inName, '\n'); //cout << "Name is read: " << inName << endl; } //if command is phone number if (Command == "Phone Number:") { //check format of phone number and read it in correct = true; //sets to defaults for (int a = 1; a < 16; a++) { Check[a] = ' '; } //read in character ReadS >> Check[1]; //checking formating if (Check[1] == '(') { for (int a = 2; a < 5; a++) { //read in character ReadS >> Check[a]; //if character is not number, format is incorrect if ((Check[a] < 48) || (Check[a] > 57)) { correct = false; } } } //else format of phone number is incorrect else { correct = false; } //read in character ReadS >> Check[5]; //sets next character to blank space Check[6] = ' '; //checks for correct format if (Check[5] == ')') { for (int a = 7; a < 10; a++) { //read in character ReadS >> Check[a]; //if character is not number, format is incorrect if ((Check[a] < 48) || (Check[a] > 57)) { correct = false; } } } //format of phone number is incorrect else { correct = false; } //reads in charactor ReadS >> Check[10]; //checks format of phone number if (Check[10] == '-') { for (int a = 11; a < 15; a++) { ReadS >> Check[a]; //if character is not number, format is incorrect if ((Check[a] < 48) || (Check[a] > 57)) { correct = false; } } } //incorrect format else { correct = false; } //if format is correct, put phone number into string if (correct = true) { for(int a = 1; a < 15; a++) { inPhone = inPhone + Check[a]; } } //ignore line ReadS.ignore(100,'\n'); } //if command is email, read in email of student if (Command == "E-mail:") { getline(ReadS, inEmail, '\n'); //cout << Command << " " << inEmail << endl; } //if command is address line 1 //then read in street address of student if (Command == "Address Line 1:") { inEntry = true; getline(ReadS, inStreet, '\n'); } //if command is address line 2 //then read in apt address of student if (Command == "Address Line 2:") { inEntry = true; getline(ReadS, inApt, '\n'); } //if command is city //then read in city if (Command == "City:") { inEntry = true; getline(ReadS, inCity, '\n'); } //if command is state //then read in state if (Command == "State:") { inEntry = true; getline(ReadS, inState, '\n'); } //if command is zipcode //then read in zipcode if (Command == "Zipcode:") { inEntry = true; getline(ReadS, inZip, '\n'); } //if command is other //read in other student information if (Command == "Other:") { getline(ReadS, inOther, '\n'); } } while (Command != "@"); //new entry //cout << "Address " << SCounter << " " << inStreet << " " << inApt << " " << inCity << " " << inState << " " << inZip << endl; //sets address if address exists if (inEntry == true) { SetAddress(inEntry, inStreet, inApt, inCity, inState, inZip); } //adds student to database (array) aStud[SCounter].Update(inName,inPhone,inEmail,inAddress,inOther); //number of students added to database SCounter++; //cout << "New Entry" << endl; //sets defaults students SetDefault(); } while (ReadS); //open items.data ifstream ReadI; ReadI.open("items.data"); //read items.data do { //set defaults correct = true; Checker = false; //read is first charactor ReadI >> Check[1]; //set command to blank Command = " "; //read in entry do { correct = true; Checker = false; //assert error check #3 assert(Check[1] == '@'); //read in command read getline (ReadI, Command, '\n'); //cout << "Command reads:" << Command << endl; //if command is item, read in item name if (Command == "Item:") { Checker = true; getline (ReadI, inItem, '\n'); } //if command is media type, read in item type if (Command == "Media Type:") { Checker = true; getline (ReadI, inType, '\n'); } //if command is by, read in author name if (Command == "by:") { Checker = true; getline (ReadI, inAuthor, '\n'); } //if command is owner, read in owner's email if (Command == "Owner:") { Checker = true; getline (ReadI, inOwner, '\n'); } //if command is price, read in price if format is correct if (Command == "Price:") { //sets to defaults for (int a = 1; a < 15; a++) { Check[a] = ' '; } correct = true; //read in character ReadI >> Check[1]; //checks for correct format if (Check[1] != '$') { correct = false; } //reas in character ReadI >> Check[2]; //if not a number, format is incorrect if ((Check[2] < 48) || (Check[2] > 57)) { correct = false; } //read in character ReadI >> Check[3]; //checks that format is correct if (Check[3] == '.') { for (a = 4; a < 6; a++) { //reads in character ReadI >> Check[a]; //if not a number, format is incorrect if ((Check[a] < 48) || (Check[2] > 57)) { correct = false; } } } //if price is correct format, put it into a string if (correct = true) { for (a = 1; a < 7; a++) { inPrice = inPrice + Check[a]; } } else { correct = true; } Checker = true; //ignore line ReadI.ignore(100,'\n'); } //if availablility is command if (Command == "Availability:") { Checker = true; getline(ReadI, inStatus, '\n'); } //if command is @, reset things if (Command == "@") { correct = false; Checker = true; } //else read in stuff else { ReadI >> Check[1]; } if (Checker == false) { correct = false; } } while (correct == true); //check that owner of item's email is in student database :) Checker = false; //checker that item owner is in student array do { //cout << aStud[j].ViewEmail() << " equals " << inOwner << endl; //owner is in student database if(aStud[j].ViewEmail() == inOwner) { //email found Checker = true; //at position j = SCounter; } else j++; } while((j < SCounter) && (Checker != true)); //author found add item to item database if(Checker) { //add item to database; aItem[ICounter].IUpdate(inItem, inType, inAuthor, inOwner, inPrice, inStatus); } //cout << "next entry" << endl; ICounter++; j = 0; Checker = false; //sets defaults items SetDefaultItem(); } while (ReadI); // end while loop //close data files ReadI.close(); ReadS.close(); } //////////////////////////////////////////////////////////////// ~Organizer // deconstructor // Organizer::~Organizer() { // empty // no dynamic data } //////////////////////////////////////////////////////////////// pStudent // Print to the file all students in the order they were added to the array. // A single endline should be added after each student // // Parameters: // Log - output log file // // Pre: Command, Student and Item files have been read // // Post: Student information 'printed' to file // // Returns: nothing // // Called by: RdCommand // Calls: SetDefault. // //print students //print to file all students in order added to array void Organizer::pStudent(ofstream& Log) { //set defaults to false bool CityCheck = false, StateCheck = false, ZipCheck = false; //set values to the defaults SetDefault(); //print to log file Log << "print students" << endl; //loop thru all students in file for(int i = 0; i < 100; i++) { //views all student information aStud[i].ViewAll(inName, inPhone, inEmail, inAddress, inOther); //if student has name, print name if(inName != DEFAULT) { Log << "Name:" << endl; Log << " " << inName << endl; } //if student has phone number, print phone number if(inPhone != "") { Log << "Phone Number:" << endl; Log << " " << inPhone << endl; } //if student has email, print email if(inEmail != DEFAULT) { Log << "Email:" << endl; Log << " " << inEmail << endl; } //if student has address entry, print 'Address:' if(inAddress.Entry) { //set defaults CityCheck = false; StateCheck = false; ZipCheck = false; //print to log Log << "Address:" << endl; //if student has street, print street if(inAddress.Street != DEFAULT) Log << " " << inAddress.Street << endl; //if student has apt, print apt if(inAddress.Apt != DEFAULT) Log << " " << inAddress.Apt << endl; //if student has city, print city if(inAddress.City != DEFAULT) { CityCheck = true; Log << " " << inAddress.City; } //if student has state, print state if(inAddress.State != DEFAULT) { StateCheck = true; if (CityCheck) Log << ", "; else Log << " "; Log << inAddress.State; } //if student has zipcode, print zidecode if(inAddress.Zip != DEFAULT) { if ((CityCheck) && (StateCheck == false)) Log << ","; ZipCheck = true; Log << " " << inAddress.Zip << endl; } if (((CityCheck == true) || (StateCheck == true)) && (ZipCheck == false)) { Log << endl; } } //if student has other information, print other if(inOther != DEFAULT) { Log << "Other:" << endl; Log << " " << inOther << endl; } if(inEmail != DEFAULT) { Log << endl; } }// end of for loop } //////////////////////////////////////////////////////////////// SetDefault // sets student information to default values // // Parameters: // None // // Pre: None // // Post: Defaults set. // // Returns: Nothing // // Called by: pStudent, Organizer // Calls: None // //sets all private values to defaults when called void Organizer::SetDefault() { inName = DEFAULT; inPhone = ""; inEmail = DEFAULT; inOther = DEFAULT; inAddress.Apt = DEFAULT; inAddress.City = DEFAULT; inAddress.State = DEFAULT; inAddress.Street = DEFAULT; inAddress.Zip = DEFAULT; inAddress.Entry = false; return; } //////////////////////////////////////////////////////////////// SetDefaultItem // set item values to default values // // Parameters: // NONE // // Pre: NONE // // Post: Items set to default values // // Returns: None // // Called by: Organizer, // Calls: NONE // void Organizer::SetDefaultItem() { inItem = DEFAULT; inType = DEFAULT; inAuthor = DEFAULT; inOwner = DEFAULT; inPrice = ""; inStatus = "EMPTY"; return; } //////////////////////////////////////////////////////////////// Quit // exits your program & closes log file // // Parameters: // Log - output stream file // // Pre: Command file read // // Post: none // // Returns: none // // Called by: RdCommand // Calls: NONE // // quit void Organizer::Quit(ofstream& Log) { Log << "quit"; Log.close(); return; } //////////////////////////////////////////////////////////////// Delete // Where emailaddress is a students email address. // This will require you to search BOTH the array of students and the array of items, // deleting any student or item with that email address. // Print "Success" or "Failure" to the output file depending on the result of the delete. // // Parameters: // Log - output log file, Cemail - read in email name // // Pre: Command file read in // // Post: student with email is deleted // // Returns: NONE // // Called by: RdCommand // Calls: // void Organizer::Delete(ofstream& Log, string& Cemail) { // print to log file Log << "delete " << Cemail << endl; //search thru student database for(int i = 0; i < 100; i++) { if(aStud[i].ViewEmail() == Cemail) { // reset to defaults, students SetDefault(); // delete student at email aStud[i].Update(inName, inPhone, inEmail, inAddress, inOther); for(int j = 0; j < 100; j++) { if(aItem[j].ViewOwner() == Cemail) { //reset to defaults, items SetDefaultItem(); //delete item at email aItem[j].IUpdate(inItem, inType, inAuthor, inOwner, inPrice, inStatus); }//end email found in item }//end search thru item database Log << "Success" << endl; //LogTest(); return; }//end email found in student }//end search thru student database Log << "Failure" << endl; //LogTest(); return; } //////////////////////////////////////////////////////////////// add item // added to FIRST EMPTY array position // print "Success" or "Failure" depending on result of add // student with that email address MUST exist to add item // // Parameters: // Log - output log file, cName - read in item name, cType - read in type // cAuthor - read in author name, cOwner - read in owner email, // cPrice - read in price, cStatus - read in status of item // // Pre: Command file read // // Post: None // // Returns: NONE // // Called by: RdCommand // Calls: Item.IUpdate, Item.ViewOwner, Student.ViewEmail // void Organizer::addItem(ofstream& Log, string& cName, string& cType, string& cAuthor, string& cOwner, string& cPrice, string& cStatus) { //sets defaults int a; bool found1, found2; found1 = false; found2 = false; a = 0; //looks for student in database for (a = 0; a < 100; a++) { if (aStud[a].ViewEmail() == cOwner) { found1 = true; } } a = 0; //look for empty item spot do { if (aItem[a].ViewOwner() == "DEFAULT") { found2 = true; } else { a++; } } while ((found2 != true) && (a < 100)); //if student found and empty item spot, add item if ((found1) && (found2)) { aItem[a].IUpdate(cName, cType, cAuthor, cOwner, cPrice, cStatus); Log << "add item" << endl; Log << "Success" << endl; } //report failure if not add item else { Log << "add item" << endl; Log << "Failure" << endl; } return; } //////////////////////////////////////////////////////////////// aStudent // student be added to FIRST EMPTY array position // print "Success" or "Failure" depending on result of add // // Parameters: // Log - output log file, cName - read in name, cPNum - read in phone number, cEmail - read in email, // cAddress - read in address, cOther - read in other // // Pre: Command file read in // // Post: NONE // // Returns: Nothing // // Called by: RdCommand // Calls: ViewEmail, Update // void Organizer::aStudent(ofstream& Log, string& cName, string& cPNum, string& cEmail, Address& cAddress, string& cOther) { //set default... int a; bool found; found = false; a = 0; //searches for first empty spot do { if (aStud[a].ViewEmail() == "DEFAULT") { found = true; } else { a++; } } while (found == false && a < 100); //if empty spot found //then update/add student to array if (found) { aStud[a].Update(cName,cPNum,cEmail,cAddress,cOther); Log << "add student" << endl; Log << "Success" << endl; } //else not student and report failure else { Log << "add student" << endl; Log << "Failure" << endl; } return; } //////////////////////////////////////////////////////////////// pItem // print items // Print all items in the order they were added to the array. // A single endline should be added after each item. // print // Where emailaddress is a students email address. // This should first print the student's data and then all items s/he has. // Do NOT print his/her email address again for each item. // // Parameters: // Log - output log file, Email - read in email from file // // Pre: Command file read // // Post: NONE // // Returns: NONE // // Called by: RdCommand // Calls: ViewEmail, ViewAll // void Organizer::pItem(ofstream& Log, string& Email) { // set defaults bool Print = false, CityCheck = false, ZipCheck = false, StateCheck = false; string Name = DEFAULT, Type = DEFAULT, Author = DEFAULT, Owner = DEFAULT, Price = "", Status = "EMPTY"; int Flag = 0; //cout << "this is what email reads: " << Email << endl; //if email is default then print all items if (Email == "DEFAULT") { Log << "print items" << endl; Flag = 1; } //else print only items belonging to person of email else { Log << "print " << Email << endl; Flag = 101; //finds matching email from read in one for (int b = 0; b < 100; b++) { if (aStud[b].ViewEmail() == Email) { Flag = b; } } //to print student information if (Flag < 100) { //view all student's values aStud[Flag].ViewAll(inName, inPhone, inEmail, inAddress, inOther); //if name exists print it if(inName != DEFAULT) { Log << "Name:" << endl; Log << " " << inName << endl; } //if phone number exists print it if(inPhone != "") { Log << "Phone Number:" << endl; Log << " " << inPhone << endl; } //if email exists print it if(inEmail != DEFAULT) { Log << "Email:" << endl; Log << " " << inEmail << endl; } //if address was entered if(inAddress.Entry) { //set defaults CityCheck = false; StateCheck = false; ZipCheck = false; //print to log Log << "Address:" << endl; //if street is there print it if(inAddress.Street != DEFAULT) Log << " " << inAddress.Street << endl; //if apt is there print it if(inAddress.Apt != DEFAULT) Log << " " << inAddress.Apt << endl; //if city is there print it if(inAddress.City != DEFAULT) { CityCheck = true; Log << " " << inAddress.City; } //if state is there print it if(inAddress.State != DEFAULT) { if (CityCheck) Log << ", "; else Log << " "; StateCheck = true; Log << inAddress.State; } //if zipcode is there print it if(inAddress.Zip != DEFAULT) { //sets up format of printing if ((CityCheck == true) && (StateCheck == false)) { Log << ","; } ZipCheck = true; Log << " " << inAddress.Zip << endl; } if (((CityCheck == true) || (StateCheck == true)) && (ZipCheck == false)) { Log << endl; } } } //email not found else { Log << Email << " not found." << endl; } } //print items for (int a = 0; a < 100; a++) { aItem[a].ViewAll(Name,Type,Author,Owner,Price,Status); // << "..." << Email << "... being compared to item: " << a << " ..." << Owner << "..." << endl; if (((Email == "DEFAULT") && (Owner != "DEFAULT")) || ((Owner == Email) && (Owner != "DEFAULT"))) { // Log << "Printing Item" << endl; Print = true; //if item name found, print it if (Name != "DEFAULT") { Log << "Item:" << endl; Log << " " << Name << endl; } //if item type found, print it if (Type != "DEFAULT") { Log << "Media Type:" << endl; Log << " " << Type << endl; } //if author found, print it if (Author != "DEFAULT") { Log << "by:" << endl; Log << " " << Author << endl; } //if owner not equal to email, print it //it not printing all items do this if (Owner != Email) { Log << "Owner:" << endl; Log << " " << Owner << endl; } //if print found, print it if (Price != "") { Log << "Price:" << endl; Log << " " << Price << endl; } //if status found, print it if (Status != "EMPTY") { Log << "Availability:" << endl; Log << " " << Status << endl; } Log << endl; } } return; } //////////////////////////////////////////////////////////////// SetAddress // set address of to inputted information // // Parameters: // inEntry - whether address exists, inStreet - read in street, inApt - read in apt, // inCity - read in City, inState - read in State, inZip read in zipcode // // Pre: NONE // // Post: NONE // // Returns: NONE // // Called by: // Calls: NONE // //set address void Organizer::SetAddress(bool inEntry, string& inStreet,string& inApt,string& inCity, string& inState, string& inZip) { inAddress.Street = inStreet; inAddress.Apt = inApt; inAddress.City = inCity; inAddress.State = inState; inAddress.Zip = inZip; inAddress.Entry = inEntry; return; }