Please do not copy and paste. Must be a somewhat original answer. Using classes,
ID: 3772092 • Letter: P
Question
Please do not copy and paste. Must be a somewhat original answer.
Using classes, design an online address book to keep track of the names, addresses, phone numbers, and dates ofbirth offamily members, close friends, and certain business associates. Your program should be able to handle a maximum of 500 entries.
a. Define a class, addressType, that can store a street address, city, state, and zip code. Use the appropriate functions to print and store the address. Also, use constructors to automatically initialize the data members.
b. Define a class extPersonType using the class personType (as defined in Example 1-12, Chapter 1), the class dateType (as designed in Programming Exercise 2 ofChapter 2), and the class addressType.Add a data member to this class to classify the person as a family member, friend, or business associate. Also, add a data member to store the phone number. Add (or override) the functions to print and store the appropriate information. Use constructors to automatically initialize the data members.
c. Derive the class addressBookType from the class arrayListType,as defined in this chapter, so that an object oftype addressBookType can store objects of type extPersonType.Anobjectoftype addressBookType should be able to process a maximum of 500 entries. Add necessary operations to the class addressBookType so that the program should perform the following operations:
i. Load the data into the address book from a disk.
ii. Search for a person by last name.
iii. Print the address, phone number, and date of birth (if it exists) of a given person.
iv. Print the names ofthe people whose birthdays are in a given month or between two given dates.
v. Print the names of all the people having the same status, such as family, friend, or business.
vi. Print the names of all the people between two last names
As a follow up to the original question
Redo Programming Exercise 9 of Chapter 3 so that the address book is stored in a vector object.
Explanation / Answer
#include "addressType.h"
#include "Person_Type_Extra.h"
#include "dataType.h"
addressType::addressType(void)
{
StreetAddress="";
City="";
State="";
Zip="";
}
addressType::addressType(string address, string city, string state, string zip)
{
StreetAddress=address;
City=city;
State=state;
Zip=zip;
}
void addressType::printAddress()
{
cout<<"Street Address: "<<StreetAddress<<endl;
cout<<"City : "<<City<<endl;
cout<<"State : "<<State<<endl;
cout<<"Zip code : "<<Zip<<endl;
}
string addressType::getStreet()
{
return StreetAddress;
}
string addressType::getCity()
{
return City;
}
string addressType::getState()
{
return State;
}
string addressType::getZip()
{
return Zip;
}
Datatype.cpp
#include "Person_Type_Extra.h"
#include "addressType.h"
#include "dataType.h"
dateType::dateType(int m, int d, int y)
{
month=m;
day=d;
year=y;
}
int dateType::getDay()
{
return day;
}
int dateType::getMonth()
{
return month;
}
int dateType::getYear()
{
return year;
}
void dateType::printDate()
{
cout<<"DOB: "<<month<<"/"<<day<<"/"<<year<<endl;
}
#include "Person_Type_Extra.h"
#include "dataType.h"
#include "addressType.h"
#include<fstream>
char displayMenu();
void readFromFile();
void writeToFile();
void sort();
class arrayListType
{
Person_Type_Extra *arrayList;
int size;
public:
arrayListType(int n=500);
Person_Type_Extra & operator[](int i);
void removeLast();// remove last element
void add (const Person_Type_Extra &);// add new element
int getSize()const;// get arrayList size
};
arrayListType::arrayListType(int n)
{
arrayList=new Person_Type_Extra[n];
size=n;
}
int arrayListType::getSize()const
{
return size;
}
void arrayListType::removeLast()
{
size--;
}
void arrayListType::add(const Person_Type_Extra &p)
{
arrayList[size++]=p;
}
Person_Type_Extra & arrayListType::operator[](int i)
{
return arrayList[i];
}
class addressBookType :public arrayListType
{
private:
Person_Type_Extra persons;
public:
void print(int i);// print personal data of [i] person
};
void addressBookType::print(int i)
{
persons.printPersonDetails();
}
int count=0;
addressBookType addressbook;
int main()
{
bool flag=true;
char ch;
readFromFile();
do
{
ch=displayMenu();
switch(ch)
{
case '1':
{
string firstName;
string lastName;
string StreetAddress;
string City;
string State;
string Zip;
string phone;
int month, day, year;
int Type;
cout<<"Enter first name: ";
cin>>firstName;
cout<<"Enter last name: ";
cin>>lastName;
cin.ignore();
cout<<"Enter street addess: ";
getline(cin,StreetAddress);
cout<<"Enter city: ";
cin>>City;
cout<<"Enter State: ";
cin>>State;
cout<<"Enter Zip: ";
cin>>Zip;
cout<<"Enter date of birth: "<<endl;
cout<<"Enter month: ";
cin>>month;
cout<<"Enter day: ";
cin>>day;
cout<<"Enter year: ";
cin>>year;
cout<<"Enter 1-Family member, 2-friend, 3-business associate: ";
cin>>Type;
cout<<"Enter phone number: ";
cin>>phone;
addressbook[count].setPerson(personType(firstName, lastName));
addressbook[count].setDOB(dateType(month, day, year));
addressbook[count].setAddress(addressType(StreetAddress, City, State, Zip));
addressbook[count].setType(Type);
addressbook[count].setPhone(phone);
count++;
}
break;
case '2'://>/>/Sort by last name
{
sort();
}
break;
case '3'://>/>/search by last name
{
int total=0;
string ln;
cout<<"Enter last name to search: ";
cin>>ln;
for(int i=0;i<count;i++)
{
if(addressbook[i].getPerson().getLastName() == ln)
{
addressbook[i].printPersonDetails();
addressbook[i].getType();
cout<<endl<<"-----------------------------"<<endl;
total++;
}
}
cout<<endl<<"Match entries found: "<<total<<endl;
}
break;
case '4'://>/>/Print the address, phone number, and date of birth
{
for(int i=0;i<count;i++)
{
addressbook[i].printPersonDetails();
addressbook[i].getType();
cout<<endl<<"-----------------------------"<<endl;
}
cout<<endl<<"Total entries: "<<count<<endl;
}
break;
case '5'://>/>/Print the names of the people whose birthdays are in a given month
{
int total=0;
int month;
cout<<"Enter month of DOB to search: ";
cin>>month;
for(int i=0;i<count;i++)
{
if(addressbook[i].getDate().getMonth() == month)
{
addressbook[i].printPersonDetails();
addressbook[i].getType();
cout<<endl<<"-----------------------------"<<endl;
total++;
}
}
cout<<endl<<"Match entries found: "<<total<<endl;
}
break;
case '6'://>/>/Print the names of all the people between two last names
{
sort();
string ln1, ln2;
cout<<"Enter last name 1: ";
cin>>ln1;
cout<<"Enter last name 2: ";
cin>>ln2;
for(int i=0;i<count;i++)
{
if(ln1 <= addressbook[i].getPerson().getLastName() && addressbook[i].getPerson().getLastName()<=ln2)
{
addressbook[i].printPersonDetails();
addressbook[i].getType();
cout<<endl<<"-----------------------------"<<endl;
}
}
}
break;
case '7'://>/>/Depending on the user's request, print the names of all family members friends, or business associates
{
int total=0;
int type;
cout<<"Enter 1-Family member, 2-friend, 3-business associate: ";
cin>>type;
for(int i=0;i<count;i++)
{
if(addressbook[i].getIntType()==type)
{
addressbook[i].printPersonDetails();
addressbook[i].getType();
cout<<endl<<"-----------------------------"<<endl;
total++;
}
}
cout<<endl<<"Match entries found: "<<total<<endl;
}
break;
case '0':
{
writeToFile();
flag=false;
}
break;
}
}while(flag);
system("pause");
return 0;
}
char displayMenu()
{
char ch;
do{
cout<<endl<<"1. Add a address"<<endl;
cout<<"2. Sort by last name"<<endl;
cout<<"3. Search by last name"<<endl;
cout<<"4. Print the address, phone number, and date of birth "<<endl;
cout<<"5. Print the names of the people whose birthdays are in a given month"<<endl;
cout<<"6. Print the names of all the people between two last names"<<endl;
cout<<"7. Depending on the user's request, print the names of all family members friends, or business associates"<<endl;
cout<<"0. Exit"<<endl;
cout<<"Enter option: ";
cin>>ch;
if(ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='0')
return ch;
cout<<endl<<"ERROR: Invalid option entered"<<endl;
}while(true);
}
void readFromFile()
{
ifstream fin;
fin.open("addresses.txt");
string firstName;
string lastName;
string StreetAddress;
string City;
string State;
string Zip;
int month, day, year;
int Type;
string phone;
while(fin>>firstName)
{
fin>>lastName;
fin.ignore();
getline(fin,StreetAddress);
fin>>City;
fin>>State;
fin>>Zip;
fin>>month;
fin>>day;
fin>>year;
fin>>Type;
fin>>phone;
addressbook[count].setPerson(personType(firstName, lastName));
addressbook[count].setDOB(dateType(month, day, year));
addressbook[count].setAddress(addressType(StreetAddress, City, State, Zip));
addressbook[count].setType(Type);
addressbook[count].setPhone(phone);
count++;
}
fin.close();
}
void sort()
{
for(int i=0;i<count;i++)
{
for(int j=i+1;j<count;j++)
{
if(addressbook[i].getPerson().getLastName() > addressbook[j].getPerson().getLastName())
{
Person_Type_Extra tPerson;
tPerson = addressbook[i].getPerson();
addressbook[i].setPerson(addressbook[j].getPerson());
addressbook[j].setPerson(tPerson);
addressType tAddress;
tAddress = addressbook[i].getAddress();
addressbook[i].setAddress(addressbook[j].getAddress());
addressbook[j].setAddress(tAddress);
dateType tdate;
tdate = addressbook[i].getDate();
addressbook[i].setDOB(addressbook[j].getDate());
addressbook[j].setDOB(tdate);
int ttype;
ttype=addressbook[i].getIntType();
addressbook[i].setType(addressbook[j].getIntType());
addressbook[j].setType(ttype);
}
}
}
}
Expersiontype.cpp
#include "addressType.h"
#include "dataType.h"
#include "Person_Type_Extra.h"
Person_Type_Extra::Person_Type_Extra()
{
}
Person_Type_Extra::Person_Type_Extra(Person_Type_Extra person, dateType dob, addressType address, string ph, int type)
{
Person=person;
DOB=dob;
Address=address;
Type=type;
phonenumber=ph;
}
void Person_Type_Extra::setType(int type)
{
if(type>=1 && type<=3)
Type = type;
else
Type=1;
}
string Person_Type_Extra::getType()
{
if(Type==1)
return "Family Member";
else if (Type==2)
return "Friend";
else if(Type==3)
return "Business Associate";
}
void Person_Type_Extra::printPersonDetails()
{
Person.print();
Address.printAddress();
DOB.printDate();
cout<<getType();
cout<<endl<<"Phone: "<<phonenumber<<endl;
}
void Person_Type_Extra::setPhone(string ph)
{
phonenumber=ph;
}
string Person_Type_Extra::getPhone()
{
return phonenumber;
}
void Person_Type_Extra::setPerson(personType person)
{
Person=person;
}
void Person_Type_Extra::setDOB(dateType dob)
{
DOB=dob;
}
void Person_Type_Extra::setAddress(addressType address)
{
Address=address;
}
personType Person_Type_Extra::getPerson()
{
return Person;
}
dateType Person_Type_Extra::getDate()
{
return DOB;
}
int Person_Type_Extra::getIntType()
{
return Type;
}
addressType Person_Type_Extra::getAddress()
{
return Address;
}
Datatype.h
#include<iostream>
#include<string>
using namespace std;
class dateType
{
int month, day, year;
public:
dateType(int m=1, int d=1, int y=1900);
int getDay();
int getMonth();
int getYear();
void printDate();
};
Person_Type_Extra.h
#include<iostream>
#include<string>
using namespace std;
class Person_Type_Extra
{
public:
string firstName;
string lastName;
public:
Person_Type_Extra();
Person_Type_Extra(string first, string last);
void print() const;
void setName(string first, string last);
string getFirstName() const;
string getLastName() const;
};
AddressType.h
#include<iostream>
#include<string>
using namespace std;
class addressType
{
private:
string StreetAddress;
string City;
string State;
string Zip;
public:
addressType(void);
addressType(string address, string city, string state, string zip);
void printAddress();
string getStreet();
string getCity();
string getState();
string getZip();
};