Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "date.hpp"
- #include "person.hpp"
- #include "tools.hpp"
- class Appointment {
- friend class ToDo;
- friend class Family;
- private: //---------------------------------------------------------------
- vector<Person> mWho; //who is the appointment with
- Date mWhen; //when is the appointment
- public: //----------------------------------------------------------------
- Appointment(Person* withWhom, const Date& when):mWho(0), mWhen(when)
- {
- cout << "\nMaking appointment...";
- mWho.push_back(*withWhom);
- }
- ~Appointment();
- void addMore(const Person*, Appointment*);
- static void makeAppt(Family*, ToDo*);
- void print(ostream &stream);
- void printFile(ofstream &streamFile);
- friend ostream &operator<<(ostream &stream, Appointment& o); //compiler throws me errors if I declare this as anything but a friend
- };
- #include "appointment.hpp"
- class Cell {
- friend class ToDo;
- private:
- Appointment* appt;
- Cell* next;
- Cell(Appointment* appt1, Cell* nx){appt = appt1; next = nx;}
- ~Cell(){};
- void print();
- };
- #include "tools.hpp"
- #define DAYS 13
- #define MONTHABR 13
- class Date {
- friend class ToDo;
- private: //------------------------------------------------------------------------
- time_t intTime;
- struct tm myTime;
- static const int nDays[DAYS];
- static const cstring monthAbr[MONTHABR];
- public: //-------------------------------------------------------------------------
- Date();
- Date(cstring dateString = NULL);
- ~Date();
- void print();
- void format(cstring, bool*, bool*, int*);
- static void instructions();
- static bool valiDate(int month, int day, int year, int hour, int min);
- bool operator<(const Date & d) const;
- int operator==(const Date & d) const;
- bool sameDay(const Date & d) const;
- bool sameTime(const Date & d) const;
- friend ostream &operator<<(ostream &stream, const Date o); //compiler throws me errors if I declare this as anything but a friend, can't access private data memeber.
- };
- #include "tools.hpp"
- //Exception class. Due to how my date constructor is built, I only need one exception class to catch errors.
- class BadDate {
- private:
- int month, day, year, hour, minute;
- public:
- BadDate(int m = 0, int d = 0, int y = 0, int h = 0, int min = 0) : month(m), day(d), year(y), hour(h), minute(min){}
- void msg();
- };
- inline void BadDate::msg()
- {
- cout << endl << "Month: " << month << endl
- << "Day: " << day << endl
- << "Year: " << year << endl
- << "Hour: " << hour << endl
- << "Minute: " << minute << endl
- << "**********************************************************************\n\a\a"
- << "One or more are invalid. Please re-read date entry instructions.\n"
- << "**********************************************************************\n";
- }
- #include "todo.hpp"
- class Family {
- friend class Appointment; //have to view family members in Appointment::makeAppt()
- private://---------------------------------------------------------
- int numFam;
- Person* famMem[];
- public://----------------------------------------------------------
- Family();
- ~Family();
- int count(){return numFam;}
- void getFamily();
- void realize();
- void print(ostream&);
- Person operator[](const int) const;
- friend ostream &operator<<(ostream &stream, Family& o);
- };
- #include "tools.hpp"
- #define SHRTNAME 20
- #define PASS 6
- #define FULLNAME 40
- #define PEEPS 1 //number of array elements for persons object, 1 for now
- class Person {
- friend class Appointment; //must be friend to allow Appointment operator extension to print Person.full_name
- friend class ToDo; //must be friend in order for ToDo to print Person.short_name to .txt file in deconstructor
- private: //---------------------------------------------------------------
- char* short_name;
- int permission_lvl;
- char* full_name;
- char* password;
- public: //----------------------------------------------------------------
- Person(const char s1[SHRTNAME] = NULL, const int n = 1, const char s2[FULLNAME] = NULL, const char s3[PASS] = NULL);
- Person(const Person& p);
- ~Person();
- void print(ostream &stream);
- void printFile(ofstream &streamFile);
- friend ostream &operator<<(ostream &stream, Person& o);
- friend ofstream &operator<<(ofstream &streamFile, Person& o);
- };
- #include "cell.hpp"
- class ToDo {
- friend class Cell;
- private://-------------------------------------------------------------------------------------------------------------------------
- Cell* head;
- Cell* scan;
- Cell* follow;
- Cell* found;
- int numCells;
- //wouldn't work without doing a return value. I tried to just set scan and have seeAppts use scan, but wouldn't work for me.
- Cell* find(const int, const int, const int, const int, const int);
- public://--------------------------------------------------------------------------------------------------------------------------
- ToDo();
- ~ToDo();
- int Count();
- void addAppt(Appointment*);
- void seeAppts(const int, const int, const int, const int, const int);
- void killAppt(ToDo);
- void print(ostream&);
- void printFile(ofstream&);
- void archive(ToDo);
- inline Cell* operator++(){scan = scan->next; return scan;}
- };
- inline ostream &operator<<(ostream &stream, ToDo& o);
- //#include "appointment.hpp"
- #include "family.hpp"
- #include "exceptions.hpp"
- //prints appointment info--------------------------------------------------------------------------------
- void Appointment::print(ostream &stream)
- {
- cout << "\nHere is your appointment information.\n";
- cout << "You are meeting ";
- for(int k = 0;k < mWho.size();++k)
- cout << mWho[k].short_name << ", ";
- cout << " on " << mWhen << "\n";
- }
- //prints appointment info to file-------------------------------------------------------------------------
- void Appointment::printFile(ofstream &streamFile)
- {
- streamFile << "\nHere is your appointment information.\n";
- streamFile << "You are meeting ";
- for(int k = 0;k < mWho.size();++k)
- streamFile << mWho[k].short_name << ", ";
- streamFile << " on " << mWhen << "\n";
- }
- //operator extension to stream Appointment print function-------------------------------------------------
- ostream &operator<<(ostream &stream, Appointment& o)
- {
- o.print(stream);
- return stream;
- }
- //deconstructor--------------------------------------------------------------------------------------------
- Appointment::~Appointment()
- {
- cout << "\nDestroying Appt. objects.\n";
- mWho.clear();
- }
- //add more family members to the appointment by pushing onto the vector-----------------------------------
- void Appointment::addMore(const Person* more, Appointment* appt)
- {
- cout << "\nAdding Person to Appointment list...\n";
- appt->mWho.push_back(*more);
- }
- //make appointment----------------------------------------
- void Appointment::makeAppt(Family* famList, ToDo* apptList)
- {
- char dateString[20];
- char password[6];
- char addFam = 'y';
- int personSelect;
- int passResult;
- bool permission = false;
- bool success = false;
- Appointment* appts;
- Date good_date();
- cout << "\n-----------------------------------------------------------------\n"
- << "Family Members"
- << "\n-----------------------------------------------------------------\n\n";
- for(int k = 0; k < famList->numFam;++k)
- cout << k + 1 << ": " << *famList->famMem[k] << "\n";
- cout << "-----------------------------------------------------------------\n"
- << "Please select your own name from the list: ";
- cin >> personSelect;
- cin.clear();
- cin.ignore(256, '\n');
- do {
- cout << "\nHello " << famList->famMem[personSelect - 1]->full_name << " \nplease enter your password (the string after permission level #): ";
- cin >> setw(6) >> password;
- passResult = strcmp(famList->famMem[personSelect - 1]->password, password);
- if(passResult != 0)
- {
- cout << "\nPassword incorrect. Please enter again.\n";
- continue;
- }
- else if(passResult == 0 && famList->famMem[personSelect - 1]->permission_lvl == 1)
- {
- cout << "\nPassword & permission level verified You may continue.\n";
- permission = true;
- }
- else
- cout << "\nYou do not have permission to create appointments.\n";
- } while(passResult != 0);
- cin.clear();
- cin.ignore(256, '\n');
- if(permission == true)
- {
- while(success != true)
- {
- Date::instructions();
- cout << "Enter a date and time: ";
- cin.getline(dateString, 19);
- //Due to block scope, I had to put the following inside the try block.
- try{
- Date d(dateString);
- success = true;
- cout << "\n\nPlease choose a person to make appointment with.";
- cout << "\n-----------------------------------------------------------------\n"
- << "Family Members"
- << "\n-----------------------------------------------------------------\n\n";
- for(int k = 0; k < famList->numFam;++k)
- cout << k + 1 << ": " << *famList->famMem[k] << "\n";
- cout << "-----------------------------------------------------------------\n"
- << "Please select a person by their associated number: ";
- cin >> personSelect;
- appts = new Appointment(famList->famMem[personSelect - 1], d);
- cout << "\n\nWould you like to add any other family members\n"
- << "to this appointment(y - yes, n - no): ";
- cin >> addFam;
- cin.clear();
- if(addFam == 'y')
- {
- char cont = NULL;
- while(cont != 'n')
- {
- cout << "\n\nPlease choose a person to add to the appointment."
- << "\n-----------------------------------------------------------------\n"
- << "Family Members"
- << "\n-----------------------------------------------------------------\n\n";
- for(int k = 0; k < famList->numFam;++k)
- cout << k + 1 << ": " << *famList->famMem[k] << "\n";
- cout << "-----------------------------------------------------------------\n"
- << "\nPlease select a person by their associated number: ";
- cin >> personSelect;
- appts->addMore(famList->famMem[personSelect - 1], appts);
- cout << "\nWould you like to add another family member: (y - yes, n - no): ";
- cin >> cont;
- cin.clear();
- }//end while
- }//end if
- else
- cout << "\nYou've decided not to add anyone else to the appointment.\n";
- apptList->addAppt(appts);
- cout << *apptList;
- cin.clear();
- cin.ignore(256, '\n');
- }//end try
- catch(BadDate & d)
- {
- d.msg();
- cout << "\nEnter date again.\n";
- continue;
- }//end catch
- catch(...)
- {
- cout << "\nDefault catch. Please try again.\n";
- continue;
- }//end catch
- }//end while
- }//end if
- }//end block
- #include "date.hpp"
- #include "exceptions.hpp"
- //initializing static class members outside of class
- const int Date::nDays[DAYS] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const cstring Date::monthAbr[MONTHABR] = {"None", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- //Date default constructor----------------------------------------------------------------------------------
- Date::Date()
- {
- myTime.tm_sec = 0;
- myTime.tm_min = 1;
- myTime.tm_hour = 1;
- myTime.tm_mday = 1;
- myTime.tm_mon = 1 - 1; //displays correct month as entered by user (tm_mon ranges 0-11)
- myTime.tm_year = 2010 - 1900;
- myTime.tm_wday = 0;
- myTime.tm_yday = 0;
- myTime.tm_isdst = -1;
- cout << "\nConverting tm struct to time_t...\n";
- intTime = mktime(& myTime);
- }
- //Date constructor-----------------------------------------------------------------------------------------
- Date::Date(cstring dateString) throw(BadDate)
- {
- int month = 0, day= 0 , year = 0, hour = 0, min = 0;
- bool formatOne = false; //01-01-2010 01:23 format
- bool formatTwo = false; //Jan 01, 2010 01:23 format
- bool formatOK = false;
- format(dateString, &formatOne, &formatTwo, &month);
- //extracts data from dateString, seperates into appropiate variables, based on user entered format
- if(formatOne == true)
- {
- istringstream parser(dateString);
- parser >> month >> day >> year >> hour >> min;
- }
- else if(formatTwo == true)
- {
- istringstream parser(dateString + 3); //+ 3 to skip month string, we have already extracted that int value
- parser >> day >> year >> hour >> min;
- }
- //show that the date variables have been succesfully parsed
- cout << "\nParsed variables\n"
- << "Month: "<< month << "\n"
- << "Day: " << day << "\n"
- << "Year: " << year << "\n"
- << "Hour: " << hour << "\n"
- << "Minute: " << min << "\n";
- if(Date::valiDate(month, day, year, hour, min))
- {
- cout << "\nDate valid.\n";
- }
- else
- throw BadDate(month, day, year, hour, min);
- //assign extracted variables to tm struct variables
- myTime.tm_sec = 0;
- myTime.tm_min = min;
- myTime.tm_hour = hour;
- myTime.tm_mday = day;
- myTime.tm_mon = month - 1; //displays correct month as entered by user (tm_mon ranges 0-11)
- myTime.tm_year = year - 1900;
- myTime.tm_wday = 0;
- myTime.tm_yday = 0;
- myTime.tm_isdst = -1;
- cout << "\nConverting tm struct to time_t...\n";
- intTime = mktime(& myTime);
- cout << "\nintTime = " << intTime;
- }
- //deconstructor is empty, for now-------------------------------------------------------------------------
- Date::~Date() {}
- //Print parsed user date in readable format---------------------------------------------------------------
- void Date::print()
- {
- char timeArray[26] = {0};
- cout << "\n\nConverting intTime to readable format...\n";
- cstring tempTime = ctime(&intTime);
- cout << "\nConverted date in readable format: " << tempTime << "\n";
- strcpy(timeArray, tempTime);
- }
- //operator<< extension to print intTime-------------------------------------------------------------------
- ostream &operator<<(ostream &stream, Date o)
- {
- stream << ctime(&o.intTime);
- return stream;
- }
- //Validate parsed data extracted from dateString-------------------------------
- bool Date::valiDate(int month, int day, int year, int hour, int min)
- {
- cout << "Validating date...\n";
- //Check if user entered data is valid
- if(month > 12 || month < 1)
- return false;
- else if(day > Date::nDays[month] || day < 1)
- return false;
- else if(year < 2010) //Assumption that the user will never need to make an appointment in the past!
- return false;
- else if(hour > 24 || hour < 1)
- return false;
- else if(min > 59 || min < 0)
- return false;
- else
- return true;
- }
- //display instructions to user of how to input correct date format-------------
- void Date::instructions()
- {
- cout << "\n\nTo enter a valid date, pleae use the following formats only.\n"
- "Month-Day-Year Hour:Minute (01-01-2010 01:10)\n"
- "OR\n"
- "Month (three letter abbreviation only) Day, Year Hour:Minute (Jan 01, 2010 01:10).\n\n";
- }
- //check format of dateString---------------------------------------------------------------------------------
- void Date::format(cstring dateString, bool* formatOne, bool* formatTwo, int* month)
- {
- //check to see if format of user entered date is of style 01-01-10
- cstring chkDash = strchr(dateString, '-');
- //check to see if format of user entered date is of style Mon Day, Year
- cstring chkComma = strchr(dateString, ',');
- if(chkDash != NULL && (chkDash - dateString + 1) == 3)
- {
- for(int k = 0;k < 3;++k)
- {
- if(k < 2)
- {
- //remove dashes from date, replace with spaces
- cstring dash = strchr(dateString, '-');
- *dash = ' ';
- }
- else
- {
- //remove colon from time, replace with space
- cstring colon = strchr(dateString, ':');
- *colon = ' ';
- }
- }
- *formatOne = true;
- }
- else if(chkComma != NULL && (chkComma - dateString + 1) == 7)
- {
- cstring chkMon;
- //remove comma between month and year in string
- cstring comma = strchr(dateString, ',');
- *comma = ' ';
- //remove collon between hour and minute in string
- cstring colon = strchr(dateString, ':');
- *colon = ' ';
- //check user entered month string, assign appropiate int value to month
- for(int k = 1;k < 13;++k)
- {
- chkMon = strstr(dateString, Date::monthAbr[k]);
- if(chkMon != NULL)
- {
- *month = k;
- break;
- }
- else if(chkMon == NULL && k != 12)
- continue;
- else
- throw BadDate();
- }
- *formatTwo = true;
- }
- else
- throw BadDate();
- cout << endl << dateString << endl;
- }
- //compare dates-----------------------------------------------------------------------
- bool Date::operator<(const Date & d) const
- {
- if(myTime.tm_mon < d.myTime.tm_mon)
- return true;
- else
- return false;
- }
- //are dates.mon equal?-----------------------------------------------------------------
- int Date::operator==(const Date & d) const
- {
- if(myTime.tm_mon == d.myTime.tm_mon)
- {
- if(myTime.tm_mday == d.myTime.tm_mday)
- {
- if(myTime.tm_hour < d.myTime.tm_hour)
- return 2;
- else if(myTime.tm_hour > d.myTime.tm_hour)
- return 3;
- else
- return 1;
- }
- else if(myTime.tm_mday < d.myTime.tm_mday)
- return 2;
- else if(myTime.tm_mday > d.myTime.tm_mday)
- return 3;
- }
- else
- return false;
- }
- //are dates.mday equal?-----------------------------------------------------------------
- bool Date::sameDay(const Date & d) const
- {
- if(myTime.tm_mday == d.myTime.tm_mday)
- return true;
- else
- return false;
- }
- //is time equal?------------------------------------------------------------------------
- bool Date::sameTime(const Date & d) const
- {
- if((myTime.tm_hour == d.myTime.tm_hour) && (myTime.tm_min == d.myTime.tm_min))
- return true;
- else
- return false;
- }
- #include "family.hpp"
- //constructor, checks if family.txt file is present or not and performs necessary actions-----------------------------------------------------------
- Family::Family()
- {
- cout << "\nChecking for family.txt file...\n";
- ifstream famFile("c:\\family.txt");
- if(famFile.is_open())
- {
- cout << "\nFile found.\n";
- realize();
- }
- else
- {
- cout << "\nFile not found.\n";
- getFamily();
- }
- }
- //deconstructor, storing Person array information into family.txt file before deleting------------------------------------------------------------------------
- Family::~Family()
- {
- cout << "\nDeconstructing Family...\n";
- ofstream famFile("c:\\family.txt");
- if(famFile.is_open())
- {
- famFile << numFam << endl;
- cout << "\nWriting Family members to file...\n";
- for(int k = 0;k < numFam;++k)
- {
- famFile << *famMem[k];
- famFile.clear();
- }
- }
- else
- cout << "\nError opening file.\n";
- famFile.close();
- }
- //collect information for Person objects if no family.txt is present (or unable to open file succesfully), store into Person array----------------------------------
- void Family::getFamily()
- {
- char s1[SHRTNAME];
- char s2[FULLNAME];
- char s3[PASS];
- int n;
- cout << "\nPlease enter number of people in family: ";
- cin >> numFam;
- for(int k = 0;k < numFam;++k)
- {
- cout << "\nPlease enter a new person.\n";
- cout << "\nPlease enter a short name: ";
- cin >> setw(19) >> s1; //not using getline(19, ' ') so that the user doesn't have to enter a ' ' at end of prompt to continue
- //next two statements prevents skipping of following inputs if user enters >19 characters in previous input
- cin.clear();
- cin.ignore(256, '\n');
- cout << "\n\nPlease enter a permission level (1-3): ";
- (cin >> n).get();
- cout << "\n\nPlease enter a full name: ";
- cin.get(s2, 39);
- //next two statements prevents skipping of following input(s) if user enters >39 characters in previous input
- cin.clear();
- cin.ignore(256, '\n');
- cout << "\n\nPlease enter a password (6 characters max): ";
- cin >> setw(29) >> s3; //not using getline(29, ' ') so that the user doesn't have to enter a ' ' at end of prompt to continue
- cin.clear();
- cin.ignore(256, '\n');
- cout << endl;
- famMem[k] = new Person(s1, n, s2, s3);
- cout << *famMem[k];
- }
- }
- //read from family.txt, create Person objects from .txt info and store into a Person array----------------------------------------------------
- void Family::realize()
- {
- char s1[SHRTNAME];
- char s2[FULLNAME];
- char s3[PASS];
- int n;
- ifstream famFile("C:\\family.txt");
- if(famFile.is_open())
- {
- cout << "\nRealizing...\n";
- famFile >> numFam;
- cout << "\nNumber of family members in file " << numFam << endl;
- for(int k = 0;k < numFam;++k)
- {
- famFile >> s1;
- famFile >> n;
- famFile >> s3;
- famFile.ignore(1); //ignore whitespace inbetween email and full_name
- famFile.get(s2, 39);
- famMem[k] = new Person(s1, n, s2, s3);
- cout << *famMem[k];
- }
- }
- else
- {
- cout << "ERROR! Could not open file. Calling getFamily()..."; //if file can't be opened, call getFamily(), no need to crash
- getFamily();
- }
- famFile.close();
- }
- //print person array in Family--------------------------------------------------------------------------------------
- void Family::print(ostream &stream)
- {
- for(int k = 0;k < numFam;++k)
- cout << *famMem[k];
- }
- //standard operator extension----------------------------------------------------------------------------------------
- ostream &operator<<(ostream &stream, Family& o)
- {
- o.print(stream);
- return stream;
- }
- //Return a const instance, to prevent operations that shouldn't be allowed.
- Person Family::operator[](const int subScript) const
- {
- return *famMem[subScript];
- }
- #include "person.hpp"
- //Person constructor, initliazing to user arguments, improved dynamic allocation for strings------------
- Person::Person(const char s1[SHRTNAME], const int n, const char s2[FULLNAME], const char s3[PASS])
- {
- int len1, len2, len3;
- len1 = strlen(s1);
- len2 = strlen(s2);
- len3 = strlen(s3);
- short_name = new char [len1 + 1];
- strcpy(short_name, s1);
- full_name = new char [len2 + 1];
- strcpy(full_name, s2);
- password = new char [len3 + 1];
- strcpy(password, s3);
- if(n > 3 || n < 1)
- {
- cout << "\n\nERROR! Permission level has to be either 1, 2, or 3.\n"
- << "Setting default permission level to 1.\n";
- permission_lvl = 1;
- }
- else
- permission_lvl = n;
- }
- //copy constructor--------------------------------------------------------------------------------
- Person::Person(const Person& p)
- {
- int len1, len2, len3;
- len1 = strlen(p.short_name);
- len2 = strlen(p.full_name);
- len3 = strlen(p.password);
- short_name = new char [len1 + 1];
- strcpy(short_name, p.short_name);
- full_name = new char [len2 + 1];
- strcpy(full_name, p.full_name);
- password = new char [len3 + 1];
- strcpy(password, p.password);
- permission_lvl = p.permission_lvl;
- }
- //Person desconstructor function that deallocates memory allocated for strings--------------------------
- Person::~Person()
- {
- cout << "\nDestroying Person objects.\n";
- delete full_name;
- delete short_name;
- delete password;
- }
- //prints Person information-----------------------------------------------------------------------------
- void Person::print(ostream &stream)
- {
- cout << short_name << " " << permission_lvl << " " << password << " " << full_name << endl;
- }
- //operator<< extension to print object info------------------------------------------------------------
- ostream &operator<<(ostream &stream, Person& o)
- {
- o.print(stream);
- return stream;
- }
- ofstream &operator<<(ofstream &streamFile, Person& o)
- {
- o.printFile(streamFile);
- return streamFile;
- }
- void Person::printFile(ofstream &streamFile)
- {
- streamFile << short_name << " " << permission_lvl << " " << password << " " << full_name << endl;
- }
- #include "todo.hpp"
- ToDo::ToDo()
- {
- numCells = 0; head = NULL; scan = NULL; follow = NULL;
- cout << "\nCreating ToDo...\n";
- cout << "\nChecking for appts.txt file...\n";
- ifstream todoFile("c:\\appts.txt");
- if(todoFile.is_open())
- {
- cout << "\nFile found.\n";
- }
- else
- {
- cout << "\nFile not found.\n";
- }
- }
- //deconstructor that deletes linked list------------------------------------------------------------
- ToDo::~ToDo()
- {
- Cell* temp;
- cout << "\nSerializing ToDo list...\n";
- ofstream myfile;
- myfile.open("C:\\appts.txt", ios::trunc);
- if(myfile.is_open())
- {
- Cell* curr;
- for(curr = head; curr != NULL; curr = curr -> next)
- {
- myfile << curr->appt->mWhen.intTime;
- for(int k = 0;k < curr->appt->mWho.size();++k)
- myfile << " " << curr->appt->mWho[k].short_name;
- myfile << endl;
- }
- }
- else
- cout << "\nERROR! File stream could not be opened.\n";
- myfile.close();
- cout << "\nDeleting linked list...\n";
- scan = head;
- while(scan != NULL)
- {
- temp = new Cell(scan->appt, scan->next);
- scan = scan->next;
- delete temp;
- temp = 0;
- }
- }
- //return number of nodes in linked list----------------------------------------------------------------------
- int ToDo::Count() {return numCells;}
- //add appts to linked list----------------------------------------------------------------------------
- void ToDo::addAppt(Appointment* appt)
- {
- cout << "\nAdding appointment to linked list...\n";
- if(head == NULL)
- {
- cout << "\nHead is null..\n";
- head = new Cell(appt, head);
- }
- else
- {
- follow = NULL;
- for(scan = head; scan != NULL; scan = scan -> next)
- {
- if(appt->mWhen < scan->appt->mWhen)
- {
- if(follow == NULL)
- {
- head = new Cell(appt, head);
- break;
- }
- else
- {
- follow->next = new Cell(appt, follow->next);
- break;
- }
- }
- else if((appt->mWhen == scan->appt->mWhen) == 1)
- {
- if(follow == NULL && scan->next != NULL)
- {
- head = new Cell(appt, head);
- break;
- }
- else
- {
- follow->next = new Cell(appt, follow->next);
- break;
- }
- }
- else if((appt->mWhen == scan->appt->mWhen) == 2)
- {
- if(follow == NULL)
- {
- head = new Cell(appt, head);
- break;
- }
- else
- {
- follow->next = new Cell(appt, follow->next);
- break;
- }
- }
- else if((appt->mWhen == scan->appt->mWhen) == 3)
- {
- scan->next = new Cell(appt, scan->next);
- break;
- }
- else if(scan->next == NULL)
- {
- scan->next = new Cell(appt, scan->next);
- break;
- }
- follow = scan;
- }
- }
- ++numCells;
- }
- //print to screen-----------------------------------------------------------------------------------
- void ToDo::print(ostream &stream)
- {
- Cell* curr;
- for(curr = head; curr != NULL; curr = curr -> next)
- curr->appt->print(stream);
- }
- //print to file-----------------------------------------------------------------------------------
- void ToDo::printFile(ofstream &streamFile)
- {
- Cell* curr;
- for(curr = head; curr != NULL; curr = curr -> next)
- curr->appt->printFile(streamFile);
- }
- //stream to screen--------------------------------------------------------------------------------
- inline ostream &operator<<(ostream &stream, ToDo& o)
- {
- o.print(stream);
- return stream;
- }
- //stream to file----------------------------------------------------------------------------------
- ofstream &operator<<(ofstream &streamFile, ToDo& o)
- {
- o.printFile(streamFile);
- return streamFile;
- }
- //search for an appt-----------------------------------------------------------------------------------
- Cell* ToDo::find(const int month, const int day, const int year, const int hour, const int min)
- {
- cout << "\nFinding appointment...\n";
- //program crashes when follow = null
- follow = head;
- for(scan = head;scan != NULL;scan = scan -> next)
- {
- if(scan->appt->mWhen.myTime.tm_mon == month &&
- scan->appt->mWhen.myTime.tm_mday == day &&
- scan->appt->mWhen.myTime.tm_year == year &&
- scan->appt->mWhen.myTime.tm_hour == hour &&
- scan->appt->mWhen.myTime.tm_min == min)
- return scan;
- else if(scan->next == NULL && scan->appt->mWhen.myTime.tm_mon != month)
- return NULL;
- follow = scan;
- }
- }
- //find appts on a particular date--------------------------------------------------------------------------
- void ToDo::seeAppts(const int month, const int day, const int year, const int hour, const int min)
- {
- for(scan = head;scan != NULL;scan = scan -> next)
- {
- found = find(month, day, year, hour, min);
- if(found != NULL)
- {
- cout << *found->appt;
- break;
- }
- else
- {
- cout << "\nAppointment not found.\n";
- break;
- }
- }
- }
- //delete user entered appointment chosen from list---------------------------------------------------------
- void ToDo::killAppt(ToDo t)
- {
- Cell* temp;
- Cell* _delete;
- int month, day, year, hour, min;
- cout << "\nList of appointments to delete.\n";
- cout << t;
- cout << "\nSelect the appointment you want to delete\n(format: month day year hour min): ";
- cin >> month >> day >> year >> hour >> min;
- found = find(month - 1, day, year - 1900, hour, min);
- cout << "\nDeleting appts...\n";
- for(scan = head;scan != NULL;scan = scan -> next)
- {
- //is the node you want to delete the head?
- if(found == head)
- {
- temp = new Cell(head->appt, head->next);
- head = head->next;
- delete temp;
- --numCells;
- }
- //is the node you want to delete the last in list?
- else if(found == scan && scan->next == NULL)
- {
- temp = new Cell(scan->appt, scan->next);
- follow -> next = NULL;
- delete temp;
- --numCells;
- }
- //is the node you want to delete in the middle of list?
- else if(found == scan)
- {
- temp = new Cell(scan->appt, scan->next);
- follow->next = scan->next;
- delete temp;
- --numCells;
- }
- //appointment requesting deletion was not found
- else if(found == NULL)
- {
- cout << "\nCouldn't find appointment. Nothing was deleted.\n";
- break;
- }
- follow = scan;
- }
- }
- void ToDo::archive(ToDo t)
- {
- cout << "\nWriting appointment list to file...\n";
- ofstream myfile;
- myfile.open("C:\\appts.txt", ios::trunc);
- if(myfile.is_open())
- myfile << t;
- else
- cout << "\nERROR! File stream could not be opened.\n";
- myfile.close();
- }
- /*********************************************************************/
- // Assignment 5 - CS626 - Dr. Fischer - Spring 2010
- //
- // Author: Nicholas DiMucci
- //
- // Description: Allow user to create an array of people
- // and dates, make appointments.
- // New Additions:
- // New ToDo and Cell classes
- // Multiple new fucntions, and linked list
- // Output linked list to file.
- //
- // Last modified: 05/29/2010 12:00pmEST
- /********************************************************************/
- #include "family.hpp"
- int main(void)
- {
- banner();
- ToDo apptList;
- Family famList;
- for(char cont = 'y';cont != 'n';)
- {
- Appointment::makeAppt(&famList, &apptList);
- cout << "\nWould you like to continue(y - yes, n - no): ";
- cin >> cont;
- cin.clear();
- }
- /*
- //testing seeAppts() ToDo function
- cout << "\nPlease enter a month, day and a year to search for\n(format: month day year hour min): ";
- cin >> month >> day >> year >> hour >> min;
- apptList.seeAppts(month - 1, day, year - 1900, hour, min);
- //testing killAppt() ToDo function
- apptList.killAppt(apptList);
- //testing archive() ToDo function
- apptList.archive(apptList);
- cout << apptList; */
- bye();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement