// balDate.cpp Implementation BAL DateType functions /******************************************************************************* // MODULE NAME: DateType // INTERFACE FILE: baldate.h // IMPLEMENTATION FILE: baldate.cpp // // PURPOSE: To provide a date type useful in an implementation of the BAL // program, with the capability of storing data in date form, assigning default // values to the objects, checking the validity of the data stored within an // object, and reporting on the contents of the DateType object // // FUNCTIONS: NAME PURPOSE // DateType Parmeterless constructor // isValid Test the data stored in the object for validity // isDefault Test whether the object contains the default // Equals Indicate whether this is the same as another // LessThan Indicate whether this date precedes another // GreaterThan Indicate whether this date follows another // Print Output data as formatted Date string // Default Output default data as formatted Date string // Limits Output string message that indicates the // constraints on the value of DateType objects // valueIs String that holds data formatted as Date // Store Store the given data into the object // Reset Store the default values into the object // // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ #include "baldate.h" /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: DateType // // DESCRIPTION OF FUNCTION: Creates a instance of the object class DateType // // DESCRIPTION OF ALGORITHM: Assigns default values sequentially to each // data member of the class // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: object has been created with default values in the data // members // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ DateType::DateType() { month=DEFAULT_MONTH; // Stores default values into each data member date=DEFAULT_DATE; // of the DateType object year=DEFAULT_YEAR; // } /*********************************OBSERVERS************************************/ /******************************************************************************* // FUNCTION NAME: isValid() // // DESCRIPTION OF FUNCTION: Indicates whether an instance of the DateType // class contains a valid date. // // DESCRIPTION OF ALGORITHM: If the object does not contain the default values, // test it against the logical limits for a date to see if it contains a valid // date. // // CALLED BY: client // CALLS: isDefault // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if date is valid or default, false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ bool DateType::isValid() { if (this->isDefault()) // Default is automatically valid return true; if ((monthDECEMBER)) // Does the month exist? return false; switch (month) // See if it contains an appropriate date for the month { case JANUARY: // These months all have 31 days in them case MARCH: // case MAY: // case JULY: // case AUGUST: // case OCTOBER: // case DECEMBER: return !((dateTHIRTY_ONE_DAYS)); break; case APRIL: // These months have 30 days in them case JUNE: // case SEPTEMBER: // case NOVEMBER: return !((dateTHIRTY_DAYS)); break; default: if (date==TWENTY_NINE_DAYS) // check for leap year return (((year%FOUR_YEARS==ZERO_REMAINDER)&& (year%CENTURY!=ZERO_REMAINDER))|| (year%FOUR_CENTURY==ZERO_REMAINDER)); else // February normally has 28 days return !((dateTWENTY_EIGHT_DAYS)); break; } } /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Test to see whether the object contains the default // date // // DESCRIPTION OF ALGORITHM: Test each data element to see if it contains the // default value for that field. // // CALLED BY: isValid, client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if the object's date is DEFAULT_DATE, month is // DEFAULT_MONTH, and year is DEFAULT_YEAR. Returns false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ bool DateType::isDefault() { if ((month==DEFAULT_MONTH)&&((date==DEFAULT_DATE)&&(year==DEFAULT_YEAR))) return true; return false; } /******************************************************************************* // FUNCTION NAME: Equals // // DESCRIPTION OF FUNCTION: Tests whether the date object contains the same // date value as another DateType object // // DESCRIPTION OF ALGORITHM: Tests each element of the object against its // corresponding element in another DateType object passed by value // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / someDay - DateType that holds second date for comparison // // PRECONDITIONS: none // // POSTCONDITIONS: If every element of this day is exactly equal to the // corresponding element in someDay, returns true. Otherwise, returns false. // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ bool DateType::Equals(DateType someDay) { return (((month==someDay.month)&&(date==someDay.date))&&(year==someDay.year)); } /******************************************************************************* // FUNCTION NAME: LessThan // // DESCRIPTION OF FUNCTION: Indicate whether this date precedes another date // // DESCRIPTION OF ALGORITHM: Tests to see if the this->year is less than the // other year OR if this->year is the same as another date's year and month is // less than that of another date OR this year is the same as another date's // year and the the months are the same and the objects date precedes another // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / someDay - date that this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: returns true if the date stored in the object is less than // the date stored in the parameter date object. // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ bool DateType::LessThan(DateType someDay) { return ((yearsomeDay.year)||((year==someDay.year)&&(month>someDay.month)))|| (((year==someDay.year)&&(month==someDay.month))&&(date>someDay.date)); } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Prints out a string that holds the data formatted // in MM/DD/YY format. // // DESCRIPTION OF ALGORITHM: Converts int member to strings, concatenates // strings together into another string. // // CALLED BY: valueIs(), client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: data has been converted to a string with MM/DD/YY format // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ string DateType::Print() { ostringstream monthString; // Strings to hold converted int values ostringstream dateString; // ostringstream yearString; // string formattedDate; // String to hold resulting date string int counter; // used to control for-loops monthString << month; // Convert the ints to strings dateString << date; // yearString << year; // // output the month to the formatted date string if ((monthString.str()).length()>=DATE_FLD_WDTH) // month fills the field formattedDate = ((monthString.str()).substr(0,DATE_FLD_WDTH) + "/"); else // month is too short to fill the field { for (counter=0;counter<(DATE_FLD_WDTH-(monthString.str()).length());counter++) formattedDate += "0"; // pad value with zeroes formattedDate += ((monthString.str()) + "/"); } // output the date to the formatted date string if ((dateString.str()).length()>=DATE_FLD_WDTH) formattedDate += ((dateString.str()).substr(0,DATE_FLD_WDTH) + "/"); else // date is too short to fill the field { for (counter=0;counter<(DATE_FLD_WDTH-(dateString.str()).length());counter++) formattedDate += "0"; // pad value with zeroes formattedDate += ((dateString.str()) + "/"); } // stick the year on the end of the formatted date string if ((yearString.str()).length()>=DATE_FLD_WDTH) // If the year's not too short formattedDate += (yearString.str()).substr((yearString.str()).length()- DATE_FLD_WDTH,DATE_FLD_WDTH); else // otherwise, pad the field with zeroes { for (counter=0;counter<(DATE_FLD_WDTH-(yearString.str()).length());counter++) formattedDate += "0"; formattedDate += (yearString.str()); } return formattedDate; } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Prints out a string that holds the current date in // MM/DD/YY format // // DESCRIPTION OF ALGORITHM: calls Print // // CALLED BY: client // CALLS: Print() // // PARAMETERS: none // // PRECONDITIONS: Print is a function of class DateType which returns a string // that holds the current date in MM/DD/YY format // // POSTCONDITIONS: Returns a string that holds the current value of the // DateType object in MM/DD/YY format // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ string DateType::valueIs() { return (this->Print()); } /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Prints out the default date value into a string // in MM/DD/YY format. // // DESCRIPTION OF ALGORITHM: Creates a new DateType variable with default // values, then Prints it // // CALLED BY: client // CALLS: DateType() constructor, Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string that hold the default value for a date in // MM/DD/YY format // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ string DateType::Default() { DateType defaultDate; // Create an instance with default parameters return defaultDate.Print(); // Print the value it holds } /******************************************************************************* // FUNCTION NAME: Limits // // DESCRIPTION OF FUNCTION: Tells what the constraints are for a DateType field // // DESCRIPTION OF ALGORITHM: Creates a string, writes a message to it, returns // the string with the message to the calling function. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been returned that holds a message indicating // the limits on a DateType variable // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ string DateType::Limits() { return ("Must be a valid date in MM/DD/YY format" + '\n'); } /*****************************TRANSFORMERS*************************************/ /******************************************************************************* // FUNCTION NAME: Store // // DESCRIPTION OF FUNCTION: Given the values for the month, date, and year, // sets the values of month, date, year to those new values // // DESCRIPTION OF ALGORITHM: Assigns whatever is passed for month and date into // month and date. If the defaults are passed, the default year is assigned to // the year data member. If the year passed is a 2-digit year, the default // century value is added to the year value passed to the function and the // result is stored into year. If the year passed is positive and contains // more than 2 digits, whatever is passed for the year is assigned to the year // data member. // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / monthValue - int that gets stored in month // / in / dateValue - int that gets stored in date // / in / yearValue - int to be stored in date // // PRECONDITIONS: none // // POSTCONDITIONS: Date has been assigned as described above. Parameters were // not changed by the function. // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ void DateType::Store(int monthValue, int dateValue, int yearValue) { month=monthValue; // no error-checking of month or date here date=dateValue; // // Don't fiddle with the century if the values are the defaults if (((yearValue==DEFAULT_YEAR)&&(monthValue==DEFAULT_MONTH))&& (dateValue==DEFAULT_DATE)) { year=DEFAULT_YEAR; return; } if (yearValue> month; dateBuffer >> delimiterChar; dateBuffer >> date; dateBuffer >> delimiterChar; dateBuffer >> year; return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Resets the object to the default date // // DESCRIPTION OF ALGORITHM: Assigns each of the elements the default value // for that element // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The object contains the default month, default date, and // default year // // AUTHOR: Amy Langill *2B DATE: 3/19/1999 *******************************************************************************/ void DateType::Reset() { month=DEFAULT_MONTH; // Stores default values into each data member date=DEFAULT_DATE; // of the DateType object year=DEFAULT_YEAR; // }