Completed 'date' program of Assignment 5.
This commit is contained in:
parent
0ab7384af2
commit
d0c2763a23
2 changed files with 272 additions and 3 deletions
|
|
@ -21,13 +21,240 @@ Dependencies: ???
|
|||
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:
|
||||
/* data */
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
public:
|
||||
Date(/* args */);
|
||||
~Date();
|
||||
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
|
||||
|
|
@ -35,6 +262,48 @@ public:
|
|||
// 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue