//******************************************************************
// IMPLEMENTATION FILE (datetype.cpp)
// This file implements the DateType member functions
//******************************************************************
#include "datetype.h"
#include "bool.h"
#include <iostream.h>

// Private members of class:
//     int month;
//     int day;
//     int year;

int DaysInMonth( int, int );  // Prototype for auxiliary function

//******************************************************************

DateType::DateType()	// Default Constructor

{
    month = 1;
    day = 1;
    year = 1583;
}

// Constructor
DateType::DateType( /* in */ int newMonth,
                    /* in */ int newDay,
                    /* in */ int newYear  )
{
   if (newMonth > 0 && newMonth <= 12)         // validate the month
      month = newMonth;
   else {
      month = 1;
      cout << "Month " << month << " invalid. Set to month 1."
           << endl;
   }

   year = newYear;                      // could also check
   day = checkDay(newDay);             // validate the day

   cout << "Date object constructor for date ";
   Print();
   cout << endl;
}

int DateType::Month() const
{
    return month;
}

int DateType::Day() const
{
    return day;
}

int DateType::Year() const
{
    return year;
}

void DateType::Print() const
{	
	static char monthString[12][10] =
    {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November",
        "December"
    };

    cout << monthString[month-1] << ' ' << day << ", " << year;
}

RelationType DateType::ComparedTo(
                         /* in */ DateType otherDate ) const
{
    if (year < otherDate.year)            // Compare years
        return BEFORE;
    if (year > otherDate.year)
        return AFTER;

    if (month < otherDate.month)            // Years are equal. Compare
        return BEFORE;                //   months
    if (month > otherDate.month)
        return AFTER;

    if (day < otherDate.day)          // Years and months are equal.
        return BEFORE;                //   Compare days
    if (day > otherDate.day)
        return AFTER;

    return SAME;                      // Years, months, and days
}                                     //   are equal

void DateType::Increment()
{
    day++;
    if (day > DaysInMonth(month, year))
    {
        day = 1;
        month++;
        if (month > 12)
        {
            month = 1;
            year++;
        }
    }
}

int DaysInMonth( /* in */ int month,
                 /* in */ int year  )
{
    static int numDays[13] =                // No. of days per month
    {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };

    if (month != 2)
        return numDays[month];

    // It's February.  Check for leap year

    if ((year % 4 == 0 && year % 100 != 0) ||
          year % 400 == 0)
        return 29;
    else
        return 28;
}

int DateType::checkDay(int testDay)
{
   if (testDay > 0 && testDay <= DaysInMonth(month, year))
      return testDay;

   cout << "Day " << testDay << " invalid. Set to day 1." << endl;

   return 1;  // leave object in consistent state if bad value
}


