$25
Calendar Events
Learning Objectives:
This assignment is designed to provide practice with the following:
v Working with Classes
v Working with Composition “has-a” relationship between classes
v Working with vectors
v Formatting output
v Working with multiple files
v Command-line arguments
v Reading data from files
v Sorting data
Overview:
You are going to read in various appointments from an input file. You will store the appointment information in a vector. You will sort and print out the appointment information. You will write three classes for this assignment as well as several helper functions that work with the three classes.
Classes:
You will have a CalendarEvent, Date, and Time class. In this document, I will give you the class declaration for each of the three classes. You will need to provide both .cpp and .hpp files for each class. You will also provide a functions.cpp and functions.hpp.
This assignment will give you practice using composition. Composition is realized when one class “hasan” instance of another class as one of the data members. The CalendarEvent class will have an instance of Date and an instance of Time as one of the data members. This document will provide the .hpp files. (CalendarEven.hpp, Date.hpp, Time.hpp, and functions.hpp). You are required to implement the class functions as well as the helper functions in the .cpp files. (CalendarEvent.cpp, Data.cpp, Time.cpp, and functions.cpp). You will also provide a driver.cpp. The driver.cpp file should have minimal amount of code in it.
CalendarEvent:
The CalendarEvent class has three private data members: a string for the name of the calendar event, an instance of Date for the date of the event, and an instance for the time of the event.
class CalendarEvent
{
private:
string eventName;
Time calTime; Date calDate;
public:
CalendarEvent(int month, int day, int year, int hour, int minute, string eventName); void printCalendar(fstream& out); bool isEventDateValid(); bool isEventTimeValid(); string getEvent()const; };
Notice there is no default constructor for the CalendarEvent class. You will need to change the provided CalendarEvent constructor so that it will provide the default values in the event an instance of CalendarEvent is created using a default constructor.
void printCalendar(fstream& out) - This function will be used to print the calendar event. It will need to print the string description of the event (eventName), it will then call the Date’s print function using the instance of the Date class (calDate), it will also use the instance the Time class (calTime) to call the Time’s print function. Notice the parameter for this function is “fstream rather than ifstream or ofstream”. Fstream allows you to specify if the file pointer being passed to a function is for input or output. This will become clearer when we go over the functions in the function.hpp file.
bool isEventDateValid() – This function is going to use the instance Date to call the isDateValid function. IsDateValid() will return true or false which will then be returned by isEvenDateValid().
bool isEventTimeValid() - This function is going to use the instance of Time to call the isTimeValid function. IsTimeValid() will return true or false which will then be returned by isEventTimeValid(). string getEvent() – This needs no explanation.
The date class has three private data members: int month, int day, and int year, each of which are selfexplanatory. It also has a public static data member that is an array of strings that represent the string value of each month.
class Date
{
private: int month; int day;
int year;
public:
Date(int month, int day, int year); void setMonth(int); void setDay(int); void setYear(int); void setDate(int, int, int); string getStrMonth(); int getMonth()const; int getDay()const; int getYear()const; void printDate(fstream&);
bool isDateValid(); bool isLeapYear();
static const string ARRAY[13];
};
Again, the Date class only has one constructor. You will need to change the provided Date constructor such that it will provide the default values in the event an instance of Date is created using a default constructor.
The regular setters and getters need no explanation, so I will not spend time on them. Other than to say you may not need this in this project but for practice I AM REQUIRING YOU TO WRITE THEM. The function getStrMonth will be used to return the string version of the month. The input document will provide the string in integer form. When printing the variable, you are required to print it in the string form.
void printDate(fstream& out) – This function is used to print the date portion of the calendar event.
bool isDateValid() – This function will be used to ensure the date is a valid date. Such as, making sure the months are with in 1 – 12, the years are between 1900 – 2020 and the day is between 1 and 31. If the month is February the day can only be 29 if it is leap year. Therefore, you will need to check this. You should also make sure the day given did not exceed the number of days for that month. If the function fells any of the validity checks you are to return false.
bool isLeapYear() – This function will determine if the year read in is a leap year. Look up the rules for determining leap year.
Time:
The time class has two data members, each of which are integers. They represent the hours and minutes of time.
class Time
{
private:
int hour; int minute; public:
Time(int hours,int mins); void setHour(int); void setMinute(int); void setTime(int, int); int getHour()const; int getMinute()const;
void printTime(fstream&)const; bool isTimeValid();
};
Just as with the previous classes discussed, you must modify the Time constructor so that default values are provided through this constructor as well as the regular constructor. The setters and getters are normal, again, you may not use them however, you are required to implement them for practice.
void printTime(fstream& out) prints the information pertaining to time. When printing the time you must convert hours from military time to regular time. The format of the printing hours:minutes 10:00 pm or am. If it is noon you are to print 12:00 noon.
bool isTimeValid() checks that the hours and minutes are between 0 and 24 and minutes are between 0 and 59. functions:
This file provides three functions, readData, checkArguments, and isOpen.
void readData(fstream&, vector<CalendarEvent>&, vector<CalendarEvent>&); void checkArguments(int argc); void isOpen(fstream&, char*);
void readData(fstream& f, vector<CalendarEvent>& good, vector<CalendarEvent>& bad) reads the information from the data file. You should read the information one set of data at a time. Once you have read a complete set of data (eventName, date, and time) you should create a temporary instance of CalendarEvent. You should then test the data by calling isEventDateValid() and isEventTimeValid() to check the validity of the date and time information. If both of these functions return true then store the calendarEvent on to the good vector otherwise store the calendarEvent on the bad vector. Depending on how you write your readData you may want to do a little research on the various ways to call ignore. We discussed this function earlier in the semester and I mentioned they there are various ways to call the ignore function. Knowing these different ways may be helpful to you.
void checkArguments(int argc) – this function is used to check the number of arguments passed on the command line. You will have 4 arugments: ./executable inputFile goodOutputFile badOutputFile As stated above if the data is not valid you are to store that calendar event on the bad vector otherwise store the data on the good vector. When printing the information, you will print the good vector to the goodOutputFile. If the information to be printed is in the bad vector that information will be printed to the badOutputFile. If the correct number of files were not passed to the command line argument you should print a message that says “Not enough command line arguments” and exit the program.
void isOpen(fstream&, char*) – this function is used to check if the file opened successfully. This function has two arguments the first is fstream. We learned that input files are usually ifstream, hence “i” for input and ofstream “o” for output. Fstream can be used for either, however when opening the file you must specify if the file is being opened for input or output. The following are links to help you with this.
http://www.cplusplus.com/doc/tutorial/files/ https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
If the file did not open correctly you should print a message that tells the user which file did not open, then exit the program. Ex. goodOutput.txt did not open!