Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Project #5 Date Design a class called Date. The class should store a

ID: 3671571 • Letter: P

Question

Programming Project #5 Date Design a class called Date. The class should store a specific date in three integers: month, day, and year. There should be member functions to print the date in the following four forms, if it is valid: 12/25/2012 (US) December 25, 2012 (US expanded) 25 December 2012 (US Military) 2012-12-25 (International) There should be member functions to get and/or set value of month, day, and year. There should be member functions to check for invalid date entered, such as 4/31/2012, or 2/29/2011, which will be rejected with an error message, and the program should ask for another date entered. Write a complete C++ program to demonstrate the class. Allow your program to run continuously as long as the user wishes. Input validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1. Do not accept any invalid date. If it is, display the message “The date is invalid”. Required output (should be similar with the sample run) The program displays a valid date in three different formats. Note: All junk data will be rejected! Enter a date (mm/dd/yyyy) or -1 to end: 2/29/2008 Invalid date/ wrong format: For both month and day, use two digits. Re-Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2009 2009 -> NOT Leap Year! 29 is not a valid day of Febraury Error!!! The entered date is invalid! Re-Enter, Please! Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2008 2008 -> Leap Year! Date: 2/29/2008 is valid. 2/29/2008 (US). Febraury 29, 2008 (US Expanded). 29 Febraury 2008 (US Military). 2008-02-29 (International). Run again (y/n)? y The program displays a valid date in three different formats.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

class Date {
private:
   string MONTHS[12] = {
       "January", "February", "March", "April", "May", "June",
       "July", "August", "September", "October", "November", "December"
   };
   int month;
   int year;
   int day;

   string getMonthString(int month) {
       return MONTHS[month];
   }

public:

   int getMonth() {
       return month;
   }

   void setMonth(int month) {
       this->month = month;
   }

   int getDay() {
       return day;
   }

   void setDay(int day) {
       this->day = day;
   }

   int getYear() {
       return year;
   }

   void setYear(int year) {
       this->year = year;
   }

   bool isLeapYear(int year){
       return (!(year % 4) && (year % 100) || !(year % 400));
   }

   bool isValid(int year, int month, int day) {
       int monthlen[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

       if (year <= 0 || month < 1 || month > 12 || day < 1 || day > 31) {
           cout << "Invalid input!" << endl;
           return false;
       }

       if (isLeapYear(year)) {
           cout << "Its a leap year!" << endl;
           if (month == 2) {
               monthlen[1]++;
           }
       }

       if (day > monthlen[month - 1]) {
           cout << "Invalid number of days!" << endl;
           return false;
       }

       return true;
   }

   void printUSFormat() {
       cout << month << "/" << day << "/" << year;
   }

   void printUSExpandedFormat() {
       cout << getMonthString(month) << " " << day << ", " << year;
   }

   void printUSMilitaryFormat() {
       cout << day << " " << getMonthString(month) << " " << year;
   }

   void printInternationalFormat() {
       cout << year << "-" << month << "-" << day;
   }
};

int main() {

   bool keepRunning = true;
   while (keepRunning) {

       // prompt user for input
       string input;
       cout << "Enter a date (mm/dd/yyyy) or -1 to end: " << endl;
       cin >> input;

       // check if user wants to exit
       if (input.size() == 2 && input[0] == '-' && input[1] == '1') {
           keepRunning = false;
           continue;
       }

       // check for length
       if (input.size() != 10) {
           cout << "Invalid date/ wrong format: For both month and day, use two digits." << endl;
           continue;
       }

       // convert string input to integers
       int month = (input[0] - '0') * 10 + (input[1] - '0');
       int day = (input[3] - '0') * 10 + (input[4] - '0');
       int year = (input[6] - '0') * 1000 + (input[7] - '0') * 100 + (input[8] - '0') * 10 + (input[9] - '0');

       // check for validity of date
       Date myDate;
       if (!myDate.isValid(year, month, day)) {
           if (!myDate.isLeapYear(year) && month == 2 && day > 28) {
               cout << "NOT a leape year " << day << " is not a valid date of February" << endl;
           }
           cout << "The entered date is invalid! Re-Enter Please!" << endl;
       } else {
           myDate.setYear(year);
           myDate.setMonth(month);
           myDate.setDay(day);

           cout << "Date: " << input << " is valid." << endl;
           myDate.printUSFormat(); cout << " (US) ";
           myDate.printUSExpandedFormat(); cout << " (US Extpanded) ";
           myDate.printUSMilitaryFormat(); cout << " (US Military) ";
           myDate.printInternationalFormat(); cout << " (International) ";
       }
   }

   return 0;
}