Created
August 27, 2017 04:51
-
-
Save karthikkondagalla/bfe46ba7a5fd97a52bc55bcb4ad21695 to your computer and use it in GitHub Desktop.
Date Validation Program in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "/home/cs689/common/689.h" | |
#include "Date.h" | |
const string defaultMonth = "January";//This global constant string variable is used for default month | |
const int defaultDay = 1;//This global constant integer variable is used for default day | |
const int defaultYear = 1900;//This global constant integer variable is used for default year | |
/* | |
This is the deafault constructor | |
*/ | |
Date::Date() | |
{ | |
month = defaultMonth; | |
day = defaultDay; | |
year = defaultYear; | |
Month(); | |
} | |
/* | |
This is the constructor that can be used to create the Date object for the month m, day d, and year y. | |
*/ | |
Date::Date(const string& m, const int& d, const int& y) | |
{ | |
month = m; | |
day = d; | |
year = y; | |
Month(); | |
} | |
/* | |
This is a public member function that is used to set the month. | |
*/ | |
void Date::setMonth(const string& m) | |
{ | |
month = m; | |
Month(); | |
} | |
/* | |
This is a public member function that is used to set the day. | |
*/ | |
void Date::setDay(const int& d) | |
{ | |
day = d; | |
} | |
/* | |
This is a public member function that is used to set the year. | |
*/ | |
void Date::setYear(const int& y) | |
{ | |
year = y; | |
} | |
/* | |
This public member function returns the month. | |
*/ | |
string Date::getMonth() const | |
{ | |
return month; | |
} | |
int Date::getDay() const//This public member function returns the day. | |
{ | |
return day; | |
} | |
/* | |
This public member function returns the year. | |
*/ | |
int Date::getYear() const | |
{ | |
return year; | |
} | |
/* | |
This public member function converts the month component of a Date object by changing its first letter to uppercase and the rest of its letters to lowercase, so the month of a date can be entered as case insensitive. | |
*/ | |
void Date::Month() | |
{ | |
unsigned int i = 0; | |
for(i=0;i<month.length();i++) | |
{ | |
if(i == 0) month[i] = toupper(month[i]); | |
else month[i] = tolower(month[i]); | |
} | |
} | |
/* | |
This public member function checks a Date object, and if it’s a valid date, it returns true; otherwise, it returns false. | |
*/ | |
bool Date::isValidDate() const | |
{ | |
int number_of_days; | |
if(isValidMonth()) | |
{ | |
number_of_days = daysInMonth(); | |
} | |
else return(false); | |
if((day <= 0) || (day > number_of_days) || (year <= 0)) return(false); | |
else return(true); | |
} | |
/* | |
This member function validates month | |
*/ | |
bool Date::isValidMonth() const | |
{ | |
string months_list[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; | |
for(unsigned int i = 0;i<12;i++) | |
{ | |
if(month.compare(months_list[i]) == 0) | |
{ | |
return(true); | |
} | |
} | |
return(false); | |
} | |
/* | |
This is for returning no. of days in february and months with 30 days and 31 days. | |
*/ | |
int Date::daysInMonth() const | |
{ | |
if(month == "February") | |
{ | |
if(isLeapYear()) return(29); | |
else return(28); | |
} | |
else//This checks the no. of days | |
{ | |
if((month == "April") || (month == "June") || (month == "September") || (month == "November")) return(30); | |
else return(31); | |
} | |
} | |
/****** | |
This is for validating the leap year. | |
*******/ | |
bool Date::isLeapYear() const | |
{ | |
if(year%4 == 0) | |
{ | |
if(year%100 != 0) return(true); | |
else | |
{ | |
if(year%400 == 0) return(true); | |
else return(false); | |
} | |
} | |
else return(false); | |
} | |
/* | |
This public member function converts the month component of a Date object in the form dd–mmm–yyyy | |
*/ | |
string Date::toString() const | |
{ | |
string date; | |
if(day < 9) date = string(intToString(0)) + intToString(day) + "-" + month.substr(0,3) + "-" + intToString(year); // Converts date to string format | |
else date = string(intToString(day)) + "-" + month.substr(0,3) + "-" + intToString(year); | |
return(date); | |
} | |
string Date::intToString(int n) const | |
{ | |
return(to_string(n)); | |
} | |
/* | |
This is for reading from the file | |
*/ | |
istream& operator>>(istream& is, Date& d) | |
{ | |
char ch; | |
is>>d.month; | |
is>>d.day; | |
is>>ch; | |
if(isdigit(ch)) is.unget(); | |
is>>d.year; | |
return is; | |
} | |
/* | |
This is for writing to a file | |
*/ | |
ostream& operator<<(ostream& os, const Date& d) | |
{ | |
os<<d.month<<" "<<d.day<<", "<<d.year; | |
return os; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PROG5_H_INCLUDED | |
#define PROG5_H_INCLUDED | |
#include "/home/cs689/common/689.h" | |
const extern string defaultMonth; | |
const extern int defaultDay; | |
const extern int defaultYear; | |
/* | |
this is the definition of the class named Date | |
*/ | |
class Date | |
{ | |
string month; | |
int day; | |
int year; | |
bool isValidMonth() const; | |
int daysInMonth() const; | |
bool isLeapYear() const; | |
string intToString(int n) const; | |
public: | |
Date(); | |
Date(const string& m, const int& d, const int& y); | |
void setMonth(const string& m); | |
void setDay(const int &d); | |
void setYear(const int& y); | |
string getMonth() const; | |
int getDay() const; | |
int getYear() const; | |
void Month(); | |
bool isValidDate() const; | |
string toString() const; | |
friend istream& operator>>(istream& is, Date& d); | |
friend ostream& operator<<(ostream& os, const Date& d); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment