c++ Create a class called AddressBook. It should be able to store a maximum of 5
ID: 3853561 • Letter: C
Question
c++ Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). 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 the address book.
Explanation / Answer
Here is the code for you:
#include <iostream>
using namespace std;
//Create a class called AddressBook.
class AddressBook
{
// It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays).
string names[50];
string phoneNumbers[50];
int numOfEntries;
int maxEntries;
public:
AddressBook()
{
numOfEntries = 0;
maxEntries = 50;
}
//It should contain a method called addName that will take a name and phone number and add it to the address book.
void addName(string name, string phoneNumber)
{
if(numOfEntries != maxEntries)
{
names[numOfEntries] = name;
phoneNumbers[numOfEntries] = phoneNumber;
numOfEntries++;
}
else
cout << "Address Book is full, and can't add any more entries....." << endl;
}
//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”.
string findName(string name)
{
for(int i = 0; i < numOfEntries; i++)
if(names[i].compare(name) == 0)
return phoneNumbers[i];
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).
void display()
{
for(int i = 0; i < numOfEntries-1; i++)
for(int j = 0; i < numOfEntries-i-1; j++)
{
if(names[j].compare(names[j+1]) > 0)
{
string temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
temp = phoneNumbers[j];
phoneNumbers[j] = phoneNumbers[j+1];
phoneNumbers[j+1] = temp;
}
}
for(int i = 0; i < numOfEntries; i++)
{
cout << names[i] << " " << phoneNumbers[i] << endl;
}
}
};
//Create a main function to test your class.
int main()
{
//Inside the main ask for five names and enter them into the address book.
AddressBook addressBook;
for(int i = 0; i < 5; i++)
{
string name, phoneNumber;
cout << "Entry #" << i+1 << ":" << endl;
cout << "Name: ";
cin >> name;
cout << "Phone number: ";
cin >> phoneNumber;
addressBook.addName(name, phoneNumber);
}
//In the main, look for one that is in the address book and test for a name that is not in the address book.
cout << "Enter the name you want to search for: ";
string name;
cin >> name;
cout << addressBook.findName(name) << endl;
cout << "Enter the name you want to search for: ";
cin >> name;
cout << addressBook.findName(name) << endl;
//Finally, in the main, display the names in the address book.
addressBook.display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.