/* Program Name: Date (exception handling) Process: ??? Function Input/Parameters: ??? Function Output/Parameters: ??? Function Output/Returned: ??? Device Input/Device: ??? Device Output/Monitor: ??? Dependencies: ??? */ #include #include #include #include #include #include #include #include using namespace std; // class definitions class InvalidDay : public exception { public: virtual const char *what() const noexcept { return "InvalidDay: Day should be between 1 and 31 (integers)."; } }; class InvalidMonth : public exception { public: virtual const char *what() const noexcept { return "InvalidMonth: Month should be between 1 and 12 (integers)."; } }; class Date { private: int month; int day; int year; public: Date() { month = 1; day = 1; year = 1970; } Date(int inMonth, int inDay, int inYear) { try { if(inMonth > 1 || inMonth < 12) { month = inMonth; } else { throw InvalidMonth(); } } catch(const InvalidMonth& e) { std::cerr << e.what() << '\n'; } // next try statement try { if(inDay > 1 || inDay < 31) { day = inDay; } else { throw InvalidDay(); } } catch(const InvalidDay& e) { std::cerr << e.what() << '\n'; } year = inYear; } ~Date() { // } // accessor functions int getDay() { return day; } int getMonth() { return month; } int getYear() { return year; } // mutator functions void setDay(int inputDay) { try { if (inputDay > 1 || inputDay < 31) { day = inputDay; } else { throw InvalidDay(); } } catch (const InvalidDay &e) { std::cerr << e.what() << '\n'; } } void setMonth(int inputMonth) { try { if (inputMonth > 1 || inputMonth < 12) { month = inputMonth; } else { throw InvalidMonth(); } } catch (const InvalidMonth &e) { std::cerr << e.what() << '\n'; } } void setYear(int inputYear) { year = inputYear; } // date printing functions void printNumericalMonthDayYear() { cout << month << "/" << day << "/" << year << endl; } void printStringMonthNumericalDayYear() { switch (month) { case 1: cout << "January"; break; case 2: cout << "February"; break; case 3: cout << "March"; break; case 4: cout << "April"; break; case 5: cout << "May"; break; case 6: cout << "June"; break; case 7: cout << "July"; break; case 8: cout << "August"; break; case 9: cout << "September"; break; case 10: cout << "October"; break; case 11: cout << "November"; break; case 12: cout << "December"; break; default: break; } cout << " " << day << ", " << year << endl; } void printNumericalDayStringMonthYear() { cout << day << " "; switch (month) { case 1: cout << "January"; break; case 2: cout << "February"; break; case 3: cout << "March"; break; case 4: cout << "April"; break; case 5: cout << "May"; break; case 6: cout << "June"; break; case 7: cout << "July"; break; case 8: cout << "August"; break; case 9: cout << "September"; break; case 10: cout << "October"; break; case 11: cout << "November"; break; case 12: cout << "December"; break; default: break; } cout << " " << year << endl; } // shorthand print functions void printNMDY() { printNumericalMonthDayYear(); } void printSMNDY() { printStringMonthNumericalDayYear(); } void printNDSMY() { printNumericalDayStringMonthYear(); } }; // function prototypes // main program int main(int argc, char **argv) { int tempDay, tempMonth, tempYear; Date linuxEpoch = Date(); Date steveBirthday = Date(2, 24, 1955); Date christmasThisYear = Date(12, 25, 2024); Date emptyDate = Date(); // values will be set be accessor/mutators // ask the user for the values for emptyDate. cout << "Start date entry:" << endl; cout << "\tPlease enter in a month: "; cin >> tempDay; cout << "\tPlease enter in a day: "; cin >> tempMonth; cout << "\tPlease enter in a year: "; cin >> tempYear; // finish the user entry. cout << endl; // attempt to enter in the date into empty date. emptyDate.setDay(tempDay); emptyDate.setMonth(tempMonth); emptyDate.setYear(tempYear); // print out each date in different formats. cout << "Date of Linux Epoch: "; linuxEpoch.printNumericalDayStringMonthYear(); cout << endl; cout << "Date of Steve Job's Birthday: "; steveBirthday.printNumericalMonthDayYear(); cout << endl; cout << "Date of Christmas this year: "; christmasThisYear.printStringMonthNumericalDayYear(); cout << endl; cout << "User inputted date in all formats: " << endl; emptyDate.printNumericalDayStringMonthYear(); emptyDate.printNumericalMonthDayYear(); emptyDate.printStringMonthNumericalDayYear(); cout << endl; // return 0 return 0; }