I want you to create the address book with a dynamic array (chapter 10 pointers)
ID: 3866221 • Letter: I
Question
I want you to create the address book with a dynamic array (chapter 10 pointers). You may rehash YOUR previous program (not someone else's program) or you may start it from scratch.
Create a class called AddressBook. When the object is created, the programmer should specify the maximum number of items in the addressbook.
(e.g.) AddressBook myAddressBook(20);
It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return “Person Not Found”. The class should have a method called display to show all the entries in the address book sorted by last name (last name, first name). Create a main function to test your class. Inside the main ask for five names and enter them into the address book. In the main, look for one that is in the address book and test for a name that is not in the address book. Finally, in the main, display the names in alphabetical order from the address book. You may have the sort inside the class AddressBook.
Explanation / Answer
Given below is the c++ code for the question along with output. Please do rate the answer if it helped. Thank you.
AddressBook.h
#ifndef AddressBook_h
#define AddressBook_h
#include <iostream>
using namespace std;
typedef struct
{
string firstname;
string lastname;
string phone;
}Entry;
class AddressBook
{
Entry *entries;
int maxSize;
int currentSize;
void sort();
public:
AddressBook(int size);
~AddressBook();
bool isFull();
bool addName(string fname, string lname, string phone);
string findName(string name);
void display();
};
#endif /* AddressBook_h */
AddressBook.cpp
#include "AddressBook.h"
#include <iomanip>
AddressBook::AddressBook(int size)
{
entries = new Entry[size];
maxSize = size;
currentSize = 0;
}
AddressBook::~AddressBook()
{
delete []entries;
}
bool AddressBook::isFull()
{
return (currentSize == maxSize);
}
bool AddressBook::addName(string fname, string lname, string phone)
{
if(isFull())
return false;
else
{
entries[currentSize].firstname = fname;
entries[currentSize].lastname = lname;
entries[currentSize].phone = phone;
currentSize++;
return true;
}
}
string AddressBook::findName(string name)
{
string currName;
for(int i = 0; i < currentSize; i++)
{
currName = entries[i].lastname + ", " + entries[i].firstname;
if(currName == name)
return entries[i].phone;
}
return "Person Not Found"; // when no match was found
}
void AddressBook::sort()
{
int minIdx;
string minName, currName;
//selection sort
for(int i = 0; i < currentSize; i++)
{
minIdx = i;
minName = entries[i].lastname + ", " + entries[i].firstname;
for(int j = i + 1; j < currentSize; j++)
{
currName = entries[j].lastname + ", " + entries[j].firstname;
if(currName < minName)
{
minIdx = j;
minName = currName;
}
}
if(i != minIdx)
{
Entry temp = entries[i];
entries[i] = entries[minIdx];
entries[minIdx] = temp;
}
}
}
void AddressBook::display()
{
sort(); //first sort and then display
string name;
cout << "The list of entries in AddressBook are - " << endl;
cout << setw(30) << left << "Name" << " Phone" << endl;
for(int i = 0; i < currentSize; i++)
{
name = entries[i].lastname + ", " + entries[i].firstname;
cout << setw(30) << left << name << " " << entries[i].phone << endl;
}
cout << endl;
}
AddressBookMain.cpp
#include "AddressBook.h"
#include <iostream>
using namespace std;
int main()
{
AddressBook myAddrBook(20);
string fname, lname, phone;
//ask for 5 entries
cout << "Enter 5 names and phone numbers - ";
for(int i = 1; i <= 5; i ++)
{
cout << " Entry #" << i << endl;
cout << " Firstname: ";
cin >> fname;
cout << " Lastname: ";
cin >> lname;
cout << " Phone: ";
cin >> phone;
myAddrBook.addName(fname, lname, phone);
}
string ans = "y", search;
while(ans == "y" || ans == "Y")
{
cin.ignore(); //flush newline
cout << "Enter a name to search (lastname, firstname): " << endl;
getline(cin, search);
cout << myAddrBook.findName(search) << endl << endl;
cout << "Search another (y/n)? ";
cin >> ans;
}
//display the addressbook;
myAddrBook.display();
}
output
Enter 5 names and phone numbers -
Entry #1
Firstname: Michael
Lastname: Jackson
Phone: 11111
Entry #2
Firstname: Richard
Lastname: Taylor
Phone: 222222
Entry #3
Firstname: Alexander
Lastname: Williams
Phone: 333333
Entry #4
Firstname: Robert
Lastname: Brown
Phone: 4444444
Entry #5
Firstname: David
Lastname: Jones
Phone: 555555
Enter a name to search (lastname, firstname):
William, Alexander
Person Not Found
Search another (y/n)? y
Enter a name to search (lastname, firstname):
Williams, Alexander
333333
Search another (y/n)? y
Enter a name to search (lastname, firstname):
Jackson, Michael
11111
Search another (y/n)? y
Enter a name to search (lastname, firstname):
Edward, Stanley
Person Not Found
Search another (y/n)? n
The list of entries in AddressBook are -
Name Phone
Brown, Robert 4444444
Jackson, Michael 11111
Jones, David 555555
Taylor, Richard 222222
Williams, Alexander 333333
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.