SlideShare a Scribd company logo
1 of 14
Download to read offline
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps
records of people’s names and the corresponding phone numbers.
The program should read a transaction file and report the result into an output file. The
transaction file can include commands such as “Add”, “Delete”, “Display”, and “Update”, and
“Search”. Each command is written in one line with a few other information.
The “Display” Command should simply display everyone in the directory on the screen
The “Add” command comes with 3 other information that includes “First Name”, “Last Name”,
and “Phone Number”. You should only add this command if the phone number is unique and it is
not assigned to any other person. Otherwise an error statement should be generated to indicate
that “Duplicate phone number is not allowed”. If the addition of the record is successful, you
need to report that in the output file as follows:
“First Name”, “Last Name”, “Phone Number” has been successfully added to the directory
If the addition fails, you should report that
“**** ERROR IN ADD **** “Phone Number” already exist in the directory
The “Delete” command comes with the “Phone Number” indicating that this record should be
deleted from the directory. You should only delete the record if you can find the phone number.
If you could remove the record from the directory, you should report:
“First Name”, “Last Name”, “Phone Number” has been successfully deleted from the directory
If the delete fails, you should report that
“**** ERROR IN DELETE **** “Phone Number” does exist in the directory
The “Update” command come with two values, “Phone Number” and “New Phone Number”.
You program should first search for “Phone Number” and find out if such a phone number exist
in the directory. If it does not find it, it should report:
“**** ERROR IN UPDATE **** The Phone Number does not exist in the directory
However, if it finds the phone number, there are two cases. If the “New Phone Number” already
exists in the directory, it should report
“**** ERROR IN UPDATE **** The New Phone Number “New Phone Number” already exist
in the directory.
If the “New Phone Number” does not exist in the directory, your program should replace the
“Old Phone Number” with the “New Phone Number” and report
The Phone Number “Old Phone Number” is successfully updated to “New Phone Number”
There are two types of “Search” commands. One is done based on the last name and another is
done based on the phone number. If it is done based on the last name, the search command can
be as follows:
Search BasedOnLastName “LastName”
In this case the search is done based on the last name. Your program should search for all records
with that last name and report them on the screen. If the search fails your report should have the
following format
“**** ERROR IN SEARCH **** No Record with the last name “Last Name” could be found”
If the search is done based on the phone number, the command should be
Search BasedOnPhoneNumber “PhoneNumber”
In this case the search is done based on the Phone Number. Your program should search for the
specific record with that phone number and if it finds it, it should report it
FirstName LastName PhoneNumber is found
on the screen; otherwise, it should display
“**** ERROR IN SEARCH ****: No Record with the phone number “Phone Number” Exist
You are required to create a vector with class called PhoneRecords.
class PhoneRecords
{
Public:
string Fname;
string Lname;
string PhoneNumber;
};
Every time a record is added it should be added to vector. You can simply use the “push_back”
operation to add it to the end of the vector. Use the following Transaction file to do run your
work.
Add Joe Garcia 858-343-2009
Add Amelia Perez 617-255-0987
Add Bob Haynes 858-765-1122
Add Tim Ducrest 760-877-1654
Add Kevin Garcia 760-543-5622
Add Suzy Walter 858-190-2307
Add Fang Yi 619-677-1212
Add Robert James 619-909-3476
Add Mary Palmer 760-435-2086
Delete 760-888-1237
Delete 760-877-1654
Add Kevin Garcia 760-543-5622
Add Robert James 619-909-3476
Search BasedOnLastName Garcia
Search BasedOnLastName Wong
Search BasedOnPhoneNumber 760-977-2654
Search BasedOnPhoneNumber 858-190-2307
Update 858-343-2119 760-877-1654
Update 617-255-0987 760-435-2086
Update 858-765-1122 800-134-2765
Display
Your output file should look like as follows:
(Joe Garcia 858-343-2009) has been successfully added to the directory
(Amelia Perez 617-255-0987) has been successfully added to the directory
(Bob Haynes 858-765-1122) has been successfully added to the directory
(Tim Ducrest 760-877-1654) has been successfully added to the directory
(Kevin Garcia 760-543-5622) has been successfully added to the directory
(Suzy Walter 858-190-2307) has been successfully added to the directory
(Fang Yi 619-677-1212) has been successfully added to the directory
(Robert James 619-909-3476) has been successfully added to the directory
(Mary Palmer 760-435-2086) has been successfully added to the directory
**** ERROR IN DELETE **** “760-888-1237” does not exist in the directory
(Tim Ducrest 760-877-1654) has been successfully deleted from the directory
**** ERROR IN ADD **** “760-543-5622” already exist in the directory
Joe Garcia 858-343-2009
Kevin Garcia 760-543-5622
**** ERROR IN SEARCH **** No Record with the last name “Wong” could be found
“**** ERROR IN SEARCH ****: No Record with the phone number “760-977-2654” Exist
(Suzy Walter 858-190-2307) is found
**** ERROR IN UPDATE **** The Phone Number 858-343-2119 does exist in the directory
“**** ERROR IN UPDATE **** The New Phone Number “760-435-2086” already exist in the
directory
The Phone Number “858-765-1122” is successfully updated to “800-134-2765”
Joe Garcia 858-343-2009
Amelia Perez 617-255-0987
Bob Haynes 800-134-2765
Kevin Garcia 760-543-5622
Suzy Walter 858-190-2307
Fang Yi 619-677-1212
Robert James 619-909-3476
Mary Palmer 760-435-2086
Kevin Garcia 760-543-5622
Robert James 619-909-3476
Place the result into a file called “Output.txt”.
//-----------------------please use the following outline to complete program, show output at the
end to make sure program works-----------------------------
#include
#include
#include
#include
usingnamespace std;
class phoneRecords
{
private:
string firstName;
string lastName;
string phoneNumber;
public:
phoneRecords();
{
firstName = lastName = phoneNumber = " ";
}
phoneRecords(string Fn, string Ln, string Phone);
{
firstName = Fn;
lastName = Ln;
phoneNumber = Phone;
}
};
class TelephoneDirectory
{
private:
ifstream fin;
vector phones;
bool success;
bool failure;
public:
TelephoneDirectory();
{
success = true;
failure = false;
};
bool AddAphone(string firstName,string lastName,string phoneNumber);
bool Remove(string phoneNumber);
bool Update(string oldPhone,string newPhone);
bool SearchBasedOnLastName(string lastName);
bool SearchBasedOnPhoneNumber(string phoneNumber);
bool DisplayRecords();
bool InvokeTransFile();
//--------------------------------------------------------------------
bool TelephoneDirectory::AddAphone(string firstName,string lastName,string phoneNumber)
{
bool status = SearchBasedOnPhoneNumber(phoneNumber);
if(status == false)
{
cout << **ERROR** << endl;
return false;
}
phoneRecords newPerson(Fn, Ln, Phone);
phones.push_back(newPerson);
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::Remove(string phoneNumber)
{
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::Update(string oldPhone,string newPhone)
{
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::SearchBasedOnLastName(string lastName)
{
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::SearchBasedOnPhoneNumber(string phoneNumber)
{
}
//--------------------------------------------------------------------
bool TelephoneDirectory::DisplayRecords()
{
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::InvokeTransFile()
{
return success;
}
//--------------------------------------------------------------------
int main()
{
TelephoneDirectory SanMarcosPhone;
SanMarcosPhone.InvokeTransFile();
return 0;
}
Solution
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class phoneRecords
{
public:
string firstName;
string lastName;
string phoneNumber;
public:
phoneRecords()
{
firstName = lastName = phoneNumber = " ";
}
phoneRecords(string Fn, string Ln, string Phone)
{
firstName = Fn;
lastName = Ln;
phoneNumber = Phone;
}
};
class TelephoneDirectory
{
private:
//ifstream fin;
vector phones;
bool success;
bool failure;
public:
TelephoneDirectory()
{
success = true;
failure = false;
}
bool AddAphone(string firstName,string lastName,string phoneNumber);
bool Remove(string phoneNumber);
bool Update(string oldPhone,string newPhone);
bool SearchBasedOnLastName(string lastName);
bool SearchBasedOnPhoneNumber(string phoneNumber);
bool DisplayRecords();
bool InvokeTransFile();
};
//--------------------------------------------------------------------
bool TelephoneDirectory::AddAphone(string firstName,string lastName,string phoneNumber)
{
bool status = SearchBasedOnPhoneNumber(phoneNumber);
if(status == true)
{
cout << "**** ERROR IN ADD **** Phone Number " << phoneNumber <<"already exist in
the directory" << endl;
return false;
}
phoneRecords newPerson(firstName, lastName, phoneNumber);
phones.push_back(newPerson);
cout << firstName << " " <::iterator it;
bool found = false;
for(it=phones.begin();it!=phones.end();it++)
{
if(it->phoneNumber.compare(phoneNumber)==0)
{
found = true;
break;
}
}
if(found == true)
{
phones.erase(it);
cout << it->firstName << " " <lastName <<" "<< it->phoneNumber <<" has been
successfully deleted to the directory" << endl;
}
else
{
cout << "**** ERROR IN DELETE **** Phone Number " << phoneNumber <<" does exist
in the directory"<::iterator it;
bool found = false;
for(it=phones.begin();it!=phones.end();it++)
{
if(it->phoneNumber.compare(oldPhone)==0)
{
found = true;
break;
}
}
if(found == true)
{
if(it->phoneNumber.compare(newPhone)==0)
cout << "**** ERROR IN UPDATE **** The New Phone Number "<< newPhone <<"
already exist in the directory."<phoneNumber = newPhone;
cout << "The Phone Number "<< oldPhone <<" is successfully updated to " << newPhone <<
endl;
}
}
else
{
return failure;
}
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::SearchBasedOnLastName(string lastName)
{
vector::iterator it;
bool found = false;
for(it=phones.begin();it!=phones.end();it++)
{
if(it->lastName.compare(lastName)==0)
{
cout << it->firstName <<" " << it->lastName << " " << it->phoneNumber << endl;
found = true;
}
}
if(found == false)
{
return failure;
}
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::SearchBasedOnPhoneNumber(string phoneNumber)
{
vector::iterator it;
bool found = false;
for(it=phones.begin();it!=phones.end();it++)
{
if((it->phoneNumber).compare(phoneNumber)==0)
{
cout << it->firstName <<" " << it->lastName << " " << it->phoneNumber << " is found" <<
endl;
found = true;
break;
}
}
if(found == true)
return success;
else
return failure;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::DisplayRecords()
{
vector::iterator it;
for(it=phones.begin();it!=phones.end();it++)
{
cout << it->firstName <<" " << it->lastName << " " << it->phoneNumber << endl;
}
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::InvokeTransFile()
{
return success;
}
//--------------------------------------------------------------------
int main()
{
TelephoneDirectory SanMarcosPhone;
//SanMarcosPhone.InvokeTransFile();
string STRING;
ifstream infile("input.txt");
//infile.open("input.txt");
if (!infile.is_open()) {
cerr << "Unable to open file" << endl;
return 1;
}
vector array;
while (getline(infile, STRING))
array.push_back(STRING);
infile.close();
int i=0,j;
for (i = 0; i < array.size(); ++i)
cout << array[i] << endl;
for (j = 0; j < array.size(); ++j) // To get you all the lines.
{
string str = array[j];
i=0;
string operation;
while(str[i] != ' ' && str[i] != '0')
operation.push_back(str[i]);
i++;
if(operation.compare("Add") == 0)
{
cout << "In Add" << endl;
string str1,str2,str3;
while(str[i] != ' ' && str[i] != '0')
str1.push_back(str[i++]);
//str1[k]='0';
i++;
while(str[i] != ' ' && str[i] != '0')
str2.push_back(str[i++]);
//str2[k]='0';
i++;
while(str[i] != ' ' && str[i] != '0')
str3.push_back(str[i++]);
//str3[k]='0';
cout << endl;
cout << str1 << " " << str2 <<" " << str3 << endl;
if(SanMarcosPhone.AddAphone(str1,str2,str3))
cout <<"success" << endl;
}
if(operation.compare("Delete") ==0)
{
string phoneNo;
while(str[i] != ' ' && str[i] != '0')
phoneNo.push_back(str[i++]);
SanMarcosPhone.Remove(phoneNo);
}
if(operation.compare("Search") ==0)
{
string basedOn;
while(str[i] != ' ' && str[i] != '0')
basedOn.push_back(str[i++]);
i++;
if(operation.compare("BasedOnLastName")==0)
{
string lName;
while(str[i] != ' ' && str[i] != '0')
lName.push_back(str[i++]);
if(!SanMarcosPhone.SearchBasedOnLastName(lName))
cout << "**** ERROR IN SEARCH **** No Record with the last name "<< lName <<"
could be found" << endl;
}
if(operation.compare("BasedOnPhoneNumber")==0)
{
string phoneNo;
while(str[i] != ' ' && str[i] != '0')
phoneNo.push_back(str[i++]);
if(!SanMarcosPhone.SearchBasedOnPhoneNumber(phoneNo))
cout << "**** ERROR IN SEARCH **** No Record with the phone number "<< phoneNo
<<" Exist" << endl;
}
}
if(operation.compare("Update") ==0)
{
string str1,str2;
int i=0;
while(str[i] != ' ' && str[i] != '0')
str1.push_back(str[i++]);
i++;
while(str[i] != ' ' && str[i] != '0')
str2.push_back(str[i++]);
if(!SanMarcosPhone.Update(str1,str2))
cout << "**** ERROR IN UPDATE **** The Phone Number "<< str1 <<" does not exist in
the directory" << endl;
}
if(operation.compare("Display") ==0)
{
cout << "In dispay" << endl;
if(SanMarcosPhone.DisplayRecords())
cout << endl;
}
str.erase(str.begin(),str.end());
}
array.erase (array.begin(),array.begin()+3);
//system ("pause");
return 0;
}

More Related Content

More from mallik3000

estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdfestion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
mallik3000
 
Discuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdfDiscuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdf
mallik3000
 
Create a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdfCreate a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdf
mallik3000
 
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdfCompare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
mallik3000
 
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdfChoose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
mallik3000
 
Change the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdfChange the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdf
mallik3000
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdf
mallik3000
 
What is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdfWhat is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdf
mallik3000
 
What methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdfWhat methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdf
mallik3000
 
What is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdfWhat is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdf
mallik3000
 
What is the difference between a dinoflagellate and a Stramenopi.pdf
What is the difference between a dinoflagellate and a Stramenopi.pdfWhat is the difference between a dinoflagellate and a Stramenopi.pdf
What is the difference between a dinoflagellate and a Stramenopi.pdf
mallik3000
 
The RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdf
The RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdfThe RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdf
The RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdf
mallik3000
 

More from mallik3000 (20)

estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdfestion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
 
Discuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdfDiscuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdf
 
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdfDiels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
 
Create a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdfCreate a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdf
 
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdfCompare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
 
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdfChoose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
 
Change the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdfChange the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdf
 
Canon Corporation had the following static budget at the beginning o.pdf
Canon Corporation had the following static budget at the beginning o.pdfCanon Corporation had the following static budget at the beginning o.pdf
Canon Corporation had the following static budget at the beginning o.pdf
 
Can someone please prove this equation is an identity. Cos^2.pdf
Can someone please prove this equation is an identity. Cos^2.pdfCan someone please prove this equation is an identity. Cos^2.pdf
Can someone please prove this equation is an identity. Cos^2.pdf
 
Write a program that finds the max binary tree height. (This is an ex.pdf
Write a program that finds the max binary tree height. (This is an ex.pdfWrite a program that finds the max binary tree height. (This is an ex.pdf
Write a program that finds the max binary tree height. (This is an ex.pdf
 
What happens when the JVM encounters a wait () callSolution=.pdf
What happens when the JVM encounters a wait () callSolution=.pdfWhat happens when the JVM encounters a wait () callSolution=.pdf
What happens when the JVM encounters a wait () callSolution=.pdf
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdf
 
Why are supplies and inventory not considered plant assetsSolut.pdf
Why are supplies and inventory not considered plant assetsSolut.pdfWhy are supplies and inventory not considered plant assetsSolut.pdf
Why are supplies and inventory not considered plant assetsSolut.pdf
 
What is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdfWhat is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdf
 
What is the role of culture in leader development What culture fact.pdf
What is the role of culture in leader development What culture fact.pdfWhat is the role of culture in leader development What culture fact.pdf
What is the role of culture in leader development What culture fact.pdf
 
What methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdfWhat methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdf
 
What is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdfWhat is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdf
 
What is the difference between a dinoflagellate and a Stramenopi.pdf
What is the difference between a dinoflagellate and a Stramenopi.pdfWhat is the difference between a dinoflagellate and a Stramenopi.pdf
What is the difference between a dinoflagellate and a Stramenopi.pdf
 
True or False O2 can be carried in the blood only when attached to .pdf
True or False O2 can be carried in the blood only when attached to .pdfTrue or False O2 can be carried in the blood only when attached to .pdf
True or False O2 can be carried in the blood only when attached to .pdf
 
The RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdf
The RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdfThe RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdf
The RIF at Keller Technology Inc.IntroductionJan Ricter is the n.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 

Write a program in c++ that maintains a telephone directory. The Tel.pdf

  • 1. Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of people’s names and the corresponding phone numbers. The program should read a transaction file and report the result into an output file. The transaction file can include commands such as “Add”, “Delete”, “Display”, and “Update”, and “Search”. Each command is written in one line with a few other information. The “Display” Command should simply display everyone in the directory on the screen The “Add” command comes with 3 other information that includes “First Name”, “Last Name”, and “Phone Number”. You should only add this command if the phone number is unique and it is not assigned to any other person. Otherwise an error statement should be generated to indicate that “Duplicate phone number is not allowed”. If the addition of the record is successful, you need to report that in the output file as follows: “First Name”, “Last Name”, “Phone Number” has been successfully added to the directory If the addition fails, you should report that “**** ERROR IN ADD **** “Phone Number” already exist in the directory The “Delete” command comes with the “Phone Number” indicating that this record should be deleted from the directory. You should only delete the record if you can find the phone number. If you could remove the record from the directory, you should report: “First Name”, “Last Name”, “Phone Number” has been successfully deleted from the directory If the delete fails, you should report that “**** ERROR IN DELETE **** “Phone Number” does exist in the directory The “Update” command come with two values, “Phone Number” and “New Phone Number”. You program should first search for “Phone Number” and find out if such a phone number exist in the directory. If it does not find it, it should report: “**** ERROR IN UPDATE **** The Phone Number does not exist in the directory However, if it finds the phone number, there are two cases. If the “New Phone Number” already exists in the directory, it should report “**** ERROR IN UPDATE **** The New Phone Number “New Phone Number” already exist in the directory. If the “New Phone Number” does not exist in the directory, your program should replace the “Old Phone Number” with the “New Phone Number” and report The Phone Number “Old Phone Number” is successfully updated to “New Phone Number” There are two types of “Search” commands. One is done based on the last name and another is done based on the phone number. If it is done based on the last name, the search command can be as follows: Search BasedOnLastName “LastName”
  • 2. In this case the search is done based on the last name. Your program should search for all records with that last name and report them on the screen. If the search fails your report should have the following format “**** ERROR IN SEARCH **** No Record with the last name “Last Name” could be found” If the search is done based on the phone number, the command should be Search BasedOnPhoneNumber “PhoneNumber” In this case the search is done based on the Phone Number. Your program should search for the specific record with that phone number and if it finds it, it should report it FirstName LastName PhoneNumber is found on the screen; otherwise, it should display “**** ERROR IN SEARCH ****: No Record with the phone number “Phone Number” Exist You are required to create a vector with class called PhoneRecords. class PhoneRecords { Public: string Fname; string Lname; string PhoneNumber; }; Every time a record is added it should be added to vector. You can simply use the “push_back” operation to add it to the end of the vector. Use the following Transaction file to do run your work. Add Joe Garcia 858-343-2009 Add Amelia Perez 617-255-0987 Add Bob Haynes 858-765-1122 Add Tim Ducrest 760-877-1654 Add Kevin Garcia 760-543-5622 Add Suzy Walter 858-190-2307 Add Fang Yi 619-677-1212 Add Robert James 619-909-3476 Add Mary Palmer 760-435-2086 Delete 760-888-1237 Delete 760-877-1654 Add Kevin Garcia 760-543-5622 Add Robert James 619-909-3476 Search BasedOnLastName Garcia
  • 3. Search BasedOnLastName Wong Search BasedOnPhoneNumber 760-977-2654 Search BasedOnPhoneNumber 858-190-2307 Update 858-343-2119 760-877-1654 Update 617-255-0987 760-435-2086 Update 858-765-1122 800-134-2765 Display Your output file should look like as follows: (Joe Garcia 858-343-2009) has been successfully added to the directory (Amelia Perez 617-255-0987) has been successfully added to the directory (Bob Haynes 858-765-1122) has been successfully added to the directory (Tim Ducrest 760-877-1654) has been successfully added to the directory (Kevin Garcia 760-543-5622) has been successfully added to the directory (Suzy Walter 858-190-2307) has been successfully added to the directory (Fang Yi 619-677-1212) has been successfully added to the directory (Robert James 619-909-3476) has been successfully added to the directory (Mary Palmer 760-435-2086) has been successfully added to the directory **** ERROR IN DELETE **** “760-888-1237” does not exist in the directory (Tim Ducrest 760-877-1654) has been successfully deleted from the directory **** ERROR IN ADD **** “760-543-5622” already exist in the directory Joe Garcia 858-343-2009 Kevin Garcia 760-543-5622 **** ERROR IN SEARCH **** No Record with the last name “Wong” could be found “**** ERROR IN SEARCH ****: No Record with the phone number “760-977-2654” Exist (Suzy Walter 858-190-2307) is found **** ERROR IN UPDATE **** The Phone Number 858-343-2119 does exist in the directory “**** ERROR IN UPDATE **** The New Phone Number “760-435-2086” already exist in the directory The Phone Number “858-765-1122” is successfully updated to “800-134-2765” Joe Garcia 858-343-2009 Amelia Perez 617-255-0987 Bob Haynes 800-134-2765 Kevin Garcia 760-543-5622 Suzy Walter 858-190-2307 Fang Yi 619-677-1212 Robert James 619-909-3476
  • 4. Mary Palmer 760-435-2086 Kevin Garcia 760-543-5622 Robert James 619-909-3476 Place the result into a file called “Output.txt”. //-----------------------please use the following outline to complete program, show output at the end to make sure program works----------------------------- #include #include #include #include usingnamespace std; class phoneRecords { private: string firstName; string lastName; string phoneNumber; public: phoneRecords(); { firstName = lastName = phoneNumber = " "; } phoneRecords(string Fn, string Ln, string Phone); { firstName = Fn; lastName = Ln; phoneNumber = Phone; } }; class TelephoneDirectory { private: ifstream fin; vector phones; bool success; bool failure;
  • 5. public: TelephoneDirectory(); { success = true; failure = false; }; bool AddAphone(string firstName,string lastName,string phoneNumber); bool Remove(string phoneNumber); bool Update(string oldPhone,string newPhone); bool SearchBasedOnLastName(string lastName); bool SearchBasedOnPhoneNumber(string phoneNumber); bool DisplayRecords(); bool InvokeTransFile(); //-------------------------------------------------------------------- bool TelephoneDirectory::AddAphone(string firstName,string lastName,string phoneNumber) { bool status = SearchBasedOnPhoneNumber(phoneNumber); if(status == false) { cout << **ERROR** << endl; return false; } phoneRecords newPerson(Fn, Ln, Phone); phones.push_back(newPerson); return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::Remove(string phoneNumber) { return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::Update(string oldPhone,string newPhone) { return success; }
  • 6. //-------------------------------------------------------------------- bool TelephoneDirectory::SearchBasedOnLastName(string lastName) { return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::SearchBasedOnPhoneNumber(string phoneNumber) { } //-------------------------------------------------------------------- bool TelephoneDirectory::DisplayRecords() { return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::InvokeTransFile() { return success; } //-------------------------------------------------------------------- int main() { TelephoneDirectory SanMarcosPhone; SanMarcosPhone.InvokeTransFile(); return 0; } Solution #include #include #include #include #include #include #include
  • 7. using namespace std; class phoneRecords { public: string firstName; string lastName; string phoneNumber; public: phoneRecords() { firstName = lastName = phoneNumber = " "; } phoneRecords(string Fn, string Ln, string Phone) { firstName = Fn; lastName = Ln; phoneNumber = Phone; } }; class TelephoneDirectory { private: //ifstream fin; vector phones; bool success; bool failure; public: TelephoneDirectory() { success = true; failure = false; } bool AddAphone(string firstName,string lastName,string phoneNumber); bool Remove(string phoneNumber); bool Update(string oldPhone,string newPhone); bool SearchBasedOnLastName(string lastName);
  • 8. bool SearchBasedOnPhoneNumber(string phoneNumber); bool DisplayRecords(); bool InvokeTransFile(); }; //-------------------------------------------------------------------- bool TelephoneDirectory::AddAphone(string firstName,string lastName,string phoneNumber) { bool status = SearchBasedOnPhoneNumber(phoneNumber); if(status == true) { cout << "**** ERROR IN ADD **** Phone Number " << phoneNumber <<"already exist in the directory" << endl; return false; } phoneRecords newPerson(firstName, lastName, phoneNumber); phones.push_back(newPerson); cout << firstName << " " <::iterator it; bool found = false; for(it=phones.begin();it!=phones.end();it++) { if(it->phoneNumber.compare(phoneNumber)==0) { found = true; break; } } if(found == true) { phones.erase(it); cout << it->firstName << " " <lastName <<" "<< it->phoneNumber <<" has been successfully deleted to the directory" << endl; } else { cout << "**** ERROR IN DELETE **** Phone Number " << phoneNumber <<" does exist in the directory"<::iterator it;
  • 9. bool found = false; for(it=phones.begin();it!=phones.end();it++) { if(it->phoneNumber.compare(oldPhone)==0) { found = true; break; } } if(found == true) { if(it->phoneNumber.compare(newPhone)==0) cout << "**** ERROR IN UPDATE **** The New Phone Number "<< newPhone <<" already exist in the directory."<phoneNumber = newPhone; cout << "The Phone Number "<< oldPhone <<" is successfully updated to " << newPhone << endl; } } else { return failure; } return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::SearchBasedOnLastName(string lastName) { vector::iterator it; bool found = false; for(it=phones.begin();it!=phones.end();it++) { if(it->lastName.compare(lastName)==0) { cout << it->firstName <<" " << it->lastName << " " << it->phoneNumber << endl; found = true; }
  • 10. } if(found == false) { return failure; } return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::SearchBasedOnPhoneNumber(string phoneNumber) { vector::iterator it; bool found = false; for(it=phones.begin();it!=phones.end();it++) { if((it->phoneNumber).compare(phoneNumber)==0) { cout << it->firstName <<" " << it->lastName << " " << it->phoneNumber << " is found" << endl; found = true; break; } } if(found == true) return success; else return failure; } //-------------------------------------------------------------------- bool TelephoneDirectory::DisplayRecords() { vector::iterator it; for(it=phones.begin();it!=phones.end();it++) { cout << it->firstName <<" " << it->lastName << " " << it->phoneNumber << endl; }
  • 11. return success; } //-------------------------------------------------------------------- bool TelephoneDirectory::InvokeTransFile() { return success; } //-------------------------------------------------------------------- int main() { TelephoneDirectory SanMarcosPhone; //SanMarcosPhone.InvokeTransFile(); string STRING; ifstream infile("input.txt"); //infile.open("input.txt"); if (!infile.is_open()) { cerr << "Unable to open file" << endl; return 1; } vector array; while (getline(infile, STRING)) array.push_back(STRING); infile.close(); int i=0,j; for (i = 0; i < array.size(); ++i) cout << array[i] << endl; for (j = 0; j < array.size(); ++j) // To get you all the lines. { string str = array[j]; i=0; string operation; while(str[i] != ' ' && str[i] != '0') operation.push_back(str[i]); i++;
  • 12. if(operation.compare("Add") == 0) { cout << "In Add" << endl; string str1,str2,str3; while(str[i] != ' ' && str[i] != '0') str1.push_back(str[i++]); //str1[k]='0'; i++; while(str[i] != ' ' && str[i] != '0') str2.push_back(str[i++]); //str2[k]='0'; i++; while(str[i] != ' ' && str[i] != '0') str3.push_back(str[i++]); //str3[k]='0'; cout << endl; cout << str1 << " " << str2 <<" " << str3 << endl; if(SanMarcosPhone.AddAphone(str1,str2,str3)) cout <<"success" << endl; } if(operation.compare("Delete") ==0) { string phoneNo; while(str[i] != ' ' && str[i] != '0') phoneNo.push_back(str[i++]); SanMarcosPhone.Remove(phoneNo); } if(operation.compare("Search") ==0) { string basedOn; while(str[i] != ' ' && str[i] != '0') basedOn.push_back(str[i++]); i++; if(operation.compare("BasedOnLastName")==0) { string lName;
  • 13. while(str[i] != ' ' && str[i] != '0') lName.push_back(str[i++]); if(!SanMarcosPhone.SearchBasedOnLastName(lName)) cout << "**** ERROR IN SEARCH **** No Record with the last name "<< lName <<" could be found" << endl; } if(operation.compare("BasedOnPhoneNumber")==0) { string phoneNo; while(str[i] != ' ' && str[i] != '0') phoneNo.push_back(str[i++]); if(!SanMarcosPhone.SearchBasedOnPhoneNumber(phoneNo)) cout << "**** ERROR IN SEARCH **** No Record with the phone number "<< phoneNo <<" Exist" << endl; } } if(operation.compare("Update") ==0) { string str1,str2; int i=0; while(str[i] != ' ' && str[i] != '0') str1.push_back(str[i++]); i++; while(str[i] != ' ' && str[i] != '0') str2.push_back(str[i++]); if(!SanMarcosPhone.Update(str1,str2)) cout << "**** ERROR IN UPDATE **** The Phone Number "<< str1 <<" does not exist in the directory" << endl; } if(operation.compare("Display") ==0) { cout << "In dispay" << endl; if(SanMarcosPhone.DisplayRecords())
  • 14. cout << endl; } str.erase(str.begin(),str.end()); } array.erase (array.begin(),array.begin()+3); //system ("pause"); return 0; }