Monday, November 2, 2009

Date Class .cpp

/*
Title: Date Class (.cpp file) - implementation
Author: D. Tanner
class: CS 1415
Date: January 5th, 2006
*/

#include "TannDate.h"

//Date Class implementations - Constructors
/* Default Constructor
Purpose: To create an object of class Date with the date at time of\
creation as the default values
Rec./Ref.: N/A
Return: N/A
*/
TannDate::TannDate()
{
//create a string that will hold the current system date
//collected w/ the strdate function
char currentDate[10];
_strdate(currentDate);

//change the different parts of the array into an int
int month = ((currentDate[0] - 48) * 10) + (currentDate[1] - 48);
int dayOfMonth = ((currentDate[3] - 48) * 10) + (currentDate[4] - 48);
int year = 2000 + ((currentDate[6] - 48) * 10) + (currentDate[7] - 48);
setDate(month, dayOfMonth, year);
}

/* Explicit Value Constructor - with month as a number
Purpose: To provide a way to create a Date object with a user-chosen date
Rec./Ref: N/A
Return: N/A
*/
TannDate::TannDate(int month, int dayOfMonth, int year)
{
setDate(month, dayOfMonth, year);
}

/* Explicit Value Constructor - w/ only the day of the year (0-365)
Purpose: same as above, only here the user can give the day of the year
*/
TannDate::TannDate(int dayOfYear, int year)
{
setDate(dayOfYear, year);
}

//Set Functions
void TannDate::setDate(int month, int dayOfMonth, int year)
{
//validate the info except for monthName
if(month < 1 || month > 12)
{
cout << "Incorrect month. Exiting program..."; exit(1);
}
else if(year % 4 && (dayOfMonth < 1 || dayOfMonth > DAYS_PER_MON[month - 1]))
{
cout << "error: Incorrect day of month. Exiting program..."; exit(1);
}
else if(dayOfMonth < 1 || dayOfMonth > DAYS_PER_MON_LPYR[month - 1])
{
cout << "error: Incorrect day of month. Exiting program..."; exit(1);
}
else if(year < 0)
{
cout << "error: Incorrect year. Exiting program..."; exit(1);
}
else

{ //set the data members
setMonth(month); setDayOfMonth(dayOfMonth); setYear(year);
calcMonthName(); calcDayOfYear();
}
}

void TannDate::setDate(int dayOfYear, int year)
{
//validation
if(year % 4 && (dayOfYear < 1 || dayOfYear > 365))
throw BadDate("error: incorrect day of year");
else if(dayOfYear < 1 || dayOfYear > 366)
throw BadDate("error: incorrect day of year");

//set data members
setDayOfYear(dayOfYear); setYear(year);
calcFromDayOfYear(); calcMonthName();
}

//Calculation functions - these will calculate some data members when others
//have appropriate values

/* CalcFromDayOfYear
Purpose: To calculate the month & day from the day of the year
Rec./Ref.:N/A
Return: N/A
*/
void TannDate::calcFromDayOfYear()
{
//create variables
int tempDays = dayOfYear_;
int count = 0;

//find the month and dayOfMonth by subtracting the # of days in each month
while(tempDays > 31)
{
if(year_ % 4)
tempDays - DAYS_PER_MON[count];
else
tempDays - DAYS_PER_MON_LPYR[count];
count ++;
}
setMonth(count + 1);
setDayOfMonth(tempDays);
}

/* Calc DayOfYear
Purpose: To calculate the day of the year from the month and day of month
*/
void TannDate::calcDayOfYear()
{
int dayOfYear = 0;
for(int count = month_ - 1; count > 0; count--)
{
if(year_ % 4)
dayOfYear += DAYS_PER_MON[count];
else
dayOfYear += DAYS_PER_MON_LPYR[count];
}
dayOfYear += dayOfMonth_;
setDayOfYear(dayOfYear);
}

/* Calc MonthName
Purpose: To calculate the name of the month based on the number
*/
void TannDate::calcMonthName()
{
switch(month_)
{
case 1:
setMonth("January");
break;
case 2:
setMonth("February");
break;
case 3:
setMonth("March");
break;
case 4:
setMonth("April");
break;
case 5:
setMonth("May");
break;
case 6:
setMonth("June");
break;
case 7:
setMonth("July");
break;
case 8:
setMonth("August");
break;
case 9:
setMonth("September");
break;
case 10:
setMonth("October");
break;
case 11:
setMonth("November");
break;
default:
setMonth("December");
break;
}
}

//Print Date Functions

/* Shorthand version
Purpose: To print out the date in shorthand format (MM/DD/YY)
*/
void TannDate::printShortDate()
{
cout << month_ << "/" << dayOfMonth_ << "/" << year_;
}

/* Long version
Purpose: To display a date in the form Month Day Of Month, Year
*/
void TannDate::printLongDate()
{
cout << monthName_ << ' ' << dayOfMonth_ << ", " << year_;
}

/* Day of Year Version
Purpose: To display a date in the form: Day of Year, Year
*/
void TannDate::printDOY()
{
cout << dayOfYear_ << ", " << year_;
}

No comments:

Post a Comment