/********************************************************************************************
date.cpp
********************************************************************************************/
#include "date.h"
#include <iostream>
using namespace std;
/*  Because it's a royal pain in the neck to do the 1-12 and 1-30 sequence without going into
    a bunch of if/else stuff, I just decided to STORE the day and month as if they were running
    from 0 to X. The print routine simply adds one to the stored value to get "the real thing"
*/

//------- Enhanced Default Constructor -------
Date::Date(const int m, const int d, const int y) {
    if ((d > 0) && (d <= 30))
        day = d - 1 ;
    else
        day = 0;
    
    if ((m > 0) && (m <= 12))
        month = m - 1;
    else
        month = 0;
    
    if (y > 0)
        year = y;
    else
        year = 1980;
}

//------- Print the object in "pretty" format -------
void Date::print(ostream& out) const {
    if (month < 9)
        out << '0';
    out << month + 1 << '/';
    if (day < 9)
        out << '0';
    out << day + 1 << '/';
    if (year < 1000)
        out << '0';
    if (year < 100)
        out << '0';
    if (year < 10)
        out << '0';
    out << year;
};

//------- "managed" addition functions -------
void Date::addYear(const int y) {
    if (y > 0)
        year += y;
}

void Date::addMonth(const int m) {
    if (m > 0) {
        int sum = month + m;
        addYear(sum/12);
        month = sum % 12;
    }
}

void Date::addDay(const int d) {
    if (d > 0) {
        int sum = day + d;
        addMonth(sum/30);
        day = sum % 30;
    }
}

void Date::addDate(const Date& D) {
    addYear(D.year);
    addMonth(D.month);
    addDay(D.day);
}