Advertisement
Guest User

Untitled

a guest
May 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.81 KB | None | 0 0
  1. #include "date.hpp"
  2. #include "person.hpp"
  3. #include "tools.hpp"
  4.  
  5. class Appointment {
  6.     friend class ToDo;
  7.     friend class Family;   
  8.     private: //---------------------------------------------------------------
  9.         vector<Person> mWho;    //who is the appointment with
  10.         Date mWhen;     //when is the appointment
  11.     public: //----------------------------------------------------------------
  12.         Appointment(Person* withWhom, const Date& when):mWho(0), mWhen(when)
  13.         {
  14.             cout << "\nMaking appointment...";
  15.             mWho.push_back(*withWhom);
  16.         }
  17.         ~Appointment();
  18.         void addMore(const Person*, Appointment*);
  19.         static void makeAppt(Family*, ToDo*);
  20.         void print(ostream &stream);
  21.         void printFile(ofstream &streamFile);
  22.         friend ostream &operator<<(ostream &stream, Appointment& o);    //compiler throws me errors if I declare this as anything but a friend
  23. };
  24.  
  25. #include "appointment.hpp"
  26.  
  27. class Cell {
  28.     friend class ToDo;
  29.     private:
  30.         Appointment* appt;
  31.         Cell* next;
  32.  
  33.         Cell(Appointment* appt1, Cell* nx){appt = appt1; next = nx;}
  34.         ~Cell(){};
  35.         void print();
  36. };
  37.  
  38. #include "tools.hpp"
  39.  
  40. #define DAYS 13
  41. #define MONTHABR 13
  42.  
  43. class Date {
  44.     friend class ToDo;
  45.     private: //------------------------------------------------------------------------
  46.         time_t intTime;
  47.         struct tm myTime;
  48.         static const int nDays[DAYS];
  49.         static const cstring monthAbr[MONTHABR];
  50.  
  51.     public: //-------------------------------------------------------------------------
  52.         Date();
  53.         Date(cstring dateString = NULL);
  54.         ~Date();
  55.         void print();
  56.         void format(cstring, bool*, bool*, int*);
  57.         static void instructions();
  58.         static bool valiDate(int month, int day, int year, int hour, int min);
  59.         bool operator<(const Date & d) const;
  60.         int operator==(const Date & d) const;
  61.         bool sameDay(const Date & d) const;
  62.         bool sameTime(const Date & d) const;
  63.         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.
  64. };
  65.  
  66. #include "tools.hpp"
  67.  
  68. //Exception class. Due to how my date constructor is built, I only need one exception class to catch errors.
  69. class BadDate {
  70.     private:
  71.         int month, day, year, hour, minute;
  72.     public:
  73.         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){}
  74.         void msg();
  75. };
  76.  
  77. inline void BadDate::msg()
  78. {
  79.     cout << endl << "Month: " << month << endl
  80.          << "Day: " << day << endl
  81.          << "Year: " << year << endl
  82.          << "Hour: " << hour << endl
  83.          << "Minute: " << minute << endl
  84.          << "**********************************************************************\n\a\a"
  85.          << "One or more are invalid. Please re-read date entry instructions.\n"
  86.          << "**********************************************************************\n";
  87. }
  88.  
  89.  
  90. #include "todo.hpp"
  91.  
  92. class Family {
  93.     friend class Appointment; //have to view family members in Appointment::makeAppt()
  94.     private://---------------------------------------------------------
  95.         int numFam;
  96.         Person* famMem[];
  97.     public://----------------------------------------------------------
  98.         Family();
  99.         ~Family();
  100.         int count(){return numFam;}
  101.         void getFamily();
  102.         void realize();
  103.         void print(ostream&);
  104.         Person operator[](const int) const;
  105.         friend ostream &operator<<(ostream &stream, Family& o);
  106. };
  107.  
  108.  
  109. #include "tools.hpp"
  110.  
  111. #define SHRTNAME 20
  112. #define PASS 6
  113. #define FULLNAME 40
  114. #define PEEPS 1     //number of array elements for persons object, 1 for now
  115.  
  116. class Person {
  117.     friend class Appointment;   //must be friend to allow Appointment operator extension to print Person.full_name
  118.     friend class ToDo;          //must be friend in order for ToDo to print Person.short_name to .txt file in deconstructor
  119.     private: //---------------------------------------------------------------
  120.         char* short_name;
  121.         int permission_lvl;
  122.         char* full_name;
  123.         char* password;
  124.     public: //----------------------------------------------------------------
  125.         Person(const char s1[SHRTNAME] = NULL, const int n = 1, const char s2[FULLNAME] = NULL, const char s3[PASS] = NULL);
  126.         Person(const Person& p);
  127.         ~Person();
  128.         void print(ostream &stream);
  129.         void printFile(ofstream &streamFile);
  130.     friend ostream &operator<<(ostream &stream, Person& o);
  131.     friend ofstream &operator<<(ofstream &streamFile, Person& o);
  132. };
  133.  
  134.  
  135. #include "cell.hpp"
  136.  
  137. class ToDo {
  138.     friend class Cell;
  139.     private://-------------------------------------------------------------------------------------------------------------------------
  140.         Cell* head;
  141.         Cell* scan;
  142.         Cell* follow;
  143.         Cell* found;
  144.         int numCells;
  145.         //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.
  146.         Cell* find(const int, const int, const int, const int, const int);
  147.     public://--------------------------------------------------------------------------------------------------------------------------
  148.         ToDo();
  149.         ~ToDo();
  150.         int Count();
  151.         void addAppt(Appointment*);
  152.         void seeAppts(const int, const int, const int, const int, const int);
  153.         void killAppt(ToDo);
  154.         void print(ostream&);
  155.         void printFile(ofstream&);
  156.         void archive(ToDo);
  157.         inline Cell* operator++(){scan = scan->next; return scan;}
  158. };
  159. inline ostream &operator<<(ostream &stream, ToDo& o);
  160.  
  161.  
  162. //#include "appointment.hpp"
  163. #include "family.hpp"
  164. #include "exceptions.hpp"
  165.  
  166. //prints appointment info--------------------------------------------------------------------------------
  167. void Appointment::print(ostream &stream)
  168. {
  169.     cout << "\nHere is your appointment information.\n";
  170.     cout << "You are meeting ";
  171.     for(int k = 0;k < mWho.size();++k)
  172.         cout << mWho[k].short_name << ", ";
  173.     cout << " on " << mWhen << "\n";
  174. }
  175.  
  176. //prints appointment info to file-------------------------------------------------------------------------
  177. void Appointment::printFile(ofstream &streamFile)
  178. {
  179.     streamFile << "\nHere is your appointment information.\n";
  180.     streamFile << "You are meeting ";
  181.     for(int k = 0;k < mWho.size();++k)
  182.         streamFile << mWho[k].short_name << ", ";
  183.     streamFile << " on " << mWhen << "\n";
  184. }  
  185.  
  186. //operator extension to stream Appointment print function-------------------------------------------------
  187. ostream &operator<<(ostream &stream, Appointment& o)
  188. {
  189.     o.print(stream);
  190.  
  191.     return stream;
  192. }
  193.  
  194. //deconstructor--------------------------------------------------------------------------------------------
  195. Appointment::~Appointment()
  196. {
  197.     cout << "\nDestroying Appt. objects.\n";
  198.     mWho.clear();
  199. }
  200.  
  201. //add more family members to the appointment by pushing onto the vector-----------------------------------
  202. void Appointment::addMore(const Person* more, Appointment* appt)
  203. {
  204.     cout << "\nAdding Person to Appointment list...\n";
  205.     appt->mWho.push_back(*more);
  206. }
  207.  
  208. //make appointment----------------------------------------
  209. void Appointment::makeAppt(Family* famList, ToDo* apptList)
  210. {
  211.     char dateString[20];
  212.     char password[6];
  213.     char addFam = 'y';
  214.     int personSelect;
  215.     int passResult;
  216.     bool permission = false;
  217.     bool success = false;
  218.     Appointment* appts;
  219.     Date good_date();
  220.  
  221.     cout << "\n-----------------------------------------------------------------\n"
  222.          << "Family Members"
  223.          << "\n-----------------------------------------------------------------\n\n";
  224.  
  225.     for(int k = 0; k < famList->numFam;++k)
  226.             cout << k + 1 << ": " << *famList->famMem[k] << "\n";
  227.    
  228.     cout << "-----------------------------------------------------------------\n"
  229.          << "Please select your own name from the list: ";
  230.     cin >> personSelect;
  231.  
  232.     cin.clear();
  233.     cin.ignore(256, '\n');
  234.  
  235.     do {
  236.         cout << "\nHello " << famList->famMem[personSelect - 1]->full_name << " \nplease enter your password (the string after permission level #): ";
  237.         cin >> setw(6) >> password;
  238.        
  239.         passResult = strcmp(famList->famMem[personSelect - 1]->password, password);
  240.        
  241.         if(passResult != 0)
  242.         {
  243.             cout << "\nPassword incorrect. Please enter again.\n";
  244.             continue;
  245.         }
  246.         else if(passResult == 0 && famList->famMem[personSelect - 1]->permission_lvl == 1)
  247.         {
  248.             cout << "\nPassword & permission level verified You may continue.\n";
  249.             permission = true;
  250.         }
  251.         else
  252.             cout << "\nYou do not have permission to create appointments.\n";
  253.     } while(passResult != 0);
  254.  
  255.     cin.clear();
  256.     cin.ignore(256, '\n');
  257.    
  258.     if(permission == true)
  259.     {
  260.         while(success != true)
  261.         {
  262.             Date::instructions();
  263.             cout << "Enter a date and time: ";
  264.             cin.getline(dateString, 19);
  265.  
  266.             //Due to block scope, I had to put the following inside the try block.
  267.             try{
  268.                 Date d(dateString);
  269.                 success = true;
  270.  
  271.                 cout << "\n\nPlease choose a person to make appointment with.";
  272.  
  273.                 cout << "\n-----------------------------------------------------------------\n"
  274.                  << "Family Members"
  275.                  << "\n-----------------------------------------------------------------\n\n";
  276.        
  277.                 for(int k = 0; k < famList->numFam;++k)
  278.                     cout << k + 1 << ": " << *famList->famMem[k] << "\n";
  279.  
  280.                 cout << "-----------------------------------------------------------------\n"
  281.                      << "Please select a person by their associated number: ";
  282.                 cin >> personSelect;
  283.  
  284.                 appts = new Appointment(famList->famMem[personSelect - 1], d);
  285.  
  286.                 cout << "\n\nWould you like to add any other family members\n"
  287.                      << "to this appointment(y - yes, n - no): ";
  288.                 cin >> addFam;
  289.                 cin.clear();
  290.  
  291.                 if(addFam == 'y')
  292.                 {
  293.                     char cont = NULL;
  294.  
  295.                     while(cont != 'n')
  296.                     {
  297.                         cout << "\n\nPlease choose a person to add to the appointment."
  298.                              << "\n-----------------------------------------------------------------\n"
  299.                              << "Family Members"
  300.                              << "\n-----------------------------------------------------------------\n\n";
  301.  
  302.                         for(int k = 0; k < famList->numFam;++k)
  303.                             cout << k + 1 << ": " << *famList->famMem[k] << "\n";
  304.  
  305.                         cout << "-----------------------------------------------------------------\n"
  306.                              << "\nPlease select a person by their associated number: ";
  307.                         cin >> personSelect;
  308.  
  309.                         appts->addMore(famList->famMem[personSelect - 1], appts);
  310.  
  311.                         cout << "\nWould you like to add another family member: (y - yes, n - no): ";
  312.                         cin >> cont;
  313.                         cin.clear();
  314.                     }//end while
  315.                 }//end if
  316.                 else
  317.                     cout << "\nYou've decided not to add anyone else to the appointment.\n";
  318.  
  319.                 apptList->addAppt(appts);
  320.                 cout << *apptList;
  321.  
  322.                 cin.clear();
  323.                 cin.ignore(256, '\n');
  324.             }//end try
  325.             catch(BadDate & d)
  326.             {
  327.                 d.msg();
  328.                 cout << "\nEnter date again.\n";
  329.                 continue;
  330.             }//end catch
  331.             catch(...)
  332.             {
  333.                 cout << "\nDefault catch. Please try again.\n";
  334.                 continue;
  335.             }//end catch
  336.         }//end while
  337.     }//end if
  338. }//end block
  339.  
  340. #include "date.hpp"
  341. #include "exceptions.hpp"
  342. //initializing static class members outside of class
  343. const int Date::nDays[DAYS] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  344. const cstring Date::monthAbr[MONTHABR] = {"None", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  345.  
  346. //Date default constructor----------------------------------------------------------------------------------
  347. Date::Date()
  348. {
  349.     myTime.tm_sec = 0;
  350.     myTime.tm_min = 1;
  351.     myTime.tm_hour = 1;
  352.     myTime.tm_mday = 1;
  353.     myTime.tm_mon = 1 - 1;  //displays correct month as entered by user (tm_mon ranges 0-11)
  354.     myTime.tm_year = 2010 - 1900;
  355.     myTime.tm_wday = 0;
  356.     myTime.tm_yday = 0;
  357.     myTime.tm_isdst = -1;
  358.  
  359.     cout << "\nConverting tm struct to time_t...\n";
  360.     intTime = mktime(& myTime);
  361. }
  362.  
  363. //Date constructor-----------------------------------------------------------------------------------------
  364. Date::Date(cstring dateString) throw(BadDate)
  365. {
  366.     int month = 0, day= 0 , year = 0, hour = 0, min = 0;
  367.     bool formatOne = false;     //01-01-2010 01:23 format
  368.     bool formatTwo = false;     //Jan 01, 2010 01:23 format
  369.     bool formatOK = false;
  370.    
  371.     format(dateString, &formatOne, &formatTwo, &month);
  372.  
  373.     //extracts data from dateString, seperates into appropiate variables, based on user entered format
  374.     if(formatOne == true)
  375.     {
  376.         istringstream parser(dateString);
  377.         parser >> month >> day >> year >> hour >> min;
  378.     }
  379.     else if(formatTwo == true)
  380.     {
  381.         istringstream parser(dateString + 3);   //+ 3 to skip month string, we have already extracted that int value
  382.         parser >> day >> year >> hour >> min;
  383.     }
  384.  
  385.     //show that the date variables have been succesfully parsed
  386.     cout << "\nParsed variables\n"
  387.             << "Month: "<< month << "\n"
  388.             << "Day: " << day << "\n"
  389.             << "Year: " << year << "\n"
  390.             << "Hour: " << hour << "\n"
  391.             << "Minute: " << min << "\n";
  392.  
  393.     if(Date::valiDate(month, day, year, hour, min))
  394.     {
  395.         cout << "\nDate valid.\n";
  396.     }
  397.     else
  398.         throw BadDate(month, day, year, hour, min);
  399.  
  400.     //assign extracted variables to tm struct variables
  401.     myTime.tm_sec = 0;
  402.     myTime.tm_min = min;
  403.     myTime.tm_hour = hour;
  404.     myTime.tm_mday = day;
  405.     myTime.tm_mon = month - 1;  //displays correct month as entered by user (tm_mon ranges 0-11)
  406.     myTime.tm_year = year - 1900;
  407.     myTime.tm_wday = 0;
  408.     myTime.tm_yday = 0;
  409.     myTime.tm_isdst = -1;
  410.  
  411.     cout << "\nConverting tm struct to time_t...\n";
  412.     intTime = mktime(& myTime);
  413.     cout << "\nintTime = " << intTime;
  414. }
  415.  
  416. //deconstructor is empty, for now-------------------------------------------------------------------------
  417. Date::~Date() {}
  418.  
  419. //Print parsed user date in readable format---------------------------------------------------------------
  420. void Date::print()
  421. {
  422.     char timeArray[26] = {0};
  423.    
  424.     cout << "\n\nConverting intTime to readable format...\n";
  425.     cstring tempTime = ctime(&intTime);
  426.    
  427.     cout << "\nConverted date in readable format: " << tempTime << "\n";
  428.  
  429.     strcpy(timeArray, tempTime);
  430. }
  431.  
  432. //operator<< extension to print intTime-------------------------------------------------------------------
  433. ostream &operator<<(ostream &stream, Date o)
  434. {
  435.     stream << ctime(&o.intTime);
  436.  
  437.     return stream;
  438. }
  439.  
  440. //Validate parsed data extracted from dateString-------------------------------
  441. bool Date::valiDate(int month, int day, int year, int hour, int min)
  442. {
  443.     cout << "Validating date...\n";
  444.            
  445.     //Check if user entered data is valid
  446.     if(month > 12 || month < 1)
  447.         return false;
  448.     else if(day > Date::nDays[month] || day < 1)
  449.         return false;
  450.     else if(year < 2010)    //Assumption that the user will never need to make an appointment in the past!
  451.         return false;
  452.     else if(hour > 24 || hour < 1)
  453.         return false;
  454.     else if(min > 59 || min < 0)
  455.         return false;
  456.     else
  457.         return true;       
  458. }
  459.  
  460. //display instructions to user of how to input correct date format-------------
  461. void Date::instructions()
  462. {
  463.     cout << "\n\nTo enter a valid date, pleae use the following formats only.\n"
  464.     "Month-Day-Year Hour:Minute (01-01-2010 01:10)\n"
  465.     "OR\n"
  466.     "Month (three letter abbreviation only) Day, Year Hour:Minute (Jan 01, 2010 01:10).\n\n";
  467. }
  468.  
  469. //check format of dateString---------------------------------------------------------------------------------
  470. void Date::format(cstring dateString, bool* formatOne, bool* formatTwo, int* month)
  471. {
  472.  
  473.     //check to see if format of user entered date is of style 01-01-10
  474.     cstring chkDash = strchr(dateString, '-');
  475.    
  476.     //check to see if format of user entered date is of style Mon Day, Year
  477.     cstring chkComma = strchr(dateString, ',');
  478.  
  479.     if(chkDash != NULL && (chkDash - dateString + 1) == 3)
  480.     {
  481.         for(int k = 0;k < 3;++k)
  482.         {
  483.             if(k < 2)
  484.             {
  485.                 //remove dashes from date, replace with spaces
  486.                 cstring dash = strchr(dateString, '-');
  487.                 *dash = ' ';
  488.             }
  489.             else
  490.             {
  491.                 //remove colon from time, replace with space
  492.                 cstring colon = strchr(dateString, ':');
  493.                 *colon = ' ';
  494.             }
  495.         }
  496.  
  497.         *formatOne = true;
  498.     }
  499.     else if(chkComma != NULL && (chkComma - dateString + 1) == 7)
  500.     {
  501.         cstring chkMon;
  502.  
  503.         //remove comma between month and year in string
  504.         cstring comma = strchr(dateString, ',');
  505.         *comma = ' ';
  506.  
  507.         //remove collon between hour and minute in string
  508.         cstring colon = strchr(dateString, ':');
  509.         *colon = ' ';
  510.  
  511.         //check user entered month string, assign appropiate int value to month
  512.         for(int k = 1;k < 13;++k)
  513.         {
  514.             chkMon = strstr(dateString, Date::monthAbr[k]);
  515.             if(chkMon != NULL)
  516.             {
  517.                 *month = k;
  518.                 break;
  519.             }
  520.             else if(chkMon == NULL && k != 12)
  521.                 continue;
  522.             else
  523.                 throw BadDate();
  524.         }
  525.  
  526.         *formatTwo = true;
  527.     }
  528.     else
  529.         throw BadDate();
  530.  
  531.     cout << endl << dateString << endl;
  532. }
  533.  
  534. //compare dates-----------------------------------------------------------------------
  535. bool Date::operator<(const Date & d) const
  536. {
  537.     if(myTime.tm_mon < d.myTime.tm_mon)
  538.         return true;   
  539.     else
  540.         return false;
  541. }
  542.  
  543. //are dates.mon equal?-----------------------------------------------------------------
  544. int Date::operator==(const Date & d) const
  545. {
  546.     if(myTime.tm_mon == d.myTime.tm_mon)
  547.     {
  548.         if(myTime.tm_mday == d.myTime.tm_mday)
  549.         {
  550.             if(myTime.tm_hour < d.myTime.tm_hour)
  551.                 return 2;
  552.             else if(myTime.tm_hour > d.myTime.tm_hour)
  553.                 return 3;
  554.             else
  555.                 return 1;
  556.         }
  557.         else if(myTime.tm_mday < d.myTime.tm_mday)
  558.             return 2;
  559.         else if(myTime.tm_mday > d.myTime.tm_mday)
  560.             return 3;
  561.     }
  562.     else
  563.         return false;
  564. }
  565.  
  566. //are dates.mday equal?-----------------------------------------------------------------
  567. bool Date::sameDay(const Date & d) const
  568. {
  569.     if(myTime.tm_mday == d.myTime.tm_mday)
  570.         return true;
  571.     else
  572.         return false;
  573. }
  574.  
  575. //is time equal?------------------------------------------------------------------------
  576. bool Date::sameTime(const Date & d) const
  577. {
  578.     if((myTime.tm_hour == d.myTime.tm_hour) && (myTime.tm_min == d.myTime.tm_min))
  579.         return true;
  580.     else
  581.         return false;
  582. }
  583.  
  584.  
  585. #include "family.hpp"
  586.  
  587. //constructor, checks if family.txt file is present or not and performs necessary actions-----------------------------------------------------------
  588. Family::Family()
  589. {
  590.     cout << "\nChecking for family.txt file...\n";
  591.  
  592.     ifstream famFile("c:\\family.txt");
  593.     if(famFile.is_open())
  594.     {
  595.         cout << "\nFile found.\n";
  596.         realize();
  597.     }
  598.     else
  599.     {
  600.         cout << "\nFile not found.\n";
  601.         getFamily();
  602.     }
  603.  
  604. }
  605.  
  606. //deconstructor, storing Person array information into family.txt file before deleting------------------------------------------------------------------------
  607. Family::~Family()
  608. {
  609.     cout << "\nDeconstructing Family...\n";
  610.     ofstream famFile("c:\\family.txt");
  611.     if(famFile.is_open())
  612.     {
  613.         famFile << numFam << endl;
  614.  
  615.         cout << "\nWriting Family members to file...\n";
  616.         for(int k = 0;k < numFam;++k)
  617.         {
  618.             famFile << *famMem[k];
  619.             famFile.clear();
  620.         }
  621.     }
  622.     else
  623.         cout << "\nError opening file.\n";
  624.  
  625.     famFile.close();
  626. }
  627.  
  628. //collect information for Person objects if no family.txt is present (or unable to open file succesfully), store into Person array----------------------------------
  629. void Family::getFamily()
  630. {
  631.     char s1[SHRTNAME];
  632.     char s2[FULLNAME];
  633.     char s3[PASS];
  634.     int n;
  635.  
  636.     cout << "\nPlease enter number of people in family: ";
  637.     cin >> numFam;
  638.  
  639.     for(int k = 0;k < numFam;++k)
  640.     {
  641.         cout << "\nPlease enter a new person.\n";
  642.  
  643.         cout << "\nPlease enter a short name: ";
  644.         cin >> setw(19) >> s1;  //not using getline(19, ' ') so that the user doesn't have to enter a ' ' at end of prompt to continue
  645.    
  646.         //next two statements prevents skipping of following inputs if user enters >19 characters in previous input
  647.         cin.clear();
  648.         cin.ignore(256, '\n');
  649.    
  650.         cout << "\n\nPlease enter a permission level (1-3): ";
  651.         (cin >> n).get();
  652.  
  653.         cout << "\n\nPlease enter a full name: ";
  654.         cin.get(s2, 39);
  655.  
  656.         //next two statements prevents skipping of following input(s) if user enters >39 characters in previous input
  657.         cin.clear();
  658.         cin.ignore(256, '\n');
  659.  
  660.         cout << "\n\nPlease enter a password (6 characters max): ";
  661.         cin >> setw(29) >> s3;  //not using getline(29, ' ') so that the user doesn't have to enter a ' ' at end of prompt to continue
  662.            
  663.         cin.clear();
  664.         cin.ignore(256, '\n');
  665.  
  666.         cout << endl;
  667.  
  668.         famMem[k] = new Person(s1, n, s2, s3);
  669.        
  670.         cout << *famMem[k];
  671.     }
  672. }
  673.  
  674. //read from family.txt, create Person objects from .txt info and store into a Person array----------------------------------------------------
  675. void Family::realize()
  676. {
  677.     char s1[SHRTNAME];
  678.     char s2[FULLNAME];
  679.     char s3[PASS];
  680.     int n;
  681.  
  682.     ifstream famFile("C:\\family.txt");
  683.     if(famFile.is_open())
  684.     {
  685.         cout << "\nRealizing...\n";
  686.  
  687.         famFile >> numFam;
  688.         cout << "\nNumber of family members in file " << numFam << endl;
  689.  
  690.         for(int k = 0;k < numFam;++k)
  691.         {
  692.             famFile >> s1;
  693.            
  694.             famFile >> n;
  695.  
  696.             famFile >> s3;
  697.  
  698.             famFile.ignore(1);  //ignore whitespace inbetween email and full_name
  699.             famFile.get(s2, 39);
  700.  
  701.             famMem[k] = new Person(s1, n, s2, s3);
  702.             cout << *famMem[k];
  703.         }
  704.     }
  705.     else
  706.     {
  707.         cout << "ERROR! Could not open file. Calling getFamily()...";   //if file can't be opened, call getFamily(), no need to crash
  708.         getFamily();
  709.     }
  710.  
  711.     famFile.close();
  712. }
  713.  
  714. //print person array in Family--------------------------------------------------------------------------------------
  715. void Family::print(ostream &stream)
  716. {
  717.     for(int k = 0;k < numFam;++k)
  718.         cout << *famMem[k];
  719. }
  720.  
  721. //standard operator extension----------------------------------------------------------------------------------------
  722. ostream &operator<<(ostream &stream, Family& o)
  723. {
  724.     o.print(stream);
  725.  
  726.     return stream;
  727. }
  728.  
  729. //Return a const instance, to prevent operations that shouldn't be allowed.
  730. Person Family::operator[](const int subScript) const
  731. {
  732.     return *famMem[subScript];
  733. }
  734.  
  735. #include "person.hpp"
  736.  
  737. //Person constructor, initliazing to user arguments, improved dynamic allocation for strings------------
  738. Person::Person(const char s1[SHRTNAME], const int n, const char s2[FULLNAME], const char s3[PASS])
  739. {  
  740.     int len1, len2, len3;
  741.  
  742.     len1 = strlen(s1);
  743.     len2 = strlen(s2);
  744.     len3 = strlen(s3);
  745.  
  746.     short_name = new char [len1 + 1];
  747.     strcpy(short_name, s1);
  748.  
  749.     full_name = new char [len2 + 1];
  750.     strcpy(full_name, s2);
  751.  
  752.     password = new char [len3 + 1];
  753.     strcpy(password, s3);
  754.  
  755.     if(n > 3 || n < 1)
  756.     {
  757.         cout << "\n\nERROR! Permission level has to be either 1, 2, or 3.\n"
  758.              << "Setting default permission level to 1.\n";
  759.        
  760.         permission_lvl = 1;
  761.     }
  762.     else
  763.         permission_lvl = n;
  764.    
  765. }
  766.  
  767. //copy constructor--------------------------------------------------------------------------------
  768. Person::Person(const Person& p)
  769. {
  770.     int len1, len2, len3;
  771.  
  772.     len1 = strlen(p.short_name);
  773.     len2 = strlen(p.full_name);
  774.     len3 = strlen(p.password);
  775.  
  776.     short_name = new char [len1 + 1];
  777.     strcpy(short_name, p.short_name);
  778.  
  779.     full_name = new char [len2 + 1];
  780.     strcpy(full_name, p.full_name);
  781.  
  782.     password = new char [len3 + 1];
  783.     strcpy(password, p.password);
  784.  
  785.     permission_lvl = p.permission_lvl;
  786. }
  787.  
  788. //Person desconstructor function that deallocates memory allocated for strings--------------------------
  789. Person::~Person()
  790. {
  791.     cout << "\nDestroying Person objects.\n";
  792.     delete full_name;
  793.     delete short_name;
  794.     delete password;
  795. }
  796.  
  797. //prints Person information-----------------------------------------------------------------------------
  798. void Person::print(ostream &stream)
  799. {
  800.     cout << short_name << " " << permission_lvl << " " << password << " " << full_name << endl;
  801. }
  802.  
  803. //operator<< extension to print object info------------------------------------------------------------
  804. ostream &operator<<(ostream &stream, Person& o)
  805. {
  806.     o.print(stream);
  807.     return stream;
  808. }
  809.  
  810. ofstream &operator<<(ofstream &streamFile, Person& o)
  811. {
  812.     o.printFile(streamFile);
  813.     return streamFile;
  814. }
  815.  
  816. void Person::printFile(ofstream &streamFile)
  817. {
  818.     streamFile << short_name << " " << permission_lvl << " " << password << " " << full_name << endl;
  819. }  
  820.  
  821. #include "todo.hpp"
  822.  
  823. ToDo::ToDo()
  824. {
  825.     numCells = 0; head = NULL; scan = NULL; follow = NULL;
  826.    
  827.     cout << "\nCreating ToDo...\n";
  828.     cout << "\nChecking for appts.txt file...\n";
  829.  
  830.     ifstream todoFile("c:\\appts.txt");
  831.     if(todoFile.is_open())
  832.     {
  833.         cout << "\nFile found.\n";
  834.     }
  835.     else
  836.     {
  837.         cout << "\nFile not found.\n";
  838.     }
  839.  
  840. }
  841.  
  842.  
  843. //deconstructor that deletes linked list------------------------------------------------------------
  844. ToDo::~ToDo()
  845. {
  846.     Cell* temp;
  847.     cout << "\nSerializing ToDo list...\n";
  848.  
  849.     ofstream myfile;
  850.  
  851.     myfile.open("C:\\appts.txt", ios::trunc);
  852.     if(myfile.is_open())
  853.     {
  854.         Cell* curr;
  855.         for(curr = head; curr != NULL; curr = curr -> next)
  856.         {
  857.             myfile << curr->appt->mWhen.intTime;
  858.             for(int k = 0;k < curr->appt->mWho.size();++k)
  859.                 myfile << " " << curr->appt->mWho[k].short_name;
  860.             myfile << endl;
  861.         }
  862.  
  863.     }
  864.     else
  865.         cout << "\nERROR! File stream could not be opened.\n";
  866.  
  867.     myfile.close();
  868.  
  869.     cout << "\nDeleting linked list...\n";
  870.  
  871.     scan = head;
  872.     while(scan != NULL)
  873.     {
  874.         temp = new Cell(scan->appt, scan->next);
  875.         scan = scan->next;
  876.         delete temp;
  877.         temp = 0;
  878.     }
  879. }
  880.  
  881. //return number of nodes in linked list----------------------------------------------------------------------
  882. int ToDo::Count() {return numCells;}
  883.  
  884. //add appts to linked list----------------------------------------------------------------------------
  885. void ToDo::addAppt(Appointment* appt)
  886. {
  887.     cout << "\nAdding appointment to linked list...\n";
  888.  
  889.     if(head == NULL)
  890.     {
  891.         cout << "\nHead is null..\n";
  892.         head = new Cell(appt, head);
  893.     }
  894.     else
  895.     {
  896.         follow = NULL;
  897.         for(scan = head; scan != NULL; scan = scan -> next)
  898.         {
  899.             if(appt->mWhen < scan->appt->mWhen)
  900.             {
  901.                 if(follow == NULL)
  902.                 {
  903.                     head = new Cell(appt, head);
  904.                     break;
  905.                 }
  906.                 else
  907.                 {
  908.                     follow->next = new Cell(appt, follow->next);
  909.                     break;
  910.                 }
  911.             }
  912.             else if((appt->mWhen == scan->appt->mWhen) == 1)
  913.             {
  914.                 if(follow == NULL && scan->next != NULL)
  915.                 {
  916.                     head = new Cell(appt, head);
  917.                     break;
  918.                 }
  919.                 else
  920.                 {
  921.                     follow->next = new Cell(appt, follow->next);
  922.                     break;
  923.                 }
  924.             }
  925.             else if((appt->mWhen == scan->appt->mWhen) == 2)
  926.             {
  927.                 if(follow == NULL)
  928.                 {
  929.                     head = new Cell(appt, head);
  930.                     break;
  931.                 }
  932.                 else
  933.                 {
  934.                     follow->next = new Cell(appt, follow->next);
  935.                     break;
  936.                 }
  937.             }
  938.             else if((appt->mWhen == scan->appt->mWhen) == 3)
  939.             {
  940.                 scan->next = new Cell(appt, scan->next);
  941.                 break;
  942.             }
  943.             else if(scan->next == NULL)
  944.             {
  945.                 scan->next = new Cell(appt, scan->next);
  946.                 break;
  947.             }
  948.  
  949.             follow = scan;
  950.         }
  951.     }
  952.  
  953.     ++numCells;
  954. }
  955.  
  956. //print to screen-----------------------------------------------------------------------------------
  957. void ToDo::print(ostream &stream)
  958. {
  959.     Cell* curr;
  960.     for(curr = head; curr != NULL; curr = curr -> next)
  961.         curr->appt->print(stream);
  962. }
  963.  
  964. //print to file-----------------------------------------------------------------------------------
  965. void ToDo::printFile(ofstream &streamFile)
  966. {
  967.     Cell* curr;
  968.     for(curr = head; curr != NULL; curr = curr -> next)
  969.         curr->appt->printFile(streamFile);
  970. }
  971.  
  972. //stream to screen--------------------------------------------------------------------------------
  973. inline ostream &operator<<(ostream &stream, ToDo& o)
  974. {
  975.     o.print(stream);
  976.  
  977.     return stream;
  978. }
  979.  
  980. //stream to file----------------------------------------------------------------------------------
  981. ofstream &operator<<(ofstream &streamFile, ToDo& o)
  982. {
  983.     o.printFile(streamFile);
  984.  
  985.     return streamFile;
  986. }
  987.  
  988. //search for an appt-----------------------------------------------------------------------------------
  989. Cell* ToDo::find(const int month, const int day, const int year, const int hour, const int min)
  990. {
  991.     cout << "\nFinding appointment...\n";
  992.  
  993.     //program crashes when follow = null
  994.     follow = head;
  995.  
  996.     for(scan = head;scan != NULL;scan = scan -> next)
  997.     {
  998.         if(scan->appt->mWhen.myTime.tm_mon == month &&
  999.            scan->appt->mWhen.myTime.tm_mday == day &&
  1000.            scan->appt->mWhen.myTime.tm_year == year &&
  1001.            scan->appt->mWhen.myTime.tm_hour == hour &&
  1002.            scan->appt->mWhen.myTime.tm_min == min)
  1003.             return scan;
  1004.         else if(scan->next == NULL && scan->appt->mWhen.myTime.tm_mon != month)
  1005.             return NULL;
  1006.  
  1007.         follow = scan;
  1008.     }
  1009. }
  1010.  
  1011. //find appts on a particular date--------------------------------------------------------------------------
  1012. void ToDo::seeAppts(const int month, const int day, const int year, const int hour, const int min)
  1013. {
  1014.     for(scan = head;scan != NULL;scan = scan -> next)
  1015.     {
  1016.         found = find(month, day, year, hour, min);
  1017.         if(found != NULL)
  1018.         {
  1019.             cout << *found->appt;
  1020.             break;
  1021.         }
  1022.         else
  1023.         {
  1024.             cout << "\nAppointment not found.\n";
  1025.             break;
  1026.         }
  1027.     }
  1028. }
  1029.  
  1030. //delete user entered appointment chosen from list---------------------------------------------------------
  1031. void ToDo::killAppt(ToDo t)
  1032. {
  1033.     Cell* temp;
  1034.     Cell* _delete;
  1035.     int month, day, year, hour, min;
  1036.  
  1037.     cout << "\nList of appointments to delete.\n";
  1038.     cout << t;
  1039.  
  1040.     cout << "\nSelect the appointment you want to delete\n(format: month day year hour min): ";
  1041.     cin >> month >> day >> year >> hour >> min;
  1042.  
  1043.     found = find(month - 1, day, year - 1900, hour, min);
  1044.    
  1045.     cout << "\nDeleting appts...\n";
  1046.  
  1047.     for(scan = head;scan != NULL;scan = scan -> next)
  1048.     {
  1049.         //is the node you want to delete the head?
  1050.         if(found == head)
  1051.         {
  1052.             temp = new Cell(head->appt, head->next);
  1053.             head = head->next;
  1054.             delete temp;
  1055.             --numCells;
  1056.         }
  1057.         //is the node you want to delete the last in list?
  1058.         else if(found == scan && scan->next == NULL)
  1059.         {
  1060.             temp = new Cell(scan->appt, scan->next);
  1061.             follow -> next = NULL;
  1062.             delete temp;
  1063.             --numCells;
  1064.         }
  1065.         //is the node you want to delete in the middle of list?
  1066.         else if(found == scan)
  1067.         {
  1068.             temp = new Cell(scan->appt, scan->next);
  1069.             follow->next = scan->next;
  1070.             delete temp;
  1071.             --numCells;
  1072.         }
  1073.         //appointment requesting deletion was not found
  1074.         else if(found == NULL)
  1075.         {
  1076.             cout << "\nCouldn't find appointment. Nothing was deleted.\n";
  1077.             break;
  1078.         }
  1079.  
  1080.         follow = scan;
  1081.     }
  1082. }
  1083.  
  1084. void ToDo::archive(ToDo t)
  1085. {
  1086.     cout << "\nWriting appointment list to file...\n";
  1087.  
  1088.     ofstream myfile;
  1089.  
  1090.     myfile.open("C:\\appts.txt", ios::trunc);
  1091.     if(myfile.is_open())
  1092.             myfile << t;
  1093.     else
  1094.         cout << "\nERROR! File stream could not be opened.\n";
  1095.  
  1096.     myfile.close();
  1097. }
  1098.  
  1099. /*********************************************************************/
  1100. //          Assignment 5 - CS626 - Dr. Fischer - Spring 2010
  1101. //         
  1102. //          Author: Nicholas DiMucci
  1103. //         
  1104. //          Description: Allow user to create an array of people
  1105. //                       and dates, make appointments.
  1106. //                       New Additions:
  1107. //                       New ToDo and Cell classes
  1108. //                       Multiple new fucntions, and linked list
  1109. //                       Output linked list to file.
  1110. //         
  1111. //          Last modified: 05/29/2010 12:00pmEST
  1112. /********************************************************************/
  1113.  
  1114. #include "family.hpp"
  1115.  
  1116. int main(void)
  1117. {
  1118.     banner();
  1119.  
  1120.     ToDo apptList;
  1121.     Family famList;
  1122.  
  1123.     for(char cont = 'y';cont != 'n';)
  1124.     {
  1125.         Appointment::makeAppt(&famList, &apptList);
  1126.  
  1127.         cout << "\nWould you like to continue(y - yes, n - no): ";
  1128.         cin >> cont;
  1129.         cin.clear();
  1130.     }
  1131.  
  1132.     /*
  1133.     //testing seeAppts() ToDo function
  1134.     cout << "\nPlease enter a month, day and a year to search for\n(format: month day year hour min): ";
  1135.     cin >> month >> day >> year >> hour >> min;
  1136.     apptList.seeAppts(month - 1, day, year - 1900, hour, min);
  1137.  
  1138.     //testing killAppt() ToDo function
  1139.     apptList.killAppt(apptList);
  1140.  
  1141.     //testing archive() ToDo function
  1142.     apptList.archive(apptList);
  1143.  
  1144.     cout << apptList; */
  1145.  
  1146.     bye();
  1147.  
  1148.     return 0;
  1149.  
  1150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement