1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/* Date.cpp - Implementation file for the Date class. */
#include <array>
#include <string>
#include <iostream>
#include "Date.h"
using std::array;
using std::string;
using std::cout;
/* Initializes the daysPerMonth array with the days per month */
const array<int, 13> daysPerMonth = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
/* Initializes the monthNames array with the names of the months of the year */
const array<string, 12> monthNames = { "JANUARY", "FEBRUARY", "MARCH", "APRIL",
"MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
"NOVEMBER", "DECEMBER" };
/* Enumerator to validate Month values */
enum Months { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY,
JUNE, JULY, AUGUST, SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER };
enum Years { LOWEST = 1950, HIGHEST = 2100 };
/* **********************************************************
Date::setDay (accepts month)
If the arguments passed to the setMonth function are equal
to or greater than JANUARY and smaller than or equal to
DECEMBER it is copied into the member variable month, and
true is returned. If it is not, the value of day remains
unchanged and false is returned.
********************************************************** */
bool Date::setMonth(int mm)
{
bool isValidMonth = false;
mm >= JANUARY && mm <= DECEMBER ? month = mm, isValidMonth = true : isValidMonth = false;
return isValidMonth;
}
/* **********************************************************
Date::setDay (accepts day member)
If the argument passed to the setDay function is greater
than 1 and less than daysPerMonth (validating that the day
entered is within range of any given month, ex: May = 31),
it is copied into the member variable day, and true is
returned. If it is not, the value of day remains unchanged
and false is returned.
********************************************************** */
bool Date::setDay(int dd)
{
bool isValidDay = false;
dd >= 1 && dd <= daysPerMonth[month] ? day = dd, isValidDay = true : isValidDay = false;
return isValidDay;
}
/* **********************************************************
Date::setYear (accepts year member)
If the argument passed to the setYear function is greater
than 1 and less than 2100, it is copied into the member
variable 'year', and true is returned. If it is not, the
value of year remains unchanged and false is returned.
********************************************************** */
bool Date::setYear(int yyyy)
{
bool isValidYear = false;
yyyy >= LOWEST && yyyy <= HIGHEST ? year = yyyy, isValidYear = true : isValidYear = false;
return isValidYear;
}
/* **********************************************************
Date::displayFormatOne
This function displays the date as 12/12/2012
********************************************************** */
void Date::displayFormatOne() const
{
cout << "\nDate Format One:\n";
cout << month << "/" << day << "/" << year << "\n\n";
}
/* **********************************************************
Date::displayFormatTwo
This function displays the date as DECEMBER 12, 2012
********************************************************** */
void Date::displayFormatTwo() const
{
cout << "Date Format Two:\n";
cout << monthNames[month - 1] << " " << day << ", " << year << "\n\n";
}
/* **********************************************************
Date::displayFormatThree
This function displays the date as 12 DECEMBER, 2012
********************************************************** */
void Date::displayFormatThree() const
{
cout << "Date Format Three:\n";
cout << day << " " << monthNames[month - 1] << " " << year << "\n";
}
|