Advertisement
Finigini

Assignment 4

Nov 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.43 KB | None | 0 0
  1. //main.cpp---------------------------------------------------------------------------------------------------
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstring>
  6. #include "extPersonType.h"
  7. #include "personType.h"
  8. #include "addressType.h"
  9. #include "dateType.h"
  10. #include "extDateType.h"
  11. #include "addressBookType.h"
  12.  
  13.  
  14. using namespace std;
  15.  
  16.  
  17. //check if the input file exists
  18. int openFile(ifstream &infile)
  19. {
  20.     infile.open("Assn2_Q1_Data.txt");
  21.     if (!&infile)
  22.     {
  23.         cout << "Cannot open the input file. Program terminates!"
  24.              << endl;
  25.         return 1;
  26.     }
  27.     return 0;
  28. }
  29.  
  30. //sort the address book by last name
  31. void addressSort(addressBookType &book)
  32. {
  33.     for(int i = 0; i < 5; i++)
  34.     {
  35.         for(int ii = i+1; ii < 6; ii++)
  36.         {
  37.             if (strcmp(book.people[i].getLastName().c_str(), book.people[ii].getLastName().c_str())>0)
  38.             {
  39.                 extPersonType temp = book.people[i];
  40.                 book.people[i] = book.people[ii];
  41.                 book.people[ii] = temp;
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. void printBook(addressBookType book)
  48. {
  49.     for(int i = 0; i < 6; i++)
  50.     {
  51.         cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
  52.         book.people[i].printDate();
  53.         cout << endl;
  54.         cout << "Address: " << book.people[i].street << endl;
  55.         cout << "City: " << book.people[i].city << endl;
  56.         cout << "State: " << book.people[i].state << endl;
  57.         cout << "Zip: " << book.people[i].zipCode << endl;
  58.         cout << "Phone: " << book.people[i].phoneNumber << endl;
  59.         cout << "Status: " << book.people[i].status << endl;
  60.         cout << "---------------------------------------------------" << endl;
  61.     }  
  62. }
  63.  
  64. void lastNameSearch(addressBookType book)
  65. {
  66.     string search;
  67.     bool found = false;
  68.     cout << "enter a last name to search for: ";
  69.     cin >> search;
  70.     cout << endl;
  71.     for(int i = 0; i < 6; i++) if (found == false)
  72.     {
  73.         if (search == book.people[i].getLastName())
  74.         {
  75.             found = true;
  76.             cout << "Search results for: " << search << endl;
  77.             cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
  78.             book.people[i].printDate();
  79.             cout << endl;
  80.             cout << "Address: " << book.people[i].street << endl;
  81.             cout << "City: " << book.people[i].city << endl;
  82.             cout << "State: " << book.people[i].state << endl;
  83.             cout << "Zip: " << book.people[i].zipCode << endl;
  84.             cout << "Phone: " << book.people[i].phoneNumber << endl;
  85.             cout << "Status: " << book.people[i].status << endl;
  86.         }
  87.     }
  88. }
  89.  
  90. void sameBirthMonth(addressBookType book)
  91. {
  92.     int search;
  93.     cout << "enter a birth month to search for (1-12): ";
  94.     cin >> search;
  95.     cout << endl;
  96.     cout << "Individuals with the same birth month of: " << search << endl << endl;;
  97.     for(int i = 0; i < 6; i++)
  98.     {
  99.         if (search == book.people[i].getMonth())
  100.         {
  101.             cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
  102.         }
  103.     }
  104. }
  105.  
  106. void searchStatus(addressBookType book)
  107. {
  108.     string search;
  109.     cout << "enter a status to search for (Family, Friend, Business): ";
  110.     cin >> search;
  111.     cout << endl;
  112.     cout << search << ":" << endl << endl;;
  113.     for(int i = 0; i < 6; i++)
  114.     {
  115.         if (search == book.people[i].status)
  116.         {
  117.             cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
  118.         }
  119.     }
  120. }
  121.  
  122. int main()
  123. {
  124.     addressBookType book;
  125.     ifstream infile;
  126.     openFile(infile);
  127.     //build the array from the file
  128.     for(int i = 0; i < 6; i++)
  129.     {
  130.         string first, last;
  131.         int m, d, y;
  132.         //
  133.         infile >> first;
  134.         infile >> last;
  135.         book.people[i].setName(first, last);
  136.         infile >> m;
  137.         infile >> d;
  138.         infile >> y;
  139.         book.people[i].setDate(m, d, y);
  140.         getline(infile,book.people[i].street);
  141.         getline(infile,book.people[i].street);
  142.         getline(infile,book.people[i].city);
  143.         getline(infile,book.people[i].state);
  144.         getline(infile,book.people[i].zipCode);
  145.         getline(infile,book.people[i].phoneNumber);
  146.         getline(infile,book.people[i].status);
  147.     }
  148.     //build the linked list from the file
  149.     addressBookType *head, *current, *newNode;
  150.     head = NULL;
  151.     for(int i = 0; i < 6; i++)
  152.     {
  153.         //create nodes
  154.         newNode = new addressBookType;
  155.         //
  156.         string first, last;
  157.         int m, d, y;
  158.         infile >> first;
  159.         infile >> last;
  160.         newNode->setName(first, last);
  161.         infile >> m;
  162.         infile >> d;
  163.         infile >> y;
  164.         newNode->setDate(m, d, y);
  165.         getline(infile,newNode->street);
  166.         getline(infile,newNode->street);
  167.         getline(infile,newNode->city);
  168.         getline(infile,newNode->state);
  169.         getline(infile,newNode->zipCode);
  170.         getline(infile,newNode->phoneNumber);
  171.         getline(infile,newNode->status);
  172.         //
  173.         newNode->link = NULL;
  174.         head = newNode;
  175.         current = newNode;
  176.     }
  177.     cout << endl;
  178.     current->getFirstName(); cout << " "; current->getLastName();
  179.    
  180.     infile.close();
  181.     return 0;
  182. }
  183. //extPersonType.h---------------------------------------------------------------------------------------------------
  184. #include <string>
  185. #include "personType.h"
  186. #include "addressType.h"
  187. #include "dateType.h"
  188. #include "extDateType.h"
  189. #ifndef extPersonType_H
  190. #define extPersonType_H
  191.  
  192. using namespace std;
  193.  
  194. class extPersonType: public personType, public dateType, public addressType
  195. {
  196.     public:
  197.         addressType address;
  198.         dateType dateOfBirth;
  199.         string phoneNumber;
  200.         string status;
  201. };
  202.  
  203. #endif
  204. //personType.h---------------------------------------------------------------------------------------------------
  205. #include <string>
  206. #ifndef personType_H
  207. #define personType_H
  208.  
  209. using namespace std;
  210.  
  211. class personType
  212. {
  213.     public:
  214.         void print() const;
  215.         void setName(string first, string last);
  216.         string getFirstName() const;
  217.         string getLastName() const;
  218.         personType(string first = "", string last = "");
  219.     private:
  220.         string firstName;
  221.         string lastName;
  222. };
  223.  
  224. #endif
  225. //personType.cpp---------------------------------------------------------------------------------------------------
  226. #include "personType.h"
  227. #include <string>
  228. #include <iostream>
  229.  
  230. using namespace std;
  231.  
  232. personType::personType(string first, string last)
  233. {
  234.    
  235. }
  236.  
  237. void personType::print() const
  238. {
  239.     cout << "Name: " << firstName << " " << lastName << endl;
  240. }
  241.  
  242. void personType::setName(string first, string last)
  243. {
  244.     firstName = first;
  245.     lastName = last;
  246. }
  247.  
  248. string personType::getFirstName() const
  249. {
  250.     return firstName;
  251. }
  252.  
  253. string personType::getLastName() const
  254. {
  255.     return lastName;
  256. }
  257. //addressType.h---------------------------------------------------------------------------------------------------
  258. #include <string>
  259. #ifndef addressType_H
  260. #define addressType_H
  261.  
  262. using namespace std;
  263.  
  264. class addressType
  265. {
  266.     public:
  267.         string street;
  268.         string city;
  269.         string state;
  270.         string zipCode;
  271. };
  272.  
  273. #endif
  274. //dateType.h---------------------------------------------------------------------------------------------------
  275. //dateType.h
  276.  
  277. #ifndef date_H
  278. #define date_H
  279.  
  280. class dateType
  281. {
  282. public:
  283.     void setDate(int month, int day, int year);
  284.       //Function to set the date.
  285.       //The member variables dMonth, dDay, and dYear are set
  286.       //according to the parameters
  287.       //Postcondition: dMonth = month; dDay = day;
  288.       //               dYear = year
  289.  
  290.     int getDay() const;
  291.       //Function to return the day.
  292.       //Postcondition: The value of dDay is returned.
  293.  
  294.     int getMonth() const;
  295.       //Function to return the month.  
  296.       //Postcondition: The value of dMonth is returned.
  297.  
  298.     int getYear() const;
  299.       //Function to return the year.    
  300.       //Postcondition: The value of dYear is returned.
  301.  
  302.     void printDate() const;
  303.       //Function to output the date in the form mm-dd-yyyy.
  304.  
  305.     bool isLeapYear();
  306.       //Function to determine whether the year is a leap year.
  307.  
  308.     dateType(int month = 1, int day = 1, int year = 1900);
  309.       //Constructor to set the date
  310.       //The member variables dMonth, dDay, and dYear are set
  311.       //according to the parameters
  312.       //Postcondition: dMonth = month; dDay = day;
  313.       //               dYear = year
  314.       //If no values are specified, the default values are
  315.       //used to initialize the member variables.
  316.     int getDateDays();
  317.    
  318.     int getNumDays(int month);
  319.    
  320.     int getDaysPassed();
  321.    
  322.     int getDaysLeft();
  323.    
  324.     void addDays();
  325.  
  326. private:
  327.     int dMonth;      //variable to store the month
  328.     int dDay;        //variable to store the day
  329.     int dYear;       //variable to store the year
  330. };
  331.  
  332. #endif
  333. //extDateType.h---------------------------------------------------------------------------------------------------
  334. #include <string>
  335. #include "dateType.h"
  336. #ifndef extdate_H
  337. #define extdate_H
  338.  
  339. using namespace std;
  340.  
  341. class extDateType: public dateType
  342. {
  343.     public:
  344.         void printLongDate();
  345.     private:
  346.         string month;
  347. };
  348.  
  349. #endif
  350. //dateTypeimp.cpp---------------------------------------------------------------------------------------------------
  351. //Implementation file date
  352.    
  353. #include <iostream>
  354. #include "dateType.h"
  355. #include "extDateType.h"
  356.  
  357. using namespace std;
  358.  
  359. void dateType::setDate(int month, int day, int year)
  360. {
  361.     if (year >= 1)
  362.         dYear = year;
  363.     else
  364.         dYear = 1900;
  365.  
  366.     if (1 <= month && month <= 12)
  367.         dMonth = month;
  368.     else
  369.         dMonth = 1;
  370.  
  371.     switch (dMonth)
  372.     {
  373.     case 1:
  374.     case 3:
  375.     case 5:
  376.     case 7:
  377.     case 8:
  378.     case 10:
  379.     case 12:
  380.         if (1 <= day && day <= 31)
  381.             dDay = day;
  382.         else
  383.             dDay = 1;
  384.         break;
  385.     case 4:
  386.     case 6:
  387.     case 9:
  388.     case 11:
  389.         if (1 <= day && day <= 30)
  390.             dDay = day;
  391.         else
  392.             dDay = 1;
  393.         break;
  394.     case 2:
  395.         if (isLeapYear())
  396.         {
  397.             if (1 <= day && day <= 29)
  398.                 dDay = day;
  399.             else
  400.                 dDay = 1;
  401.         }
  402.         else
  403.         {
  404.             if (1 <= day && day <= 28)
  405.                 dDay = day;
  406.             else
  407.                 dDay = 1;
  408.         }
  409.     }
  410. }
  411.  
  412. int dateType::getDay() const
  413. {
  414.     return dDay;
  415. }
  416.  
  417. int dateType::getMonth() const
  418. {
  419.     return dMonth;
  420. }
  421.  
  422. int dateType::getYear() const
  423. {
  424.     return dYear;
  425. }
  426.  
  427. bool dateType::isLeapYear()
  428. {
  429.  
  430.     //if the year is divisible by 4 it is a leap year
  431.     if (dYear % 4 == 0)
  432.     {
  433.         return true;
  434.     }
  435.     else
  436.     {
  437.         return false;
  438.     }
  439. }
  440.  
  441. int dateType::getDateDays()
  442. {
  443.     int days;
  444.     switch (dMonth)
  445.     {
  446.         if (isLeapYear())
  447.         {
  448.         case 1:
  449.         case 3:
  450.         case 5:
  451.         case 7:
  452.         case 8:
  453.         case 10:
  454.         case 12:
  455.             days = 31;
  456.             break;
  457.         case 4:
  458.         case 6:
  459.         case 9:
  460.         case 11:
  461.             days = 30;
  462.             break;
  463.         case 2:
  464.             if (isLeapYear())
  465.             {
  466.                 days = 29;
  467.             }
  468.             else
  469.             {
  470.                 days = 28;
  471.             }
  472.         }
  473.     }
  474.     return days;
  475. }
  476.  
  477. int dateType::getNumDays(int month)
  478. {
  479.     int days;
  480.     switch (month)
  481.     {
  482.         if (isLeapYear())
  483.         {
  484.         case 1:
  485.         case 3:
  486.         case 5:
  487.         case 7:
  488.         case 8:
  489.         case 10:
  490.         case 12:
  491.             days = 31;
  492.             break;
  493.         case 4:
  494.         case 6:
  495.         case 9:
  496.         case 11:
  497.             days = 30;
  498.             break;
  499.         case 2:
  500.             if (isLeapYear())
  501.             {
  502.                 days = 29;
  503.             }
  504.             else
  505.             {
  506.                 days = 28;
  507.             }
  508.         }
  509.     }
  510.     return days;
  511. }
  512.  
  513. int dateType::getDaysPassed()
  514. {
  515.     int days = 0;
  516.     for (int i = 1; i < dMonth; i++)
  517.     {
  518.         days += getNumDays(i); 
  519.     }
  520.         days += dDay;
  521.     return days;
  522. }
  523.  
  524. int dateType::getDaysLeft()
  525. {
  526.     int days = 0;
  527.     if (isLeapYear())
  528.     {
  529.         days = 366-getDaysPassed();
  530.     }
  531.     else
  532.     {
  533.         days = 365-getDaysPassed();
  534.     }
  535.     return days;
  536. }
  537.  
  538. void dateType::addDays()
  539. {
  540.     int days, newMonth = dMonth, newDay = dDay, newYear = dYear;
  541.     cout << "Enter a number of days to add: ";
  542.     cin >> days;
  543.     cout << endl;
  544.     while (days > 0)
  545.     {
  546.         if (newDay < getNumDays(newMonth))
  547.         {
  548.             days -= 1;
  549.             newDay += 1;
  550.         }
  551.         else
  552.         if (newDay = getNumDays(newMonth) and newMonth < 12)
  553.         {
  554.             days -= 1;
  555.             newMonth += 1;
  556.         }
  557.         else
  558.         {
  559.             days -= 1;
  560.             newYear += 1;
  561.         }
  562.     }
  563.     cout << "The new date is: " << newMonth << "-" << newDay << "-" << newYear;
  564. }
  565.  
  566. void dateType::printDate() const
  567. {
  568.     cout << dMonth << "-" << dDay << "-" << dYear;
  569. }
  570.  
  571.     //constructor
  572. dateType::dateType(int month, int day, int year)
  573. {
  574.     setDate(month, day, year);
  575. }
  576. //extDateType.cpp---------------------------------------------------------------------------------------------------
  577. #include "extDateType.h"
  578. #include "dateType.h"
  579. #include <string>
  580. #include <iostream>
  581.         void extDateType::printLongDate()
  582.         {
  583.             switch (getMonth())
  584.             {
  585.                 case 1:
  586.                     month = "January";
  587.                     break;
  588.                 case 2:
  589.                     month = "February";
  590.                     break;
  591.                 case 3:
  592.                     month = "March";
  593.                     break;
  594.                 case 4:
  595.                     month = "April";
  596.                     break;
  597.                 case 5:
  598.                     month = "May";
  599.                     break;
  600.                 case 6:
  601.                     month = "June";
  602.                     break;
  603.                 case 7:
  604.                     month = "July";
  605.                     break;
  606.                 case 8:
  607.                     month = "August";
  608.                     break;
  609.                 case 9:
  610.                     month = "September";
  611.                     break;
  612.                 case 10:
  613.                     month = "October";
  614.                     break;
  615.                 case 11:
  616.                     month = "November";
  617.                     break;
  618.                 case 12:
  619.                     month = "December";
  620.                     break;
  621.             }
  622.             cout << month << " " << getDay() << ", " << getYear();
  623.         }
  624. //addressBookType.h---------------------------------------------------------------------------------------------------
  625. #include "extPersonType.h"
  626. #include <iostream>
  627. #include <string>
  628.  
  629. using namespace std;
  630.  
  631. class addressBookType: public extPersonType
  632. {
  633.     public:
  634.         extPersonType people[500];
  635.         int entrys;
  636.         //linked list
  637.         extPersonType data;
  638.         addressBookType *link;
  639. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement