// implementation of commandClass #include "commandClass.h" // UTILITY METHODS: bool commandClass::found( const string MATCH ) const { for( int i = 0; i < numStudents; i++ ) // cycle thru students if( studentArray[i].flag() && studentArray[i] == MATCH ) // check for match return true; return false; } bool commandClass::found( const mediaClass ITEM ) const { for( int i = 0; i < numItems; i++ ) // cycle thru items if( itemArray[i].flag() && itemArray[i] == ITEM && ITEM.ownedBy( itemArray[i].emailEcho() ) ) // check for match return true; return false; } // MUTATORS: bool commandClass::remove( const string EMAIL ) { if( !found( EMAIL ) ) // check that the student is in the list return false; for( int i = 0; i < numStudents; i++ ) // cycle thru students if( studentArray[i] == EMAIL ) // check for match studentArray[i].flag( false ); // flag the student as removed for( int i = 0; i < numItems; i++ ) // cycle thru items if( itemArray[i].ownedBy( EMAIL ) ) // check for match itemArray[i].flag( false ); // flag item removed return true; } bool commandClass::add( const mediaClass& ITEM ) { if( !found( ITEM.emailEcho() ) ) // checks that the student who owns the item is in the list return false; for( int i = 0; i < numItems; i ++ ) // cycle thru items if( !itemArray[i].flag() ) // find next first empty place in array { itemArray[i] = ITEM; // put item in that place return true; } itemArray[numItems] = ITEM; // put item at end of array numItems++; return true; } bool commandClass::add( const studentClass& STUDENT ) { if( found( STUDENT.emailEcho() ) ) // check if student is already defined to prevent duplicates return false; for( int i = 0; i < numStudents; i++ ) // cycle thru students if( !studentArray[i].flag() ) // find first empty spot in array { studentArray[i] = STUDENT; // put STUDENT in that spot return true; } studentArray[numStudents] = STUDENT; // put student at end of list numStudents++; return true; } // ACCESSORS: string commandClass::printStudents() const { ostringstream tempOStream; // used to store student info till return time for( int i = 0; i < numStudents; i++ ) // cycle thru students if( studentArray[i].flag() ) tempOStream << studentArray[i]; // put student info in list return tempOStream.str(); // return list } string commandClass::printItems() const { ostringstream tempOStream; // item list for( int i = 0; i < numItems; i++ ) // cycle thru items if( itemArray[i].flag() ) tempOStream << itemArray[i].print( true ); // add item data to list return tempOStream.str(); // return list } string commandClass::printSpecific( const string EMAIL ) const { if( !found( EMAIL ) ) // check that the student is in the array return EMAIL + " not found.\n"; ostringstream tempOStream; // list of student info and his media for( int i = 0; i < numStudents; i++ ) // cycle thru students if( studentArray[i].flag() && studentArray[i] == EMAIL ) tempOStream << studentArray[i]; // print the student's info for( int i = 0; i < numItems; i++ ) // cycle thru items if( itemArray[i].flag() && itemArray[i].ownedBy( EMAIL ) ) tempOStream << itemArray[i].print( false ); // print the items that belong to EMAIL return tempOStream.str(); } // CONSTRUCTORS: commandClass::commandClass() { numStudents = numItems = 0; } commandClass::commandClass( const char* STUDENT_FILE, const char* ITEM_FILE ) { ifstream studentFile( STUDENT_FILE ); // student data fstream ifstream itemFile( ITEM_FILE ); // item data fstream numStudents = numItems = 0; while( studentFile ) // read in students { studentClass tempStudent; studentFile >> tempStudent; add( tempStudent ); // add the student that was just read } while( itemFile ) // read in items (must be read after students) { mediaClass tempItem; itemFile >> tempItem; add( tempItem ); // add the item } // close files: studentFile.close(); itemFile.close(); }