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

In C++, Using classes, design an online address book to keep track of the names,

ID: 3738084 • Letter: I

Question

In C++,

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 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 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.

As well as a separate program with the same requirements but instead stores the address book in a vector object

Explanation / Answer

// C++ code, make sure Address.txt is kept beside the cpp code

#include<iostream>

#include<ctime>

#include<cstdlib>

#include <stdio.h>

#include <fstream>

#include<sstream>

using namespace std;

class addressType

{

private:

string userStreetAddress;

string UserCity;

string UserState;

string UserZipcode;

public:

void print()

{

cout<<" ----- User ADDRESS ----- ";

cout<<" Street: "<<userStreetAddress;

cout<<" UserCity: "<<UserCity;

cout<<" UserState: "<<UserState;

cout<<" UserZipcode: "<<UserZipcode;

}

addressType()

{

userStreetAddress = UserCity = UserState = UserZipcode = "";

}

addressType(string sa, string c, string st, string z)

{

userStreetAddress = sa;

UserCity = c;

UserState = st;

UserZipcode = z;

}

void setuserStreetAddress(string sa)

{

userStreetAddress = sa;

}

string getuserStreetAddress()

{

return userStreetAddress;

}

void setUserCity(string c)

{

UserCity = c;

}

string getUserCity()

{

return UserCity;

}

void setUserState(string sa)

{

UserState = sa;

}

string getUserState()

{

return UserState;

}

void setUserZipcode(string z)

{

UserZipcode = z;

}

string getUserZipcode()

{

return UserZipcode;

}

};

class dateType

{

private:

int date, month, year;

public:

void print()

{

cout<<" Date of Birth: "<<date<<":"<<month<<":"<<year;

}

dateType()

{

date = month = year = 0;

}

dateType(int d, int m, int y)

{

date = d;

month = m;

year = y;

}

void setDate(int d)

{

date = d;

}

int getDate()

{

return date;

}

void setMonth(int m)

{

month = m;

}

int getMonth()

{

return month;

}

void setYear(int y)

{

year = y;

}

int getYear()

{

return year;

}

};

class personType

{

public:

personType(string first = " ", string last = " ")

{

firstName = first;

lastName = last;

}

void print() const

{

cout<<" First Name: "<<firstName<<" Last Name: "<<lastName;

}

void setName(string first, string last)

{

firstName = first;

lastName = last;

}

string getFirstName( ) const

{

return firstName;

}

string getLastName( ) const

{

return lastName;

}

private:

string firstName;

string lastName;

};

class extPersonType : public personType

{

private:

string phoneNumber;

string personStatus;

public:

addressType address;

dateType dateOfBirth;

void print() const

{

cout<<" Phone Number: "<<phoneNumber;

cout<<" Person Type: "<<personStatus;

}

extPersonType()

{ }

extPersonType(addressType a, dateType d, string ph, string ps, string fi, string la):personType(fi, la)

{

address = a;

dateOfBirth = d;

phoneNumber = ph;

personStatus = ps;

}

void setPhoneNumber(string p)

{

phoneNumber = p;

}

string getPhoneNumber()

{

return phoneNumber;

}

void setPersonStatus(string p)

{

personStatus = p;

}

string getPersonStatus()

{

return personStatus;

}

};

class addressBookType

{

private:

extPersonType ept[100];

int numberOfRecord;

public:

int getNumberOfRecord()

{

return numberOfRecord;

}

void readFile();

void displayFile();

void sortLastName();

void searchLastName(string, int);

void duplicateMonth();

void duplicateType(string);

};

void addressBookType::duplicateType(string type)

{

int x, y;

for(x = 0; x < getNumberOfRecord(); x++)

{

if(ept[x].getPersonStatus() == type)

{

cout<<" ******************* Person Information ****************";

ept[x].personType::print();

ept[x].address.print();

ept[x].dateOfBirth.print();

ept[x].print();

}

}

}

void addressBookType::duplicateMonth()

{

int x, y;

int month;

for(x = 0; x < getNumberOfRecord(); x++)

{

month = ept[x].dateOfBirth.getMonth();

for(y = x+1; y < getNumberOfRecord(); y++)

{

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();

}

}

}

}

void addressBookType::searchLastName(string name, int no)

{

int x;

int flag = 0;

for(x = 0; x < getNumberOfRecord(); x++)

{

if(ept[x].personType::getLastName() == name)

{

if(no == 2)

{

cout<<" ******************* Person Information ****************";

ept[x].personType::print();

ept[x].address.print();

ept[x].dateOfBirth.print();

ept[x].print();

}

else

{

cout<<" ******************* Person address phone number and date of birth ****************";

ept[x].address.print();

ept[x].dateOfBirth.print();

ept[x].print();

}

flag = 1;

}

}

if(flag != 1)

cout<<" Record for "<<name<<" not found.";

}

void addressBookType::readFile()

{

int co = 0;

ifstream rFile;

cout << "opening Address.txt file ";

rFile.open("Address.txt");

string f, l;

int d;

while(!rFile.eof())

{

rFile>>f;

rFile>>l;

ept[co].setName(f, l);

rFile>>l;

ept[co].address.setuserStreetAddress(l);

rFile>>l;

ept[co].address.setUserCity(l);

rFile>>l;

ept[co].address.setUserState(l);

rFile>>l;

ept[co].address.setUserZipcode(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);

co++;

}

rFile.close();

numberOfRecord = co;

}

void addressBookType::displayFile()

{

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();

}

}

void addressBookType::sortLastName()

{

int x, y;

extPersonType temp;

for(x = 0; x < getNumberOfRecord(); x++)

{

for(y = 0; y < getNumberOfRecord() - x - 1; y++)

{

if(ept[y].personType::getLastName() > ept[y + 1].personType::getLastName())

{

temp = ept[y];

ept[y] = ept[y + 1];

ept[y + 1] = temp;

}

}

}

}

int menu()

{

int choice;

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";

cout<<" Enter your choice: ";

cin>>choice;

return choice;

}

int main()

{

addressBookType ad;

ad.readFile();

int choice;

string data;

do

{

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!";

}

}while(1);

}



//sample output


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