Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please be original work. (Online Address Book Revisited) Programming Exercise 9

ID: 3773613 • Letter: P

Question

Please be original work.

(Online Address Book Revisited) Programming Exercise 9 in Chapter 3 could handle a maximum of only 500 entries. Using linked lists, redo the program to handle as many entries as required. Add the following operations to your program: a. Add or delete a new entry to the address book. b. When the program terminates, write the data in the address book to a disk.

Here is the code I submitted for the Chapter 3 Exercise 9

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include<vector>

using namespace std;
class addressType
{
string streetAddress;
string city;
string state;
int pinCode;
  
public:
addressType(){}
addressType(string, string, string, int);
void displayAddress();
  
};
addressType::addressType(string a, string b, string c, int d)
{
streetAddress = a;
city = b;
state =c;
pinCode = d;
}
void addressType::displayAddress()
{
cout << "The address is: " << endl;
cout << "Street Address: " << streetAddress << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "Zip Code: " << pinCode << endl;
}
class personType
{
string firstName;
string lastName;
  
public:
personType(){}
personType(string, string);
  
void displayFullName();
  
void setFullName(string,string);
  
string fetchFirstName();
  
string fetchLastname();
  
};
personType::personType(string a, string b)
{
firstName = a;
lastName = b;
}
void personType::displayFullName()
{
cout << "The persons name is: " << endl;
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
  
}
void personType::setFullName(string first, string last)
{
firstName = first;
lastName = last;
}
string personType::fetchFirstName()
{
return firstName;
}
string personType::fetchLastname()
{
return lastName;
}
class dateType
{
int month;
int day;
int year;
  
public:
dateType(){}
dateType(int, int, int);
  
void displayDate();
  
void setCompleteDate(int, int, int);
  
int fetchDay();
int fetchMonth();
int fetchYear();
  
};
dateType::dateType(int a, int b, int c)
{
month = a;
day = b;
year = c;
}
void dateType::displayDate()
{
cout << "The date is: ";
cout << day << "/" << month <<"/" << year << endl;
  
}
void dateType:: setCompleteDate(int a, int b, int c)
{
if(a >= 1 && a <= 12)
month = a;
  
if(b >= 1 && b <= 7)
day = b;
  
if(c >=1900 && c <= 2013)
year = c;
}
int dateType::fetchDay()
{
return day;
}
int dateType::fetchMonth()
{
return month;
}
int dateType::fetchYear()
{
return year;
}
class extPersonType: public personType, public dateType, public addressType
{
string personStatus;
int phoneNumber;
public:
extPersonType(){}
extPersonType(string first, string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int phone);
void displayPersonalInfo();
string fetchPersonStatus();
int fetchPhoneNumber();
};
extPersonType::extPersonType(string first, string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int phone) : personType(first, last), dateType(month, day, year), addressType(streetAdr, city, state, pin)
{
personStatus = status;
phoneNumber = phone;
}
void extPersonType::displayPersonalInfo()
{

cout << "Personal status: " << personStatus << endl;
cout << "Phone Number: " << phoneNumber << endl;
}
string extPersonType::fetchPersonStatus()
{
return personStatus;
}
int extPersonType::fetchPhoneNumber()
{
return phoneNumber;
}
  
class addressBookType: public extPersonType
{
private:
vector<extPersonType> list;
public:
addressBookType(){}
void add_newPerson(string first, string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int ph);
  
void search_lastName(string);
  
void display_details(string, string);
  
void fetchNames_birthdayMonth(int);
  
void fetchNames_birthdayDates(int, int);
  
void fetchNames_sameStatus(string);
  
void fetchNames_betweenLast(string, string);
  
};
void addressBookType::add_newPerson(string first,string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int ph)
{
extPersonType *p = new extPersonType(first, last, month, day, year, streetAdr, city, state, pin, status, ph);
list.push_back(*p);
}
void addressBookType::search_lastName(string last)
{
for(int i = 0; i < list.size(); i++)
{
if(list[i].fetchLastname() ==last)
{
cout << "The personal details are: " << endl;
list[i].displayPersonalInfo();
}
}
}
void addressBookType::display_details(string first, string last)
{
for(int i = 0; i < list.size(); i++)
{
if((list[i].fetchFirstName() == first) && (list[i].fetchLastname() == last))
{
cout << "The personal details are: " << endl;
list[i].displayPersonalInfo();
}
}
}
void addressBookType::fetchNames_birthdayMonth(int month)
{
for(int i = 0; i < list.size(); i++)
{
if(list[i].fetchMonth() == month)
cout<< list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;
}
}
void addressBookType::fetchNames_birthdayDates(int date1, int date2)
{
for(int i = 0; i < list.size(); i++)
{
if((list[i].fetchDay() >= date1) && (list[i].fetchDay() <= date2))
cout << list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;
}
}
void addressBookType::fetchNames_sameStatus(string status)
{
for(int i = 0; i < list.size(); i++)
  
{
if(list[i].fetchPersonStatus() == status)
cout << list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;
}
}
void addressBookType::fetchNames_betweenLast(string last1, string last2)
{
for(int i = 0; i < list.size(); i++)
{
if((last1.compare(list[i].fetchLastname())<0)&&(last2.compare(list[i].fetchLastname())>0))
cout << list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;   
}
}
int main ()
{
addressBookType *obj = new addressBookType();
  
obj->add_newPerson("Leanna", "Smith", 3, 17, 1976, "Sector-1", "Columbia", "Missouri", 65298, "friend", 5553539999);
obj->add_newPerson("Kimberly", "Fulton", 11, 29, 1972, "Sector-1", "Columbia", "Missouri", 65298, "business associate", 5553531111);
obj->add_newPerson("Ariel", "Mathers", 2, 29, 2001, "Sector-1", "Columbia", "Missouri", 65298, "family", 5553543221);
  
string lastName = "";
  
cout << "Enter the last name to search: " << endl;
cin >> lastName;
  
obj->search_lastName(lastName);
  
string firstName = "";
cout << "Search by first name: " << endl;
cin >> firstName;
  
lastName = "";
cout << "Search by last name: " << endl;
cin >> lastName;
obj->display_details(firstName, lastName);
  
int month;
  
cout << "Search by birth month(month number): ";
cin >> month;
cout << endl;
  
obj->fetchNames_birthdayMonth(month);
  
int date1, date2;
  
cout << "Search between dates: ";
cout << "Enter the first date: ";
cin >> date1;
  
cout << "Enter the second date: ";
cin >> date2;
  
obj->fetchNames_birthdayDates(date1, date2);
  
int choice;
  
string status = "";
  
cout << "Enter the status to search: " << endl;
  
  
cout << "1. Family 2.Friend 3.Business Associate: ";
  
cin >> choice;
  
if(choice==1)
status = "family";
  
else if(choice==2)
status = "friend";
  
else if(choice==3)
status = "business";
  
  
cout << endl;
  
obj->fetchNames_sameStatus(status);
  
string lastName1 = "";
  
cout << "Search between 2 last names: ";
cout << "Enter the first last name " << endl;
cin >> lastName1;
string lastName2 = "";
  
cout << "Enter the second last name: " << endl;
cin >> lastName2;
cout << "Results:" << endl;
obj->fetchNames_betweenLast(lastName1, lastName2);
return 0;
}

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include<vector>
using namespace std;
class addressType
{
string streetAddress;
string city;
string state;
int pinCode;
  
public:
addressType(){}
addressType(string, string, string, int);
void displayAddress();
  
};
addressType::addressType(string a, string b, string c, int d)
{
streetAddress = a;
city = b;
state =c;
pinCode = d;
}
void addressType::displayAddress()
{
cout << "The address is: " << endl;
cout << "Street Address: " << streetAddress << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "Zip Code: " << pinCode << endl;
}
class personType
{
string firstName;
string lastName;
  
public:
personType(){}
personType(string, string);
  
void displayFullName();
  
void setFullName(string,string);
  
string fetchFirstName();
  
string fetchLastname();
  
};
personType::personType(string a, string b)
{
firstName = a;
lastName = b;
}
void personType::displayFullName()
{
cout << "The persons name is: " << endl;
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
  
}
void personType::setFullName(string first, string last)
{
firstName = first;
lastName = last;
}
string personType::fetchFirstName()
{
return firstName;
}
string personType::fetchLastname()
{
return lastName;
}
class dateType
{
int month;
int day;
int year;
  
public:
dateType(){}
dateType(int, int, int);
  
void displayDate();
  
void setCompleteDate(int, int, int);
  
int fetchDay();
int fetchMonth();
int fetchYear();
  
};
dateType::dateType(int a, int b, int c)
{
month = a;
day = b;
year = c;
}
void dateType::displayDate()
{
cout << "The date is: ";
cout << day << "/" << month <<"/" << year << endl;
  
}
void dateType:: setCompleteDate(int a, int b, int c)
{
if(a >= 1 && a <= 12)
month = a;
  
if(b >= 1 && b <= 7)
day = b;
  
if(c >=1900 && c <= 2013)
year = c;
}
int dateType::fetchDay()
{
return day;
}
int dateType::fetchMonth()
{
return month;
}
int dateType::fetchYear()
{
return year;
}
class extPersonType: public personType, public dateType, public addressType
{
string personStatus;
int phoneNumber;
public:
extPersonType(){}
extPersonType(string first, string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int phone);
void displayPersonalInfo();
string fetchPersonStatus();
int fetchPhoneNumber();
};
extPersonType::extPersonType(string first, string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int phone) : personType(first, last), dateType(month, day, year), addressType(streetAdr, city, state, pin)
{
personStatus = status;
phoneNumber = phone;
}
void extPersonType::displayPersonalInfo()
{
cout << "Personal status: " << personStatus << endl;
cout << "Phone Number: " << phoneNumber << endl;
}
string extPersonType::fetchPersonStatus()
{
return personStatus;
}
int extPersonType::fetchPhoneNumber()
{
return phoneNumber;
}
  
class addressBookType: public extPersonType
{
private:
std::list<extPersonType*> list;
public:
addressBookType(){}
void add_newPerson(string first, string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int ph);
  
void search_lastName(string);
  
void display_details(string, string);
  
void fetchNames_birthdayMonth(int);
  
void fetchNames_birthdayDates(int, int);
  
void fetchNames_sameStatus(string);
  
void fetchNames_betweenLast(string, string);
  
};
void addressBookType::add_newPerson(string first,string last, int month, int day, int year, string streetAdr, string city, string state, int pin, string status, int ph)
{
extPersonType *p = new extPersonType(first, last, month, day, year, streetAdr, city, state, pin, status, ph);
list.insert(list.begin(), *p);
}

void addressBookType::delete_entry(string last){
for(int i=0;i<list.size();i++){
if(list[i].fetchLastname() == last){
list.remove(list[i]);
}
}
}

void addressBookType::printToAddressBook(void){
//outputting to file
ofstream out_data("output.txt");

for(int i = 0; i < list.size(); i++)
{
if((list[i].fetchFirstName() == first) && (list[i].fetchLastname() == last))
{
out_data << "Personal status: " << list[i].personStatus << "Phone Number: " << list[i].phoneNumber<<" "<< endl;
}
}
}


void addressBookType::search_lastName(string last)
{
for(int i = 0; i < list.size(); i++)
{
if(list[i].fetchLastname() ==last)
{
cout << "The personal details are: " << endl;
list[i].displayPersonalInfo();
}
}
}
void addressBookType::display_details(string first, string last)
{
for(int i = 0; i < list.size(); i++)
{
if((list[i].fetchFirstName() == first) && (list[i].fetchLastname() == last))
{
cout << "The personal details are: " << endl;
list[i].displayPersonalInfo();
}
}
}
void addressBookType::fetchNames_birthdayMonth(int month)
{
for(int i = 0; i < list.size(); i++)
{
if(list[i].fetchMonth() == month)
cout<< list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;
}
}
void addressBookType::fetchNames_birthdayDates(int date1, int date2)
{
for(int i = 0; i < list.size(); i++)
{
if((list[i].fetchDay() >= date1) && (list[i].fetchDay() <= date2))
cout << list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;
}
}
void addressBookType::fetchNames_sameStatus(string status)
{
for(int i = 0; i < list.size(); i++)
  
{
if(list[i].fetchPersonStatus() == status)
cout << list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;
}
}
void addressBookType::fetchNames_betweenLast(string last1, string last2)
{
for(int i = 0; i < list.size(); i++)
{
if((last1.compare(list[i].fetchLastname())<0)&&(last2.compare(list[i].fetchLastname())>0))
cout << list[i].fetchFirstName() << " " << list[i].fetchLastname() << endl;   
}
}
int main ()
{
addressBookType *obj = new addressBookType();
  
obj->add_newPerson("Leanna", "Smith", 3, 17, 1976, "Sector-1", "Columbia", "Missouri", 65298, "friend", 5553539999);
obj->add_newPerson("Kimberly", "Fulton", 11, 29, 1972, "Sector-1", "Columbia", "Missouri", 65298, "business associate", 5553531111);
obj->add_newPerson("Ariel", "Mathers", 2, 29, 2001, "Sector-1", "Columbia", "Missouri", 65298, "family", 5553543221);
  
string lastName = "";
  
cout << "Enter the last name to search: " << endl;
cin >> lastName;
  
obj->search_lastName(lastName);
  
string firstName = "";
cout << "Search by first name: " << endl;
cin >> firstName;
  
lastName = "";
cout << "Search by last name: " << endl;
cin >> lastName;
obj->display_details(firstName, lastName);
  
int month;
  
cout << "Search by birth month(month number): ";
cin >> month;
cout << endl;
  
obj->fetchNames_birthdayMonth(month);
  
int date1, date2;
  
cout << "Search between dates: ";
cout << "Enter the first date: ";
cin >> date1;
  
cout << "Enter the second date: ";
cin >> date2;
  
obj->fetchNames_birthdayDates(date1, date2);
  
int choice;
  
string status = "";
  
cout << "Enter the status to search: " << endl;
  
  
cout << "1. Family 2.Friend 3.Business Associate: ";
  
cin >> choice;
  
if(choice==1)
status = "family";
  
else if(choice==2)
status = "friend";
  
else if(choice==3)
status = "business";
  
  
cout << endl;
  
obj->fetchNames_sameStatus(status);
  
string lastName1 = "";
  
cout << "Search between 2 last names: ";
cout << "Enter the first last name " << endl;
cin >> lastName1;
string lastName2 = "";
  
cout << "Enter the second last name: " << endl;
cin >> lastName2;
cout << "Results:" << endl;
obj->fetchNames_betweenLast(lastName1, lastName2);
return 0;
}
Hide