Question

In: Computer Science

(LANGUAGE: C++) 1)     For this lab, you will fill in what’s missing to make the attached file...

(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.

Solutions

Expert Solution

Please find the solution below.

  • Comments have been added in the program for your convenience.
  • All input examples have been tested as described in the question

#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);

}


Related Solutions

sing Data Set C, fill in the missing data.
(a) Using Data Set C, fill in the missing data. (Round your p-values to 4 decimal places and other answers to 2 decimal places.)   R2    ANOVA table   Source F p-value   Regression          Variables p-value   Intercept       Floor       Offices       Entrances       Age       Freeway       (b) The predictors whose p-values are less than 0.05 are (You may select more than one answer. Click the box with a check mark for the correct answer and double...
Language: C++ I am starting to make a Bigint ADT and i have the hpp file...
Language: C++ I am starting to make a Bigint ADT and i have the hpp file finished now i need to make the methods for the functions. Problem: The data type int in C++ is limited to the word size of the CPU architecture (e.g., 32 or 64 bit). Therefore you can only work with signed integers up to 2,147,483,647 (in the case of signed 32 bit). Unsigned 32 bit is still only 10 digits. Maxint for 64 is somewhat...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
6. Use the periodic chart to fill in all the missing items so as to make...
6. Use the periodic chart to fill in all the missing items so as to make the nuclear decays complete. I.e., specify completely (i.e. including subscripts and superscripts) Z and X in each of the two separate reactions below. 92^238U + 0^1 n --> 57^140La + Z + 2 0^1n 88^226Ra --> X + 2^4 He
Lab 11 C++ Download the attached program and run it - as is. It will prompt...
Lab 11 C++ Download the attached program and run it - as is. It will prompt for a Fibonacci number then calculate the result using a recursive algorithm. Enter a number less then 40 unless you want to wait for a very long time. Find a number that takes between 10 and 20 seconds. (10,000 - 20,000 milliseconds). Modify the program to use the static array of values to "remember" previous results. Then when the function is called again with...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
c++   In this lab you will create a program to make a list of conference sessions...
c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...) You can hard code 10 Sessions in the beginning of your main program. For example, I have session UK1("Descaling agile",    "Gojko Adzic",       60);        session UK2("Theory of constraints", "Pawel Kaminski", 90); and then:        List l;        l.add(&UK1);        l.add(&UK2); Your Session struct should have the following member data: The Title The Speaker The session...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT