Saturday, August 12, 2017

Programming Challenge 13.1 - Date

Example File: Date.h
                         Date.cpp
                         DateDemo.cpp

Date.h


/* Date.h - Specification file for the Date class. */

#ifndef DATE_H
#define DATE_H

#include <array>
#include <string>

class Date
{
    private:       
       
        int month;        // Holding the month
        int day;            // Holding the day 
        int year;        // Holding the year

        static const std::array<int, 13> daysPerMonth;            // Holds number of days per month
        static const std::array<std::string, 13> monthNames;    // Holds month names (JAN -> DEC)

    public:
        static constexpr int LOWEST_YEAR = 1950;                    // The lowest year value accepted
        static constexpr int HIGHEST_YEAR = 2100;                    // The highest year value accepted

        Date();                                                                // Constructor
        Date(int, int, int);                                                // Constructor accepting arguments
        ~Date() {}                                                            // Destructor

        bool setYear(int);
        bool setMonth(int);
        bool setDay(int, int);

        void getFormatOne() const;
        void getFormatTwo() const;
        void getFormatThree() const;

        bool isLeapYear() const;
};
#endif

Date.cpp


/* Date.cpp - Implementation file for the Date class. */

#include <array>
#include <iostream>
#include <string>
#include "Date.h"

// Initializes the daysPerMonth array with the days per month
const std::array<int, 13> Date::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 std::array<std::string, 13> Date::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
    The constructor initializes year to LOWEST_YEAR, month to
    1, and year 1950.
   ********************************************************** */

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

/* **********************************************************
                Date::Date
    The constructor accepts arguments for year, month and day.
   ********************************************************** */

Date::Date(int yyyy, int mm, int dd)
{
    year = yyyy;
    month = mm;
    day = dd;
}

/* **********************************************************
                Date::setYear (accepts year member)
    If the argument passed to the setYear function is greater
    than or equal to LOWEST_YEAR and less than or equal to
    HIGHEST_YEAR, it is copied into the 'year' member, and
    true is returned. If it is not, the value of year remains
    unchanged and false is returned.
   ********************************************************** */

bool Date::setYear(int yyyy)
{
    if (yyyy >= LOWEST_YEAR && yyyy <= HIGHEST_YEAR)
    {
        year = yyyy;
        return true;
    }
    else
    {
        std::cout << "\nYou entered an invalid year.\n";
    }

    return false;
}

/* **********************************************************
                Date::setMonth (accepts month member)
    If the argument passed to the setMonth function is greater
    than or equal to JANUARY and lower than or equal to
    DECEMBER, it is copied into the member variable month and
    true is returned. If it is not, the value of day remains
    unchanged and false is returned.
   ********************************************************** */

bool Date::setMonth(int mm)
{
    if (mm >= JANUARY && mm <= DECEMBER)
    {
        month = mm;
        return true;
    }
    else
    {
        std::cout << "\nYou entered an invalid month\n";
    }

    return false;
}

/* **********************************************************
                Date::setDay (accepts day and month members)
    If the argument passed to the setDay function is greater
    than 1 and less than or equal to daysPerMonth (ex: April
    has 30 days), it is copied into the member variable day
    and true is returned. If it is a leap year, determined by
    the appropriate function, days receives a copy of 29 and
    true is returned. If neither of these conditions are met,
    the value of day remains unchanged and false is returned.
   ********************************************************** */

bool Date::setDay(int dd, int mm)
{   
    if (dd >= 1 && dd <= daysPerMonth[mm])
    {
        day = dd;
        return true;
    }       
    else if (dd == 29 && mm == FEBRUARY && isLeapYear())
    {
        day = daysPerMonth[mm] + 1;
        return true;
    }
    else if (dd > daysPerMonth[mm])
    {
        std::cout << "\nYou entered an invalid day.\n"
                      << monthNames[mm] << " only has "
                      << daysPerMonth[mm] << " days\n";
    }
    else
    {
        std::cout << "\nYou entered an invalid day.\n";
    }

    return false;
}

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

bool Date::isLeapYear() const
{
    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
{
    std::cout << "\nSlash Style:\n";
    std::cout << month << "/" << day << "/" << year;
}

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

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

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

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

DateDemo.cpp 


/* Date Demo - This program demonstrates the Date class. It allows the
    user to enter a date. If the date entered is valid, it is displayed
    in three different formats:
   
        * 1/1/1950
        * JANUARY 1, 1950
        * 1 JANUARY, 1950 */

#include <iostream>
#include "Date.h"

using std::cin;
using std::cout;

int main()
{
    int month = 0;
    int day = 0;
    int year = 0;

    // Declare a Date object
    Date date;

    cout << "DATE CLASS 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";

    // Get a year, month and day value from the user
    cout << "Enter a year [" << date.LOWEST_YEAR
          << "-" << date.HIGHEST_YEAR << "]: ";
    cin >> year;

    while (!date.setYear(year))            // Evaluates the year input
    {
        cout << "Enter a year: ";
        cin >> year;
    }

    cout << "Enter a month: ";
    cin >> month;

    while (!date.setMonth(month))            // Evaluates the month input
    {
        cout << "Enter a month: ";
        cin >> month;
    }

    cout << "Enter a day: ";
    cin >> day;

    while (!date.setDay(day, month))        // Evaluates the day input
    {
        cout << "Enter a day: ";
        cin >> day;
    }

    // Display the date in three formats
    date.getFormatOne();
    date.getFormatTwo();
    date.getFormatThree();

    cout << "\n\nThank you for trying the Date class demo. Have a nice day!";

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

Example Output:






No comments:

Post a Comment