// baltime.cpp Implementation for BAL TimeType /******************************************************************************* // MODULE NAME: TimeType // INTERFACE FILE: baltime.h // IMPLEMENTATION FILE: baltime.cpp // // PURPOSE: To create a TimeType useful in an implementation of the BAL program, // allowing the user to create a new object, change its value, determine if it // contains an invalid or default value, and print the current or default value // // FUNCTIONS: Name Purpose // TimeType Parameterless Constructor // isValid Tests whether the time is valid // isDefault Tests whether the time is a default value // Equals Show whether this time === another // LessThan Show whether this time < another // GreaterThan Show whether this time > another // Print Print the current value of the object // Default Print the default value of the object // valueIs Print the current value of the object // Limits Show the limits on the object // Store Set the time as indicated by 2 ints // Store Set the time as indicated by a string // Reset Change the time to its default value // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ #include "baltime.h" // CONSTRUCTORS /******************************************************************************* // FUNCTION NAME: TimeType // // DESCRIPTION OF FUNCTION: Parameterless Constructor // // DESCRIPTION OF ALGORITHM: Assigns DEFAULT_HOURS to hours and DEFAULT_MINUTES // to minutes // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Object has been created w/default hours and minutes // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ TimeType::TimeType() { hours=DEFAULT_HOURS; minutes=DEFAULT_MINUTES; } // OBSERVERS /******************************************************************************* // FUNCTION NAME: isValid // // DESCRIPTION OF FUNCTION: Determines if the object contains a valid time // // DESCRIPTION OF ALGORITHM: Tests to see if hours is greater than or equal to // HOURS_MIN and less than or equal to HOURS_MAX, then tests to see if minutes // is greater than or equal to MINUTES_MIN and less than or equal to // MINUTES_MAX // // CALLED BY: client // CALLS: isDefault // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if value is default or contains hours and // minutes that fit the logical limits for time values. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool TimeType::isValid() { // If the variable contains the default, that's OK if (this->isDefault()) return true; if ((hoursHOURS_MAX)) // Are the hours valid? return false; if ((minutesMINUTES_MAX)) // Are the minutes valid? return false; return true; // If it gets this far, nothing's wrong } /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Indicate whether the object contains the default // value // // DESCRIPTION OF ALGORITHM: Compares hours with DEFAULT_HOURS for exact // equality and minutes with DEFAULT_MINUTES for exact equality // // CALLED BY: client, isValid // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if hours is exactly equal to DEFAULT_HOURS and // minutes is exactly equal to DEFAULT_MINUTES, and returns false otherwise // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool TimeType::isDefault() { return ((hours==DEFAULT_HOURS)&&(minutes==DEFAULT_MINUTES)); } /******************************************************************************* // FUNCTION NAME: Equals // // DESCRIPTION OF FUNCTION: Determine whether this TimeType object is equal // to another one // // DESCRIPTION OF ALGORITHM: Compare the elements of each object directly // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / someTime - TimeType this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if both elements of this object are exactly // equal to their corresponding elements in the other object. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool TimeType::Equals(TimeType someTime) { return ((hours==someTime.hours)&&(minutes==someTime.minutes)); } /******************************************************************************* // FUNCTION NAME: LessThan // // DESCRIPTION OF FUNCTION: Determine if this object should precede another // // DESCRIPTION OF ALGORITHM: Determines the relationship between the hours of // this object and the hours of the other object, then examines the // relationship between the minutes of this object and the other object // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / someTime - TimeType this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if this object comes before someTime // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool TimeType::LessThan(TimeType someTime) { if (hours>someTime.hours) return false; else { if ((hours==someTime.hours)&&(minutes>=someTime.minutes)) return false; } return true; } /******************************************************************************* // FUNCTION NAME: GreaterThan // // DESCRIPTION OF FUNCTION: Determine if this object should follow another // // DESCRIPTION OF ALGORITHM: Determines the relationship between the hours of // this object and the hours of the other object, then examines the // relationship between the minutes of this object and the other object // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / someTime - TimeType this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if this object comes after someTime // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool TimeType::GreaterThan(TimeType someTime) { if (hours=TIME_FLD_WDTH) // if hours string isn't too short formattedTime = ((hourString.str()).substr(0,TIME_FLD_WDTH) + ":"); else // if the hours string isn't long enough to fill the field, { // pad it with enough extra spaces for (counter=0;counter<(TIME_FLD_WDTH-(hourString.str()).length());counter++) formattedTime += "0"; formattedTime += ((hourString.str()) + ":"); } if ((minuteString.str()).length()>=TIME_FLD_WDTH) // if string isn't too short formattedTime += (minuteString.str()).substr(0,TIME_FLD_WDTH); else // if minutes string isn't long enough to fill the field, { // pad it with enough extra spaces to fill the field for (counter=0;counter<(TIME_FLD_WDTH-(minuteString.str()).length());counter++) formattedTime += "0"; formattedTime += (minuteString.str()); } return formattedTime; // pass the pretty string to the calling function } /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Creates a string that holds the default value for // the TimeType object // // DESCRIPTION OF ALGORITHM: Creates a default object and calls the Print // member function to return the string // // CALLED BY: client // CALLS: Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string which contains the default value of the // object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string TimeType::Default() { TimeType defaultTime; return (defaultTime.Print()); } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Prints the current value of the object // // DESCRIPTION OF ALGORITHM: Calls this->Print // // CALLED BY: client // CALLS: Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string which contains the current value of the // object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string TimeType::valueIs() { return (this->Print()); } /******************************************************************************* // FUNCTION NAME: Limits // // DESCRIPTION OF FUNCTION: Creates a string which reports on the limits for // a TimeType field // // DESCRIPTION OF ALGORITHM: Returns a literal string message // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns the literal string // "Must be a valid time in military HH:MM format" // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string TimeType::Limits() { return "Must be a vlid time in military HH:MM format"; } /********************************TRANSFORMERS**********************************/ /******************************************************************************* // FUNCTION NAME: Set // // DESCRIPTION OF FUNCTION: Change the value of the time to one specifiec by // two ints. // // DESCRIPTION OF ALGORITHM: Assigns hoursValue to hours, minutesValue to // minutes // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / hoursValue - int that specifies what hours should be // / in / minutesValue - int that specifies what minutes should be // // PRECONDITIONS: none // // POSTCONDITIONS: Hours and minutes have been changed to the specified values // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void TimeType::Set(int hoursValue, int minutesValue) { hours=hoursValue; minutes=minutesValue; return; } /******************************************************************************* // FUNCTION NAME: Store // // DESCRIPTION OF FUNCTION: Change the value of the object to a time as // specified by a string // // DESCRIPTION OF ALGORITHM: Creates an input stringstream, then extracts out // elements from the stringstream into individual data members // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newTime - string that contains the new time in military // HH:MM format // // PRECONDITIONS: newTime must be properly formatted in HH:MM format // // POSTCONDITIONS: hours and mintues have been changed to the specified values // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void TimeType::Store(string newTime) { istringstream timeString(newTime); char extraChar; timeString >> hours >> extraChar >> minutes; return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Change the values to their defaults // // DESCRIPTION OF ALGORITHM: Assign DEFAULT_HOURS to hours, DEFAULT_MINUTES to // minutes // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: hours and minutes now contain their respective default // values // // AUTHOR: Amy Langill *2B DATE: 3/20/199 *******************************************************************************/ void TimeType::Reset() { hours=DEFAULT_HOURS; minutes=DEFAULT_MINUTES; }