In: Computer Science
(LANGUAGE: C++)
1) For this lab, you will fill in what’s missing to make the attached file cust_leads.cpp work
2) The Lead class is for a very limited amount of information about a potential customer
· Include string fields for name and email
· A constructor that takes values for these two fields
· Include a getter for the email field
· Overload the == operator
· 2 Lead’s are equal if the names are the same (the emails don’t need to match)
· the first few lines of main() test this operator, and then you use it when adding a new lead and getting contact info about a lead
3) The Command class represents an option for the main menu. It includes:
· A char to pick this command
· A string to describe what the command does
· A constructor that takes these two parameters
· A “friend” line to allow an overloaded << operator to output a Command
· Write the overloaded << operator as a separate, global function, just as in the textbook and notes
4) The add function to add a new lead. It takes a vector<Lead> & parameter
· Read in name and email values to create a new Lead
· Use the == operator to compare this new lead against what is already in the vector. You can write the search code yourself or use the find() function of the <algorithm> library
· If you want to use find() (and it is not required), note that it takes 3 parameters: a begin iterator, an end iterator and a target
(1) The iterators are what you would get from vector’s begin() and end() functions
(2) The target must be of type Lead, so you need to create a “dummy” Lead object with the name to search and use this object as the 3rd parameter – the name is just a string and will not work
(3) Also note that find() returns an iterator:
(a) It matches the end() iterator if the target was not found
(b) Otherwise, you can dereference the iterator to get the full Lead object that was found
· If the name does not exist yet in the vector, add the new lead to the vector
· If the name already exists in the parameter, output the error message indicated below
5) The get_contact function to look up a lead. It takes a vector<Lead> & parameter
· Read in the name to lookup
· If the name exists in the vector parameter, display the contact info
· If not, display the error message indicated below:
OUTPUT:
Test of == operator for Lead's
correct: Jack != Jill
correct: both have the same name
Program to manage leads for possible customers
Enter option from below:
q: quit program
a: add new lead
g: get contact info
a
Enter name: Mac
Enter email: [email protected]
Enter option from below:
q: quit program
a: add new lead
g: get contact info
a
Enter name: Mac
Mac is already on the list
Enter option from below:
q: quit program
a: add new lead
g: get contact info
a
Enter name: Nina
Enter email: [email protected]
Enter option from below:
q: quit program
a: add new lead
g: get contact info
g
Enter name to lookup: Nina
Email contact: [email protected]
Enter option from below:
q: quit program
a: add new lead
g: get contact info
g
Enter name to lookup: Zen
Person not found
Enter option from below:
q: quit program
a: add new lead
g: get contact info
q
Exiting program
GIVEN CODE
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; // Put classes and functions here int main() { const char QUIT = 'q'; const char ADD = 'a'; const char GET_CONTACT = 'g'; char cmd = ' '; cout << "Test of == operator for Lead's" << endl; Lead lead1("Jack", "[email protected]"), lead2("Jill", "[email protected]"), lead3("Jill", "[email protected]"); if (lead1 == lead2) cout << "error: should not match Jack and Jill" << endl; else cout << "correct: Jack != Jill" << endl; if (lead2 == lead3) cout << "correct: both have the same name" << endl; else cout << "error: should match leads with same name, even if email is different" << endl; cout << "Program to manage leads for possible customers" << endl << endl; vector<Command> cmds; cmds.push_back(Command(QUIT, "quit program")); cmds.push_back(Command(ADD, "add new lead")); cmds.push_back(Command(GET_CONTACT, "get contact info")); vector<Lead> leads; do { cout << "Enter option from below:" << endl; for (unsigned int i = 0; i < cmds.size(); i++) cout << cmds[i]; cin >> cmd; cmd = tolower(cmd); switch(cmd) { case QUIT: cout << "Exiting program" << endl; break; case ADD: add(leads); break; case GET_CONTACT: get_contact(leads); break; default: cerr << "Unrecognized command: " << cmd << endl; break; } } while (cmd != QUIT); }
Make sure to add comments for each class/function. Thank you.
Please find the solution below.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Lead class
class Lead {
public:
// name of the person
string name;
// email of the person
string email;
// constructor to initialize a new lead
Lead(string name, string email) {
this->name = name;
this->email = email;
}
// getter for email
string get_email() {
return email;
}
//overloading the == operator
bool operator==(Lead l1){
return name.compare(l1.name) == 0;
}
};
// command class
class Command {
public:
// input character
char c;
// task associated with input character
string task;
//constructor to initialize Command
Command(char c, string task) {
this->c = c;
this->task = task;
}
// friend line to enable << operator overloading
friend ostream& operator<<(ostream& os, const
Command& cmd);
};
//overloading << operator to handle command
differently
ostream& operator<<(ostream& os, Command&
cmd)
{
os << cmd.c <<": "<< cmd.task<< endl;
return os;
}
/** get contact if it exists in list of contacts
*
* prints email of contact if contact is found
* prints person not found if contact is not found
*/
void get_contact(vector<Lead> &leads) {
int found = 0;
cout<<"Enter name to lookup: ";
string name = "";
cin >> name;
//create a dummy lead to compare
Lead leadToFind(name, "email");
//iterate over leads to find contact and print if found
for(auto i = leads.begin();i != leads.end(); ++i) {
Lead lead = *i;
if(leadToFind == lead) {
cout<<"Email contact: "<< lead.get_email() <<
endl;
found = 1; // setting flag if contact is found
break;
}
}
if(found == 0) {
cout<<"Person not found"<<endl;
}
}
/** add contact if it does not exist in list of leads
*
* adds contact in Leads if contact is not found
*/
void add(vector<Lead> &leads) {
string name = "", email = "";
int found=0;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter email: ";
cin>>email;
Lead newLead(name, email);
//iterate over leads to find contact
for(auto i = leads.begin();i != leads.end(); ++i) {
Lead lead = *i;
if(lead == newLead) {
found = 1; // setting flag if contact is found
break;
}
}
if(found==0) { //add to list if not already there
leads.push_back(newLead);
} else { // do not add if contact is found in list
cout << name << " is already on the list" <<
endl;
}
}
int main() {
const char QUIT = 'q';
const char ADD = 'a';
const char GET_CONTACT = 'g';
char cmd = ' ';
cout << "Test of == operator for Lead's" << endl;
Lead lead1("Jack", "[email protected]"),
lead2("Jill", "[email protected]"),
lead3("Jill", "[email protected]");
if (lead1 == lead2)
cout << "error: should not match Jack and Jill" <<
endl;
else cout << "correct: Jack != Jill" << endl;
if (lead2 == lead3)
cout << "correct: both have the same name" <<
endl;
else cout << "error: should match leads with same name, even
if email is different" << endl;
cout << "Program to manage leads for possible customers" << endl << endl;
vector<Command> cmds;
cmds.push_back(Command(QUIT, "quit program"));
cmds.push_back(Command(ADD, "add new lead"));
cmds.push_back(Command(GET_CONTACT, "get contact info"));
vector<Lead> leads;
do {
cout << "Enter option from below:" << endl;
for (unsigned int i = 0; i < cmds.size(); i++)
cout << cmds[i];
cin >> cmd;
cmd = tolower(cmd);
switch(cmd) {
case QUIT:
cout << "Exiting program" << endl;
break;
case ADD:
add(leads);
break;
case GET_CONTACT:
get_contact(leads);
break;
default:
cerr << "Unrecognized command: " << cmd <<
endl;
break;
}
} while (cmd != QUIT);
}