Question

In: Computer Science

Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of...

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

using namespace 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;

}

Solutions

Expert Solution

#include<iostream>
#include <bits/stdc++.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>
#include<vector>
#include<fstream>
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<phoneRecords> 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 << " " <<lastName <<" "<< phoneNumber <<" has been successfully added to the directory" << endl;
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::Remove(string phoneNumber)
{
vector<phoneRecords>::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 << " " <<it->lastName <<" "<< it->phoneNumber <<" has been successfully deleted to the directory" << endl;
}
else
{
cout << "**** ERROR IN DELETE **** Phone Number " << phoneNumber <<" does exist in the directory"<<endl;
return failure;
}
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::Update(string oldPhone,string newPhone)
{
vector<phoneRecords>::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."<<endl;
else
{
it->phoneNumber = newPhone;
cout << "The Phone Number "<< oldPhone <<" is successfully updated to " << newPhone << endl;
}
}
else
{
return failure;
}
return success;
}
//--------------------------------------------------------------------
bool TelephoneDirectory::SearchBasedOnLastName(string lastName)
{
vector<phoneRecords>::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<phoneRecords>::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<phoneRecords>::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<string> 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;
}


Related Solutions

Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (%...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (% & loop) 1. Euclid’s Algorithm An alternative of the Euclidean algorithm for finding greatest common divisors (GCD) is repeatedly performing the modulo operation on two numbers until the remainder is 0. Here is the pseudocode for find the GCD of two positive numbers a and b using the Euclidean algorithm :while b ≠ 0 temp = b b = a mod t a =...
in c++ you need to write a program that maintains the gradebook for a specific course....
in c++ you need to write a program that maintains the gradebook for a specific course. For each student registered in this course, the program keeps track of the student’s name, identification number (id), four exam grades, and final grade. The program performs several functionalities such as: enrolling a student in the course, dropping a student from the course, uploading student’s exam grades, displaying the grades of a specific student, displaying the grades of all students in the course, and...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
The Springfield Emergency Medical Service keeps records of emergency telephone calls. A study of 150 five-minute...
The Springfield Emergency Medical Service keeps records of emergency telephone calls. A study of 150 five-minute time intervals resulted in the distribution of number of calls as follows. For example, during 18 of the 5-minute intervals, no calls occurred. Use the chi-square goodness-of-fit test and α = .01 to determine whether this distribution is Poisson. Number of Calls (per 5-minute interval) Frequency 0 18 1 28 2 47 3 21 4 16 5 11 6 or more 9
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking integers until the user enters -100. In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered. Sample Input/Output 1: Input the number: 0 2 3 -9 -6 -4 -100 Number of positive numbers: 2 Number of Negative numbers: 3 Number of Zero: 1
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the telephone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area-code string to int and convert...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers...
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. This should the answer when finished Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT