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

C++ Design an address book that keeps track of the names, addresses, phone numbe

ID: 3586504 • Letter: C

Question

C++

Design an address book that keeps track of the names, addresses, phone numbers, date of birth of family members, friends and business.

Design a class called addressType that defines address as an Abstract Data Type (ADT).

Each object of the class addressType can hold the following information about a book:

a. Street address

b. City

c. State

d. Zip code

Add appropriate constructors and functions to print and store address.

Define a class called extPersonType using these classes:

personType

dateType

addressType

The personType class is shown below.

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;

};

Each object of the class extPersonType can hold the following information about an extPersonType:

address

date of birth

phone number

person Status to differentiate a friend, family members or business associates.

Add (or override) functions to print and store appropriate information. Also add appropriate constructors.

Note that extPersonType will be an inheritance from personType and address data component will be of type addressType and date of birth will be of type dateType

Then define a class called addressBookType that will 2 data components:

a. an array of size 500 of type extPersonType

b. an integer to keep a count of elements in the array.

Add appropriate functions and constructors.

Lastly write a test program to perform the following operations:

1. Load data from file to an addressBookType object.

2. Sort the address book by last name.

3. Search for a person by last name.

4. Print the address phone number and date of birth of a given person (if exist).

5. Print names of people whose birthdays are in the same month.

6. Depending on request, print all family members, friends or business associates.

Explanation / Answer

#include<iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
//Class addressType definition
class addressType
{
private:
//Data member to store street
string streetAddress;
//Data member to store city
string city;
//Data member to store state
string state;
//Data member to store pin
string zipcode;
public:
//Member function to print address
void print()
{
cout<<" ----- ADDRESS ----- ";
cout<<" Street: "<<streetAddress;
cout<<" City: "<<city;
cout<<" State: "<<state;
cout<<" Zipcode: "<<zipcode;
}//End of function

//Default constructor
addressType()
{
streetAddress = city = state = zipcode = "";
}//End of constructor

//Parameterized constructor
addressType(string sa, string c, string st, string z)
{
streetAddress = sa;
city = c;
state = st;
zipcode = z;
}//End of constructor

//Getter and setter methods
void setStreetAddress(string sa)
{
streetAddress = sa;
}//End of function
string getStreetAddress()
{
return streetAddress;
}//End of function
void setCity(string c)
{
city = c;
}//End of function
string getCity()
{
return city;
}//End of function

void setState(string sa)
{
state = sa;
}//End of function
string getState()
{
return state;
}//End of function
void setZipcode(string z)
{
zipcode = z;
}//End of function
string getZipcode()
{
return zipcode;
}//End of function
};//End of class

//Class dateType definition
class dateType
{
private:
//Data member to store date, month, and year
int date, month, year;
public:
//Function to print date of birth
void print()
{
cout<<" Date of Birth: "<<date<<":"<<month<<":"<<year;
}//End of function

//Default constructor
dateType()
{
date = month = year = 0;
}//End of constructor

//Parameterized constructor
dateType(int d, int m, int y)
{
date = d;
month = m;
year = y;
}//End of constructor

//Getter and setter methods
void setDate(int d)
{
date = d;
}//End of function
int getDate()
{
return date;
}//End of function
void setMonth(int m)
{
month = m;
}//End of function
int getMonth()
{
return month;
}//End of function
void setYear(int y)
{
year = y;
}//End of function
int getYear()
{
return year;
}//End of function
};//End of class

//Class personType definition
class personType
{
public:
//Parameterized constructor
personType(string first = " ", string last = " ")
{
firstName = first;
lastName = last;
}//End of constructor

//Function to print person name
void print() const
{
cout<<" First Name: "<<firstName<<" Last Name: "<<lastName;
}//End of function

//Getter and setter methods
void setName(string first, string last)
{
firstName = first;
lastName = last;
}//End of function
string getFirstName( ) const
{
return firstName;
}//End of function
string getLastName( ) const
{
return lastName;
}//End of function

private:
//Data member to store first name and last name
string firstName;
string lastName;
};//End of class

//Class extPersonType derived from personType
class extPersonType : public personType
{
private:
//Data member to store phone number and person status
string phoneNumber;
string personStatus;
public:
//Delegation mechanism
//Declares an object of addressType class and dateType class
addressType address;
dateType dateOfBirth;

//Function to print data
void print() const
{
cout<<" Phone Number: "<<phoneNumber;
cout<<" Person Type: "<<personStatus;
}//End of function
extPersonType()
{ }
//Parameterized constructor
extPersonType(addressType a, dateType d, string ph, string ps, string fi, string la):personType(fi, la)
{
address = a;
dateOfBirth = d;
phoneNumber = ph;
personStatus = ps;
}//End of constructor

//Getter and setter methods
void setPhoneNumber(string p)
{
phoneNumber = p;
}//End of function
string getPhoneNumber()
{
return phoneNumber;
}//End of function
void setPersonStatus(string p)
{
personStatus = p;
}//End of function
string getPersonStatus()
{
return personStatus;
}//End of function
};//End of function

//Class addressBookType definition
class addressBookType
{
private:
//Declares an array of objects of extPersonType class
extPersonType ept[100];
//To store number of records
int numberOfRecord;
public:

//Function to return number of records
int getNumberOfRecord()
{
return numberOfRecord;
}//End of function
//Prototype of the functions
void readFile();
void displayFile();
void sortLastName();
void searchLastName(string, int);
void duplicateMonth();
void duplicateType(string);
};//End of class

//Function to display duplicate person status
void addressBookType::duplicateType(string type)
{
//Loop variable
int x, y;
//Loops till number of records
for(x = 0; x < getNumberOfRecord(); x++)
{
//Checks if current person status is equal to the person status given as parameter then display the person information
if(ept[x].getPersonStatus() == type)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}//End of if condition
}//End of for loop
}//End of function

//Function to display the person whose month of the date of birth matches
void addressBookType::duplicateMonth()
{
int x, y;
int month;
//Loops till number of records
for(x = 0; x < getNumberOfRecord(); x++)
{
//Stores the current month
month = ept[x].dateOfBirth.getMonth();
//Loops till number of records from x index position
for(y = x+1; y < getNumberOfRecord(); y++)
{
//Checks if the current month is equal to previous month then display the person information
if(ept[y].dateOfBirth.getMonth() == month)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();

cout<<" ******************* Person Information ****************";
ept[y].personType::print();
ept[y].address.print();
ept[y].dateOfBirth.print();
ept[y].print();
}//End of if
}//End of inner for
}//End of outer for
}//End of function

//Function to search a person last name and display person information
void addressBookType::searchLastName(string name, int no)
{
int x;
int flag = 0;
//Loops till number of records
for(x = 0; x < getNumberOfRecord(); x++)
{
//Checks if the current person last name is equal to person name in the parameter
if(ept[x].personType::getLastName() == name)
{
//Check if no is 2 then display complete person information
if(no == 2)
{
cout<<" ******************* Person Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}//End of inner if
//If no is 3 display only address phone number and date of birth
else
{
cout<<" ******************* Person address phone number and date of birth ****************";
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}//End of else
flag = 1;
}//End of if
}//End of for
//Checks if flag value is not one person name not found
if(flag != 1)
cout<<" Record for "<<name<<" not found.";
}//End of function

//Function to read data from file and store in the class respective data member
void addressBookType::readFile()
{
int co = 0;
//Creates an object of ifstream
ifstream rFile;
//Opens the file Address.txt for reading
rFile.open("Address.txt");
string f, l;
int d;
//Loops till end of file
while(!rFile.eof())
{
//Read the data from file and stores in data members
rFile>>f;
rFile>>l;
ept[co].setName(f, l);
rFile>>l;
ept[co].address.setStreetAddress(l);
rFile>>l;
ept[co].address.setCity(l);
rFile>>l;
ept[co].address.setState(l);
rFile>>l;
ept[co].address.setZipcode(l);
rFile>>d;
ept[co].dateOfBirth.setDate(d);
rFile>>d;
ept[co].dateOfBirth.setMonth(d);
rFile>>d;
ept[co].dateOfBirth.setYear(d);
rFile>>l;
ept[co].setPhoneNumber(l);
rFile>>l;
ept[co].setPersonStatus(l);
//Increase the counter by one
co++;
}//End of while
//Close file
rFile.close();
//Assigns the counter value to the number of records
numberOfRecord = co;
}//End of function

//Function to display person information
void addressBookType::displayFile()
{
//Loops till end of the record
for(int x = 0; x < numberOfRecord; x++)
{
cout<<" ******************* Person "<<(x + 1)<<" Information ****************";
ept[x].personType::print();
ept[x].address.print();
ept[x].dateOfBirth.print();
ept[x].print();
}//End of for
}//End of function

//Function to sort the person information based on last name
void addressBookType::sortLastName()
{
int x, y;
//Creates an temporary object
extPersonType temp;
//Loops till end of the record
for(x = 0; x < getNumberOfRecord(); x++)
{
//Loops till end of the record minus one and x value
for(y = 0; y < getNumberOfRecord() - x - 1; y++)
{
//Checks if the current person last name is greater than next person last name
if(ept[y].personType::getLastName() > ept[y + 1].personType::getLastName())
{
//Swapping process
temp = ept[y];
ept[y] = ept[y + 1];
ept[y + 1] = temp;
}//End of if
}//End of inner for
}//End of outer for
}//End of function

//Displays menu, accepts user choice and return it
int menu()
{
int choice;
//Displays menu
cout<<" 1. Sort the address book by last name. ";
cout<<" 2. Search for a person by last name. ";
cout<<" 3. Print the address phone number and date of birth of a given person (if exist). ";
cout<<" 4. Print names of people whose birthdays are in the same month. ";
cout<<" 5. Depending on request, print all family members, friends or business associates. ";
cout<<" 6. Exit";
//Accept user choice
cout<<" Enter your choice: ";
cin>>choice;
//Return user choice
return choice;
}//End of function

//Main function definition
int main()
{
//Creates an object of addressBookType class
addressBookType ad;
//Read the file contents
ad.readFile();
int choice;
string data;
//Loops till user choice is not 6
do
{
//Displays menu and stores the user choice
choice = menu();
switch(choice)
{
case 1:
ad.sortLastName();
ad.displayFile();
break;
case 2:
cout<<" Enter the last name to search record: ";
cin>>data;
ad.searchLastName(data, 2);
break;
case 3:
cout<<" Enter the last name to print address phone number and date of birth: ";
cin>>data;
ad.searchLastName(data, 3);
break;
case 4:
ad.duplicateMonth();
break;
case 5:
cout<<" Enter the person type (family / friends / business): ";
cin>>data;
ad.duplicateType(data);
break;
case 6:
exit(0);
default:
cout<<" Invalid choice!";
}//End of switch - case
}while(1);//End of do - while
}//End of main function

Sample Run:


1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 2

Enter the last name to search record: Panda


******************* Person Information ****************
First Name: Ram Last Name: Panda
----- ADDRESS -----
Street: Zebracross City: BBSR State: Orissa Zipcode: 760014
Date of Birth: 37:5:1887
Phone Number: 9055996587
Person Type: friends

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 2

Enter the last name to search record: Padhy

Record for Padhy not found.

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 3

Enter the last name to print address phone number and date of birth: Padhy

Record for Padhy not found.

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 3

Enter the last name to print address phone number and date of birth: Sahu


******************* Person address phone number and date of birth ****************
----- ADDRESS -----
Street: Bankcolony City: Berhampur State: Orissa Zipcode: 760004
Date of Birth: 27:3:1997
Phone Number: 9040996587
Person Type: family

******************* Person address phone number and date of birth ****************
----- ADDRESS -----
Street: Bapuji City: RKL State: Orissa Zipcode: 760114
Date of Birth: 20:4:1977
Phone Number: 9040996587
Person Type: friends

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 2

Enter the last name to search record: Sahu


******************* Person Information ****************
First Name: pyari Last Name: Sahu
----- ADDRESS -----
Street: Bankcolony City: Berhampur State: Orissa Zipcode: 760004
Date of Birth: 27:3:1997
Phone Number: 9040996587
Person Type: family

******************* Person Information ****************
First Name: Punit Last Name: Sahu
----- ADDRESS -----
Street: Bapuji City: RKL State: Orissa Zipcode: 760114
Date of Birth: 20:4:1977
Phone Number: 9040996587
Person Type: friends

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 4


******************* Person Information ****************
First Name: pyari Last Name: Sahu
----- ADDRESS -----
Street: Bankcolony City: Berhampur State: Orissa Zipcode: 760004
Date of Birth: 27:3:1997
Phone Number: 9040996587
Person Type: family

******************* Person Information ****************
First Name: Pyari Last Name: Mohan
----- ADDRESS -----
Street: Anculy City: BBSR State: Orissa Zipcode: 760014
Date of Birth: 12:3:1987
Phone Number: 9050990087
Person Type: family

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 5

Enter the person type (family / friends / business): family


******************* Person Information ****************
First Name: pyari Last Name: Sahu
----- ADDRESS -----
Street: Bankcolony City: Berhampur State: Orissa Zipcode: 760004
Date of Birth: 27:3:1997
Phone Number: 9040996587
Person Type: family

******************* Person Information ****************
First Name: Pyari Last Name: Mohan
----- ADDRESS -----
Street: Anculy City: BBSR State: Orissa Zipcode: 760014
Date of Birth: 12:3:1987
Phone Number: 9050990087
Person Type: family

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 5

Enter the person type (family / friends / business): friends


******************* Person Information ****************
First Name: Ram Last Name: Panda
----- ADDRESS -----
Street: Zebracross City: BBSR State: Orissa Zipcode: 760014
Date of Birth: 37:5:1887
Phone Number: 9055996587
Person Type: friends

******************* Person Information ****************
First Name: Punit Last Name: Sahu
----- ADDRESS -----
Street: Bapuji City: RKL State: Orissa Zipcode: 760114
Date of Birth: 20:4:1977
Phone Number: 9040996587
Person Type: friends

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 5

Enter the person type (family / friends / business): business


******************* Person Information ****************
First Name: Rakesh Last Name: Padhi
----- ADDRESS -----
Street: Langipoli City: RKL State: Orissa Zipcode: 760004
Date of Birth: 11:8:1900
Phone Number: 9055900817
Person Type: business

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 8

Invalid choice!

1. Sort the address book by last name.
2. Search for a person by last name.
3. Print the address phone number and date of birth of a given person (if exist).
4. Print names of people whose birthdays are in the same month.
5. Depending on request, print all family members, friends or business associates.
6. Exit
Enter your choice: 6