Question: From the Appointment class you developed in the previous assignment, derive two classes: ClientSession and CourtSession (instead of Session class). The ClientSession will have all
From the Appointment class you developed in the previous assignment, derive two classes: ClientSession and CourtSession (instead of Session class). The ClientSession will have all the data and functions of Session (as in previous assignment). CourtSession will also have courtName and location (both c-strings), in addition to the Session previously done. They must also have the same functions as before, with get and print functions of CourtSession taking the additional information into account.
In main, create an array of Appointment pointers and ask the user repeatedly if they want to enter a ClientSession or CourSession. In either case, create the requested session, read the required information, save the pointer in the array of Appointment pointers, and when done, list all created sessions, printing all their information. Also, print the calculated charge for each session.
For example:
It might show:
Client ID, client name, date, time
Hours spent, cost per hour, total charges
Client ID, client name, Court name, Court location, date, time
Hours spent, cost per hour, total charges
Client ID, client name, date, time
Hours spent, cost per hour, total charges
Client ID, client name, Court name, Court location, date, time
Hours spent, cost per hour, total charges
Post your .h and .cpp files separately without zipping them.
Session.h
#ifndef Session_h
#define Session_h
#include
#include
#include "MemberFunctions.h"
using namespace std;
class Session : public Appointment
{
private:
char client_id[40], fname[40], lname[40];
int hourly_wage;
public:
Session();
void get();
void print() const;
char *get_id();
char *get_lname();
char *get_fname();
int calc_charge();
};
bool conflict(Appointment *, Appointment *[], int, int&);
int select_appt(Appointment *[], int);
enum classType { appt, session };
#endif
Session.cpp
#include
#include
#include
#include "MemberFunctions.h"
using namespace std;
char* Session::get_id() // Finds ID of searched client
{
return client_id;
}
char* Session::get_lname() // Finds last name of searched client
{
return lname;
}
char* Session::get_fname() // Finds first name of searched client
{
return fname;
}
int Session::calc_charge() //Calculates total charges
{
return (end_time-start_time) * hourly_wage;
}
Session::Session() : Appointment() // Constructor
{
strcpy(client_id, "");
strcpy(fname, "");
strcpy(lname, "");
}
void Session::print() const //prints out client data
{
cout << " Client ID: " << client_id;
cout << " First Name: " << fname;
cout << " Last Name: " << lname;
cout << " Description: " << description;
cout << " Location: " << location;
cout << "Sessiont Date: ";
date.print();
cout << " Start time: ";
start_time.print();
cout << " End time: ";
end_time.print();
}
void Session::get() //gets input from client
{
char c;
date.get();
do
{
cout << "Start time - ";
start_time.get();
cin.ignore(20, ' ');
cout << "End time - ";
end_time.get();
cin.ignore(20, ' ');
if (start_time.get_hour() > end_time.get_hour())
cout << " End time cannot be earlier than start time" << endl;
} while (start_time.get_hour() > end_time.get_hour());
cout << "Enter Client ID: ";
i = 0;
cin.get(c);
while (c != ' ')
{
client_id[i++] = c;
cin.get(c);
}
client_id[i] = '\0';
cout << "Enter First Name: ";
i = 0;
cin.get(c);
while (c != ' ')
{
fname[i++] = c;
cin.get(c);
}
fname[i] = '\0';
cout << "Enter Last Name: ";
i = 0;
cin.get(c);
while (c != ' ')
{
lname[i++] = c;
cin.get(c);
}
lname[i] = '\0';
cout << "Enter description: ";
int i = 0;
cin.get(c);
while (c != ' ')
{
description[i++] = c;
cin.get(c);
}
description[i] = '\0';
cout << "Enter location: ";
i = 0;
cin.get(c);
while (c != ' ')
{
location[i++] = c;
cin.get(c);
}
location[i] = '\0';
}
int select_appt(Appointment *ap[], int size) // Searches for appointmnet or session returns -1 if index of the array was not found
{
Date date;
int choice, int_array[10];
cout << " Enter appointment or session date: ";
date.get();
int i = 0, x = 0;
for (i; i < size; i++) // search for date
if (date == ap[i]->get_date())
{
int_array[x++] = i; // Store found date in int_array
cout << x << endl;
ap[i]->print();
cout << endl;
}
if (x == 0) //Appointment or Session not found
return -1;
else // Appointment or Session found
{
do
{
cout << " Select the appointment " << x < cin >> choice; } while (choice < 1 || choice > j); return int_array[choice - 1]; } } MemberFunctions.h (Appointment Class) #ifndef MemberFunctions_h #define MemberFunctions_h #include #include using namespace std; class Date { private: int day, month, year; public: Date(int = 1, int = 1, int = 1900); void set(int = 1, int = 1, int = 1900); void get(); void print() const; bool operator==(const Date &) const; }; class Time { private: int hour, minute; public: Time(int = 0, int = 0); void set(int = 0, int = 0); void get(); void print() const; int get_hour() const; int get_minute() const; Time operator-(const Time &) const; // finds difference between time objects bool operator==(const Time &) const; // compares time objects to see if they are equal bool operator<(const Time &) const; // compares two Time to see if the first is less than the second bool operator>(const Time &) const; // compares two Time objects to see if the first is greater than the second class Appointment { protected: Date date; Time start_time, end_time; char description[40], location[40]; public: Appointment(); void get(); void print() const; Date get_date() const; Time get_start_time() const; Time get_end_time() const; }; #endif #endif MemberFunctions.cpp #include #include "MemberFunctions.h" using namespace std; Date::Date(int d, int m, int y) { set(d, m, y); } void Date::set(int d, int m, int y) { if (d >= 1 && d <= 31) day = d; else { cout << " invalid day"; cout << " Press any key to continue"; system("pause"); exit(1); } if (m >= 1 && m <= 12) month = m; else { cout << " invalid month"; cout << " Press any key to continue"; system("pause"); exit(1); } if (y >= 1900 && y <= 3000) year = y; else { cout << " invalid year"; cout << " Press any key to continue"; system("pause"); exit(1); } } void Date::get() { char ch; cout << "Enter date in mm/dd/yyyy format: "; cin >> month >> ch >> day >> ch >> year; while (day < 1 || day > 31 || month < 1 || month > 12 || year < 1900 || year > 3000) { cout << " Invalid date entered" << endl; cout << " Enter date in mm/dd/yyyy format: "; cin >> day >> ch >> month >> ch >> year; } } void Date::print() const { cout << day << "/" << month << "/" << year; } bool Date::operator==(const Date &d) const { return day == d.day && month == d.month && year == d.year; } Time::Time(int h, int m) { set(h, m); } void Time::set(int h, int m) { if (h >= 0 && h <= 24) hour = h; else { cout << " Invalid hour"; cout << " Press any key to continue"; system("pause"); exit(1); } if (m >= 0 && m <= 60) minute = m; else { cout << " Invalid minute"; cout << " Press any key to continue"; system("pause"); exit(1); } } void Time::get() { char ch; cout << "Enter time in military [hour:minute] format: "; cin >> hour >> ch >> minute; while (hour < 0 || hour > 24 || minute < 0 || minute > 60) { cout << " Invalid time entered"; cout << " Enter time in military [hour:minute] format: "; cin >> hour >> ch >> minute; } } void Time::print() const { cout << hour << ":" << minute; } int Time::get_hour() const { return hour; } int Time::get_minute() const { return minute; } Time Time::operator-(const Time &t) const { if (minute >= t.minute) return Time(hour - t.hour, minute - t.minute); else return Time(hour - t.hour - 1, minute + 60 - t.minute); } bool Time::operator==(const Time &t) const { return hour == t.hour && minute == t.minute; } bool Time::operator<(const Time &t) const { if (hour < t.hour) return true; else if ((hour == t.hour) && (minute < t.minute)) return true; else return false; } bool Time::operator>(const Time &t) const { return !(*this == t) && !(*this < t); //must be greater if not equal and not less } Appointment::Appointment() : date(), start_time(), end_time() { strcpy(description, ""); strcpy(location, ""); } void Appointment::get() { char c; date.get(); do { cout << "Start time - "; start_time.get(); cin.ignore(20, ' '); cout << "End time - "; end_time.get(); cin.ignore(20, ' '); if (start_time.get_hour() > end_time.get_hour()) cout << " End time cannot be earlier than start time" << endl; } while (start_time.get_hour() > end_time.get_hour()); cout << "Enter description: "; int i = 0; cin.get(c); while (c != ' ') { description[i++] = c; cin.get(c); } description[i] = '\0'; cout << "Enter location: "; i = 0; cin.get(c); while (c != ' ') { location[i++] = c; cin.get(c); } location[i] = '\0'; } void Appointment::print() const { cout << " Appointment Date: "; date.print(); cout << " Start time: "; start_time.print(); cout << " End time: "; end_time.print(); cout << " Description: " << description; cout << " Location: " << location; } Date Appointment::get_date() const { return date; } Time Appointment::get_start_time() const { return start_time; } Time Appointment::get_end_time() const { return end_time; } bool conflict(Appointment *p, Appointment *ap[], int size, int& i) { Date date = p->get_date(); Time start = p->get_start_time(); Time end = p->get_end_time(); for (i = 0; i < size; i++) if (date == ap[i]->get_date()) if ((start == ap[i]->get_start_time()) || (start < ap[i]->get_start_time()) && (end > ap[i]->get_start_time()) || (start > ap[i]->get_start_time()) && (start < ap[i]->get_end_time())) return true; return false; } main.cpp #include #include #include "MemberFunctions.h" #include "Session.h" using namespace std; int main() { Appointment *ap[100], *temp; Session *sp; classType type[100]; int choice, selection, count = 0, index, i; char id[11], fname[21], lname[21]; Date date; float charges = 0.0; bool found; do { cout << " Choose one of the following: " << " 1. Create a new appointment" << " 2. Create a new session" << " 3. View all appointments and sessions" << " 4. Edit appointment/session" << " 5. Check a date for appointments/sessions" << " 6. View sessions for a client" << " 7. View charges for a client" << " 8. View total charges for all clients" << " 9. Delete an appointment or session" << " 10. Quit" << " ----------------------------->"; cin >> choice; system("cls"); switch (choice) { case 1: temp = new Appointment; //temp Appointment pointer temp->get(); if (conflict(temp, ap, count, index)) // checks to see if there is a conflicting appointment { cout<<"Conflicts with "< } else { ap[count] = temp; type[count] = appt; // object stored in array count++; } break; case 2: temp = new Session; //temp Session pointer temp->get(); if (conflict(temp, ap, count, index)) // checks to see if there is a conflicting session { cout << " Conflicts with "< } else { ap[count] = temp; type[count] = session;// object stored in array count++; } break; case 3: for (i = 0; i < count; i++) { ap[i]->print(); cout << endl; } break; case 4: if ((choice = select_appt(ap, count)) == -1) //date not found { cout << " No available appointments or sessions on this date."; break; } else { temp = ap[choice]; if (choice < count - 1) for (i = choice; i < count; i++) { ap[i - 1] = ap[i]; type[i - 1] = type[i]; } count--; } do { cout << " Enter 1 to select an appointment and 2 to choose a session: "; cin >> selection; if (selection == 1) { ap[count] = new Appointment; type[count] = appt; //identify object stored in array } else if (selection == 2) { ap[count] = new Session; type[count] = session; //identify object stored in array } } while (selection < 1 || selection > 2); ap[count]->get(); if (conflict(ap[count], ap, count, index)) // checks for an existing Appoint or Session { cout << " Conflicts with "< delete ap[count]; // delete the one added ap[count++] = temp; // copy temp back into ap } else count++; break; case 5: cout << " Enter the date you would like to check: "; date.get(); found = false; for (i = 0; i < count; i++) { if (date == ap[i]->get_date()) { found = true; ap[i]->print(); cout << endl; } if (date == sp->get_date()) { found = true; sp->print(); cout << endl; } } if (!found) { cout << " You have no appointments or sessions on "; date.print(); } break; case 6: do { cout << " Enter 1 to search by I.D. or 2 to search by name: "; cin >> choice; found = false; if (choice == 1) { cout << " Enter the client's I.D.: "; cin >> id; for (i = 0; i < count; i++) if (type[i] == session) { sp = (Session*)(ap[i]); if (!strcmp(id, sp->get_id())) //Sessions only, no id for appoinments { sp->print(); found = true; } } if (!found) cout << " The client was not found. "; } else if (choice == 2) { cout << " Enter the client's last name: "; cin >> lname; cout << " Enter the client's first name: "; cin >> fname; for (i = 0; i < count; i++) if (type[i] == session) { sp = (Session*)(ap[i]); if (!strcmp(lname, sp->get_lname()) && !strcmp(fname, sp->get_fname())) { sp->print(); found = true; } } if (!found) cout << " The client was not found. "; } else cout << " Enter either 1 or 2 to try again: "; } while (choice < 1 || choice > 2); break; case 7: charges = 0.0; found = false; do { cout << " Enter 1 to search by I.D. or 2 to search by name: "; cin >> choice; if (choice == 1) { cout << " Enter the client's I.D.: "; cin >> id; for (i = 0; i < count; i++) if (type[i] == session) { sp = (Session*)(ap[i]); if (!strcmp(id, sp->get_id())) { found = true; charges += sp->calc_charge(); cout << " Charges for " << id << " is " << charges; } } if (!found) cout << " The client was not found. "; } else if (choice == 2) { cout << " Enter the client's last name: "; cin >> lname; cout << " Enter the client's first name: "; cin >> fname; for (i = 0; i < count; i++) { if (type[i] == session) { sp = (Session*)(ap[i]); if (!strcmp(lname, sp->get_lname()) && !strcmp(fname, sp->get_fname())) { found = true; charges += sp->calc_charge(); cout << " Charges for " << fname << " " << lname << " is: " << charges; } } } if (!found) cout << " The client was not found. "; } else cout << " Enter either 1 or 2 to try again: "; } while (choice < 1 || choice > 2); break; case 8: charges = 0.0; for (i = 0; i < count; i++) if (type[i] == session) { sp = (Session*)(ap[i]); charges += sp->calc_charge(); } cout << " Total charges for all clients are " << charges; charges = 0.0; break; case 9: if ((index = select_appt(ap, count)) == -1) // Date could not be found { cout << " There are no appointments or sessions on this date."; break; } else if (index < count - 1) for (int i = index; i < count - 1; i++) //Appointment objects moved to lower index in array (*(ap[i])) = (*(ap[i + 1])); else delete ap[count - 1]; count--; break; case 10: return 0; default: cout << " Entry not between 1 and 10."; } } while (1); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
