diff --git a/feladat4/feladat4.slnx b/feladat4/feladat4.slnx index 17bc585..a4a5456 100644 --- a/feladat4/feladat4.slnx +++ b/feladat4/feladat4.slnx @@ -3,6 +3,6 @@ - + diff --git a/feladat4/feladat4/cmydate.cpp b/feladat4/feladat4/cmydate.cpp index 1e9a73e..ee4eadf 100644 --- a/feladat4/feladat4/cmydate.cpp +++ b/feladat4/feladat4/cmydate.cpp @@ -4,41 +4,35 @@ #include "cmydate.h" using namespace std; -static bool IsLeapYear(int year); - -CMyDate::CMyDate() { - m_iYear = 1970; - m_iMonth = 1; - m_iDay = 1; -} -CMyDate::CMyDate(int year, int month, int day) { - if (!IsValid(year, month, day)) +static bool IsLeapYear(int year) { + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { - throw new exception; // rewrite to correct exception + return true; } - m_iYear = year; - m_iMonth = month; - m_iDay = day; + return false; } -CMyDate::~CMyDate(){ - //hey jimmy, get me a desctructor with nothing -} -void CMyDate::Display() const { -} -int CMyDate::DayOfTheYear() const { -} -bool CMyDate::IsValid(int year, int month, int day) const { +static bool IsValidYear(int year) { if (year < 1970) { - return false; + throw new CMyDateException(CMyDateException::ErrMinYear); } - if (month < 1 || month > 12) + return true; +} +static bool IsValidMonth(int month) { + if (month < 1) { - return false; + throw new CMyDateException(CMyDateException::ErrMinMonth); } + if (month > 12) + { + throw new CMyDateException(CMyDateException::ErrMaxMonth); + } + return true; +} +static bool IsValidDay(int year, int month, int day) { if (day < 1) { - return false; + throw new CMyDateException(CMyDateException::ErrMinDay); } switch (month) { @@ -51,7 +45,7 @@ bool CMyDate::IsValid(int year, int month, int day) const { case 12: if (day > 31) { - return false; + throw new CMyDateException(CMyDateException::ErrMaxDay); } break; case 4: @@ -60,18 +54,96 @@ bool CMyDate::IsValid(int year, int month, int day) const { case 11: if (day > 30) { - return false; + throw new CMyDateException(CMyDateException::ErrMaxDay); } break; case 2: if (day > 29 || (day > 28 && !IsLeapYear(year))) { - return false; + throw new CMyDateException(CMyDateException::ErrMaxDay); } break; default: + throw new CMyDateException(CMyDateException::ErrUnexpected); break; } +} + +CMyDate::CMyDate() { + m_iYear = 1970; + m_iMonth = 1; + m_iDay = 1; +} +CMyDate::CMyDate(int year, int month, int day) { + IsValid(year, month, day); + SetYear(year); + SetMonth(month); + SetDay(day); +} +CMyDate::~CMyDate(){ + //hey jimmy, get me a destructor with nothing +} +void CMyDate::Display() const { + char monthName[11] = {}; + switch (m_iMonth) + { + case 1: + strncpy(monthName, "Január", 7); + break; + case 2: + strncpy(monthName, "Február", 8); + break; + case 3: + strncpy(monthName, "Március", 8); + break; + case 4: + strncpy(monthName, "Április", 9); + break; + case 5: + strncpy(monthName, "Május", 6); + break; + case 6: + strncpy(monthName, "Június", 7); + break; + case 7: + strncpy(monthName, "Július", 7); + break; + case 8: + strncpy(monthName, "Augusztus", 10); + break; + case 9: + strncpy(monthName, "Szeptember", 11); + break; + case 10: + strncpy(monthName, "Október", 8); + break; + case 11: + strncpy(monthName, "November", 9); + break; + case 12: + strncpy(monthName, "December", 9); + break; + default: + throw new CMyDateException(CMyDateException::ErrUnexpected); + break; + } + cout << m_iYear << " " << monthName << " " << m_iDay << "."; +} +int CMyDate::DayOfTheYear() const { + int d = 30.4375 * (m_iMonth - 1); //avg day in a month + return d + m_iDay - (int)IsLeapYear(m_iYear); +} +bool CMyDate::IsValid(int year, int month, int day) const { + try + { + IsValidYear(year); + IsValidMonth(month); + IsValidDay(year, month, day); + } + catch (const CMyDateException& e) + { + + } return true; } void CMyDate::AddDays(int days) { @@ -92,28 +164,20 @@ int CMyDate::GetYear() const { return m_iYear; } void CMyDate::SetDay(int day) { - if (IsValid(m_iYear, m_iMonth, day)) + if (IsValidDay(m_iYear, m_iMonth, day)) { m_iDay = day; } } void CMyDate::SetMonth(int month) { - if (IsValid(m_iYear, month, m_iDay)) + if (IsValidMonth(month)) { m_iMonth = month; } } void CMyDate::SetYear(int year) { - if (IsValid(year, m_iMonth, m_iDay)) + if (IsValidYear(year)) { m_iYear = year; } } - -static bool IsLeapYear(int year) { - if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) - { - return true; - } - return false; -} \ No newline at end of file diff --git a/feladat4/feladat4/cmydateexception.cpp b/feladat4/feladat4/cmydateexception.cpp index 51e8d13..6755035 100644 --- a/feladat4/feladat4/cmydateexception.cpp +++ b/feladat4/feladat4/cmydateexception.cpp @@ -1 +1,8 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 \ No newline at end of file +//Tóth Dániel Mátyás | IFPQYY | V1 +#include "cmydateexception.h" +#include + +CMyDateException::CMyDateException(const char* s) +{ + strncpy(msg, s, sizeof(s)); +} diff --git a/feladat4/feladat4/cmydateexception.h b/feladat4/feladat4/cmydateexception.h index 0b3d20a..29c1739 100644 --- a/feladat4/feladat4/cmydateexception.h +++ b/feladat4/feladat4/cmydateexception.h @@ -1,2 +1,14 @@ //Tóth Dániel Mátyás | IFPQYY | V1 #pragma once +class CMyDateException { +public: + CMyDateException(const char* s); + static constexpr const char* ErrMinDay = "Hiba: nap kisebb, mint 1!"; + static constexpr const char* ErrMaxDay = "Hiba: nap nagyobb, mint az adott hónap napszáma!"; + static constexpr const char* ErrMinMonth = "Hiba: hónap kisebb, mint 1!"; + static constexpr const char* ErrMaxMonth = "Hiba: hónap nagyobb, mint 12!"; + static constexpr const char* ErrMinYear = "Hiba: év kisebb, mint 1970!"; + static constexpr const char* ErrUnexpected = "Váratlan hiba"; +private: + char* msg; +}; \ No newline at end of file