Sunday, February 18, 2018

Programming Challenge 16.1 - Date Exceptions

Example Files: DateExceptions.7z

Date.h


#ifndef DATE_H
#define DATE_H

#include <string>

class Date
{
    private:       
        int month;   
        int day;           
        int year;       

    public:       
        // Exception class
        class InvalidDay
        {
            private:
                int invalidDay;

            public:
                InvalidDay(int dayVal)
                { invalidDay = dayVal; }

                int getInvalidDay() const
                { return invalidDay; }
        };

        // Exception class
        class InvalidMonth
        {
            private:
                int invalidMonth;

            public:
                InvalidMonth(int monthVal)
                { invalidMonth = monthVal; }

                int getInvalidMonth() const
                { return invalidMonth; }
        };
       
        Date();

        // Mutator functions
        void setYear(int);
        bool isLeapYear();
        void setMonth(int);
        void setDay(int);

        // Accessor functions
        void getFormatOne() const;
        void getFormatTwo() const;
        void getFormatThree() const;
};

#endif

Date.cpp


#include "Date.h"

#include <array>
using std::array;

#include <iostream>
using std::cout;

#include <iostream>
using std::string;

// Initializes the daysPerMonth array with the days per month
const array<int, 13> daysPerMonth { 0, 31, 28, 31, 30, 31, 30,
                                    31, 31, 30, 31, 30, 31 };

// Initializes the monthNames array with the names of the months of the year
const array<string, 13> monthNames { "", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
                                                 "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };

enum Months { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE,
              JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };

/* **********************************************************
                Date::Date() - Constructor
   ********************************************************** */

Date::Date()
{
    year = 1900;
    month = JANUARY;
    day = 1;
}

/* **********************************************************
            Date::setYear() : int
   This functions sets the value of the year member variable.
    If it is invalid, the year member variable is assigned the
    value 1900.
   ********************************************************** */

void Date::setYear(int yyyy)
{
    if (yyyy >= 0 && yyyy <= 2200)
    {
        year = yyyy;
    }
     else
     {
         year = 1900;
     }
}

/* **********************************************************
            Date::setMonth() : int mm
   This function sets the value of the member variable month.
    InvalidMonth is thrown when an invalid value is passed.
   ********************************************************** */

void Date::setMonth(int mm)
{
    if (mm >= JANUARY && mm <= DECEMBER)
    {
        month = mm;
    }
    else
    {
         throw InvalidMonth(mm);
    }
}

/* **********************************************************
            Date::setDay() : int
    This function sets the value of the member variable day.
    InvalidDay is thrown if an invalid value is passed.
   ********************************************************** */

void Date::setDay(int dd)
{  
    if (month == FEBRUARY && isLeapYear() && dd == 29)
    {
        day = dd;
    }
    else if (dd < 1 || dd > daysPerMonth[month])
    {
        throw InvalidDay(dd);
    }
    else
    {
        day = dd;
    }
}

/* **********************************************************
                Date::isLeapYear (void)
    This function determines whether a year is a leap year. If
    it is true is returned, else false is returned.
   ********************************************************** */

bool Date::isLeapYear()
{
    if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
    {
        return true;
    }

    return false;
}

/* **********************************************************
                Date::displayFormatOne (void)
    This function formats the date as 12/12/2012 and returns
    it.
   ********************************************************** */

void Date::getFormatOne() const
{
    cout << "\nSlash Style:\n";
    cout << month << "/" << day << "/" << year << "\n";
}

/* **********************************************************
                Date::displayFormatTwo (void)
    This function formats the date as DECEMBER 12, 2012 and
    returns it.
   ********************************************************** */

void Date::getFormatTwo() const
{
    cout << "\nU.S. Style:\n";
    cout << monthNames[month] << " " << day << ", " << year << "\n";
}

/* **********************************************************
                Date::displayFormatThree (void)
    This function formats the date as 12 DECEMBER, 2012 and
    returns it.
   ********************************************************** */

void Date::getFormatThree() const
{
    cout << "\nEuropean Style:\n";
    cout << day << " " << monthNames[month] << " " << year << "\n\n";
}

DateDm.cpp


#include "Date.h"

#include <iostream>
using std::cin;
using std::cout;

int main()
{
   int  month = 0;
   int  day = 0;
   int  year = 0;
    bool tryAgain = true;

    Date dateObj;

   cout << "DATE EXCEPTION DEMO\n\n"
        << "This program lets you enter a date. If it is a valid date,\n"
        << "it is displayed in three formats.\n\n";
    cout << "Please enter a year, a month, and a day (Ex: 2008 02 29): ";
    cin >> year >> month >> day;

    dateObj.setYear(year);

    while (tryAgain)
    {
        try
        {       
            dateObj.setMonth(month);
            dateObj.setDay(day);

            cout << "\nThis is your date in three different formats:\n";
            dateObj.getFormatOne();
            dateObj.getFormatTwo();
            dateObj.getFormatThree();

            tryAgain = false;
        }
        catch (Date::InvalidDay dayVal)
        {
            cout << "\nError: Day " << dayVal.getInvalidDay() << " is invalid.\n";
            cout << "Please enter a valid day: ";
            cin >> day;
        }
        catch (Date::InvalidMonth monthVal)
        {
            cout << "\nError: Month " << monthVal.getInvalidMonth() << " does not exist.\n";
            cout << "Please enter a valid month: ";
            cin >> month;
        }
    }

    cout << "Thank you for trying the Date Exception Demo! Have a nice day!";

    cin.ignore();
   cin.get();
    return 0;
}

Example Output:




No comments:

Post a Comment