In: Computer Science
You are asked to write a simple C++ phonebook application program. Here are the requirements for the application.
A sample run:
***MY PHONEBOOK APPLICATION***
Please choose an operation:
A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): A
Enter name: MARY SMITH
Enter phone: 5062396
A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): S
Enter name: MARY SMITH
Phone Number: 5062396
A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): L
BARBARA BROWN 4059171
ELIZABETH JONES 2736877
LINDA WILLIAMS 3532665
PATRICIA JOHNSON 973437
…
A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): D
Enter name: LINDA WILLIAMS
A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): L
BARBARA BROWN 4059171
ELIZABETH JONES 2736877
PATRICIA JOHNSON 973437
…
A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): Q
code of header file "Contact.h"
using namespace std;
int pos = 0;
int capacity = 100;
class contact{
public:
string fName;
string sname;
string phone;
static void listContacts(contact *vec){
for(int i=0;i<pos;i++){
cout<<vec[i].fName<<" "<<vec[i].sname<<" "<<vec[i].phone<<endl;
}
}
static contact * addContact(contact *vec){
contact c;
cout<<"Enter name: ";
cin>>c.fName>>c.sname;
cout<<"Enter phone: ";
cin>>c.phone;
if(pos<capacity){
vec[pos] = c;
pos++;
}
else{
capacity+=20;
contact *temp = new contact[capacity];
for(int i=0;i<pos;i++)
temp[i] = vec[i];
vec = temp;
vec[pos] = c;
pos++;
}
return vec;
}
static contact * searchContact(contact * vec){
string f_name,s_name;
cout<<"Enter name ";
cin>>f_name>>s_name;
for(int i=0;i<pos;i++){
if(vec[i].fName==f_name && vec[i].sname==s_name){
cout<<"Phone Number: "<<vec[i].phone<<endl;
return vec;
}
}
cout<<"Contact not found!"<<endl;
return vec;
}
static contact * deleteContact(contact *vec){
string f_name,s_name;
cout<<"Enter name ";
cin>>f_name>>s_name;
for(int i=0;i<pos;i++){
if(vec[i].fName==f_name && vec[i].sname==s_name){
for(int j=i+1;j<pos;j++){
vec[j-1] = vec[j];
}
pos--;
return vec;
}
}
return vec;
}
};
Code for the implementation file containing main():
#include <iostream>
#include <vector>
#include <sstream>
#include <bits/stdc++.h>
#include "Contact.h"
using namespace std;
int main(){
contact *vec = new contact[capacity];
ifstream fin;
fin.open("phonebook.txt");
string line;
while(fin){
getline(fin,line);
istringstream ss(line);
contact record;
ss>>record.fName>>record.sname>>record.phone;
if(pos<capacity){
vec[pos] = record;
pos++;
}
else{
capacity+=20;
contact *temp = new contact[capacity];
for(int i=0;i<pos;i++)
temp[i] = vec[i];
vec = temp;
vec[pos] = record;
pos++;
}
}
pos--;
char choice;
do{
cout<<"Please choose an operation:"<<endl;
cout<<"A(Add)|S(Search)|D(Delete)|L(List)|Q(Quit):";
cin>>choice;
switch(choice){
case 'A': vec = contact::addContact(vec);
break;
case 'S': contact::searchContact(vec);
break;
case 'D': vec = contact::deleteContact(vec);
break;
case 'L': contact::listContacts(vec);
}
}while(choice!='Q');
fin.close();
ofstream fout;
fout.open("phonebook.txt");
for(int i=0;i<pos;i++){
fout<<vec[i].fName<<" "<<vec[i].sname<<" "<<vec[i].phone<<endl;
}
}
output is as following: