// Date.h
#ifndef Date_H
#define Date_H
namespace DateSpace
{
class Date
{
private:
int Day;
int Month;
int Year;
public:
Date()
{
Day = 1;
Month = 1;
Year = 1900;
}
Date(int dd, int mm, int yy)
{
Day = dd;
Month = mm;
Year = yy;
}
int getDay(){ return Day;}
int getMonth(){ return Month; }
int getYear(){ return Year; }
void setDay();
void setMonth();
void setYear();
void ShowDate();
void CheckAnotherDate();
bool operator>(Date &rhs);
bool operator<(Date &rhs);
bool operator>=(Date & rhs);
bool operator<=(Date & rhs);
};
int IsValidDate(int Day, int Month, int Year);
int IsLeapYear(int Year);
}
#endif
=============================
//Date.cpp
// File: Date.cpp; Name: Ethan Castanon
#include "Date.h"
#include <iostream>
namespace DateSpace
{
int DateSpace::IsLeapYear(int Year)
{
if (Year % 4 != 0) return 0; //no, it is not a Leap Year
if (Year % 400 == 0) return 1; // yes, it is a Leap Year
if (Year % 100 == 0) return 0; // no, it is not a Leap Year
return 1;
////int isLeap;
//if ((Year % 100 == 0) && (Year % 400 == 0))
// return 1;
//else
//if ((Year % 100 != 0) && (Year % 4 == 0))5
// return 1;
//else
// return 0;
////return isLeap;
}
int DateSpace::IsValidDate(int Day, int Month, int Year)
{
int ret = IsLeapYear(Year);
if ( (Month == 2) && (ret==1) && ( (0 <Day) && (Day <= 29)))
return 1;
if ((Month==2) && (ret == 0) && (0< Day && Day <= 28))
return 1;
if (((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) && (0 < Day && Day<31))
return 1;
if (((Month == 1) || (Month == 3) || (Month == 5) || (Month == 7) || (Month == 8) || (Month == 10) || (Month == 12) ) && (0 < Day && Day < 32))
return 1;
std::cout << "not a valid date\n";
//Day = 1;
//Month = 1;
//Year = 1900;
return 0;
}
void Date::setDay()
{
int theDay;
std::cout << "Please enter your date's day value in integer form." << std::endl;
std::cin >> theDay;
//while ( 0>=theDay || 32<=theDay)
//{
// std::cout << "Please enter your date's day value in integer form." << std::endl;
// std::cin >> theDay;
//}
while(IsValidDate(theDay,Month,Year)==0)
{
std::cout << "Error, please re-enter your date's day value in integer form." << std::endl;
std::cin >> theDay;
}
Day = theDay;
}
void Date::setMonth()
{
int theMonth;
std::cout << "Please enter your date's month value in integer form." << std::endl;
std::cin >> theMonth;
while (IsValidDate(Day,theMonth,Year)==0)
{
std::cout << "Error, please re-enter your date's month value in integer form." << std::endl;
std::cin >> theMonth;
}
Month = theMonth;
while (theMonth<= 0 || theMonth>= 13)
{
std::cout << "You have committed a *violation*. The months's value is unchanged" << std::endl;
std::cout<< "and remains the default month, which is 1." << std::endl;
std::cout<< "The month value you enter must be an integer value ranging from 1 to 12." << std::endl;
std::cout << "Please enter your date's month value in integer form." << std::endl;
std::cin >> theMonth;
}
}
void Date::setYear()
{
int theYear;
std::cout << "Please enter your date's year value in integer form." << std::endl;
std::cin >> theYear;
while (IsValidDate(Day,Month,theYear)==0)
{
std::cout << "Error, please re-enter your date's year value in integer form." << std::endl;
std::cin >> theYear;
}
Year = theYear;
}
void Date::ShowDate()
{
std::cout << "The date is: " << Month << "/" << Day << "/" << Year << std::endl;
}
void Date::CheckAnotherDate()
{
int YesNo;
std::cout << "Do you want to check another date?" << std::endl;
std::cout << "Enter the integer 1 for yes; enter integer 0 for no" << std::endl;
std::cin >> YesNo;
while (YesNo == 1)
{
DateSpace::Date theDate;
theDate.ShowDate();
theDate.setYear();
theDate.setMonth();
theDate.setDay();
theDate.ShowDate();
std::cout << "Do you want to check another date?" << std::endl;
std::cout << "Enter the integer 1 for yes; enter integer 0 for no." << std::endl;
std::cin >> YesNo;
}
}
bool Date::operator<=(Date &rhs)
{
if (Year <= rhs.Year)
return true;
if (Month <= rhs.Month)
return true;
if (Day <= rhs.Day)
return true;
return false;
}
bool Date::operator<(Date &rhs)
{
if (Year < rhs.Year)
return true;
if (Month < rhs.Month)
return true;
if (Day < rhs.Day)
return true;
return false;
}
bool Date::operator>=(Date &rhs)
{
if (Year >= rhs.Year)
return true;
if (Month >= rhs.Month)
return true;
if (Day >= rhs.Day)
return true;
return false;
}
bool Date::operator>(Date &rhs)
{
if (Year > rhs.Year)
return true;
if (Month > rhs.Month)
return true;
if (Day > rhs.Day)
return true;
return false;
}
}
==========================================================
// DateDemo.cpp
#include <iostream>
#include "Date.h"
#include "stdafx.h"
using namespace std;
using namespace DateSpace;
int _tmain(int argc, _TCHAR* argv[])
{
Date Date1;
Date Date2(2,14,1976);
if (Date1>Date2)
cout << "Date1 is greater than Date2" << endl;
else
cout << "Date2 is greater than Date1" << endl;
DateSpace::Date theDate;
theDate.ShowDate();
theDate.setYear();
theDate.setMonth();
theDate.setDay();
theDate.ShowDate();
theDate.CheckAnotherDate();
system("PAUSE");
return 0;
}