// balchar.cpp Implementation file for setChar class #include "balchar.h" /******************************************************************************* // MODULE NAME: setChar // INTERFACE FILE: balchar.h // IMPLEMENTATION FILE: balchar.cpp // // PURPOSE: To provide a type useful in the BAL program which allows the user to // store a char and a string which contains the valid chars for the object so // that the user can test for validity, assign defaults, print the current // values and print the constraints in place upon the field // // FUNCTIONS: Name Purpose // setChar Parameterless constructor // setChar Constructor w/string of valid characters // isValid Test to see if char is included in valid string // isDefault Test to see if char contains the default value // valueIs Report on the value currently stored in object // Print Create a formatted string with the value in it // Default Create a string with the defalt value in it // Limits Create a string which tells what the constraints // are on this object // setString Store a new string to test the value against // Store Store a new char into the value // ResetValue Store the default char into the value // ResetString Store the default string into checking string // Reset Reset both the string and the value to their // default values // // AUTHOR: Amy Langill *2B Date: 3/20/1999 *******************************************************************************/ /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: setChar // // DESCRIPTION OF FUNCTION: Parameterless constructor // // DESCRIPTION OF ALGORITHM: Assigns default values sequentially to each data // member of the class // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Instance of this object type has been created with default // values in the data members // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ setChar::setChar() { value=DEFAULT_CHAR; // Record default information validChars=DEFAULT_CHAR_STRING; // } /******************************************************************************* // FUNCTION NAME: setChar // // DESCRIPTION OF FUNCTION: Constructor with specified string of valid chars // // DESCRIPTION OF ALGORITHM: Assigns default value to char data member and // user-supplied string to string data member // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / charsToUse - string that indicates which chars are valid // choices for this object // // PRECONDITIONS: none // // POSTCONDITIONS: Instance of this object type has been created with default // value and user-specified string for checking the validity of the value // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ setChar::setChar(string charsToUse) { value=DEFAULT_CHAR; // Assign default to value validChars=charsToUse; // Record user-supplied information } /*********************************OBSERVERS************************************/ /******************************************************************************* // FUNCTION NAME: isValid // // DESCRIPTION OF FUNCTION: Indicates whether the object contains valid data // // DESCRIPTION OF ALGORITHM: If the data contained is not the default, checks // to see if the char stored in this->value can be found in this->validChars // // CALLED BY: client // CALLS: isDefault // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: If the data is not the default and cannot be found in the // string for checking validity, returns false. Returns true otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool setChar::isValid() { bool charOK=false; int loop; if (this->isDefault()) // don't error-check the default return true; for (loop=0;loopvalue is exactly equal to // the defined default character DEFAULT_CHAR // // CALLED BY: isValid, client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if the value stored is the default character, // and false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool setChar::isDefault() { return (value==DEFAULT_CHAR); } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Reports on the character currently stored in value // // DESCRIPTION OF ALGORITHM: Returns value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a copy of the value stored in the object, does not // change the value stored in the object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ char setChar::valueIs() { return value; } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Prints the value into a string // // DESCRIPTION OF ALGORITHM: Converts char to string and returns string // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Resulting string contains only the character that was stored // in value // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string setChar::Print() { string outputString; outputString=value; return outputString; } /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Prints a string that holds the default value // // DESCRIPTION OF ALGORITHM: Converts default value to string & returns string // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns the string value of the default char // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string setChar::Default() { string outputString; outputString = DEFAULT_CHAR; return outputString; } /******************************************************************************* // FUNCTION NAME: Limits // // DESCRIPTION OF FUNCTION: Creates a string holding a message which indicates // what limits are in effect on this field. // // DESCRIPTION OF ALGORITHM: Writes a brief message to the output string, then // writes each character of the validity string sequentially, separated by // multiple spaces. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string is created which contains a message indicating // which characters are valid for this setChar object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string setChar::Limits() { string outputString; if (validChars==DEFAULT_CHAR_STRING) // If validChars wasn't set, there are outputString = "No Limits"; // no limits on the object else outputString = "Must be one of the following characters: " + validChars ; return outputString; } /*****************************TRANSFORMERS*************************************/ /******************************************************************************* // FUNCTION NAME: Store // // DESCRIPTION OF FUNCTION: Saves a user-supplied char into the value // // DESCRIPTION OF ALGORITHM: Assigns the new value to this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newValue - char that is to be stored // // PRECONDITIONS: none // // POSTCONDITIONS: The value of the setChar has been changed to the specified // newValue. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void setChar::Store(char newValue) { value=toupper(newValue); return; } /******************************************************************************* // FUNCTION NAME: setString // // DESCRIPTION OF FUNCTION: Saves a user-supplied string of valid characters // into this->validChars // // DESCRIPTION OF ALGORITHM: Assigns the new value to this->validChars // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / charsToUse - string of valid characters to be stored // // PRECONDITIONS: none // // POSTCONDITIONS: the value of validChars has been changed to the specified // string charsToUse // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void setChar::setString(string charsToUse) { this->validChars=charsToUse; return; } /******************************************************************************* // FUNCTION NAME: ResetValue // // DESCRIPTION OF FUNCTION: Resets the value stored in the object to the // default character DEFAULT_CHAR // // DESCRIPTION OF ALGORITHM: Assigns DEFAULT_CHAR to value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The value member of the setChar has been assigned the // DEFAULT_CHAR // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void setChar::ResetValue() { value=DEFAULT_CHAR; return; } /******************************************************************************* // FUNCTION NAME: ResetString // // DESCRIPTION OF FUNCTION: Resets the string used for checking the validity // of the object's value to the default string DEFAULT_CHAR_STRING // // DESCRIPTION OF ALGORITHM: Assigns DEFAULT_CHAR_STRING to this->validChars // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The validChars member of the setChar has been assigned the // value of DEFAULT_CHAR_STRING // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void setChar::ResetString() { validChars=DEFAULT_CHAR_STRING; return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Resets all the data members to their defaults // // DESCRIPTION OF ALGORITHM: Assigns DEFAULT_CHAR to this->value and // DEFAULT_CHAR_STRING to this->validChars // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The value has been changed to the DEFAULT_CHAR, and the // validChars member has been changed to DEFAULT_CHAR_STRING // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void setChar::Reset() { value=DEFAULT_CHAR; validChars=DEFAULT_CHAR_STRING; return; }