Using classes, design an online address book to keep track of the names, address
ID: 667237 • Letter: U
Question
Using classes, design an online address book to keep track of the names,
addresses, phone numbers, and dates of birth of family 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 of Chapter 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 of type addressBookType can store objects of type extPersonType. An object of type addressBookType should be able to process a maximum of 500 entries. Add necessary operations to the class addressBookType so that the program should performthe 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 of the 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.
Explanation / Answer
due to character limitation we cant include code of orderedLinkedList.h and linkedListType.h
PersonType.h
#ifndef _PersonType_H
#define _PersonType_H
#include <string>
#include <iostream>
using namespace std;
class PersonType
{
private:
string firstName; //variable holder for the first name
string lastName; //variable holder for the last name
public:
void setName(string f, string l);
//A function that will set the first and last name
string getFirstName() const;
//return the first name
string getLastName() const;
//return the last name
void getName(string& f, string& l) const;
//return both first and last name
void print() const;
//default constructor
PersonType();
//constructor that accepts 2 parameter
PersonType(string f, string l);
};
#endif
PersonType.cpp
#include "PersonType.h"
#include <string>
#include <iostream>
using namespace std;
void PersonType::setName(string f, string l)
{
firstName = f; //assign f to firstName
lastName = l; //assign l to lastName
}
string PersonType::getFirstName() const
{
return firstName;
}
string PersonType::getLastName() const
{
return lastName;
}
void PersonType::getName(string& f, string& l) const
{
f = firstName;
l = lastName;
}
void PersonType::print() const
{
//print the last name then first
cout << lastName << " " << firstName << endl;
}
PersonType::PersonType()
{
firstName = "";
lastName = "";
}
PersonType::PersonType(string n, string l)
{
firstName = n;
lastName = l;
}
ExtPersonType.h
#ifndef _ExtPersonType_h
#define _ExtPersonType_h
#include "PersonType.h"
#include "AddressType.h"
#include "DateType.h"
#include <iostream>
#include <string>
using namespace std;
class ExtPersonType : public PersonType
{
private:
string phoneNumber;
string personStatus;
AddressType address;
DateType dob;
public:
void printAddress() const;
//call the print function of the PersonType then call the print function of the addressType
void printInfo() const;
//functionm to print name, date of birth, phone nummber, person's status, and address
void setInfo(string f, string l, int m, int d, int y, string street, string city, string state, string zipcode, string phone, string person);
//function that accepts 11 parameters to set a person's information
void setInfo(string f, string l, AddressType a, DateType d, string phone, string status);
//function that accepts 6 parameters to set a person's information
ExtPersonType(string f, string l, int m, int d, int y, string street, string city, string state, string zipcode, string phone, string person);
//constructor that accepts 11 parameters
ExtPersonType();
//default constructor
string getStatus() const;
//funtion that return the personStatus
AddressType getAddress() const;
//function to return the object address
DateType getDate() const;
//function to return the object dob
string getPhoneNumber() const;
//function to return phoneNumber
bool operator==(ExtPersonType& rhs) const;
//function to overload operator ==
bool operator!=(ExtPersonType& rhs) const;
//function to overload operator ==
};
#endif
ExtPersonType.cpp
#include "ExtPersonType.h"
#include "AddressType.h"
#include "DateType.h"
#include <iostream>
#include <string>
using namespace std;
void ExtPersonType::printAddress() const
{
this->print();
address.print();
}
void ExtPersonType::printInfo() const
{
this->print();
dob.printDate();
cout << "Phone Number: " << phoneNumber << endl <<
"Person Type: " << personStatus << endl;
address.print();
}
void ExtPersonType::setInfo(string f, string l, int m, int d, int y, string street, string city, string state, string zipcode, string phoneN, string stat)
{
this->setName(f, l);
dob.setDate(m, d, y);
address.setAddress(street, city, state, zipcode);
phoneNumber = phoneN;
personStatus = stat;
}
void ExtPersonType::setInfo(string firstName, string lastName, AddressType addressType, DateType dateType, string phoneN, string stat)
{
string st, city, state, zip;
int m, d, y;
this->setName(firstName, lastName);
addressType.getAddress(st, city, state, zip);
address.setAddress(st, city, state, zip);
dateType.getDate(m, d, y);
dob.setDate(m, d, y);
phoneNumber = phoneN;
personStatus = stat;
}
ExtPersonType::ExtPersonType(string f, string l, int m, int d, int y, string street, string city, string state, string zipcode, string phoneN, string stat)
{
this->setName(f, l);
dob.setDate(m, d, y);
address.setAddress(street, city, state, zipcode);
phoneNumber = phoneN;
personStatus = stat;
}
ExtPersonType::ExtPersonType()
{
}
string ExtPersonType::getStatus() const
{
return personStatus;
}
AddressType ExtPersonType::getAddress() const
{
return address;
}
DateType ExtPersonType::getDate() const
{
return dob;
}
string ExtPersonType::getPhoneNumber() const
{
return phoneNumber;
}
bool ExtPersonType::operator==(ExtPersonType& rhs) const
{
return (this->getFirstName() == rhs.getFirstName() &&
this->getLastName() == rhs.getLastName() &&
this->getAddress() == rhs.getAddress() &&
this->getDate() == rhs.getDate() &&
this->getStatus() == rhs.getStatus() &&
this->getPhoneNumber() == rhs.getPhoneNumber());
}
bool ExtPersonType::operator!=(ExtPersonType& rhs) const
{
return !(this->getFirstName() == rhs.getFirstName() &&
this->getLastName() == rhs.getLastName() &&
this->getAddress() == rhs.getAddress() &&
this->getDate() == rhs.getDate() &&
this->getStatus() == rhs.getStatus() &&
this->getPhoneNumber() == rhs.getPhoneNumber());
}
DateType.h
#ifndef H_DateType
#define H_DateType
#include <iostream>
using namespace std;
class DateType
{
public:
void setDate(int month, int day, int year);
void getDate(int &month, int &day, int &year);
int getDay() const;
int getMonth() const;
int getYear() const;
void printDate() const;
//Function to print the date in the format: Month/Day/Year
bool operator==(DateType& rhs) const;
DateType(int month, int day, int year);
//defalt constructor
DateType();
// constructor
private:
int month; // variable to store the month
int day; // variable to store the day
int year; // variable to store the year
bool isLeapYear(); //check if year is a leap year
};
#endif
DateType.cpp
#include <iostream>
#include "DateType.h"
using namespace std;
DateType::DateType(){
month = 1;
day = 1;
year = 1900;
}
DateType::DateType(int month, int day, int year){
this->month = month;
this->day = day;
this->year = year;
}
void DateType::setDate(int month, int day, int year){
this->day = day;
this->month = month;
this->year = year;
}
int DateType::getDay() const{
return day;
}
int DateType::getMonth() const{
return month;
}
int DateType::getYear() const{
return year;
}
void DateType::getDate(int &month, int &day, int &year){
this->month = month;
this->day = day;
this->year = year;
}
bool DateType::operator==(DateType& rhs) const
{
return (this->day == rhs.day &&
this->month == rhs.month &&
this->year == rhs.year);
}
void DateType::printDate() const{
cout << month <<"/" << day << "/" << year << endl;
}
bool DateType::isLeapYear(){
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
AddressType.h
#define H_AddressType
#include <iostream>
#include <string>
using namespace std;
class AddressType
{
public:
void setAddress(string, string, string, string);
void getAddress(string &streetAddress, string &city, string &state, string &zipCode);
void print()const;
bool operator==(AddressType& rhs) const;
AddressType();
//constructor
AddressType(string &streetAddress, string &city, string &state, string &zipCode);
//defalt constructor
private:
string streetAddress; // variable to store the streetAddress
string city; // variable to store the city
string state; // variable to store the state
string zipCode; // variable to store the zipcode
};
#endif
AddressType.cpp
#include <iostream>
#include <string>
#include "AddressType.h"
using namespace std;
void AddressType::setAddress(string strAddr, string c, string st, string z){
streetAddress = strAddr;
city = c;
state = st;
zipCode = z;
}
void AddressType::getAddress(string &streetAddress, string &city, string &state, string &zipCode){
streetAddress = this->streetAddress;
city = this->city;
state = this->state;
zipCode = this->zipCode;
}
void AddressType::print()const{
cout << streetAddress << endl << city << ", " << state << " " << zipCode << endl;
}
bool AddressType::operator==(AddressType& rhs) const{
return (this->streetAddress == rhs.streetAddress &&
this->city == rhs.city &&
this->state == rhs.state &&
this->zipCode == rhs.zipCode);
}
AddressType::AddressType(){
streetAddress = "";
city = "";
state = "";
zipCode = "";
}
AddressType::AddressType(string &streetAddress, string &city, string &state, string &zipCode){
this->streetAddress = streetAddress;
this->city = city;
this->state = state;
this->zipCode = zipCode;
}
AddressBookType.h
#include <iostream>
#include <fstream>
#include "orderedlinkedlist.h"
#include "ExtPersonType.h"
using namespace std;
class AddressBookType : public orderedLinkedList<ExtPersonType>
{
public:
void print();
void printNameInTheMonth(int);
int search(string);
void printInfoOf(string);
void printNamesWithStatus(string);
void printAt(int);
void printNamesBetweenLastNames(string, string);
void saveData(ofstream&);
bool lexicalCompare(string, string);
void AddressBookType::deletePerson(const string firstName, const string lastName);
nodeType<ExtPersonType>* firstNode();
AddressBookType();
};
// Constructor - Default
AddressBookType::AddressBookType()
{
// Intentionally left blank
}
// Print - Default
void AddressBookType::print()
{
nodeType<ExtPersonType> *currentNode;
if (!(currentNode = firstNode()))
{
return;
}
while (currentNode != NULL)
{
currentNode->info.printInfo();
cout << endl;
currentNode = currentNode->link;
}
}
// Print - Name In The Month
void AddressBookType::printNameInTheMonth(int month)
{
nodeType<ExtPersonType> *currentNode;
if (!(currentNode = firstNode()))
{
return;
}
while (currentNode != NULL)
{
if (currentNode->info.getDate().getMonth() == month)
currentNode->info.print();
currentNode = currentNode->link;
}
}
// Print - Info Of
void AddressBookType::printInfoOf(string lastName)
{
nodeType<ExtPersonType> *currentNode;
if (!(currentNode = firstNode()))
{
return;
}
int index = search(lastName);
if (index != -1)
{
for (int i = 0; i < index; i++)
currentNode = currentNode->link;
currentNode->info.printInfo();
}
else
{
cout << "Person not found!" << endl << endl;
}
}
// Print - Names with Status
void AddressBookType::printNamesWithStatus(string status)
{
nodeType<ExtPersonType> *currentNode = first;
while (currentNode != NULL)
{
if (currentNode->info.getStatus() == status)
currentNode->info.print();
currentNode = currentNode->link;
}
}
// Print - Print At
void AddressBookType::printAt(int index)
{
nodeType<ExtPersonType> *currentNode;
if (!(currentNode = firstNode()))
{
return;
}
// Progress the node to the correct index
for (int i = 0; i < index; i++)
{
currentNode = currentNode->link;
// Check to see if the index is out of bounds
if (currentNode == NULL)
{
cout << "Index out of bounds!" << endl << endl;
return;
}
}
// Print the information of the node
currentNode->info.printInfo();
}
nodeType<ExtPersonType>* AddressBookType::firstNode()
{
if (first != NULL)
return first;
else
{
cout << "No list is present!" << endl << endl;
return NULL;
}
}
// Print - Print Names Between Last Names
void AddressBookType::printNamesBetweenLastNames(string s1, string s2)
{
nodeType<ExtPersonType> *currentNode;
if (!(currentNode = firstNode()))
{
return;
}
while (currentNode != NULL)
{
string lastName = currentNode->info.getLastName();
if (lexicalCompare(lastName, s1) && lexicalCompare(s2, lastName))
currentNode->info.print();
currentNode = currentNode->link;
}
}
bool AddressBookType::lexicalCompare(string s1, string s2)
{
if (s1 == s2) return true;
int i = 0;
for (;;)
{
if (NULL == s1[i]) return false;
if (NULL == s2[i]) return true;
char a, b;
a = tolower(s1[i]);
b = tolower(s2[i]);
if (a < b) return false;
if (b < a) return true;
i++;
}
}
// Search
int AddressBookType::search(string lastName)
{
int index = 0;
nodeType<ExtPersonType> *currentNode;
if (!(currentNode = firstNode()))
{
return -1;
}
while (currentNode != NULL)
{
if (currentNode->info.getLastName() == lastName)
return index;
currentNode = currentNode->link;
index++;
}
return -1;
}
// Save Data
void AddressBookType::saveData(ofstream& outFile)
{
nodeType<ExtPersonType> *currentNode;
currentNode = firstNode();
int entryCount = 0;
while (currentNode != NULL)
{
DateType d = currentNode->info.getDate();
AddressType a = currentNode->info.getAddress();
string aMembers[4];
a.getAddress(aMembers[0], aMembers[1], aMembers[2], aMembers[3]);
outFile << currentNode->info.getFirstName() << " " << currentNode->info.getLastName() << endl;
outFile << d.getMonth() << " " << d.getDay() << " " << d.getYear() << endl;
outFile << aMembers[0] << endl;
outFile << aMembers[1] << endl;
outFile << aMembers[2] << endl;
outFile << aMembers[3] << endl;
outFile << currentNode->info.getPhoneNumber() << endl;
outFile << currentNode->info.getStatus();
if (entryCount++ + 1 != this->count)
outFile << endl;
currentNode = currentNode->link;
}
}
//DeletingPerson
void AddressBookType::deletePerson(const string firstName, const string lastName)
{
ExtPersonType personInList;
nodeType<ExtPersonType> *currentNode;
currentNode = firstNode();
while (currentNode != NULL)
{
if (currentNode->info.getFirstName() == firstName && currentNode->info.getLastName() == lastName)
{
personInList = currentNode->info;
this->deleteNode(personInList);
cout << firstName << " " << lastName << " has been deleted. " << endl << endl;
return;
}
currentNode = currentNode->link;
}
cout << firstName << " " << lastName << " was not found. " << endl << endl;
}
AddressBookType.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "AddressBookType.h"
#include "ExtPersonType.h"
#define ENTRY_SIZE 8
using namespace std;
void showMenu();
void resetInput();
void optionOne();
void optionTwo();
void optionThree();
void optionFour();
void optionFive();
void optionSix();
void optionSeven();
void optionEight();
AddressBookType addressBook;
int main()
{
ifstream inFile;
inFile.open("data.txt", ifstream::in);
if (inFile.is_open())
{
while (inFile.good())
{
// Entries are read in this format.
string firstName, lastName;
int month, day, year;
string street;
string city;
string state;
string zipCode;
string phoneNumber;
string status;
string line[ENTRY_SIZE];
for (int i = 0; i < ENTRY_SIZE; i++)
{
getline(inFile, line[i]);
}
istringstream iss(line[0]);
iss >> firstName >> lastName;
iss.clear();
iss.str(line[1]);
iss >> month >> day >> year;
street = line[2];
city = line[3];
state = line[4];
zipCode = line[5];
phoneNumber = line[6];
status = line[7];
ExtPersonType person(firstName, lastName,
month, day, year,
street, city, state, zipCode,
phoneNumber, status);
addressBook.insert(person);
}
}
else
{
cout << "Error: File Not Found" << endl;
system("PAUSE");
return 1;
}
int userSelection = 0;
while (userSelection != 9)
{
cout << "++=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=++" << endl;
cout << "Welcome to the address book program." << endl << endl;
cout << "Choose among the following options." << endl << endl;
showMenu();
for (;;)
{
cout << "Your choice : ";
if (cin >> userSelection)
{
if (userSelection >= 1 && userSelection <= 9)
{
break;
}
else
{
cout << "Please pick a number between 1 and 9!" << endl;
resetInput();
}
}
else
{
cout << "Please, use only integers to select an option." << endl;
resetInput();
}
}
switch(userSelection)
{
case 1:
//See if person is in address book
optionOne();
break;
case 2:
// Print a person's full info
optionTwo();
break;
case 3:
// Print people with birthday in specified month
optionThree();
break;
case 4:
// Print people between two last names
optionFour();
break;
case 5:
// Print people of a particular status
optionFive();
break;
case 6:
// Print the entire address book
optionSix();
break;
case 7:
//Add/Delete a person
optionSeven();
break;
case 8:
// Save data
optionEight();
break;
case 9:
// Terminate program
break;
default:
break;
}
}
system("PAUSE");
return 0;
}
void showMenu()
{
cout << "1: To see if a person is in the address book" << endl;
cout << "2: Print the information of a person" << endl;
cout << "3: Print the names of a person having a birthday in a particular month" << endl;
cout << "4: Print the names of persons between two last names" << endl;
cout << "5: Print the names of person having a particular status" << endl;
cout << "6: Print the address book" << endl;
cout << "7: Add/Delete a person to the address book" << endl;
cout << "8: Save data" << endl;
cout << "9: Terminate the program" << endl;
cout << endl;
}
void optionOne()
{
string lastName;
for (;;)
{
cout << "Enter the last name of the person: ";
if (cin >> lastName)
{
int loc = addressBook.search(lastName);
if (loc != -1)
{
cout << lastName << " found at location " << loc + 1 << endl << endl;
}
else
{
cout << "Entry not found." << endl << endl;
}
return;
}
else
{
cout << "Invalid input!" << endl;
resetInput();
}
}
}
void optionTwo()
{
string lastName;
for (;;)
{
cout << "Enter the last name of the person: ";
if (cin >> lastName)
{
int loc = addressBook.search(lastName);
if (loc != -1)
{
cout << endl;
addressBook.printInfoOf(lastName);
cout << endl << endl;
}
else
{
cout << "Entry not found." << endl << endl;
}
return;
}
else
{
cout << "Invalid input!" << endl << endl;
resetInput();
}
}
}
void optionThree()
{
int birthMonth;
for (;;)
{
cout << "Enter the birthday month you wish to search: ";
if (cin >> birthMonth)
{
cout << endl;
addressBook.printNameInTheMonth(birthMonth);
cout << endl << endl;
return;
}
else
{
cout << "Invalid input!" << endl;
resetInput();
}
}
}
void optionFour()
{
string startName, endName;
for (;;)
{
cout << "Enter starting last name: ";
if (cin >> startName)
{
cout << "Enter ending last name: ";
if (cin >> endName)
{
cout << endl;
addressBook.printNamesBetweenLastNames(startName, endName);
cout << endl << endl;
return;
}
else
{
cout << "Invalid input!" << endl;
resetInput();
}
}
else
{
cout << "Invalid input!" << endl;
resetInput();
}
}
}
void optionFive()
{
string status;
for (;;)
{
cout << "Enter the status you wish to search: ";
if (cin >> status)
{
cout << endl;
addressBook.printNamesWithStatus(status);
cout << endl << endl;
return;
}
else
{
cout << "Invalid input!" << endl;
resetInput();
}
}
}
void optionSix()
{
addressBook.print();
return;
}
void optionSeven()
{
int answer = 0, month = 0, day = 0, year = 0;
string lastName = "", firstName = "", streetAddress = " ", city = "",
state = "", zipcode = "", phoneNumber = "", status = "";
cout << "Enter 1 to add or 2 to delete: ";
cin >> answer;
while(answer != 1 && answer != 2)
{
cout << "Invalid choice! Enter 1 or 2 only" << endl;
cout << "Enter 1 to add or 2 to delete: ";
cin >> answer;
}
if (answer == 1)
{
cout << "Enter first name: ";
resetInput();
getline(cin, firstName);
cout << endl << "Enter last name: ";
getline(cin, lastName);
cout << endl << "Enter birth month: ";
cin >> month;
cout << endl << "Enter birth day: ";
cin >> day;
cout << endl << "Enter birth year: ";
cin >> year;
cout << endl << "Enter street address: ";
resetInput();
getline(cin, streetAddress);
cout << endl << "Enter city: ";
getline(cin, city);
cout << endl << "Enter state: ";
getline(cin, state);
cout << endl << "Enter zip code: ";
cin >> zipcode;
cout << endl << "Enter phone number: ";
cin >> phoneNumber;
cout << endl << "Enter personal status (Family, Friend, or Business): ";
cin >> status;
cout << endl;
//assign the values to newPerson
ExtPersonType newPerson(firstName, lastName, month, day, year, streetAddress, city, state, zipcode, phoneNumber, status);
//insert it to linklist
addressBook.insert(newPerson);
}
else
{
cout << "Enter the person's last name to be deleted: ";
cin >> lastName;
cout << endl << "Enter the person's first name to be deleted: ";
cin >> firstName;
//call function to delete the matching info in the list
addressBook.deletePerson(firstName, lastName);
}
}
void optionEight()
{
ofstream outFile;
outFile.open("output.txt");
addressBook.saveData(outFile);
outFile.close();
}
void resetInput()
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.