Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that maintains a telephone directory. The Telephone directory ke

ID: 3795205 • Letter: W

Question

Write a program 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 <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 show output to make sure program works thank you!

Explanation / Answer

C++ code:

#include <bits/stdc++.h>
using namespace std;

class PhoneRecords
{
public:
string Fname;
string Lname;
string PhoneNumber;
};

int main()
{
   string line;
   std::map<string, string> mymap;
   ifstream myfile ("record.txt");
   ofstream myfileout;
   myfileout.open("output.txt");
   if (myfile.is_open())
   {
   while ( getline (myfile,line) )
   {
   string buf; // Have a buffer string
   stringstream ss(line); // Insert the string into a stream
   vector<string> tokens; // Create vector to hold our words
   while (ss >> buf)
   tokens.push_back(buf);
   if(tokens[0] == "Add")
   {
       if(mymap.find(tokens[3]) == mymap.end())
       {
           string s1 = tokens[1];
           string s2 = tokens[2];
           // strcat (s1,s2);
           mymap[tokens[3]] = s1 + " " + s2;
           myfileout << tokens[1] << " " << tokens[2] << " " << tokens[3] << " has been successfully added to the directory ";
       }
       else
       {
           myfileout << "**** ERROR IN ADD ****" << tokens[3] << " already exist in the directory" << endl;
       }
   }
   else if(tokens[0] == "Delete")
   {
       std::map<string,string>::iterator it;
       if(mymap.find(tokens[1]) != mymap.end())
       {
           myfileout << mymap[tokens[1]] << " " << tokens[1] << " " << " has been successfully deleted from the directory ";          
               it=mymap.find(tokens[1]);
               mymap.erase (it);
       }
       else
       {
           myfileout << "**** ERROR IN DELETE **** " << tokens[1] <<" does exist in the directory "<< endl;
       }      
   }
   else if(tokens[0] == "Update")
   {
       if(mymap.find(tokens[1]) != mymap.end())
       {
           if(mymap.find(tokens[2]) == mymap.end())
           {
               myfileout << "The Phone Number " << tokens[1] <<" is successfully updated to "<< tokens[2] <<endl;
               mymap[tokens[2]] = mymap[tokens[1]];              
           }
           else
           {
               myfileout << "**** ERROR IN UPDATE ****" << "The new Phone Number" << tokens[2] <<" already exist in the directory ";
           }                          
       }
       else
       {
           myfileout << "**** ERROR IN UPDATE ****" << "The old Phone Number" << tokens[1] <<" does not exist in the directory ";
       }                          
   }
   else if(tokens[0] == "Search")
   {
       if(tokens[1] == "BasedOnLastName")
       {
           bool flag = true;
           std::map<string,string>::iterator it;
               for (it=mymap.begin(); it!=mymap.end(); ++it)
               {
                   if( (it->second).find(tokens[2]) != std::string::npos)
                   {
                   myfileout << (it->second) << " " << it->first << " is found ";
                   flag = false;
                   }
               }
               if(flag == true)
               {
                 myfileout << "**** ERROR IN SEARCH ****" << "No Record with the last name "<< tokens[2] << " Exist" << endl;
               }
       }
       else if(tokens[1] == "BasedOnPhoneNumber")
       {

           if(mymap.find(tokens[2]) != mymap.end())
           {
               myfileout << mymap[tokens[2]] << " " << tokens[2] << " " << " is found ";
           }
           else
           {
               myfileout << "**** ERROR IN SEARCH ****" << "No Record with the phone number "<< tokens[2] << " Exist" << endl;
           }
       }
   }
   else if(tokens[0] == "Display")
       {
           cout << endl;
           std::map<string,string>::iterator it;
               for (it=mymap.begin(); it!=mymap.end(); ++it)
               {
                   myfileout << (it->first) << " " << it->second << " ";
               }
       }
   }
   myfile.close();
   }
   else
   {
   myfileout << "Unable to open file" << endl;
   exit(1);
   }          
}

record.txt

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

Output.txt

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 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
**** ERROR IN ADD ****619-909-3476 already exist in the directory
Kevin Garcia 760-543-5622 is found
Joe Garcia 858-343-2009 is found
**** ERROR IN SEARCH ****No Record with the last name Wong Exist
**** ERROR IN SEARCH ****No Record with the phone number 760-977-2654 Exist
Suzy Walter 858-190-2307 is found
**** ERROR IN UPDATE ****The old Phone Number858-343-2119 does not exist in the directory
**** ERROR IN UPDATE ****The new Phone Number760-435-2086 already exist in the directory
The Phone Number 858-765-1122 is successfully updated to 800-134-2765
617-255-0987 Amelia Perez
619-677-1212 Fang Yi
619-909-3476 Robert James
760-435-2086 Mary Palmer
760-543-5622 Kevin Garcia
800-134-2765 Bob Haynes
858-190-2307 Suzy Walter
858-343-2009 Joe Garcia
858-765-1122 Bob Haynes

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote