// // 091705897.cpp // Wayne Swanson // 091-70-5897 // Project2 MulitiMedia Finished on 7/17/03 // // usage: project1.exe [1 or 2 or 3] // 1 = switch1 prints out students to test file // 2 = switch2 prints out data to test file // 3 = swithc3 prints out student with the email // the switches are optional // // Summary: // This program first parses out the information for debuggin purposes // the switches are listed above and a test file will be created at each // request. // // The program is to organize and list all the students and their muli-media // collection. The student will have various information that will be kept // with them. Namely their name,email,phone etc. which will be store int the // array list. Then the items will be stored in the items array which will hold // all all the multimedia items. The items can be inserted only if a student // with that email exists. There can only be one student in the student list. // // The program starts out with the creating mediaManager which invokes the // creation of the students and items objects. They will then open the files // to be processed. The student and item array should be loaded and the // manager is the only interface between the user command inputs and the // students and items. All commands will come from the commands.data file // which then will be processed. They valid commands will then invoke // the appropriate mediaManager.accesscor . // #include #include //for ifstream,ofstream #include //for isstringstream,ostringstream to memory i/o #include //for strlen(); #include #include #define success true: using namespace std; enum validCmd {invalidCmd,printOnlyStudents,printOnlyItems,printStudentData, addStudent,deleteStudent,addItem,deleteItem,quit}; class classPerson { //define friend class for printing out to stream friend ostringstream &operator<<(ostringstream&, classPerson &); //friend istream &operator<<(istream&, classPerson &); //friend ostream &(ostream&, classPerson &); private: string strName; //holds person's name string strPhone; //holds a phone number string strEmail; //holds email address string strStreet1; //holds first part of addres string strStreet2; //holds seoncd part of address string strCity; //holds city address string strState; //holds state string strOther; //holds misc information string strZipCode; //stores the zipcode public: classPerson(); //constructor; ~classPerson(); //destructor string getName(); //returns the person's name string getPhone(); string getEmail(); //returns the email address string getStreet1(); //holds the person's street address string getStreet2(); //holds the person's street address2 string getCity(); //holds the city string getState(); //holds the state string getZip(); //holds the zipcode string getOther(); //returns misc info of person void setName(string name); //sets the name of person void setEmail(string email); //sets the email address void setPhone(string phone); //sets the phone number of person void setStreet1(string); //sets the street1 void setStreet2(string); //sets the second street add void setCity(string); //sets the city void setState(string); //sets the state void setOther(string); //sets misc notes of owner void setZipCode(string); //sets the zipcode bool Exists(string email); //true if person exists void clearAll(); //sets all private members="" bool isFreeNode(); void print(ofstream &outFile); }; //------------------------classPerson.cpp------------------------------- //Default constructor for person //should set all strings to NULL classPerson::classPerson() { strName =""; //holds person's name strPhone = ""; //holds a phone number strEmail = ""; //holds email address strStreet1=""; //holds first part of addres strStreet2=""; //holds seoncd part of address strCity =""; //holds city address strState =""; //holds state strOther = ""; //holds misc information strZipCode =""; //stores the zipcode } classPerson::~classPerson() { } //----------------------------------------------------------------- string classPerson::getEmail() { return strEmail; //holds email address } //-----------------------------classPerson::freeNode()---------------- //Description: Returns true if the email is not assigned to NULL //Precondition: Available array //Postcondition: None // bool classPerson::isFreeNode() { //cout <<"classPerson::freeNode --->" << strEmail<< "<---" << endl; if(strEmail == "") return true; //email does not contain data else return false; //email contains data } //-----------------------------classPerson::getName()--------------------------- //Description: Returns the person's name string string classPerson::getName() { return strName; } //----------------------------classPerson::getPhone()--------------------------- //Description: Returns the person's phone number string string classPerson::getPhone() { return strPhone; } //----------------------------classPerson::getStreet1()--------------------------- //Description: Returns the person's street1 string string classPerson::getStreet1() { return strStreet1; } //----------------------------classPerson::getStreet2()--------------------------- //Description: Returns the person's street2 string string classPerson::getStreet2() { return strStreet2; } //----------------------------classPerson::getCity()--------------------------- //Description: Returns the person's city string string classPerson::getCity() { return strCity; } //----------------------------classPerson::getState()--------------------------- //Description: Returns the person's city string string classPerson::getState() { return strState; } //------------------------------classPerson::getZip()--------------------------- //Description: Returns the person's city string string classPerson::getZip() { return strZipCode; } //----------------------------classPerson::getOther()--------------------------- //Description: Returns the person's other misc information string string classPerson::getOther() { return strOther; } //----------------------------classPerson::clearAll()--------------------------- void classPerson::clearAll() { strName =""; //holds person's name strPhone = ""; //holds a phone number strEmail = ""; //holds email address strStreet1=""; //holds first part of addres strStreet2=""; //holds seoncd part of address strCity =""; //holds city address strState =""; //holds state strOther = ""; //holds misc information strZipCode =""; //stores the zipcode } //----------------------------classPerson::setName()--------------------------- //Description: sets the name of the person void classPerson::setName(string name ) { strName = name; //return 0; } //----------------------------classPerson::setPhone()--------------------------- //Description: sets the contents of the person phone information void classPerson::setEmail(string email ) { strEmail = email; //return 0; } //----------------------------classPerson::setZipCode()------------------------- //Description: sets the contents of the person zipcode information void classPerson::setZipCode(string zip ) { strZipCode = zip; //return 0; } //----------------------------classPerson::setStreet1()------------------------- //Description: sets the contents of the person street1 information void classPerson::setStreet1(string street ) { strStreet1=street; //return 0; } //----------------------------classPerson::setStreet2()------------------------- //Description: sets the contents of the person street2 information void classPerson::setStreet2(string street ) { strStreet2=street; //return 0; } //----------------------------classPerson::setOther()--------------------------- //Description: sets the contents of the person other information // void classPerson::setOther(string note ) { strOther=note; //return 0; } //----------------------------classPerson::setCity()--------------------------- //Description: sets the contents of the person city information void classPerson::setCity(string cty) { strCity=cty; } //----------------------------classPerson::setState()--------------------------- //Description: sets the contents of the person state information void classPerson::setState(string state) { strState=state; } //----------------------------classPerson::setPhone()--------------------------- //Description: sets the contents of the person phone information // void classPerson::setPhone(string number) { strPhone = number; } //----------------------------classPerson::Exists()--------------------------- //Description: Determines if the person being passed in exists //returns true if person matches the email bool classPerson::Exists(string email) { if(strEmail==email) return true; else return false; } //-----------------------operator overload for cout ---------------- //Operator Overload for cout << classPerson //which will print out the required fields //function returns ostream //returns a stream of data if the field contains data ostringstream &operator<<(ostringstream &output, classPerson &person) { if(person.strName !="") output << "Name:\n" << " " <" << strEmail<< "<---" << endl; if(strOwner == "") return true; //owner's email does contain data else return false; //owner's email contains data } //---------------------------------classMedia::setItem()----------------------------------- //Description: sets the item of the media available void classMediaType::setItem(string title) { strItem=title; //set title of media } //---------------------------------classMedia::setArtist()--------------------------------- //Description: sets the artist of the item available for the media void classMediaType::setArtist(string artist){ strArtist=artist; //set artist of media } //---------------------------------classMedia::setOwner()--------------------------------- //Description: sets the owner of the item available for the media void classMediaType::setOwner(string owner){ strOwner=owner; //sets owner of media } //---------------------------------classMedia::setType()--------------------------------- //Description: sets the type of the item available for the media void classMediaType::setType(string type) { strType=type; } //---------------------------------classMedia::setPrice()--------------------------------- //Description: sets the price of the item available for the medai void classMediaType::setPrice(string price){ strPrice=price; //sets price of media } //---------------------------------classMedia::setOptions()--------------------------------- //Description: sets the message contents of the options available for the medai void classMediaType::setOptions(string option) { strOptions=option; } //---------------------------------classMedia::setShowOwner()------------------------------- //Description: turns on/off the owner's information email void classMediaType::setShowOwner(bool choice) { showOwner=choice; } //---------------------------------classMedia::getOwner()--------------------------------- //Description: sets the owner of the item avail string classMediaType::getOwner() { return strOwner; } //---------------------------------classMedia::getArtist()--------------------------------- //Description: sets the artist of the item available for the media string classMediaType::getArtist() { return strArtist; } //---------------------------------classMedia::setItem()----------------------------------- //Description: sets the item of the media available string classMediaType::getItem() { return strItem; } //---------------------------------classMedia::setPrice()--------------------------------- //Description: sets the price of the item available for the medai string classMediaType::getPrice() { return strPrice; } //---------------------------------classMedia::setType()--------------------------------- //Description: sets the type of the item available for the media string classMediaType::getType() { return strType; } //---------------------------------classMedia::setOptions()--------------------------------- //Description: sets the message contents of the options available for the medai string classMediaType::getOptions() { return strOptions; } //-----------------------------classMedia::validOwner()-------------------------------------- //Description: Returns true if the email matches the requested email bool classMediaType::validOwner(string email) { if(strOwner ==email) return true; else return false; } //---------------------------------classMedia::Print()--------------------------------------- //Description: prints the contens of the media item class //FLAG-need to check \n problem void classMediaType::print(ofstream &outFile) { #if 0 cout << endl; if(strItem !="") cout << "Item: \n " << strItem << endl; if(strType !="") cout << "Media Type: \n " << strType << endl; if(strArtist !="") cout << "by: \n " << strArtist << endl; if((strOwner != "") && (showOwner)) cout << "Owner: \n " << strOwner << endl; if(strPrice != "") cout << "Price: \n "<< strPrice << endl; if(strOptions != "") cout << "Availability: \n " << strOptions << endl; #endif //Wayne-added 5:29 if(strItem !="") outFile << "Item: \n " << strItem << endl; if(strType !="") outFile << "Media Type: \n " << strType << endl; if(strArtist !="") outFile << "by: \n " << strArtist << endl; if((strOwner != "") && (showOwner)) outFile << "Owner: \n " << strOwner << endl; if(strPrice != "") outFile << "Price: \n "<< strPrice << endl; if(strOptions != "") outFile << "Availability: \n " << strOptions << endl; outFile << endl; //Wayne Look here } //---------------------------------classMedia::clearAll()-------------------------------------- //Description: clears the contens of the media item class // void classMediaType::clearAll() { strItem=""; //holds title strArtist = ""; //holds the artists name strOwner = ""; //holds the owner of media-email strType = ""; //holds the valid media strPrice = ""; //price of media strOptions = ""; //sets media option to blank showOwner=true; } //-----------------------operator overload for string stream-------------------- //friend of classMediaType //Operator Overload for cout << classPerson //which will print out the required fields //function returns ostream //returns a stream of data if the field contains data ostringstream &operator<<(ostringstream &output, classMediaType &media) { if(media.strItem !="") output << "Item: \n " << media.strItem << endl; if(media.strType !="") output << "Media Type: \n " << media.strType << endl; if(media.strArtist !="") output << "by: \n " << media.strArtist << endl; if((media.strOwner != "") && media.showOwner) output << "Owner: \n " << media.strOwner << endl; if(media.strPrice != "") output << "Price: \n "<< media.strPrice << endl; if(media.strOptions != "") output << "Availability: \n " << media.strOptions << endl; return output; } //---------------------------------classManger-------------------------------------------------- //---------------------------------------------------------------------------------------------- class classManager{ private: //indexes the person with the media //convient for locating person[1] and media[] element classPerson Students[100]; //declare an array of Students of type classPerson classMediaType Media[100]; //declare an array of Media of type media bool organizeStudents(); //organizes the students-last entrance=last array bool organizeMedia(); //organizes the media-last entrance=last array bool removeMedia(); //removes all the media associated with student int itemsIndex; //number of elements in items int studentIndex; //number students in student array int loadStudents(); //returns student count AFTER initializing all data int loadItems(); //returns items count AFTER initialize all data public: classManager(); //constructor int getItemsIndex(); //returns index count of Array of Items int getStudentIndex(); //returns index count of Array of Students bool addStudent(ifstream &inFile,string data); //adds student bool removeStudent(string email); //removes the student from list bool insertStudent(istream &input); //stream inserts of the student informaiton bool insertMedia(istream &input); //stream insert of the media //void printStudents(ofstream &outFile); //print student in entrance ordr void printItems(string,ofstream &outFile); //prints media in entrance ordr bool printStudentAndItems(string email,ofstream &outFile ); bool deleteStudent(string email); int getStudentFreeNodeIndex(); //get the students first free node index int getItemFreeNodeIndex(); //get the item array first free index number bool addItem(ifstream &inFile,string data); //adds an item into the void printStudentsOnly(ofstream &outFile); void printMyStudentsOnly(ofstream &outFile) ; void printMyItemsOnly(ofstream &outFile) ; bool deleteMyStudent(string email); }; //-----------------------------------classManager::Constructor--------------------------------------- //Init students and items and load the data classManager::classManager(){ studentIndex=loadStudents(); //loads students array and returns index count itemsIndex=loadItems(); //loads items array and returns index count } //-----------------------------------classManager::Destructor---------------------------------------- classManager::~classManager() { } //-----------------------------------classManager::getFreeNodeIndex----------------------------------- //Description: This will return the index item number if it finds a free node in the array //Precondition: Array is filled with students, perhaps missing some studnets in various array loc. //Postcondition: Returns INT number of FIRST available free spot in memory int classManager::getStudentFreeNodeIndex() { char bob; for(int i=0;i> bob; } //----------------------------------classManager::AddStudent---------------------------------------- //Description: This will add a student retrieved from the instream to an empty node in the // array. // //Precondition: Student Array has been defined //Calls Made : getFreeNodeIndex(); //Postcondtion: Inserts a student in the first available free node // //Last Modified: 7-16-03 MWS // bool classManager::addStudent(ifstream &inStudents,string data) { string newLine=""; string string1,string2,string3,string4,string5,string6,string7,string8,string9; char bob; bool itemsComplete=false; int index=0; //go through the command file and extract the items that need to ge while(!itemsComplete) { getline(inStudents,newLine); //item if(newLine =="@Name:")getline(inStudents,string1); else if(newLine =="@Phone Number:")getline(inStudents,string2); else if(newLine =="@E-mail:")getline(inStudents,string3); else if(newLine =="@Address Line 1:") getline(inStudents,string4); else if(newLine =="@Address Line 2:") getline(inStudents,string5); else if(newLine =="@City:") getline(inStudents,string6); else if(newLine =="@State:") getline(inStudents,string7); else if(newLine =="@Zipcode:") getline(inStudents,string8); else if(newLine =="@Other:") getline(inStudents,string9); else if(newLine=="@@") itemsComplete=true; } if(itemsComplete) { #if 0 cout << "Found Name : " << string1 << endl; cout << "Found Phone : " << string2 << endl; cout << "Found Email : " << string3 << endl; cout << "Found Addr 1: " << string4 << endl; cout << "Found Addr 2: " << string5 << endl; cout << "Found City : " << string6 << endl; cout << "Found State : " << string7 << endl; cout << "Found Zip : " << string8 << endl; cout << "Found Other : " << string9 << endl; #endif #if 1 index=getStudentFreeNodeIndex(); //gets the first free node available if(index < 0) { //verify index is within range cout << "OUT OF ARRAY BOUNDS or NO FREE NODE AVAILABLE! " << endl; return false; } //cout << "FreeNodeFound at: " << index << endl; Students[index].setName(string1); Students[index].setPhone(string2); Students[index].setEmail(string3); Students[index].setStreet1(string4); Students[index].setStreet2(string5); Students[index].setCity(string6); Students[index].setState(string7); Students[index].setZipCode(string8); Students[index].setOther(string9); #endif itemsComplete=false; } //cin >> bob; return true; } //-----------------------------------loadStudents--------------------------------------- //Description: Loads the students from the students.data file and stores them // in the Student array keeping track of the number of students going in // //Precondition: Should have an array with enough free space available //Postcondtion: The student array will be filled with the contens of person int classManager::loadStudents() { ifstream inStudents; string newLine=""; string string1,string2,string3,string4,string5,string6,string7,string8,string9; char bob; bool itemsComplete=false; int index=0; inStudents.open("students.data"); if(!inStudents) { //cout << "problems reading students.dat" << endl; //cin >> bob; exit(1); } //while the file is open and data is left in there, keeping //retrieving the students while(inStudents) { getline(inStudents,newLine); //item if(newLine =="@Name:")getline(inStudents,string1); else if(newLine =="@Phone Number:")getline(inStudents,string2); else if(newLine =="@E-mail:")getline(inStudents,string3); else if(newLine =="@Address Line 1:") getline(inStudents,string4); else if(newLine =="@Address Line 2:") getline(inStudents,string5); else if(newLine =="@City:") getline(inStudents,string6); else if(newLine =="@State:") getline(inStudents,string7); else if(newLine =="@Zipcode:") getline(inStudents,string8); else if(newLine =="@Other:") getline(inStudents,string9); else if(newLine=="@@") itemsComplete=true; if(itemsComplete) { #if 0 cout << "Found Name : " << string1 << endl; cout << "Found Phone : " << string2 << endl; cout << "Found Email : " << string3 << endl; cout << "Found Addr 1: " << string4 << endl; cout << "Found Addr 2: " << string5 << endl; cout << "Found City : " << string6 << endl; cout << "Found State : " << string7 << endl; cout << "Found Zip : " << string8 << endl; cout << "Found Other : " << string9 << endl; #endif Students[index].setName(string1); Students[index].setPhone(string2); Students[index].setEmail(string3); Students[index].setStreet1(string4); Students[index].setStreet2(string5); Students[index].setCity(string6); Students[index].setState(string7); Students[index].setZipCode(string8); Students[index].setOther(string9); itemsComplete=false; string1=""; string2=""; string3=""; string4=""; string5=""; string6=""; string7=""; string8=""; string9=""; index++; //cin >> bob; //debugging purpose } } //cout << "--------------Finished Loading Students---------" << endl; //cin >> bob; //debugging purpose // assert(index < 101); return index; } //-----------------------------------loadItems--------------------------------------- ///Description: Loads the items from the items.data file and stores them // in the Student array keeping track of the number of items going in // //Precondition: Should have an array with enough free space available //Loads the file items.data into array // int classManager::loadItems() { ifstream inItems; string newLine=""; string string1,string2,string3,string4,string5,string6; char bob; bool itemsComplete=false; int index=0; inItems.open("items.data"); //our original input file if(!inItems) { cout << "problems reading items.dat" << endl; //flag if any problems with cout << "error opening file" << endl; //opening the file exit(1); } while(inItems) { getline(inItems,newLine); //item if(newLine =="@Item:")getline(inItems,string1); else if(newLine =="@Media Type:")getline(inItems,string2); else if(newLine =="@by:") getline(inItems,string3); else if(newLine =="@Owner:") getline(inItems,string4); else if(newLine =="@Price:") getline(inItems,string5); else if(newLine =="@Availability:") getline(inItems,string6); else if(newLine=="@@") itemsComplete=true; //go through the items filed and processes out all the items in the file //one complete item is from @ to @@ if(itemsComplete) { #if 0 cout << "Found Item : " << string1 << endl; cout << "Found Media: " << string2 << endl; cout << "Found by : " << string3 << endl; cout << "Found Owner: " << string4 << endl; cout << "Found Price: " << string5 << endl; cout << "Found Avail: " << string6 << endl; cout << "----------------Finishing Loading Items-----------------" << endl; #endif Media[index].setItem(string1); //sets the title of Media Media[index].setType(string2); //sets the media type Media[index].setArtist(string3); //sets the artist title Media[index].setOwner(string4); //sets the owner of the media Media[index].setPrice(string5); //sets the price of media Media[index].setOptions(string6); //sets the media options Media[index].setShowOwner(true); //sets the show owner field string1=""; //clear all strings string2=""; //load new item data string3=""; string4=""; string5=""; string6=""; index++; //increment item index //cin >> bob; //debugging purpose itemsComplete=false; //gets out of the loop } } assert(index < 101); return index; } //--------------------------------------printStudentsOnly------------------------------------------ //Description: goes through the students and prints them out in order of the array //Precondition: Media Item Array has been defined //Postcondtion: None // //Last Modified: 7-16-03 MWS void classManager::printStudentsOnly(ofstream &outFile) { int index=studentIndex; //get the number of items for(int i=0;i 2) { cout << "Too Many Parameter" << endl; cout << "Please use filename.exe [1 or 2 or 3]" << endl; exit(1); } if(argc ==2) aoptions=atoi(argv[1]); //convert string to integer value if(aoptions==1) { ofstream testfile; //open a test file testfile.open("test.txt"); mediaManager.printMyStudentsOnly(testfile); //print to students to tesfile testfile.close(); exit(1); } if(aoptions==2) { ofstream testfile; //open a test file testfile.open("test.txt"); mediaManager.printMyItemsOnly(testfile);//print items to testfile testfile.close(); exit(1); } if(aoptions==3) { ofstream testfile; //open a test file testfile.open("test.txt"); //and prints students email an string tempEmail; //and items cout << "enter students email " << endl; getline(cin,tempEmail); mediaManager.printStudentAndItems(tempEmail,testfile); testfile.close(); exit(1); } //read the first line from commands.dat into string inputLine getline(inFile,inputLine); //declare tempString of type istringstream to write to memory //i.e. copy string inputLine to tempString in memory istringstream tempString(inputLine); tempString >>command >> data; //get the data part of the command //cout << command << " "<< data << endl; //print out the commands inputCmd=getCommands(command,data); //get the commands if (inputCmd == quit) quitProgram=true; //check for quit while(!quitProgram) { //keep reading all commands until quit command found switch (inputCmd) { //switch between input commands case printOnlyStudents: //working-but needs to print to a stream outFile << command << " " << data << endl; mediaManager.printMyStudentsOnly(outFile); break; case addStudent: outFile <>command >> data; //cout << command << " "<< data << endl; inputCmd=getCommands(command,data); //cout << "inputCmd ===" << inputCmd << endl; //cin >> bob; if (inputCmd == quit) { outFile << command << endl; quitProgram=true; } } //while return 0; } //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 // Wayne Swanson