How to build this program in c++? Lab 4 - Contact Database due Mon. 4/30 Write a
ID: 3720984 • Letter: H
Question
How to build this program in c++? Lab 4 - Contact Database due Mon. 4/30 Write a program that implements a database system. The program will use an array of type contact to store contact information (maximum size 100). The program should be written using 2 class files. One class will implement a contact object and the second class will implement the database and include the main method. The contact class should include instance variables to store the contact's first name, last name, phone number, and email address. Add appropriate constructors, accessor and mutator methods, an input method, and overloaded << function. Your database class should include a method to display a menu that allows the client to add a contact, display all contacts, search for a contact by phone number, and delete a contact. You may add a method to edit a contact for extra credit. Input the existing contact information for 10 contacts into the database's array instance variable from a file (use an initialize method)
Explanation / Answer
EXECUTABLE CODE
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
class Contact//Contact class
{
private:string firstNameElement;
private:string lastNameElement;
private:string phoneNumberElement;
private:string email;
public: void setFirstName(string first)
{
firstNameElement=first;
}
public: void setLastName(string last)
{
lastNameElement=last;
}
public: void setPhoneNumber(string ph)
{
phoneNumberElement=ph;
}
public: void setEmail(string mail)
{
email=mail;
}
public:string getFirstName()
{
return firstNameElement;
}
public:string getLastName()
{
return lastNameElement;
}
public:string getPhoneNumber()
{
return phoneNumberElement;
}
public:string getEmail()
{
return email;
}
public: void add()
{
readInput();
}
public: void readInput()
{
string s;
cout<<"Enter First Name for Contact: ";
cin>>s;
setFirstName(s);
cout<<" Enter Last name for Contact: ";
cin>>s;
setLastName(s);
cout<<" Enter phone number of Contact: ";
cin>>s;
setPhoneNumber(s);
cout<<" Enter email address of Contact: ";
cin>>s;
setEmail(s);
}
friend ostream& operator<< (ostream& out,const Contact& contact)
{
out<<"Name is: "<<contact.firstNameElement<<" "<<contact.lastNameElement<<endl;
out<<"Phone Number is: "<<contact.phoneNumberElement<<endl;
out<<"Email is: "<<contact.email<<endl<<endl;
return out;
}
};
class DataBase
{
Contact contactListElement[100];
public: int size=0;
public: void intializeContacts()
{
ifstream infile;
infile.open("data.txt");
while(!infile.eof())
{
string a,b,c,d;
infile>>a;
infile>>b;
infile>>c;
infile>>d;
contactListElement[size].setFirstName(a);
contactListElement[size].setLastName(b);
contactListElement[size].setPhoneNumber(c);
contactListElement[size].setEmail(d);
size++;
}
infile.close();
}
public: int menu()
{
int choiceValue;
cout<<" Please select one of the following options: ";
cout<<" 1:Add Contact";
cout<<" 2:Search Contact using phone number";
cout<<" 3:Delete Contact";
cout<<" 4:View all Contacts";
cout<<" 5:Modify Contact";
cout<<" 6:Exit ";
cout<<"Your choiceValue: ";
cin>>choiceValue;
cout<<endl;
return choiceValue;
}
public: void addContact()
{
if(size<100)
{
contactListElement[size].add();
size ++;
}
else
{
cout<<" Contact list full!! No more contacts can be added ";
}
}
public:int searchcontactMethod(string ph)
{
int i;
for( i=0;i<size;i++)
{
if(ph==contactListElement[i].getPhoneNumber())
break;
}
if(i==size)
{
cout<<" Couldnt find any contact details with phone number:"<<ph<<endl;
return -1;
}
else
{
cout<<" Contact Found ";
cout<<contactListElement[i];
return i;
}
}
public: void deletecontactMethod(string ph)
{
int index=searchcontactMethod(ph);
if(index!=-1)
{
while(index<=size)
{
contactListElement[index].setFirstName(contactListElement[index+1].getFirstName());
contactListElement[index].setLastName(contactListElement[index+1].getLastName());
contactListElement[index].setPhoneNumber(contactListElement[index+1].getPhoneNumber());
contactListElement[index].setEmail(contactListElement[index+1].getEmail());
index++;
}
cout<<" Contact deleted Successfully"<<endl;
size--;
}
}
public: void display()
{
for(int i=0;i<size;i++)
{
cout<<"Contact "<<i+1<<endl;
cout<<contactListElement[i];
}
}
public: void modifyContact()
{
string ph,modified;
int ch=1;
cout<<"Enter the phone number of contact you want to edit: ";
cin>>ph;
int index=searchcontactMethod(ph);
if(index!=-1)
{
while(ch!=5)
{
cout<<" choiceValue of Fields to Modify 1:First Name 2:Last Name 3:Phone NUmber 4:Email 5:exit from modification Enter Your choiceValue(1/2/3/4/5): ";
cin>>ch;
switch(ch)
{
case 1: cout<<" Enter the new value for First Name: ";
cin>>modified;
contactListElement[index].setFirstName(modified);
break;
case 2:
cout<<" Enter the new value for Last Name: ";
cin>>modified;
contactListElement[index].setLastName(modified);
break;
case 3:
cout<<" Enter the new value for Phone Number: ";
cin>>modified;
contactListElement[index].setPhoneNumber(modified);
break;
case 4:
cout<<" Enter the new value for Email: ";
cin>>modified;
contactListElement[index].setEmail(modified);
break;
case 5:
cout<<" All Modifications saved ";
break;
default:
cout<<" Invalid choiceValue please enter number from 1 to 5 ";
}
}
}
}
};
//main method
int main()
{
DataBase d;//create object for database.
int choiceValue;
string ph;
d.intializeContacts();
do
{
choiceValue=d.menu();
switch(choiceValue)
{
case 1:d.addContact();
break;
case 2:
cout <<" Enter the Phone Number of contact that you want to search: ";
cin>>ph;
d.searchcontactMethod(ph);
break;
case 3:
cout <<" Enter the Phone Number of contact that you want to delete: ";
cin>>ph;
d.deletecontactMethod(ph);
break;
case 4:
d.display();
break;
case 5:
d.modifyContact();
break;
case 6:
cout<<" BYE!!";
break;
default:
cout<<" Invalid choiceValue Please select an option from 1 to 6:";
}
}while(choiceValue!=6);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.