// bamrecord.cpp Implementation file for the BamRecord class. /******************************************************************************* // MODULE NAME: BamRecord // INTERFACE FILE: bamrecord.h // IMPLEMENTATION FILE: bamrecord.cpp // // PURPOSE: To provide a BamRecord type useful in implementing the BAM // program, which allows the user to create a record, update a field in the // record, print the current value of a field in the record, test the validity // of a field in the record, compare one record to another to determine sorting // order, and produce strings with the record information in them. // // FUNCTIONS: Name Purpose // BamRecord Default Constructor // Matches Does this record contains a desired string? // FieldContains Print out contents of specified field // IndexIs Indicate index of current record // GreaterThan Determine if this record should follow another // Equals Determine if this record has same index value // as another record // Update Store a new value into specified field // NewIndex Change index of record to specified value // Replace Replace all values of record with the contents // of another record // Reset Change all values in record to default values // Reset Change value of specified field to default value // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ #include "bamrecord.h" using namespace std; /******************************************************************************* // FUNCTION NAME: BamRecord // // DESCRIPTION OF FUNCTION: Default Constructor for BamRecord type // // DESCRIPTION OF ALGORITHM: Calls BALString::SetSize for each element of // the BamRecord // // CALLED BY: client // CALLS: BALString::SetSize // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: BamRecord has been created with default values in every // field. Widths of fields have been set to their proper values // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ BamRecord::BamRecord() { Alley.SetSize(BR_ALLEY_WIDTH); Event.SetSize(BR_EVENT_WIDTH); Date.SetSize(BR_DATE_WIDTH); Ball.SetSize(BR_BALL_WIDTH); Lift.SetSize(BR_LIFT_WIDTH); Cond.SetSize(BR_COND_WIDTH); Marks[0].SetSize(BR_MARK_WIDTH); Marks[1].SetSize(BR_MARK_WIDTH); Marks[2].SetSize(BR_MARK_WIDTH); Marks[3].SetSize(BR_MARK_WIDTH); Marks[4].SetSize(BR_MARK_WIDTH); Marks[5].SetSize(BR_MARK_WIDTH); Marks[6].SetSize(BR_MARK_WIDTH); Marks[7].SetSize(BR_MARK_WIDTH); Marks[8].SetSize(BR_MARK_WIDTH); Marks[9].SetSize(BR_MARK_WIDTH); Scores[0].SetSize(BR_SCORE_WIDTH); Scores[1].SetSize(BR_SCORE_WIDTH); Scores[2].SetSize(BR_SCORE_WIDTH); Scores[3].SetSize(BR_SCORE_WIDTH); Scores[4].SetSize(BR_SCORE_WIDTH); Scores[5].SetSize(BR_SCORE_WIDTH); Scores[6].SetSize(BR_SCORE_WIDTH); Scores[7].SetSize(BR_SCORE_WIDTH); Scores[8].SetSize(BR_SCORE_WIDTH); Scores[9].SetSize(BR_SCORE_WIDTH); } /******************************************************************************* // FUNCTION NAME: Matches // // DESCRIPTION OF FUNCTION: Determines if the value in a specified field // matches a string passed in as a parameter // // DESCRIPTION OF ALGORITHM: Determines the length of matchString, then // depending on the value of matchField, attempts to match the first // matchString.length() characters of that field with matchString. If they are // exactly equal, returns true. Returns false otherwise. // // CALLED BY: client // CALLS: BALString::Print,string::substr,string::length // // PARAMETERS: / in / matchString - string that contains the desired sequence // of characters // / in / matchField - BamRecordFieldType that indicates which // field of the record is to be tested. // // PRECONDITIONS: none // // POSTCONDITIONS: If the first matchString.length() characters of the // appropriate field are exactly equal to matchString, returns true. Returns // false otherwise // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ bool BamRecord::Matches(string matchString,BamRecordFieldType matchField) { int charsToMatch=matchString.length(); string recordString; bool foundAMatch=false; switch(matchField) { case BR_ALLEY: recordString = this->Alley.Print().substr(0,charsToMatch); foundAMatch = (recordString == matchString); break; case BR_EVENT: recordString = this->Event.Print().substr(0,charsToMatch); foundAMatch = (recordString == matchString); break; case BR_DATE: recordString = this->Date.Print().substr(0,charsToMatch); foundAMatch = (recordString == matchString); break; } return foundAMatch; } /******************************************************************************* // FUNCTION NAME: GreaterThan // // DESCRIPTION OF FUNCTION: Tests whether this record should follow another in // an ascending order sequence // // DESCRIPTION OF ALGORITHM: Directly compares this->RecordIndex with // another.RecordIndex using the > operator // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / another - BamRecord that this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if the RecordIndex of this record is greater // than the RecordIndex of the other record. // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ bool BamRecord::GreaterThan(BamRecord another) { return (this->RecordIndex > another.RecordIndex); } /******************************************************************************* // FUNCTION NAME: Equals // // DESCRIPTION OF FUNCTION: Tests whether this record contains the same index // value as another record // // DESCRIPTION OF ALGORITHM: Directly compares this->RecordIndex with // another.RecordIndex using the == operator // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / another - BamRecord that this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if the RecordIndex of this record is exactly // equal to the RecordIndex of the other record. // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ bool BamRecord::Equals(BamRecord another) { return (this->RecordIndex == another.RecordIndex); } /******************************************************************************* // FUNCTION NAME: FieldContains // // DESCRIPTION OF FUNCTION: Returns a string containing the contents of the // desired field. // // DESCRIPTION OF ALGORITHM: Depending on the value of currentField, calls the // BALString::Print member function on the appropriate field. // // CALLED BY: client // CALLS: BALString::Print // // PARAMETERS: / in / currentField - BamRecordFieldType that indicates which // field's value is to be returned // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string of the appropriate length containing the // current contents of the specified field // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ string BamRecord::FieldContains(BamRecordFieldType currentField) { string result; switch(currentField) { case BR_ALLEY: result = Alley.Print(); break; case BR_EVENT: result = Event.Print(); break; case BR_DATE: result = Date.Print(); break; case BR_BALL: result = Ball.Print(); break; case BR_LIFT: result = Lift.Print(); break; case BR_COND: result = Cond.Print(); break; case BR_MARK1: result = Marks[0].Print(); break; case BR_MARK2: result = Marks[1].Print(); break; case BR_MARK3: result = Marks[2].Print(); break; case BR_MARK4: result = Marks[3].Print(); break; case BR_MARK5: result = Marks[4].Print(); break; case BR_MARK6: result = Marks[5].Print(); break; case BR_MARK7: result = Marks[6].Print(); break; case BR_MARK8: result = Marks[7].Print(); break; case BR_MARK9: result = Marks[8].Print(); break; case BR_MARK10: result = Marks[9].Print(); break; case BR_SCORE1: result = Scores[0].Print(); break; case BR_SCORE2: result = Scores[1].Print(); break; case BR_SCORE3: result = Scores[2].Print(); break; case BR_SCORE4: result = Scores[3].Print(); break; case BR_SCORE5: result = Scores[4].Print(); break; case BR_SCORE6: result = Scores[5].Print(); break; case BR_SCORE7: result = Scores[6].Print(); break; case BR_SCORE8: result = Scores[7].Print(); break; case BR_SCORE9: result = Scores[8].Print(); break; case BR_SCORE10: result = Scores[9].Print(); break; } return result; } /******************************************************************************* // FUNCTION NAME: IndexIs // // DESCRIPTION OF FUNCTION: Determines the index value of the record // // DESCRIPTION OF ALGORITHM: Returns this->RecordIndex // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: none // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ int BamRecord::IndexIs() { return RecordIndex; } /******************************************************************************* // FUNCTION NAME: Update // // DESCRIPTION OF FUNCTION: Change the value of a field specified by // fieldToChange to a different value contained in dataString // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToChange, calls // the BALString::Store member function on the appopriate field // // CALLED BY: client // CALLS: BALString::Store // // PARAMETERS: / in / dataString - string that contains the value to be stored // into the desired field // / in / fieldToChange - BamRecordFieldType that indicates which // field of the record is to be changed to // the new value // // PRECONDITIONS: none // // POSTCONDITIONS: The appropriate field has been changed to the value supplied // in dataString // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ void BamRecord::Update(string dataString,BamRecordFieldType fieldToChange) { switch (fieldToChange) { case BR_ALLEY: Alley.Store(dataString); break; case BR_EVENT: Event.Store(dataString); break; case BR_DATE: Date.Store(dataString); break; case BR_BALL: Ball.Store(dataString); break; case BR_LIFT: Lift.Store(dataString); break; case BR_COND: Cond.Store(dataString); break; case BR_MARK1: Marks[0].Store(dataString); break; case BR_MARK2: Marks[1].Store(dataString); break; case BR_MARK3: Marks[2].Store(dataString); break; case BR_MARK4: Marks[3].Store(dataString); break; case BR_MARK5: Marks[4].Store(dataString); break; case BR_MARK6: Marks[5].Store(dataString); break; case BR_MARK7: Marks[6].Store(dataString); break; case BR_MARK8: Marks[7].Store(dataString); break; case BR_MARK9: Marks[8].Store(dataString); break; case BR_MARK10: Marks[9].Store(dataString); break; case BR_SCORE1: Scores[0].Store(dataString); break; case BR_SCORE2: Scores[1].Store(dataString); break; case BR_SCORE3: Scores[2].Store(dataString); break; case BR_SCORE4: Scores[3].Store(dataString); break; case BR_SCORE5: Scores[4].Store(dataString); break; case BR_SCORE6: Scores[5].Store(dataString); break; case BR_SCORE7: Scores[6].Store(dataString); break; case BR_SCORE8: Scores[7].Store(dataString); break; case BR_SCORE9: Scores[8].Store(dataString); break; case BR_SCORE10: Scores[9].Store(dataString); break; } return; } /******************************************************************************* // FUNCTION NAME: NewIndex // // DESCRIPTION OF FUNCTION: Change the index of the record to a new value // // DESCRIPTION OF ALGORITHM: Assigns newIndexValue to RecordIndex // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newIndexValue - int that RecordIndex is to be changed to // // PRECONDITIONS: none // // POSTCONDITIONS: RecordIndex has been changed to newIndexValue // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ void BamRecord::NewIndex(int newIndexValue) { RecordIndex = newIndexValue; return; } /******************************************************************************* // FUNCTION NAME: Replace // // DESCRIPTION OF FUNCTION: Replaces the contents of all the fields of the // record with the contents of another record // // DESCRIPTION OF ALGORITHM: For each field in the record, calls // BamRecord::Update to change the value of that record's field to the value // stored in the other record. // // CALLED BY: client // CALLS: BamRecord::Update,BamRecord::FieldContains // // PARAMETERS: / in / otherRecord - BamRecord whose contents are to replace the // contents of this->record // // PRECONDITIONS: none // // POSTCONDITIONS: All of the values of all of the fields in otherRecord have // been stored into the corresponding fields of this->record // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ void BamRecord::Replace(BamRecord otherRecord) { BamRecordFieldType currFld; this->RecordIndex = otherRecord.RecordIndex; for (currFld=BR_ALLEY;currFld<=BR_SCORE10;currFld=BamRecordFieldType(currFld+1)) { this->Update(otherRecord.FieldContains(currFld),currFld); } return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Change the values of all the fields of the record // to their default values // // DESCRIPTION OF ALGORITHM: Calls BALString::ResetValue() for every field in // the record // // CALLED BY: client // CALLS: BALString::ResetValue() // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: All of the fields of the record now contain appropriate // default values // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ void BamRecord::Reset() { Alley.ResetValue(); Event.ResetValue(); Date.ResetValue(); Ball.ResetValue(); Lift.ResetValue(); Cond.ResetValue(); Marks[0].ResetValue(); Marks[1].ResetValue(); Marks[2].ResetValue(); Marks[3].ResetValue(); Marks[4].ResetValue(); Marks[5].ResetValue(); Marks[6].ResetValue(); Marks[7].ResetValue(); Marks[8].ResetValue(); Marks[9].ResetValue(); Scores[0].ResetValue(); Scores[1].ResetValue(); Scores[2].ResetValue(); Scores[3].ResetValue(); Scores[4].ResetValue(); Scores[5].ResetValue(); Scores[6].ResetValue(); Scores[7].ResetValue(); Scores[8].ResetValue(); Scores[9].ResetValue(); return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Change the value of a specified field to its // default value // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToReset, calls the // BALString::ResetValue member function for the appropriate field // // CALLED BY: client // CALLS: BALString::ResetValue // // PARAMETERS: / in / fieldToReset - BamRecordFieldType that indicates which // field of the record is to be reset to its // default value // // PRECONDITIONS: none // // POSTCONDITIONS: The appropriate field has been changed to its default value // // AUTHOR: Amy Langill *2B DATE: 5/1/1999 *******************************************************************************/ void BamRecord::Reset(BamRecordFieldType fieldToReset) { switch (fieldToReset) { case BR_ALLEY: Alley.ResetValue(); break; case BR_EVENT: Event.ResetValue(); break; case BR_DATE: Date.ResetValue(); break; case BR_BALL: Ball.ResetValue(); break; case BR_LIFT: Lift.ResetValue(); break; case BR_COND: Cond.ResetValue(); break; case BR_MARK1: Marks[0].ResetValue(); break; case BR_MARK2: Marks[1].ResetValue(); break; case BR_MARK3: Marks[2].ResetValue(); break; case BR_MARK4: Marks[3].ResetValue(); break; case BR_MARK5: Marks[4].ResetValue(); break; case BR_MARK6: Marks[5].ResetValue(); break; case BR_MARK7: Marks[6].ResetValue(); break; case BR_MARK8: Marks[7].ResetValue(); break; case BR_MARK9: Marks[8].ResetValue(); break; case BR_MARK10: Marks[9].ResetValue(); break; case BR_SCORE1: Scores[0].ResetValue(); break; case BR_SCORE2: Scores[1].ResetValue(); break; case BR_SCORE3: Scores[2].ResetValue(); break; case BR_SCORE4: Scores[3].ResetValue(); break; case BR_SCORE5: Scores[4].ResetValue(); break; case BR_SCORE6: Scores[5].ResetValue(); break; case BR_SCORE7: Scores[6].ResetValue(); break; case BR_SCORE8: Scores[7].ResetValue(); break; case BR_SCORE9: Scores[8].ResetValue(); break; case BR_SCORE10: Scores[9].ResetValue(); break; } return; }