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

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

ID: 674658 • 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 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.

Explanation / Answer

a.

Code for addressType:

//Header file addressType.h

#ifndef H_addressType

#define H_addressType

#include <iostream>

#include<string>

using namespace std;

class addressType

{

public:

     void setStreet( string myStreet );

     void setCity( string myCity );

     void setState( string myState );

     void setZIP ( int myZIP );

     string getStreet() const;

     string getCity() const;

     string getState() const;

     int getZIP() const;

     void printAddress() const;

     addressType( string = "", string = "", string = "", int = 0 );

private:

     string street;

     string city;

     string state;

     int ZIP;

};   //   end class addressType

#endif

//Definitions of member functions of class

//function definition for setStreet

void addressType::setStreet( string myStreet )

{

     street = myStreet;

}   

//function definition for setCity

void addressType::setCity( string myCity )

{

     city = myCity;

}   

//function definition for setState

void addressType::setState( string myState )

{

     state = myState;

}

//function definition for setZIP

void addressType::setZIP ( int myZIP )

{

     ZIP = myZIP;

}

//function definition for getStreet

string addressType::getStreet() const

{

     return street;

}   

//function definition for getCity

string addressType::getCity() const

{

     return city;

}   

//function definition for getState

string addressType::getState() const

{

     return state;

}   

//function definition for getZIP

int addressType::getZIP() const

{

     return ZIP;

}   

//function to print address

void addressType::printAddress() const

{

     cout << " " << getStreet() << ", " << getCity() << " " << getState() << " - " << getZIP();

}   

//Constructor of class to initialize data by calling each set member function.

addressType::addressType( string myStreet, string myCity, string myState, int myZIP )

{

     setStreet( myStreet );

     setCity( myCity );

     setState( myState );

     setZIP( myZIP );

}   

b.

Code for class extPersonType:

//Header file extPersonType.h

#ifndef H_extPersonType

#define H_extPersonType

#include <iostream>

#include<string>

#include "addressType.h"

#include "personType.h"

#include "dateType.h"

using namespace std;

class extPersonType

{

public:

void print() const;

void setBirthDate( const dateType );

void getBirthDate( dateType& ) const;

void setAddress( const addressType address );

void getAddress( addressType& address ) const;

void setPerson( const string, const string );

void getPerson( string&, string& )const;

void setAssociation( const string );

string getAssociation() const;

void setPhone( const string );

string getPhone() const;

void setExtPerson( string, string, int, int, int, string, string, string, int, string, string );

extPersonType( string = "", string = "", int = 1, int = 1, int = 1900, string = "", string = "", string = "", int = 0,

string = "", string = "" );

private:

personType person;

dateType birthDate;

addressType myAddress;

string association;

string phone;

}; // end class extPersonType

#endif

//function to set birthDate

void extPersonType::setBirthDate( const dateType myDate )

{

birthDate.setDate( myDate.getMonth(), myDate.getDay(),

myDate.getYear() );

}

//function to obtain birthDate

void extPersonType::getBirthDate( dateType& myDate ) const

{

myDate.setDate( birthDate.getMonth(),

birthDate.getDay(), birthDate.getYear() );

}

//function to setAddress

void extPersonType::setAddress( const addressType address )

{

myAddress.setStreet( address.getStreet() );

myAddress.setCity( address.getCity() );

myAddress.setState( address.getState() );

myAddress.setZIP( address.getZIP() );

}

// function for getAddress

void extPersonType::getAddress(addressType& address ) const

{

address.setStreet( myAddress.getStreet() );

address.setCity( myAddress.getCity() );

address.setState( myAddress.getState() );

address.setZIP( myAddress.getZIP() );

}

//function to print the date, person details, adddress

void extPersonType::print() const

{

birthDate.printDate();

person.print();

myAddress.printAddress();

cout << " " << association << ", " << phone;

}

//function to setPerson name

void extPersonType::setPerson( const string first, const string last )

{

person.setFirstName( first );

person.setLastName( last );

}

//function for getPerson name

void extPersonType::getPerson( string& first, string& last ) const

{

first = person.getFirstName();

last = person.getLastName();

}

//function for setAssociation

void extPersonType::setAssociation( const string myAssociation )

{

association = myAssociation;

}

//function for setPhone

void extPersonType::setPhone( const string myPhone )

{

phone = myPhone;

}

//function for getAssociation

string extPersonType::getAssociation() const

{

return association;

}

//function for getPhone

string extPersonType::getPhone() const

{

return phone;

}

//Function for setExtPerson

void extPersonType::setExtPerson(string first, string last,

int month, int day, int year, string myStreet, string myCity, string myState, int myZIP, string asso, string ph )

{

person.setFirstName( first);

person.setLastName( last );

birthDate.setDate( month, day, year );

myAddress.setStreet( myStreet );

myAddress.setCity( myCity );

myAddress.setState( myState );

myAddress.setZIP( myZIP );

setAssociation( asso );

setPhone( ph );

}

//Constructor for extPersonType

extPersonType::extPersonType( string first, string last, int month, int day, int year, string myStreet,

string myCity, string myState, int myZIP, string asso, string ph )

: person(first, last), birthDate(month, day, year), myAddress( myStreet, myCity, myState, myZIP )

{

setAssociation( asso );

setPhone( ph );

}

Code for class pesonType:

// Header file personType.h

#ifndef H_personType

#define H_personType

#include <iostream>

#include<string>

using namespace std;

class personType

{

public:

void setFirstName( string myFirstName );

void setLastName( string myLastName );

string getFirstName() const;

string getLastName() const;

void print() const;

personType( string = "", string = "" );

private:

string firstName;

string lastName;

}; // end class personType

#endif

//function to set the first name of a person

void personType::setFirstName( string myFirstName )

{

firstName = myFirstName;

}

//function to set the last name of a person

void personType::setLastName( string myLastName )

{

lastName = myLastName;

}

//function to obtain the first name of a person

string personType::getFirstName() const

{

return firstName;

}

//function to obtain the last name of a person

string personType::getLastName() const

{

return lastName;

}

//function to print the first name of a person

void personType::print() const

{

cout << " " << firstName << " " << lastName;

}

//constructor for personType

personType::personType( string myFirstName, string myLastName )

{

setFirstName( myFirstName );

setLastName( myLastName );

}

Code for class dateType:

//Header file dateType.h

#ifndef H_dateType

#define H_dateType

#include <iostream>

#include <ctime>

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

     void printDate()const;

     dateType(int month = 1, int day = 1, int year = 1900);

private:

     int dMonth;

     int dDay;

     int dYear;

};   //   end class dateType

#endif

//function to set the date

void dateType::setDate( int month, int day, int year )

{

     //   get the current date

     char dateStr[9];

     _strdate(dateStr);

     //   obtain the year from the date

     int thisYear = 2000 + ( dateStr[ 6 ] - 48 ) * 10

                                + dateStr[ 7 ] - 48;

     //   check for validity of the year and Assign the year

     if ( ( year >= 1900 ) && ( year <= thisYear ) )

           dYear=year;

     else

           dYear = 1900;

     //   check for validity of the month

     // and Assign the month

     if ( month >=1 && month <= 12 )

           dMonth = month;

     else

           dMonth = 1;

     //Enter the number of days in each month

     int days[ 12 ] = { 31, 28, 31, 30, 31, 30,

                           31, 31, 30, 31, 30, 31 };

     // Condition to check for leap year

     if ( dYear % 400 == 0 || ( dYear % 100 != 0 && dYear % 4 == 0 ) )

           days[ 1 ]++;

     //   check for validity of the day and assign the day.

     // The day should be between 1 and 31.

     if ( day >= 1 && day <= days[ dMonth - 1 ] )

           dDay = day;

     else

           dDay = 1;

}    //   end function setDate

// function to get the date

void dateType::getDate( int& month, int& day, int& year )

{

     month = dMonth;

     day = dDay;

     year = dYear;

}   

// function to get the Day

int dateType::getDay()const

{

     return dDay;

}   

// function to get the month

int dateType::getMonth()const

{

     return dMonth;

}   

//function to get the year

int dateType::getYear()const

{

     return dYear;

}   

//function to print the date

void dateType::printDate()const

{

     cout << " " << dMonth << "-" << dDay << "-" << dYear;

}   

//Constructor for the dateType

dateType::dateType( int month, int day, int year )

{

     setDate( month, day, year );

}

//Function to check for leap year

void dateType::isLeapYear()

{

     //   check for the leap year and print the result

     if ( dYear % 400 == 0 || ( dYear % 100 != 0 && dYear % 4 == 0 ) )

           cout << " Year " << dYear << " is a leap year.";

     else

           cout << " Year " << dYear << " is not a leap year.";

}   

c.

Code for addressBookType:

//Header file addressBookType.h

#ifndef H_addressBookType

#define H_addressBookType

#include <iostream>

#include<string>

#include<fstream>

#include<cstdlib>

#include "extPersonType.h"

using namespace std;

const int MAX_ENTRIES = 500;

class addressBookType

{

public:

addressBookType();

void sortByLastName();

int searchByName() const;

void printAPerson() const;

void printAddressBook() const;

void printByBirthday() const;

void printNamesBetween() const;

void printAssociation() const;

private:

extPersonType persons[ MAX_ENTRIES ];

int ind;

void swap( int, int );

}; // end class addressBookType

#endif

//Function to swap the details in addressBook

void addressBookType::swap( int m, int n )

{

string str1, str2, str3, str4;

// get the swapped names

persons[ m ].getPerson( str1, str2 );

persons[ n ].getPerson( str3, str4 );

// set the swapped names

persons[ m ].setPerson( str3, str4 );

persons[ n ].setPerson( str1, str2 );

dateType bd1, bd2;

// get the swapped birhDates

persons[ m ].getBirthDate( bd1 );

persons[ n ].getBirthDate( bd2 );

// set the swapped birthDates

persons[ m ].setBirthDate( bd2 );

persons[ n ].setBirthDate( bd1 );

// swap the addresses

addressType add1, add2;

// get the swapped addresses

persons[ m ].getAddress( add1 );

persons[ n ].getAddress( add2 );

// set the swapped addresses

persons[ m ].setAddress( add2 );

persons[ n ].setAddress( add1 );

// get the swapped associations

str1 = persons[ m ].getAssociation();

str2 = persons[ n ].getAssociation();

// set the swapped associations

persons[ m ].setAssociation( str2 );

persons[ n ].setAssociation( str1 );

// swap and get the phone numbers

str1 = persons[ m ].getPhone();

str2 = persons[ n ].getPhone();

// set the swapped phone numbers

persons[ m ].setPhone( str2 );

persons[ n ].setPhone( str1 );

}

//Constructor for addressBookType

addressBookType::addressBookType()

{

// declaration of filestream

ifstream inFile;

//variable declaration

string fileName = "";

// prompt the user to input the file name

cout << " Enter input file name : ";

// read the file name

cin >> fileName;

// opening the input file

inFile.open( fileName.c_str() );

// if condition to check whether the input file was opened or not

if ( !inFile.is_open() )

{

cout << " Error reading in input file";

system( "pause" );

exit( 0 );

}

string strs[ 7 ];

int nums[ 4 ];

ind = 0;

//while condition to set details of each person

while ( ( inFile >> strs[0] >> strs[1] >> nums[0] >> nums[1] >> nums[2] >> strs[2] >> strs[3] >> strs[4] >>

nums[3] >> strs[5] >> strs[6] ) != NULL )

{

persons[ ind ].setExtPerson(strs[0], strs[1], nums[0], nums[1], nums[2], strs[2], strs[3], strs[4], nums[3], strs[5], strs[6] );

ind++;

}

}

//function to print the address book

void addressBookType::printAddressBook() const

{

//print details of each person

for ( int i = 0; i < ind; i++ )

{

cout << " ";

persons[ i ].print();

}

}

//function to implement search operation

int addressBookType::searchByName() const

{

string keyName, name, temp;

int count = 0;

// enter the lastname of person to search the addressBook.

cout << " Enter the last name to search : ";

// Read the lastname in the variable keyName

cin >> keyName;

// for loop to go through the address book

for ( int i = 0; i < ind; i++ )

{

//obtain the last names from address book.

persons[ i ].getPerson( temp, name );

// using if statement to compare the names

if ( keyName.compare( name ) == 0 )

return i;

}

return -1;

}

// Print the details of given person

void addressBookType::printAPerson() const

{

int result = searchByName();

// if condition to find the name of a person

if ( result == -1 )

cout<<" No person found as specified by you.";

else

{

cout << " ";

persons[ result ].print();

}

}

// Function to print names of people using Birthdays

void addressBookType::printByBirthday() const

{

//variable declartion

dateType bd;

int month, count = 0;

cout<<" Search address book for a given month of birthdays."

<< " Please remember name is case sensitive.";

// prompt the user to enter the month of the birthday to search

cout << " Enter the month number (1..12) : ";

cin >> month;

// for loop to go through the address book

for ( int i = 0; i < ind; i++ )

{

// obtain the birthDate of evary person

persons[ i ].getBirthDate( bd );

// if statement to compare the birth months

if ( bd.getMonth() == month )

{

// print the names when match is found

cout << " ";

persons[ i ].print();

// increment count value

count++;

}

}

if ( count == 0 )

cout<<" No person found as specified by you.";

}

//Function to print the names between a range

void addressBookType::printNamesBetween() const

{

string keyName1, keyName2, name, temp;

int count = 0;

// prompt the user to enter person name to search

cout <<" Search address book between two given names."

<< " Please remember name is case sensitive.";

cout << " Enter the last name1 : ";

cin >> keyName1;

cout << " Enter the last name2 : ";

cin >> keyName2;

// if condition to swap the names in case of reverse order

if ( keyName1.compare( keyName2 ) > 0 )

keyName1.swap( keyName2 );

// for loop to go through the address book

for ( int i = 0; i < ind; i++ )

{

// obtain the last name of each person

persons[ i ].getPerson( temp, name );

// if statement to compare the names

if ( ( keyName1.compare( name ) <= 0 ) && ( keyName2.compare( name ) >= 0 ) )

{

// print the name when the match is found

cout << " ";

persons[ i ].print();

//increment the value of count

count++;

}

}

if ( count == 0 )

cout<<" No person found as specified by you.";

}

//function for association

void addressBookType::printAssociation() const

{

// variable declaration

string asso;

//defining association types

string types[ 3 ] = { "FamilyMember", "FamilyFriend", "BusinessAssociate" };

int choice, count = 0;

cout << " Search address book by association."<< " Please remember name is case sensitive.";

// prompt the user the related association

cout << " Association Types";

cout << " Family Member : 1";

cout << " Family Friend : 2";

cout << " Business Associate : 3";

cout << " Enter the association type : ";

//Read the association values

cin >> choice;

// use for loop and go through the address book

for ( int i = 0; i < ind; i++ )

{

// get the associations of a person

asso = persons[ i ].getAssociation();

// if statement to compare the association

if ( asso.compare( types[ choice - 1 ] ) == 0 )

{

// print the name when the match is found

cout << " ";

persons[ i ].print();

// increment the value of count

count++;

}

}

// using If statement print error message upon not found

if ( count == 0 )

cout << " No person found as specified by you.";

}

Program Code:

#include <iostream>

#include "addressBookType.h"

using namespace std;

//main program execution

int main()

{

cout << " A program that works with addressBookType.";

// create a object for addressBookType

addressBookType add1;

// Display the addressBook

cout << " Before sorting, the addresses are:";

add1.printAddressBook();

// print the address book

cout << " The addresses are:";

add1.printAddressBook();

// using if statement search for a person name in the address book

if ( add1.searchByName() == -1 )

cout<<" No person found as specified by you.";

else

cout <<" Person found as specified by you.";

// print the person names in address book between 2 given names

add1.printNamesBetween();

// print names in address book for a given birthday month

add1.printByBirthday();

// print names in address upon the association choice of the user

add1.printAssociation();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote