Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp---------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstring>
- #include "extPersonType.h"
- #include "personType.h"
- #include "addressType.h"
- #include "dateType.h"
- #include "extDateType.h"
- #include "addressBookType.h"
- using namespace std;
- //check if the input file exists
- int openFile(ifstream &infile)
- {
- infile.open("Assn2_Q1_Data.txt");
- if (!&infile)
- {
- cout << "Cannot open the input file. Program terminates!"
- << endl;
- return 1;
- }
- return 0;
- }
- //sort the address book by last name
- void addressSort(addressBookType &book)
- {
- for(int i = 0; i < 5; i++)
- {
- for(int ii = i+1; ii < 6; ii++)
- {
- if (strcmp(book.people[i].getLastName().c_str(), book.people[ii].getLastName().c_str())>0)
- {
- extPersonType temp = book.people[i];
- book.people[i] = book.people[ii];
- book.people[ii] = temp;
- }
- }
- }
- }
- void printBook(addressBookType book)
- {
- for(int i = 0; i < 6; i++)
- {
- cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
- book.people[i].printDate();
- cout << endl;
- cout << "Address: " << book.people[i].street << endl;
- cout << "City: " << book.people[i].city << endl;
- cout << "State: " << book.people[i].state << endl;
- cout << "Zip: " << book.people[i].zipCode << endl;
- cout << "Phone: " << book.people[i].phoneNumber << endl;
- cout << "Status: " << book.people[i].status << endl;
- cout << "---------------------------------------------------" << endl;
- }
- }
- void lastNameSearch(addressBookType book)
- {
- string search;
- bool found = false;
- cout << "enter a last name to search for: ";
- cin >> search;
- cout << endl;
- for(int i = 0; i < 6; i++) if (found == false)
- {
- if (search == book.people[i].getLastName())
- {
- found = true;
- cout << "Search results for: " << search << endl;
- cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
- book.people[i].printDate();
- cout << endl;
- cout << "Address: " << book.people[i].street << endl;
- cout << "City: " << book.people[i].city << endl;
- cout << "State: " << book.people[i].state << endl;
- cout << "Zip: " << book.people[i].zipCode << endl;
- cout << "Phone: " << book.people[i].phoneNumber << endl;
- cout << "Status: " << book.people[i].status << endl;
- }
- }
- }
- void sameBirthMonth(addressBookType book)
- {
- int search;
- cout << "enter a birth month to search for (1-12): ";
- cin >> search;
- cout << endl;
- cout << "Individuals with the same birth month of: " << search << endl << endl;;
- for(int i = 0; i < 6; i++)
- {
- if (search == book.people[i].getMonth())
- {
- cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
- }
- }
- }
- void searchStatus(addressBookType book)
- {
- string search;
- cout << "enter a status to search for (Family, Friend, Business): ";
- cin >> search;
- cout << endl;
- cout << search << ":" << endl << endl;;
- for(int i = 0; i < 6; i++)
- {
- if (search == book.people[i].status)
- {
- cout << book.people[i].getFirstName() << " " << book.people[i].getLastName() << endl;
- }
- }
- }
- int main()
- {
- addressBookType book;
- ifstream infile;
- openFile(infile);
- //build the array from the file
- for(int i = 0; i < 6; i++)
- {
- string first, last;
- int m, d, y;
- //
- infile >> first;
- infile >> last;
- book.people[i].setName(first, last);
- infile >> m;
- infile >> d;
- infile >> y;
- book.people[i].setDate(m, d, y);
- getline(infile,book.people[i].street);
- getline(infile,book.people[i].street);
- getline(infile,book.people[i].city);
- getline(infile,book.people[i].state);
- getline(infile,book.people[i].zipCode);
- getline(infile,book.people[i].phoneNumber);
- getline(infile,book.people[i].status);
- }
- //build the linked list from the file
- addressBookType *head, *current, *newNode;
- head = NULL;
- for(int i = 0; i < 6; i++)
- {
- //create nodes
- newNode = new addressBookType;
- //
- string first, last;
- int m, d, y;
- infile >> first;
- infile >> last;
- newNode->setName(first, last);
- infile >> m;
- infile >> d;
- infile >> y;
- newNode->setDate(m, d, y);
- getline(infile,newNode->street);
- getline(infile,newNode->street);
- getline(infile,newNode->city);
- getline(infile,newNode->state);
- getline(infile,newNode->zipCode);
- getline(infile,newNode->phoneNumber);
- getline(infile,newNode->status);
- //
- newNode->link = NULL;
- head = newNode;
- current = newNode;
- }
- cout << endl;
- current->getFirstName(); cout << " "; current->getLastName();
- infile.close();
- return 0;
- }
- //extPersonType.h---------------------------------------------------------------------------------------------------
- #include <string>
- #include "personType.h"
- #include "addressType.h"
- #include "dateType.h"
- #include "extDateType.h"
- #ifndef extPersonType_H
- #define extPersonType_H
- using namespace std;
- class extPersonType: public personType, public dateType, public addressType
- {
- public:
- addressType address;
- dateType dateOfBirth;
- string phoneNumber;
- string status;
- };
- #endif
- //personType.h---------------------------------------------------------------------------------------------------
- #include <string>
- #ifndef personType_H
- #define personType_H
- using namespace std;
- class personType
- {
- public:
- void print() const;
- void setName(string first, string last);
- string getFirstName() const;
- string getLastName() const;
- personType(string first = "", string last = "");
- private:
- string firstName;
- string lastName;
- };
- #endif
- //personType.cpp---------------------------------------------------------------------------------------------------
- #include "personType.h"
- #include <string>
- #include <iostream>
- using namespace std;
- personType::personType(string first, string last)
- {
- }
- void personType::print() const
- {
- cout << "Name: " << firstName << " " << lastName << endl;
- }
- void personType::setName(string first, string last)
- {
- firstName = first;
- lastName = last;
- }
- string personType::getFirstName() const
- {
- return firstName;
- }
- string personType::getLastName() const
- {
- return lastName;
- }
- //addressType.h---------------------------------------------------------------------------------------------------
- #include <string>
- #ifndef addressType_H
- #define addressType_H
- using namespace std;
- class addressType
- {
- public:
- string street;
- string city;
- string state;
- string zipCode;
- };
- #endif
- //dateType.h---------------------------------------------------------------------------------------------------
- //dateType.h
- #ifndef date_H
- #define date_H
- class dateType
- {
- public:
- void setDate(int month, int day, int year);
- //Function to set the date.
- //The member variables dMonth, dDay, and dYear are set
- //according to the parameters
- //Postcondition: dMonth = month; dDay = day;
- // dYear = year
- int getDay() const;
- //Function to return the day.
- //Postcondition: The value of dDay is returned.
- int getMonth() const;
- //Function to return the month.
- //Postcondition: The value of dMonth is returned.
- int getYear() const;
- //Function to return the year.
- //Postcondition: The value of dYear is returned.
- void printDate() const;
- //Function to output the date in the form mm-dd-yyyy.
- bool isLeapYear();
- //Function to determine whether the year is a leap year.
- dateType(int month = 1, int day = 1, int year = 1900);
- //Constructor to set the date
- //The member variables dMonth, dDay, and dYear are set
- //according to the parameters
- //Postcondition: dMonth = month; dDay = day;
- // dYear = year
- //If no values are specified, the default values are
- //used to initialize the member variables.
- int getDateDays();
- int getNumDays(int month);
- int getDaysPassed();
- int getDaysLeft();
- void addDays();
- private:
- int dMonth; //variable to store the month
- int dDay; //variable to store the day
- int dYear; //variable to store the year
- };
- #endif
- //extDateType.h---------------------------------------------------------------------------------------------------
- #include <string>
- #include "dateType.h"
- #ifndef extdate_H
- #define extdate_H
- using namespace std;
- class extDateType: public dateType
- {
- public:
- void printLongDate();
- private:
- string month;
- };
- #endif
- //dateTypeimp.cpp---------------------------------------------------------------------------------------------------
- //Implementation file date
- #include <iostream>
- #include "dateType.h"
- #include "extDateType.h"
- using namespace std;
- void dateType::setDate(int month, int day, int year)
- {
- if (year >= 1)
- dYear = year;
- else
- dYear = 1900;
- if (1 <= month && month <= 12)
- dMonth = month;
- else
- dMonth = 1;
- switch (dMonth)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- if (1 <= day && day <= 31)
- dDay = day;
- else
- dDay = 1;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- if (1 <= day && day <= 30)
- dDay = day;
- else
- dDay = 1;
- break;
- case 2:
- if (isLeapYear())
- {
- if (1 <= day && day <= 29)
- dDay = day;
- else
- dDay = 1;
- }
- else
- {
- if (1 <= day && day <= 28)
- dDay = day;
- else
- dDay = 1;
- }
- }
- }
- int dateType::getDay() const
- {
- return dDay;
- }
- int dateType::getMonth() const
- {
- return dMonth;
- }
- int dateType::getYear() const
- {
- return dYear;
- }
- bool dateType::isLeapYear()
- {
- //if the year is divisible by 4 it is a leap year
- if (dYear % 4 == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int dateType::getDateDays()
- {
- int days;
- switch (dMonth)
- {
- if (isLeapYear())
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- days = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = 30;
- break;
- case 2:
- if (isLeapYear())
- {
- days = 29;
- }
- else
- {
- days = 28;
- }
- }
- }
- return days;
- }
- int dateType::getNumDays(int month)
- {
- int days;
- switch (month)
- {
- if (isLeapYear())
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- days = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = 30;
- break;
- case 2:
- if (isLeapYear())
- {
- days = 29;
- }
- else
- {
- days = 28;
- }
- }
- }
- return days;
- }
- int dateType::getDaysPassed()
- {
- int days = 0;
- for (int i = 1; i < dMonth; i++)
- {
- days += getNumDays(i);
- }
- days += dDay;
- return days;
- }
- int dateType::getDaysLeft()
- {
- int days = 0;
- if (isLeapYear())
- {
- days = 366-getDaysPassed();
- }
- else
- {
- days = 365-getDaysPassed();
- }
- return days;
- }
- void dateType::addDays()
- {
- int days, newMonth = dMonth, newDay = dDay, newYear = dYear;
- cout << "Enter a number of days to add: ";
- cin >> days;
- cout << endl;
- while (days > 0)
- {
- if (newDay < getNumDays(newMonth))
- {
- days -= 1;
- newDay += 1;
- }
- else
- if (newDay = getNumDays(newMonth) and newMonth < 12)
- {
- days -= 1;
- newMonth += 1;
- }
- else
- {
- days -= 1;
- newYear += 1;
- }
- }
- cout << "The new date is: " << newMonth << "-" << newDay << "-" << newYear;
- }
- void dateType::printDate() const
- {
- cout << dMonth << "-" << dDay << "-" << dYear;
- }
- //constructor
- dateType::dateType(int month, int day, int year)
- {
- setDate(month, day, year);
- }
- //extDateType.cpp---------------------------------------------------------------------------------------------------
- #include "extDateType.h"
- #include "dateType.h"
- #include <string>
- #include <iostream>
- void extDateType::printLongDate()
- {
- switch (getMonth())
- {
- case 1:
- month = "January";
- break;
- case 2:
- month = "February";
- break;
- case 3:
- month = "March";
- break;
- case 4:
- month = "April";
- break;
- case 5:
- month = "May";
- break;
- case 6:
- month = "June";
- break;
- case 7:
- month = "July";
- break;
- case 8:
- month = "August";
- break;
- case 9:
- month = "September";
- break;
- case 10:
- month = "October";
- break;
- case 11:
- month = "November";
- break;
- case 12:
- month = "December";
- break;
- }
- cout << month << " " << getDay() << ", " << getYear();
- }
- //addressBookType.h---------------------------------------------------------------------------------------------------
- #include "extPersonType.h"
- #include <iostream>
- #include <string>
- using namespace std;
- class addressBookType: public extPersonType
- {
- public:
- extPersonType people[500];
- int entrys;
- //linked list
- extPersonType data;
- addressBookType *link;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement