-
Notifications
You must be signed in to change notification settings - Fork 1
/
date.cpp
executable file
·52 lines (44 loc) · 1.23 KB
/
date.cpp
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
#include<iostream>
#include "date.hpp"
const int Date::maxDaysPerMonth = 31;
std::string Date::getMonth() const { return month; }
int Date::getDay() const { return day; }
int Date::getYear() const { return year; }
Date::Date( std::string month, int year, int day ) {
setMonth( month );
setDay( day );
setYear( year );
}
bool Date::setMonth( std::string month ) {
this->month = month;
return true;
}
bool Date::setDay( int day ) {
if ( ( day < 1 ) || ( day > maxDaysPerMonth) ) {
return false; // do not set and just return false
} else {
// day may be set
this->day = day;
}
return true;
}
bool Date::setYear( int year ) {
// validate the year
if ( year < 0 ) {
// return false since year is invalid
return false;
} else {
// set year: year is valid
this->year = year;
}
return true;
}
bool Date::setFull( std::string month, int day, int year ) {
if ( !setMonth( month ) || !setDay( day ) || !setYear( year ) ) {
return false; // return false if any of them were invalid
}
return true; // is valid -> return true
}
void Date::print() const {
std::cout << month << " " << day << ", " << year;
}