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

C++ Programming Help to create Rolodex Program, In the provided skeleton Rolodex

ID: 3866777 • Letter: C

Question

C++ Programming Help to create Rolodex Program, In the provided skeleton Rolodex program, there is an executable application (pre-built) that you can run to understand more in depth how the user interactions are but the TODO code is what is needed. LIke:

********

This code will be allowed:

vector<PhoneRecord> ContactRecord::GetPhoneNumberList() const
{
vector<PhoneRecord> numberList;
    for (int i = 0; i < this->PhoneNumberList.size(); i++)
    {
   numberList.push_back( this->PhoneNumberList[i]);
    }

    return numberList;
}

*****************

Must have user interaction to:

1. Add new contacts

2. Edit Existing contacts

3. Remove Existing Contacts

4. List All Contacts

5. Add three phone numbers for Contacts

6. Remove Phone From Contact

7. Quit Application

Code provided in body:

1. ContactRecord.h – Each contact can have more than one phone numbers (up to 3 to be exact). The phone numbers to be stored are for HOME, WORK, or MOBILE.

2. ContactRecord.cpp – Look for all methods that have TODO markers since I don't know what to code there.

3. ContactDataManager.h – To support multiple phone numbers per contact, the ContactDataManager class is now expanded to include new methods to handle/manage the list of phone numbers per contact.

4. ContactDataManager.cpp – Look for all methods that have TODO markersince I don't know what to code there.

5. ContactDataInputManager.h – The ContactDataInputManager class drives all the user interactions which allow him/her to add, edit, remove contacts as well as their phone numbers.

6. ContactDataInputManager.cpp – Look for all methods that have TODO markersince I don't know what to code there.

7. PhoneRecord.h - No work needed, this is part of the project to better understand

8. Main.cpp - No work needed, this is part of the project to better understand

***************************

1. ContactRecord.h – Each contact can have more than one phone numbers (up to 3 to be exact). The phone numbers to be stored are for HOME, WORK, or MOBILE.

***************************

#pragma once

#include

#include

#include

#include "PhoneRecord.h"

using namespace std;

class ContactRecord

{

private:

int ContactID;

string FirstName;

string LastName;

string MiddleName;

vector PhoneNumberList;

private:

inline string FromPhoneTypeToString(PHONE_TYPE phoneType) const

{

switch (phoneType)

{

case HOME: return "HOME";

case WORK: return "WORK";

case MOBILE: return "MOBILE";

default: return "HOME";

}

}

private:

void CopyData(const ContactRecord & src);

public:

int GetContactID() { return this->ContactID; }

string GetFirstName() { return this->FirstName; }

string GetLastName() { return this->LastName; }

string GetMiddleName() { return this->MiddleName; }

void SetContactID(int v) { this->ContactID = v;}

void SetFirstName(string v) { this->FirstName = v; }

void SetLastName(string v) { this->LastName = v; }

void SetMiddleName(string v) { this->MiddleName = v; }

vector GetPhoneNumberList() const;

bool GetPhoneNumberByType(PHONE_TYPE phoneType, PhoneRecord & phoneInfo) const;

bool AddPhoneNumber(PHONE_TYPE phoneType, string phoneNumber);

bool UpdatePhoneNumber(PHONE_TYPE phoneType, string phoneNumber);

bool DeletePhoneNumber(PHONE_TYPE phoneType);

public:

void Print() const;

public:

ContactRecord();

ContactRecord(string firstName, string lastName, string middleName);

ContactRecord(const ContactRecord & src);

public:

const ContactRecord & operator=(const ContactRecord & src);

bool operator==(const ContactRecord & src);

};

***************************

2. ContactRecord.cpp – Look for all methods that have TODO markers since I don't know what to code there.

***************************

#include "ContactRecord.h"

bool ContactRecord::AddPhoneNumber(PHONE_TYPE phoneType, string phoneNumber)

{

// TODO: Need to implement this method.

}

bool ContactRecord::UpdatePhoneNumber(PHONE_TYPE phoneType, string phoneNumber)

{

// TODO: Need to implement this method.

}

bool ContactRecord::DeletePhoneNumber(PHONE_TYPE phoneType)

{

// TODO: Need to implement this method.

}

void ContactRecord::Print() const

{

cout << "ID: " << this->ContactID << ", "

<< "Last Name: " << this->LastName << ", "

<< "First Name: " << this->FirstName << ", "

<< "Middle Name: " << this->MiddleName << endl

<< "Phone Numbers: " << endl;

for (size_t i = 0; i < this->PhoneNumberList.size(); i++)

{

cout << " - (" << FromPhoneTypeToString(this->PhoneNumberList[i].Type) << ") "

<< this->PhoneNumberList[i].PhoneNumber

<< endl;

}

}

ContactRecord::ContactRecord()

{

this->FirstName = "";

this->LastName = "";

this->MiddleName = "";

this->ContactID = 0;

}

ContactRecord::ContactRecord(string firstName, string lastName, string middleName) :

FirstName(firstName),

LastName(lastName),

MiddleName(middleName),

ContactID(0)

{

}

ContactRecord::ContactRecord(const ContactRecord & src)

{

CopyData(src);

}

void ContactRecord::CopyData(const ContactRecord & src)

{

// TODO: Need to implement this method.

}

vector ContactRecord::GetPhoneNumberList() const

{

// TODO: Need to implement this method.

}

bool ContactRecord::GetPhoneNumberByType(PHONE_TYPE phoneType, PhoneRecord & phoneInfo) const

{

// TODO: Need to implement this method.

}

const ContactRecord & ContactRecord::operator=(const ContactRecord & src)

{

CopyData(src);

return *this;

}

bool ContactRecord::operator==(const ContactRecord & src)

{

return (this->ContactID == src.ContactID);

}

***************************

3. ContactDataManager.h – To support multiple phone numbers per contact, the ContactDataManager class is now expanded to include new methods to handle/manage the list of phone numbers per contact.

***************************

#pragma once

#include

#include "ContactRecord.h"

class ContactDataManager

{

private:

static int NextContactID;

private:

vector ContactList;

private:

void CopyData(const ContactDataManager & src);

ContactRecord * FindContactByID(int nContactID);

public:

ContactDataManager();

ContactDataManager(const ContactDataManager & src);

public:

size_t GetContactCount();

int AddContact(string firstName, string lastName, string middleName);

bool EditContact(int nContactID, string firstName, string lastName, string middleName);

bool AddPhoneNumber(int nContactID, PHONE_TYPE phoneType, string phoneNumber);

bool UpdatePhoneNumber(int nContactID, PHONE_TYPE phoneType, string phoneNumber);

bool RemovePhoneNumber(int nContactID, PHONE_TYPE phoneType);

vector GetPhoneNumberList(int nContactID);

bool RemoveContact(int nContactID);

bool RetrieveContact(int nContactID, ContactRecord & contact);

vector GetContactIDs();

public:

const ContactDataManager & operator=(const ContactDataManager & src);

};

***************************

4. ContactDataManager.cpp – Look for all methods that have TODO markersince I don't know what to code there.

***************************

#include "ContactDataManager.h"

int ContactDataManager::NextContactID = 0;

void ContactDataManager::CopyData(const ContactDataManager & src)

{

this->ContactList.clear();

for (size_t i = 0; i < src.ContactList.size(); i++)

{

this->ContactList.push_back(src.ContactList[i]);

}

}

ContactRecord * ContactDataManager::FindContactByID(int nContactID)

{

for (size_t i = 0; i < this->ContactList.size(); i++)

{

if (this->ContactList[i].GetContactID() == nContactID)

return &this->ContactList[i];

}

return NULL;

}

ContactDataManager::ContactDataManager()

{

}

ContactDataManager::ContactDataManager(const ContactDataManager & src)

{

CopyData(src);

}

size_t ContactDataManager::GetContactCount()

{

return this->ContactList.size();

}

int ContactDataManager::AddContact(string firstName, string lastName, string middleName)

{

ContactRecord rec(firstName, lastName, middleName);

rec.SetContactID(++ContactDataManager::NextContactID);

this->ContactList.push_back(rec);

return rec.GetContactID();

}

bool ContactDataManager::EditContact(int nContactID, string firstName, string lastName, string middleName)

{

for (size_t i = 0; i < this->ContactList.size(); i++)

{

ContactRecord & rec = this->ContactList[i];

if (rec.GetContactID() == nContactID)

{

rec.SetFirstName(firstName);

rec.SetLastName(lastName);

rec.SetMiddleName(middleName);

return true;

}

}

return false;

}

bool ContactDataManager::AddPhoneNumber(int nContactID, PHONE_TYPE phoneType, string phoneNumber)

{

// TODO: Need to implement this method.

}

bool ContactDataManager::UpdatePhoneNumber(int nContactID, PHONE_TYPE phoneType, string phoneNumber)

{

// TODO: Need to implement this method.

}

bool ContactDataManager::RemovePhoneNumber(int nContactID, PHONE_TYPE phoneType)

{

// TODO: Need to implement this method.

}

vector ContactDataManager::GetPhoneNumberList(int nContactID)

{

// TODO: Need to implement this method.

}

bool ContactDataManager::RemoveContact(int nContactID)

{

for (std::vector::iterator it = this->ContactList.begin(); it != this->ContactList.end(); ++it)

{

ContactRecord & rec = *it;

if (rec.GetContactID() == nContactID)

{

this->ContactList.erase(it);

return true;

}

}

return false;

}

bool ContactDataManager::RetrieveContact(int nContactID, ContactRecord & contact)

{

ContactRecord * pRec = FindContactByID(nContactID);

if (!pRec)

return false;

contact = *pRec;

return true;

}

vector ContactDataManager::GetContactIDs()

{

vector contactIds;

for (std::vector::iterator it = this->ContactList.begin(); it != this->ContactList.end(); ++it)

{

ContactRecord & rec = *it;

contactIds.push_back(rec.GetContactID());

}

return contactIds;

}

const ContactDataManager & ContactDataManager::operator=(const ContactDataManager & src)

{

CopyData(src);

return *this;

}

***************************

5. ContactDataInputManager.h – The ContactDataInputManager class drives all the user interactions which allow him/her to add, edit, remove contacts as well as their phone numbers.

***************************

#pragma once

#include

#include "ContactDataManager.h"

class ContactDataInputManager

{

private:

ContactDataManager * pContactDataManager;

private:

string WaitForDataLine();

int WaitForNumericValue();

int WaitForNumericSelection();

int WaitForMainOptionSelection();

PHONE_TYPE FromStringToPhoneType(string type) const;

private:

void AddNewContact();

void EditExistingContact();

void RemoveExistingContact();

void DisplayAllExistingContacts();

void AddPhoneNumberForContact();

void RemovePhoneFromContact();

public:

ContactDataInputManager(ContactDataManager & contactDataManager);

public:

void Run();

private:

// Prevent from copying and assigning.

ContactDataInputManager(const ContactDataInputManager & src);

const ContactDataInputManager & operator=(const ContactDataInputManager & src);

};

***************************

6. ContactDataInputManager.cpp – Look for all methods that have TODO markersince I don't know what to code there.

***************************

#include

#include

#include

#include

#include

#include "ContactDataInputManager.h"

string ContactDataInputManager::WaitForDataLine()

{

// Get data line.

string dataLine;

std::getline(cin, dataLine);

// Left trim for spaces.

dataLine.erase(dataLine.begin(), std::find_if(dataLine.begin(), dataLine.end(), std::not1(std::ptr_fun(std::isspace))));

// Right trim for spaces.

dataLine.erase(std::find_if(dataLine.rbegin(), dataLine.rend(), std::not1(std::ptr_fun(std::isspace))).base(), dataLine.end());

return dataLine;

}

int ContactDataInputManager::WaitForNumericValue()

{

try

{

string dataLine = WaitForDataLine();

return atoi(dataLine.c_str());

}

catch (...)

{

return -1;

}

}

int ContactDataInputManager::WaitForNumericSelection()

{

// First character will be the actual selection.

int selection = std::getchar();

if (selection == EOF)

return -1;

// The second character is the newline (caused by pressing the ENTER button).

int newline = std::getchar();

// Return the selection;

if ((selection >= '0') && (selection <= '9'))

return (selection - '0');

return -1;

}

int ContactDataInputManager::WaitForMainOptionSelection()

{

cout << "[MAIN MENU]" << endl

<< "Select the one of the below options:" << endl

<< " (1) Add new contact" << endl

<< " (2) Edit existing contact" << endl

<< " (3) Remove existing contact" << endl

<< " (4) List all contacts" << endl

<< " (5) Add phone number for contact" << endl

<< " (6) Remove phone from contact" << endl

<< " (7) Quit application" << endl

<< "==> ";

return WaitForNumericSelection();

}

PHONE_TYPE ContactDataInputManager::FromStringToPhoneType(string type) const

{

if (type.length() == 0)

return HOME;

transform(type.begin(), type.end(), type.begin(), ::toupper);

if (type == "HOME")

return HOME;

if (type == "WORK")

return WORK;

if (type == "MOBILE")

return MOBILE;

return HOME;

}

void ContactDataInputManager::AddNewContact()

{

cout << endl;

cout << "[ADD NEW CONTACT]" << endl;

cout << "Enter first name: ";

string firstName = WaitForDataLine();

cout << "Enter last name: ";

string lastName = WaitForDataLine();

cout << "Enter middle name: ";

string middleName = WaitForDataLine();

// TODO: Need to provide additional code to complete this method.

}

void ContactDataInputManager::EditExistingContact()

{

cout << endl;

cout << "[EDIT EXISTING CONTACT]" << endl;

cout << "Enter contact ID: ";

int contactID = WaitForNumericValue();

cout << "Enter first name: ";

string firstName = WaitForDataLine();

cout << "Enter last name: ";

string lastName = WaitForDataLine();

cout << "Enter middle name: ";

string middleName = WaitForDataLine();

// TODO: Need to provide additional code to complete this method.

}

void ContactDataInputManager::RemoveExistingContact()

{

cout << endl;

cout << "[REMOVE EXISTING CONTACT]" << endl;

cout << "Enter contact ID: ";

int contactID = WaitForNumericValue();

  

// TODO: Need to provide additional code to complete this method.

}

void ContactDataInputManager::DisplayAllExistingContacts()

{

cout << endl;

cout << "[AVAILABLE CONTACTS]" << endl;

// TODO: Need to provide additional code to complete this method.

}

void ContactDataInputManager::AddPhoneNumberForContact()

{

cout << endl;

cout << "[ADD PHONE NUMBER FOR CONTACT]" << endl;

cout << "Enter contact ID: ";

int contactID = WaitForNumericValue();

cout << "Enter phone number: ";

string phoneNumber = WaitForDataLine();

cout << "Enter phone type (HOME, WORK, MOBILE): ";

string phoneTypeText = WaitForDataLine();

PHONE_TYPE phoneTypeEnum = FromStringToPhoneType(phoneTypeText);

// TODO: Need to provide additional code to complete this method.

}

void ContactDataInputManager::RemovePhoneFromContact()

{

cout << endl;

cout << "[REMOVE PHONE FROM CONTACT]" << endl;

cout << "Enter contact ID: ";

int contactID = WaitForNumericValue();

cout << "Enter phone type (HOME, WORK, MOBILE): ";

string phoneTypeText = WaitForDataLine();

PHONE_TYPE phoneTypeEnum = FromStringToPhoneType(phoneTypeText);

// TODO: Need to provide additional code to complete this method.

}

ContactDataInputManager::ContactDataInputManager(ContactDataManager & contactDataManager)

{

this->pContactDataManager = &contactDataManager;

}

void ContactDataInputManager::Run()

{

bool loop = true;

while (loop)

{

int mainSelection = WaitForMainOptionSelection();

switch (mainSelection)

{

case 1:

AddNewContact();

break;

case 2:

EditExistingContact();

break;

case 3:

RemoveExistingContact();

break;

case 4:

DisplayAllExistingContacts();

break;

case 5:

AddPhoneNumberForContact();

break;

case 6:

RemovePhoneFromContact();

break;

case 7:

loop = false;

break;

default:

break;

}

}

}

ContactDataInputManager::ContactDataInputManager(const ContactDataInputManager & src)

{

throw new exception("The ContactDataInputManager object cannot be assigned or copied!");

}

const ContactDataInputManager & ContactDataInputManager::operator=(const ContactDataInputManager & src)

{

throw new exception("The ContactDataInputManager object cannot be assigned or copied!");

}

***************************

7. PhoneRecord.h - No work needed, this is part of the project to better understand

***************************

#pragma once

#include

#include

using namespace std;

typedef enum

{

HOME,

WORK,

MOBILE

} PHONE_TYPE;

struct PhoneRecord

{

PHONE_TYPE Type;

string PhoneNumber;

};

***************************

8. Main.cpp - No work needed, this is part of the project to better understand

***************************

#include "ContactRecord.h"

#include "ContactDataManager.h"

#include "ContactDataInputManager.h"

int main(int argc, char *argv[], char *envp[])

{

ContactDataManager contactDataManager;

ContactDataInputManager ContactDataInputManager(contactDataManager);

ContactDataInputManager.Run();

return 0;

}

Explanation / Answer

Hi,

There are 2 things you should read first..

1. The code in question does not show all the #include lines completely ... I tried to include the needed files as needed. So the order of #include lines in each file in answer may not match the original order you have given in question. Request you to copy paste the #include lines for each of the files as you originally have.

2. For me, The ContactDataInputManager.cpp file shows error on dataLine.erase() lines in WaitForDataLine() and also in throw new exception("...") in ContactDataInputManager::ContactDataInputManager() and ContactDataInputManager::operator=() functions. So in order to test the implementation, I just commented out these lines and tested. The program works as intented after commenting out these lines. If these lines don't show any errors for you , you don't need to comment them out.

Let me know if you have any doubts. Please do rate the answer if it helped. Thank you.

ContactRecord.cpp

#include "ContactRecord.h"
bool ContactRecord::AddPhoneNumber(PHONE_TYPE phoneType, string phoneNumber)
{
PhoneRecord rec;
if(GetPhoneNumberByType(phoneType, rec) == false)
{
rec.PhoneNumber = phoneNumber;
rec.Type = phoneType;
PhoneNumberList.push_back(rec);
return true;
}
else
return false;//already phone with phoneType exists
}
bool ContactRecord::UpdatePhoneNumber(PHONE_TYPE phoneType, string phoneNumber)
{
for(int i = 0; i < PhoneNumberList.size(); i++)
if(PhoneNumberList[i].Type == phoneType)
{
PhoneNumberList[i].PhoneNumber = phoneNumber;
return true;
}
  
return false;//when no match was found
}
bool ContactRecord::DeletePhoneNumber(PHONE_TYPE phoneType)
{
for(vector<PhoneRecord>::iterator it =PhoneNumberList.begin(); it != PhoneNumberList.end(); ++it)
{
if( (*it).Type == phoneType)
{
PhoneNumberList.erase(it);
return true;
}
}
return false;//when not found
}
void ContactRecord::Print() const
{
cout << "ID: " << this->ContactID << ", "
<< "Last Name: " << this->LastName << ", "
<< "First Name: " << this->FirstName << ", "
<< "Middle Name: " << this->MiddleName << endl
<< "Phone Numbers: " << endl;
for (size_t i = 0; i < this->PhoneNumberList.size(); i++)
{
cout << " - (" << FromPhoneTypeToString(this->PhoneNumberList[i].Type) << ") "
<< this->PhoneNumberList[i].PhoneNumber
<< endl;
}
}
ContactRecord::ContactRecord()
{
this->FirstName = "";
this->LastName = "";
this->MiddleName = "";
this->ContactID = 0;
}
ContactRecord::ContactRecord(string firstName, string lastName, string middleName) :
FirstName(firstName),
LastName(lastName),
MiddleName(middleName),
ContactID(0)
{
}
ContactRecord::ContactRecord(const ContactRecord & src)
{
CopyData(src);
}
void ContactRecord::CopyData(const ContactRecord & src)
{
this->ContactID = src.ContactID;
this->FirstName = src.FirstName;
this->LastName = src.LastName;
this->MiddleName = src.MiddleName;
this->PhoneNumberList = src.GetPhoneNumberList();
}
vector<PhoneRecord> ContactRecord::GetPhoneNumberList() const
{
vector<PhoneRecord> numberList;
for (int i = 0; i < this->PhoneNumberList.size(); i++)
{
numberList.push_back( this->PhoneNumberList[i]);
}
  
return numberList;
}
bool ContactRecord::GetPhoneNumberByType(PHONE_TYPE phoneType, PhoneRecord & phoneInfo) const
{
for (int i = 0; i < this->PhoneNumberList.size(); i++)
{
if(PhoneNumberList[i].Type == phoneType)
{
phoneInfo = PhoneNumberList[i];
return true;
}
}
  
return false;//no match
}
const ContactRecord & ContactRecord::operator=(const ContactRecord & src)
{
CopyData(src);
return *this;
}
bool ContactRecord::operator==(const ContactRecord & src)
{
return (this->ContactID == src.ContactID);
}

ContactDataManager.cpp

#include "ContactDataManager.h"
int ContactDataManager::NextContactID = 0;
void ContactDataManager::CopyData(const ContactDataManager & src)
{
this->ContactList.clear();
for (size_t i = 0; i < src.ContactList.size(); i++)
{
this->ContactList.push_back(src.ContactList[i]);
}
}
ContactRecord * ContactDataManager::FindContactByID(int nContactID)
{
for (size_t i = 0; i < this->ContactList.size(); i++)
{
if (this->ContactList[i].GetContactID() == nContactID)
return &this->ContactList[i];
}
return NULL;
}
ContactDataManager::ContactDataManager()
{
}
ContactDataManager::ContactDataManager(const ContactDataManager & src)
{
CopyData(src);
}
size_t ContactDataManager::GetContactCount()
{
return this->ContactList.size();
}
int ContactDataManager::AddContact(string firstName, string lastName, string middleName)
{
ContactRecord rec(firstName, lastName, middleName);
rec.SetContactID(++ContactDataManager::NextContactID);
this->ContactList.push_back(rec);
return rec.GetContactID();
}
bool ContactDataManager::EditContact(int nContactID, string firstName, string lastName, string middleName)
{
for (size_t i = 0; i < this->ContactList.size(); i++)
{
ContactRecord & rec = this->ContactList[i];
if (rec.GetContactID() == nContactID)
{
rec.SetFirstName(firstName);
rec.SetLastName(lastName);
rec.SetMiddleName(middleName);
return true;
}
}
return false;
}
bool ContactDataManager::AddPhoneNumber(int nContactID, PHONE_TYPE phoneType, string phoneNumber)
{
for(int i = 0; i < ContactList.size(); i++)
{
if(ContactList[i].GetContactID() == nContactID)
{
return ContactList[i].AddPhoneNumber(phoneType, phoneNumber);
}
}
return false;
}
bool ContactDataManager::UpdatePhoneNumber(int nContactID, PHONE_TYPE phoneType, string phoneNumber)
{
for(int i = 0; i < ContactList.size(); i++)
{
if(ContactList[i].GetContactID() == nContactID)
{
return ContactList[i].UpdatePhoneNumber(phoneType, phoneNumber);
}
}
return false;

}
bool ContactDataManager::RemovePhoneNumber(int nContactID, PHONE_TYPE phoneType)
{
for(int i = 0; i < ContactList.size(); i++)
{
if(ContactList[i].GetContactID() == nContactID)
{
return ContactList[i].DeletePhoneNumber(phoneType);
  
}
}
return false;

}
vector<PhoneRecord> ContactDataManager::GetPhoneNumberList(int nContactID)
{
vector<PhoneRecord> phones;
for(int i = 0; i < ContactList.size(); i++)
{
if(ContactList[i].GetContactID() == nContactID)
{
phones = ContactList[i].GetPhoneNumberList();
break;
}
}
return phones;
  
}
bool ContactDataManager::RemoveContact(int nContactID)
{
for (std::vector<ContactRecord>::iterator it = this->ContactList.begin(); it != this->ContactList.end(); ++it)
{
ContactRecord & rec = *it;
if (rec.GetContactID() == nContactID)
{
this->ContactList.erase(it);
return true;
}
}
return false;
}
bool ContactDataManager::RetrieveContact(int nContactID, ContactRecord & contact)
{
ContactRecord * pRec = FindContactByID(nContactID);
if (!pRec)
return false;
contact = *pRec;
return true;
}
vector<int> ContactDataManager::GetContactIDs()
{
vector<int> contactIds;
for (std::vector<ContactRecord>::iterator it = this->ContactList.begin(); it != this->ContactList.end(); ++it)
{
ContactRecord & rec = *it;
contactIds.push_back(rec.GetContactID());
}
return contactIds;
}
const ContactDataManager & ContactDataManager::operator=(const ContactDataManager & src)
{
CopyData(src);
return *this;
}

ContactDataInputManager.cpp


#include <iostream>
#include <algorithm>
#include <functional>
#include <exception>
#include <cctype>

#include "ContactDataInputManager.h"
using namespace std;
string ContactDataInputManager::WaitForDataLine()
{
// Get data line.
string dataLine;
std::getline(cin, dataLine);
// Left trim for spaces.
dataLine.erase(dataLine.begin(), std::find_if(dataLine.begin(), dataLine.end(), std::not1(std::ptr_fun(std::isspace))));
// Right trim for spaces.
dataLine.erase(std::find_if(dataLine.rbegin(), dataLine.rend(), std::not1(std::ptr_fun(std::isspace))).base(), dataLine.end());
return dataLine;
}
int ContactDataInputManager::WaitForNumericValue()
{
try
{
string dataLine = WaitForDataLine();
return atoi(dataLine.c_str());
}
catch (...)
{
return -1;
}
}
int ContactDataInputManager::WaitForNumericSelection()
{
// First character will be the actual selection.
int selection = std::getchar();
if (selection == EOF)
return -1;
// The second character is the newline (caused by pressing the ENTER button).
int newline = std::getchar();
// Return the selection;
if ((selection >= '0') && (selection <= '9'))
return (selection - '0');
return -1;
}
int ContactDataInputManager::WaitForMainOptionSelection()
{
cout << "[MAIN MENU]" << endl
<< "Select the one of the below options:" << endl
<< " (1) Add new contact" << endl
<< " (2) Edit existing contact" << endl
<< " (3) Remove existing contact" << endl
<< " (4) List all contacts" << endl
<< " (5) Add phone number for contact" << endl
<< " (6) Remove phone from contact" << endl
<< " (7) Quit application" << endl
<< "==> ";
return WaitForNumericSelection();
}
PHONE_TYPE ContactDataInputManager::FromStringToPhoneType(string type) const
{
if (type.length() == 0)
return HOME;
transform(type.begin(), type.end(), type.begin(), ::toupper);
if (type == "HOME")
return HOME;
if (type == "WORK")
return WORK;
if (type == "MOBILE")
return MOBILE;
return HOME;
}
void ContactDataInputManager::AddNewContact()
{
cout << endl;
cout << "[ADD NEW CONTACT]" << endl;
cout << "Enter first name: ";
string firstName = WaitForDataLine();
cout << "Enter last name: ";
string lastName = WaitForDataLine();
cout << "Enter middle name: ";
string middleName = WaitForDataLine();
int contactID = pContactDataManager->AddContact(firstName, lastName, middleName);
cout << "New contact added with ID = " << contactID << endl;
}
void ContactDataInputManager::EditExistingContact()
{
cout << endl;
cout << "[EDIT EXISTING CONTACT]" << endl;
cout << "Enter contact ID: ";
int contactID = WaitForNumericValue();
cout << "Enter first name: ";
string firstName = WaitForDataLine();
cout << "Enter last name: ";
string lastName = WaitForDataLine();
cout << "Enter middle name: ";
string middleName = WaitForDataLine();
if(pContactDataManager->EditContact(contactID, firstName, lastName, middleName))
cout << "Contact with ID " << contactID << " edited successfully " << endl;
else
cout << "Contact with ID " << contactID << " not found" << endl;
  

}
void ContactDataInputManager::RemoveExistingContact()
{
cout << endl;
cout << "[REMOVE EXISTING CONTACT]" << endl;
cout << "Enter contact ID: ";
int contactID = WaitForNumericValue();
if(pContactDataManager->RemoveContact(contactID))
cout << "Contact with ID " << contactID << " removed successfully " << endl;
else
cout << "Contact with ID " << contactID << " not found" << endl;

}
void ContactDataInputManager::DisplayAllExistingContacts()
{
cout << endl;
cout << "[AVAILABLE CONTACTS]" << endl;
ContactRecord rec;
vector<int> ids = pContactDataManager->GetContactIDs();
for(int i = 0; i < ids.size(); i++)
{
pContactDataManager->RetrieveContact(ids[i], rec);
rec.Print();
cout << endl;
}
}
void ContactDataInputManager::AddPhoneNumberForContact()
{
cout << endl;
cout << "[ADD PHONE NUMBER FOR CONTACT]" << endl;
cout << "Enter contact ID: ";
int contactID = WaitForNumericValue();
cout << "Enter phone number: ";
string phoneNumber = WaitForDataLine();
cout << "Enter phone type (HOME, WORK, MOBILE): ";
string phoneTypeText = WaitForDataLine();
PHONE_TYPE phoneTypeEnum = FromStringToPhoneType(phoneTypeText);
if(pContactDataManager->AddPhoneNumber(contactID, phoneTypeEnum, phoneNumber))
cout << "Added new phone number to contact with ID " << contactID << endl;
else
cout << "Could not add phone number to contact with ID " << contactID << endl;
}
void ContactDataInputManager::RemovePhoneFromContact()
{
cout << endl;
cout << "[REMOVE PHONE FROM CONTACT]" << endl;
cout << "Enter contact ID: ";
int contactID = WaitForNumericValue();
cout << "Enter phone type (HOME, WORK, MOBILE): ";
string phoneTypeText = WaitForDataLine();
PHONE_TYPE phoneTypeEnum = FromStringToPhoneType(phoneTypeText);
if(pContactDataManager->RemovePhoneNumber(contactID, phoneTypeEnum))
cout << "Removed phone of type " << phoneTypeText << " from contact Id " << contactID << endl;
else
cout << "Could not remove phone number for contact Id " << contactID << endl;
}
ContactDataInputManager::ContactDataInputManager(ContactDataManager & contactDataManager)
{
this->pContactDataManager = &contactDataManager;
}
void ContactDataInputManager::Run()
{
bool loop = true;
while (loop)
{
int mainSelection = WaitForMainOptionSelection();
switch (mainSelection)
{
case 1:
AddNewContact();
break;
case 2:
EditExistingContact();
break;
case 3:
RemoveExistingContact();
break;
case 4:
DisplayAllExistingContacts();
break;
case 5:
AddPhoneNumberForContact();
break;
case 6:
RemovePhoneFromContact();
break;
case 7:
loop = false;
break;
default:
break;
}
}
}
ContactDataInputManager::ContactDataInputManager(const ContactDataInputManager & src)
{
throw new exception("The ContactDataInputManager object cannot be assigned or copied!");
}
const ContactDataInputManager & ContactDataInputManager::operator=(const ContactDataInputManager & src)
{
throw new exception("The ContactDataInputManager object cannot be assigned or copied!");
//return *this;
}

output

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 1

[ADD NEW CONTACT]
Enter first name: JOHN
Enter last name: SMITH
Enter middle name: P
New contact added with ID = 1
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 5

[ADD PHONE NUMBER FOR CONTACT]
Enter contact ID: 1
Enter phone number: 1111
Enter phone type (HOME, WORK, MOBILE): HOME
Added new phone number to contact with ID 1
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 4

[AVAILABLE CONTACTS]
ID: 1, Last Name: SMITH, First Name: JOHN, Middle Name: P
Phone Numbers:
- (HOME) 1111

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 1

[ADD NEW CONTACT]
Enter first name: RICHARD
Enter last name: WILLIAMS
Enter middle name: K
New contact added with ID = 2
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 5

[ADD PHONE NUMBER FOR CONTACT]
Enter contact ID: 2
Enter phone number: 2222
Enter phone type (HOME, WORK, MOBILE): WORK
Added new phone number to contact with ID 2
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 4

[AVAILABLE CONTACTS]
ID: 1, Last Name: SMITH, First Name: JOHN, Middle Name: P
Phone Numbers:
- (HOME) 1111

ID: 2, Last Name: WILLIAMS, First Name: RICHARD, Middle Name: K
Phone Numbers:
- (WORK) 2222

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 5

[ADD PHONE NUMBER FOR CONTACT]
Enter contact ID: 2
Enter phone number: 3333
Enter phone type (HOME, WORK, MOBILE): WORK
Could not add phone number to contact with ID 2
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 4

[AVAILABLE CONTACTS]
ID: 1, Last Name: SMITH, First Name: JOHN, Middle Name: P
Phone Numbers:
- (HOME) 1111

ID: 2, Last Name: WILLIAMS, First Name: RICHARD, Middle Name: K
Phone Numbers:
- (WORK) 2222

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 2

[EDIT EXISTING CONTACT]
Enter contact ID: 2
Enter first name: RICHIE
Enter last name: WILL
Enter middle name: K
Contact with ID 2 edited successfully
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 4

[AVAILABLE CONTACTS]
ID: 1, Last Name: SMITH, First Name: JOHN, Middle Name: P
Phone Numbers:
- (HOME) 1111

ID: 2, Last Name: WILL, First Name: RICHIE, Middle Name: K
Phone Numbers:
- (WORK) 2222

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 5

[ADD PHONE NUMBER FOR CONTACT]
Enter contact ID: 1
Enter phone number: 3333
Enter phone type (HOME, WORK, MOBILE): MOBILE
Added new phone number to contact with ID 1
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 4

[AVAILABLE CONTACTS]
ID: 1, Last Name: SMITH, First Name: JOHN, Middle Name: P
Phone Numbers:
- (HOME) 1111
- (MOBILE) 3333

ID: 2, Last Name: WILL, First Name: RICHIE, Middle Name: K
Phone Numbers:
- (WORK) 2222

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 6

[REMOVE PHONE FROM CONTACT]
Enter contact ID: 1
Enter phone type (HOME, WORK, MOBILE): HOME
Removed phone of type HOME from contact Id 1
[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 4

[AVAILABLE CONTACTS]
ID: 1, Last Name: SMITH, First Name: JOHN, Middle Name: P
Phone Numbers:
- (MOBILE) 3333

ID: 2, Last Name: WILL, First Name: RICHIE, Middle Name: K
Phone Numbers:
- (WORK) 2222

[MAIN MENU]
Select the one of the below options:
(1) Add new contact
(2) Edit existing contact
(3) Remove existing contact
(4) List all contacts
(5) Add phone number for contact
(6) Remove phone from contact
(7) Quit application
==> 7

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