diff options
date mutable change
4 files changed, 23 insertions, 11 deletions
| -rw-r--r-- | Sem. 06/Event/Date.cpp | 23 | ||||
| -rw-r--r-- | Sem. 06/Event/Date.h | 7 | ||||
| -rw-r--r-- | Sem. 06/Event/Event.cpp | 2 | ||||
| -rw-r--r-- | Sem. 06/Event/Event.h | 2 |
diff --git a/Sem. 06/Event/Date.cpp b/Sem. 06/Event/Date.cpp index 51543c3..b4df47a 100644 --- a/Sem. 06/Event/Date.cpp +++ b/Sem. 06/Event/Date.cpp @@ -6,12 +6,16 @@ using namespace std; bool Date::isLeapYear() const
{
-if (year <= 1916)
- return year % 4 == 0;
-return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
+ if (year <= 1916)
+ return year % 4 == 0;
+ return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
-
+void Date::validateDateByChangedCalendars()
+{
+ if (year == 1916 && month == 4 && day >= 1 && day <= 13) // invalid days due to change in callendars
+ day = 15;
+}
Date::Date(size_t day, size_t month, size_t year)
{
@@ -40,6 +44,7 @@ void Date::setYear(size_t year) else
MAX_DAYS[1] = 28;
isModified = true;
+ validateDateByChangedCalendars();
}
void Date::setMonth(size_t month)
{
@@ -48,7 +53,12 @@ void Date::setMonth(size_t month) month = 1;
}
this->month = month;
+
+ if (day > MAX_DAYS[month - 1])
+ day = MAX_DAYS[month - 1];
+
isModified = true;
+ validateDateByChangedCalendars();
}
void Date::setDay(size_t day)
{
@@ -56,8 +66,9 @@ void Date::setDay(size_t day) {
day = 1;
}
- isModified = true;
this->day = day;
+ isModified = true;
+ validateDateByChangedCalendars();
}
void Date::print() const
{
@@ -86,7 +97,7 @@ bool Date::areEqual(const Date& other) const return year == other.year && month == other.month && day == other.day;
}
-int Date::getDayOfWeek()
+int Date::getDayOfWeek() const
{
if (!isModified)
diff --git a/Sem. 06/Event/Date.h b/Sem. 06/Event/Date.h index c1c071f..2e28ffa 100644 --- a/Sem. 06/Event/Date.h +++ b/Sem. 06/Event/Date.h @@ -8,10 +8,11 @@ class Date size_t MAX_DAYS[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int dayOfWeek;
- bool isModified = true;
+ mutable int dayOfWeek;
+ mutable bool isModified = true;
bool isLeapYear() const;
+ void validateDateByChangedCalendars();
public:
Date(size_t day, size_t month, size_t year);
@@ -28,6 +29,6 @@ public: bool areEqual(const Date& other) const;
- int getDayOfWeek(); //cant be const, because of changings dayOfWeek and isModified
+ int getDayOfWeek() const; //cant be const, because of changings dayOfWeek and isModified
// can we get around this ??
};
diff --git a/Sem. 06/Event/Event.cpp b/Sem. 06/Event/Event.cpp index 0962843..b69669c 100644 --- a/Sem. 06/Event/Event.cpp +++ b/Sem. 06/Event/Event.cpp @@ -35,7 +35,7 @@ const char* Event::getName() const return name;
}
-int Event::getEventsDayOfWeek()
+int Event::getEventsDayOfWeek() const
{
return date.getDayOfWeek();
}
diff --git a/Sem. 06/Event/Event.h b/Sem. 06/Event/Event.h index d836a9f..c4810f8 100644 --- a/Sem. 06/Event/Event.h +++ b/Sem. 06/Event/Event.h @@ -19,6 +19,6 @@ public: void print() const;
- int getEventsDayOfWeek(); //can't be const (date.getDayOfWeek is not a constant function)
+ int getEventsDayOfWeek() const; //can't be const (date.getDayOfWeek is not a constant function)
// how can we fix it ???
};
\ No newline at end of file |
