// mediaClass implementation #include "mediaClass.h" // MUTATORS: bool mediaClass::readAll( istream& inStream ) { // initialize fields to blank: authorData = availData = ownerData = titleData = typeData = ""; priceData = -1; string label; // stores the field flag getline( inStream, label ); // initial field flag read while( inStream && label != "@@" ) // reads until end of item data flag or eof { if( label[0] == '@' && inStream.peek() != '@' ) // checks that the field is not empty if( label == "@Item:" ) title( inStream ); else if( label == "@Media Type:" ) type( inStream ); else if( label == "@by:" ) author( inStream ); else if( label == "@Owner:" ) owner( inStream ); else if( label == "@Price:" ) price( inStream ); else if( label == "@Availability:" ) avail( inStream ); else cout << "error reading item\n"; getline( inStream, label ); // read next label } if( ownerData == "" ) // make sure email address has been read (every student must have an email address, all other fields are optional) { flagData = false; authorData = availData = ownerData = titleData = typeData = "UNINITIALIZED\n"; priceData = -1; return false; } flagData = true; // flag this as a valid student return true; } bool mediaClass::type( istream& inStream ) { getline( inStream, typeData ); return inStream && typeData.length() > 0; } bool mediaClass::avail( istream& inStream ) { getline( inStream, availData ); return inStream && availData.length() > 0; } bool mediaClass::title( istream& inStream ) { getline( inStream, titleData ); return inStream && titleData.length() > 0; } bool mediaClass::author( istream& inStream ) { getline( inStream, authorData ); return inStream && authorData.length() > 0; } bool mediaClass::owner( istream& inStream ) { assert( ownerData == "" ); // make sure there are not two email addresses for one student getline( inStream, ownerData ); return inStream && ownerData.length() > 0; } bool mediaClass::price( istream& inStream ) { char tempChar; // next char being read int tempInt; // next in being read inStream >> tempChar; // read first char if( tempChar == '$' ) // first char must be $ if( inStream >> priceData ) // read dollars { inStream >> tempChar; if( tempChar == '.' ) // next char must be . if( inStream >> tempInt ) // read cents priceData = priceData * 100 + tempInt; else { priceData = -1; return false; } else return false; } else return false; else return false; return true; } // ACCESSORS: bool mediaClass::operator ==( const mediaClass& ITEM ) const { return titleData == ITEM.titleData && authorData == ITEM.authorData; } bool mediaClass::ownedBy( const string MATCH ) const { return MATCH == ownerData; } string mediaClass::print( const bool PRINT_OWNER ) const { ostringstream tempOStream; if( PRINT_OWNER ) // prints item info including owner field { if( titleData != "" ) tempOStream << title(); if( typeData != "" ) tempOStream << type(); if( authorData != "" ) tempOStream << author(); if( ownerData != "" ) tempOStream << owner(); if( priceData != -1 ) tempOStream << price(); if( availData != "" ) tempOStream << avail(); } else // prints item info without owner field { if( titleData != "" ) tempOStream << title(); if( typeData != "" ) tempOStream << type(); if( authorData != "" ) tempOStream << author(); if( priceData != -1 ) tempOStream << price(); if( availData != "" ) tempOStream << avail(); } return tempOStream.str() + '\n'; } string mediaClass::price() const { ostringstream tempOStream; tempOStream << '$' << priceData / 100; // print dollars tempOStream << setfill('0') << '.' << setw(2) << priceData % 100; // print cents return "Price:\n\t" + tempOStream.str() + '\n'; // return formatted price } // CONSTRUCTORS: mediaClass::mediaClass() { flagData = false; // flag as unitialized // set values to blank flags: authorData = availData = ownerData = titleData = typeData = ""; priceData = -1; } mediaClass::mediaClass( istream& inStream ) { readAll( inStream ); } // EXTERNAL OPERATORS: bool operator >>( istream& inFile, mediaClass& item ) { istringstream tempIStream( copyCommand( inFile ) ); // gets a copy of the first item's data from the stream to be read return( item.readAll( tempIStream ) ); // returns the value of readAll() } bool operator <<( ostream& outFile, const mediaClass& item ) { outFile << item.print( true ); return true; }