using c++, Create the class phonebook(A “contact” is a structured data that hold
ID: 3805048 • Letter: U
Question
using c++,
Create the class phonebook(A “contact” is a structured data that holds the person’s last name, first name, mobile contact and email address) with a private storage vector and public function definitions corresponding to following functionality:
Menu: Menu of the application. Should print out a menu for the user to choose from (something similar to above). Executes(calls) the function related to the choice. Will return to the main function only if user selects “0 - quit”. Keeps recursing if any other option is chosen, after executing those specific functions.
import: This function should import all the contacts from the file of your choosing (phonebook.txt) and save(store) them into your phonebook application. You should consider writing a utility function that takes a filename as a parameter that does the import.
Add Contact: given a first name, last name, mobile number and email (in that order), this function should add a contact in your phonebook with these details.
Find Contact: Accepts a search string and finds all the contacts in your phonebook that has the string in either the first name or the last name (string could be in any position). Print out the details (last, first, mobile and email) of all those contacts. The function should be a Boolean – returns a false if no contact is found; returns true in any other case.
Edit Contact – Edit contact should accept a search name. After the contact(unique) is found ask user to input new first name, last name, mobile and email to replace. If the search name yields more than one contact print a message stating the same and print out all those contacts. Do not edit. If no contact is found print out a message stating the same. In the above two cases you can ask the user to re-enter search string and loop back. You can use the find contact as a helper function for this.
Delete Contact – Works similar to edit contact, except the function deletes the contact from the phonebook.
Export Contacts –Exports(write) all the contacts to the specified file. You will need to prompt for this file. You may want to use “newphonebook.txt” for the filename. You should consider writing a utility function that takes a filename as a parameter that does the export.
Display - display to the screen the Complete Contact List vector. The starter code already has one you can use.
Use any helper functions if required.
Explanation / Answer
#include <iostream.h>
#include<fstream.h>
#include <cstdio.h>
#include<string.h>
using namespace std;
#define max 3000
fstream inputfile;
fstream outputfile;
struct Contact
{
char firstname[20]; /*First Name*/
char lastname[30]; /*Last Name*/
char phonenumber[10]; /*Telephone Number*/
char email[60]; /*E-Mail*/
};
class PhoneBook
{
Contact contact;
public:
void import();
void Addcontact();
void Editcontact();
void Delete()
{
contact.first[0]='\';
};
void Display(bool showall);
bool FindContact(char s[60]);
void export();
};
void PhoneBook::import(inputfile)
{
int i=0;
int last=inputfile.tellg();
int n=last/sizeof(book); //n is the number of objects in the file
if(i<=n))
{
while(inputfile.read((char*)&book[i],sizeof(book[i])))
{
contact[i].firstname = firstname;
comtact[i].lastname=lastname;
contact[i].email=email;
contact[i].phonenumber=phonenumber;
}
i++;
}
}
void Phonebook::export(outputfile)
{
for(int i=0;i<=max;i++)
{
outputfile.write((char*)&book[i],sizeof(book[i]));
}
}
void PhoneBook::Addcontact()
{
cout << endl << "FirstName: ";
cin >> contact.first;
cout << endl << "LastName: ";
cin >> contact.last;
cout << endl << "Phone Number: ";
cin >> contact.phonenumber;
cout << endl << "e-mail: ";
cin >> contact.email;
}
void PhoneBook::Editcontact()
{
cout << endl << "FirstName: ";
cin >> contact.first;
cout << endl << "LastName: ";
cin >> contact.last;
cout << endl << " Phone Number: ";
cin >> contact.phonenumber;
cout << endl << "e-mail: ";
cin >> contact.email;
cout << "OK." << endl;
}
void PhoneBook::Display(bool b)
{
cout << endl << "FirstName: " << contact.first
<< endl << "LastName: " << contact.last;
if(b){
cout << endl << " Phone Number: " << contact.phonenumber
<< endl << "e-mail: " << contact.email
<< endl << "---" << endl << "OK." << endl;
}
}
bool PhoneBook::FindContact(char s[60])
{
if(!strcmp(contact.first, s))
{ return true;
}
else if(!strcmp(contact.last, s))
{
return true;
}
else
{
return false;
}
}
int main()
{
PhoneBook book[max];
inputfile.open("phonebook.txt",ios::in|ios::out);
outputfile.open("newphonebook.txt",ios.in|ios.out);
char Mnuoption='0';
int i;
char buff[60];
bool found;
for(int j=0; j<max; j++)
book[j].Delete();
clearscreen();
while(Mnuoption != '6')
{
found=false;
i=0;
cout <<"1.Import data"<<endl;
cout << "2. Add Contact" << endl;
cout << "3.Edit Contact"<<endl;
cout << "4. Delete a Contact" << endl;
cout << "5. Search a Contact" << endl;
cout << "6. Export data" << endl;
cout << "0. Exit << endl;
cout << endl << "Selection: ";
cin >> Mnuoption;
switch(Mnuoption)
{
case '1':
import(inputfile);
break;
case '2':
book[i].Addcontact();
break;
case '3':
cout << "Enter the data to edit" << endl;
cin >> buff;
if(book[i].FindContact(buff))
{
cout << endl << "EDITING: ";
book[i].Print(false);
book[i].Edit();
}
else
{
i++;
}
break;
case '4':
cout << "Enter data to delete." << endl;
cin >> buff;
if(book[i].FindContact(buff))
{
cout << endl << "DELETING: ";
book[i].Print(true);
cout << endl << "Are you sure? (y/n)";
cin >> buff[0];
if(buff[0]=='y') book[i].Delete();
}
else
{
i++;
}
break;
case '5':
cout << endl << "Search: " << endl;
cin >> buff;
if(book[i].SearchContact(buff) )
{
book[i].Print(true);
cout << endl;
}
else
{
i++;
}
break;
cout << endl << "Type a number to continue ";
cin >> i;
case '6':
export();
break;
case '0':
exit();
}
}
cout << endl << endl << "END. " << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.