-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.cpp
224 lines (197 loc) · 3.34 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Date.cpp
*
* Created on: Mar 24, 2023
* Author: abdullahahmed
*/
#include "Date.hpp"
#include <iomanip>
Date::Date() {
// TODO Auto-generated constructor stub
this->day = 0;
this->month = 0;
this->year = 0;
}
Date::~Date() {
// TODO Auto-generated destructor stub
}
// Function to check for the number of days for a specific month
bool Date::numDaysInMonth()
{
if (this->month == 2)
{
return 28;
return true;
}
else if (this->month == 4 or this->month == 6 or this->month == 9 or this->month == 11)
{
return 30;
return true;
}
else if(this->month == 1 or this->month == 3 or this->month == 5 or this->month == 7 or this->month == 8 or this->month == 10 or this->month == 12)
{
return 31;
return true;
}
else
{
return false;
}
}
bool Date::operator++(int day)
{
if (this->day == 0 or this->month == 0 or this->year == 0)
{
return false;
}
this->day++;
if (this->day > numDaysInMonth())
{
this->month++;
this->day = 1;
}
if (this->month > 12)
{
this->year++;
this->month = 1;
}
return true;
}
bool Date::operator--(int day)
{
if (this->day == 0 or this->month == 0 or this->year == 0)
{
return false;
}
else
{
this->day--;
}
if (this->day == 0)
{
this->month--;
}
if (this->month == 0)
{
this->year--;
this->month = 12;
this->day = numDaysInMonth();
}
return true;
}
bool Date::operator==(const Date& rightDate)
{
if (this->day == rightDate.day and this->month == rightDate.month and this->year == rightDate.year)
{
return true;
}
else
{
return false;
}
}
ostream &operator<<(ostream &myOutput, const Date& dateObject)
{
if (dateObject.day == 0 or dateObject.month == 0 or dateObject.year == 0)
{
myOutput << "Invalid date sent";
}
else
{
myOutput << setw(2) << setfill('0') << dateObject.month << "/" << setw(2) << setfill('0') << dateObject.day << "/" << setw(2) << setfill('0') << dateObject.year;
}
return myOutput;
}
bool Date::operator<(const Date& rightDate)
{
if (this->year < rightDate.year)
{
return true;
}
else if (this->year == rightDate.year and this->month < rightDate.month)
{
return true;
}
else if (this->year == rightDate.year and this->month == rightDate.month and this->day < rightDate.day)
{
return true;
}
else
{
return false;
}
}
bool Date::operator>(const Date& rightDate)
{
if (this->year > rightDate.year)
{
return true;
}
else if (this->year == rightDate.year and this->month > rightDate.month)
{
return true;
}
else if(this->year == rightDate.year and this->month == rightDate.month and this->day > rightDate.day)
{
return true;
}
else
{
return false;
}
}
int Date::getDay() const {
return day;
}
bool Date::setDay(int day) {
if (this->year == 0 or this->month == 0)
{
return false;
}
if (this->month == 0)
{
this->day = 0;
return true;
}
else if (this->day < 1 or this->day > numDaysInMonth())
{
return false;
}
else
{
this->day = day;
return true;
}
}
int Date::getMonth() const {
return month;
}
bool Date::setMonth(int month) {
if (this->year == 0)
{
return false;
}
else if (this->month < 1 or this->month > 12)
{
return false;
}
else
{
this->month = month;
return true;
}
}
int Date::getYear() const {
return year;
}
bool Date::setYear(int year) {
if (this->year < 1 or this->year > 9999)
{
return false;
}
else
{
this->year = year;
return true;
}
}