/* Lawrenceville Press CollegeClass type IMPLEMENTATION */ /* This library file has been modified to operate with CodeWarrior 3.3 */ /* March 1999 abort changed to eixt */ //----------------------------------------------------- CollegeClass::CollegeClass() /* Pre: Database file is in this format: Adelphi University Garden City NY Private 4153 <----Enrollment 12250 <----Tuition 6000 <----Room and Board Post: Database has been opened, and Current data holds the first item in the file. */ { DBFile.open("colleges.txt"); /* NOTICE: Path may need to be changed depending on environment */ if (!DBFile.good()) { cout << "Error opening Colleges.txt file" << endl; exit(1); } else { IsValid = true; GetNext(); } } //----------------------------------------------------- CollegeClass::~CollegeClass() /* Pre: CollegeClass object exists Post: object destroyed */ { DBFile.close(); } //----------------------------------------------- bool CollegeClass::GetNext() /* Next item becomes Current item Post: Values of next item loaded into Current values, and true returned. If next item not available, false returned and IsValid flag is set to false. */ { getline(DBFile, CurrentName); if (CurrentName=="END") IsValid = false; else { getline(DBFile, CurrentTown); getline(DBFile, CurrentState); getline(DBFile, CurrentPubOrPri); DBFile >> CurrentEnrollment; DBFile.ignore(); DBFile >> CurrentTuition; DBFile.ignore(); DBFile >> CurrentRoomAndBoard; DBFile.ignore(); } return (IsValid); }; //------------------------------------------------------ bool CollegeClass::CurrentIsValid() /* Post: true returned iff values are from the file (i.e. have not read past the end of the file) */ { return(IsValid); }; //----------------------------------------------- void CollegeClass::Reset() /* Moves the current pointer to the start of the file. Post: Current values read for first item in database */ { DBFile.close(); DBFile.open("colleges.txt"); /* NOTICE: Path may need to be changed depending on environment */ if (DBFile.fail()) { cout << "Error opening Colleges.txt file" << endl; exit(1); } else { IsValid = true; GetNext(); } } //------------------------------------------------------ /* The following access functions returns values of the current item. If there is no current item (i.e. the end of the file has been reached and therefore CurrentIsValid() returns false) then they may not be called. EXCEPTION: GetName() will return "END". */ //------------------------------------------------------ lvpstring CollegeClass::GetName() const { return (CurrentName); } //------------------------------------------------------ lvpstring CollegeClass::GetTown() const { return (CurrentTown); } //------------------------------------------------------ lvpstring CollegeClass::GetState() const { return (CurrentState); } //------------------------------------------------------ lvpstring CollegeClass::GetPubOrPri() const { return (CurrentPubOrPri); } //------------------------------------------------------ long CollegeClass::GetEnrollment() const { return (CurrentEnrollment); } //------------------------------------------------------ long CollegeClass::GetTuition() const { return (CurrentTuition); } //------------------------------------------------------ long CollegeClass::GetRoomAndBoard() const { return (CurrentRoomAndBoard); } //------------------------------------------------------ ostream & operator << (ostream & os, const CollegeClass & C) /* postcondition: College data inserted in os one per line; os returned */ { os << C.GetName() << " " << C.GetTown() << " " << C.GetState() << " " << C.GetPubOrPri() << " " << C.GetEnrollment() << " " << C.GetTuition() << " " << C.GetRoomAndBoard() << endl; return (os); }