CECS 282
LAB ASSIGNMENT 7
Assigned date: 10/26
Due date: 11/2
30 points

Problem 1 - Tuesday 10/31

Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example "see an advisor") and a date and a time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether a day of the month matches. Then fill a vector of Appointment* with a mixtures of appointments. Have user enter a date and printout all appointments that happens on that date.

#include <iostream>
#include <string>
#include <ctime>
#include <vector>

using namespace std;

const int DAYS_PER_MONTH = 30;
/**
A class that describes a time of day
(between 00:00:00 and 23:59:59)
*/
class Time
{
public:
/**
Constructs a time of day.
@param hour the hours
@param min the minutes
@param sec the seconds
*/
Time(int hour, int min, int sec);
/**
Constructs a Time object that is set to
the time at which the constructor executes.
*/
Time();

/**
Gets the hours of this time.
@return the hours
*/
int get_hours() const;
/**
Gets the minutes of this time.
@return the minutes
*/
int get_minutes() const;
/**
Gets the seconds of this time.
@return the seconds
*/
int get_seconds() const;

/**
Computes the seconds between this time and another.
@param t the other time
@return the number of seconds between this time and t
*/
int seconds_from(Time t) const;
/**
Adds a number of seconds to this time.
@param s the number of seconds to add
*/
void add_seconds(int s);

private:
int time_in_secs;
};

/**
Computes the correct remainder for negative dividend
@param a - an integer
@param n - an integer > 0
@return the mathematically correct remainder r such that
a - r is divisible by n and 0 <= r and r < n
*/
int remainder(int a, int n)
{
if (a >= 0)
{
return a % n;
}
else
{
return n - 1 - (-a - 1) % n;
}
}

Time::Time(int hour, int min, int sec)
{
time_in_secs = 60L * 60 * hour + 60 * min + sec;
}

Time::Time()
{
time_in_secs = 0;
}

int Time::get_hours() const
{
return time_in_secs / (60 * 60);
}

int Time::get_minutes() const
{
return (time_in_secs / 60) % 60;
}

int Time::get_seconds() const
{
return time_in_secs % 60;
}

int Time::seconds_from(Time t) const
{
return time_in_secs - t.time_in_secs;
}

void Time::add_seconds(int s)
{
const int SECONDS_PER_DAY = 60 * 60 * 24;
time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY);
}

//..................................................................

class Date
{
public:
Date();
Date(int y, int m, int d);
void print() const;
bool equals(Date other) const;
private:
int day;
int month;
int year;
};

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

Date::Date(int y, int m, int d)
{
day = d;
month = m;
year = y;
}

void Date::print() const
{
cout << year << "/" << month << "/" << day;
}

bool Date::equals(Date other) const
{
return day == other.day && month == other.month
&& year == other.year;
}

//..................................................................

int main()
{
vector<Appointment*> schedule(3);
schedule[0]= new Onetime("see the dentist", Date(1998, 9, 4),
Time(11, 30, 0), Time(12, 30, 0));
schedule[1]= new Daily("brush my teeth", Time(8,0,0),
Time(8, 5, 0));
schedule[2]= new Monthly("clean the house", 4,
Time(14,0,0), Time(16, 0, 0));

cout << "Enter year month day: ";
int year;
int month;
int day;
cin >> year >> month >> day;

cout << "You have these appointments: " << "\n";
for (int i = 0; i < schedule.size(); i++)
{
if (schedule[i]->occurs_on(year, month, day))
{
schedule[i]->print();
}
}
return 0;
}

Problem 2 - Thursday 11/2

Improve the appointment book program in problem 1. Give the user the option to add new appointments. The user must specify the type of the appointment, the descriptoin, and the date and the time.

Grading