// balst.cpp Implementation for the BALState class /******************************************************************************* // MODULE NAME: BALState // INTERFACE FILE: balst.h // IMPLEMENTATION FILE: balst.cpp // // PURPOSE: To provide a state code storage type useful in implementing the BAL // program which allows the user to create a new BALState, assign a value to it // test the value to see if it is valid or contains a default, print the value // or the default value, and reset the value to its default // // FUNCTIONS: Name Purpose // BALState Parameterless Constructor // isValid Test whether value could be a valid state code // isDefault Test whether object contains default value // valueIs Report current value of object // Print Print, w/correct width, current value of object // Default Print, w/correct width, default value of object // Limits Report limits on a BALState object // Store Change the value to a specified one // Reset Change the value to the default value // // AUTHOR: Amy Langill *2B DATE: 3/22/199 *******************************************************************************/ #include "balst.h" /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: BALState // // DESCRIPTION OF FUNCTION: Creates a default BALState object // // DESCRIPTION OF ALGORITHM: assigns DEFAULT_STATE to value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Object has been created w/default value // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ BALState::BALState() { value=DEFAULT_STATE; } /*********************************OBSERVERS************************************/ /******************************************************************************* // FUNCTION NAME: isValid // // DESCRIPTION OF FUNCTION: Determines whether object could hold a valid state // code or not // // DESCRIPTION OF ALGORITHM: Tests each character in the value string to // determine if it is alphabetic. // // CALLED BY: client // CALLS: isDefault // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if object is default or is not more than 2 // alphabetic characters long // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ bool BALState::isValid() { if (this->isDefault()) return true; return ((value.length()<=STATE_WIDTH)&& ((isalpha(value[FIRST_CHAR]))&&(isalpha(value[SECOND_CHAR])))); } /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Determines if the object contains the default value // // DESCRIPTION OF ALGORITHM: Compares value for exact equality w/DEFAULT_STATE // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if the value string is exactly equal to // DEFAULT_STATE // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ bool BALState::isDefault() { return (value==DEFAULT_STATE); } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Reports the current value of the object, without // any special formatting // // DESCRIPTION OF ALGORITHM: Returns the value string // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns the value string // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ string BALState::valueIs() { return value; } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Creates a string of the appropriate length that // holds the current value of the object // // DESCRIPTION OF ALGORITHM: If the value string is not wide enough to fill the // field, pads it with spaces in front to make it wider, otherwise, prints out // the first STATE_WIDTH characters of the value string // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been createc with length STATE_WIDTH that // contains the current value of the object // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ string BALState::Print() { int counter; // for controlling loop string outputString; // to hold resultant string if (value.length()