Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. Contact Manager: 50 Points Files: contact.cpp, contact.h contacts_main.cpp In order to finish this assignment, you need to learn how to use std::map. What makes

3. Contact Manager: 50 Points Files: contact.cpp, contact.h contacts_main.cpp In order to finish this assignment, you need to learn how to use std::map. What makes associative containers like std::map powerful and useful is that they can associate keys with values and quickly find the value associated with a specific key. For example in a contact manager we want to be able to associate a persons name with some information about them (like phone number, email, etc.) and quickly look up this information given a first and last name. In this problem you will create a contact management tool that will store some information about the users contacts and allow them to list them, add or remove contacts and search for a contact by name. Contact Information For each contact you will store the following data: First name Last name Phone number. Note: while phone numbers are numbers they are not numeric (like an int), they are strings of characters in [0, 9]. Do not store the phone number as an int. Email address The contact information should be stored in a class (maybe called Contact) declared in contact.h and defined in contact.cpp. Your contact class should also provide a default constructor (one that takes no parameters) as this is needed by the std::map to perform some operations. User Interaction The user will need to be able to interact with the contact manager by adding/removing contacts, searching by name and listing all contacts in the manager. They should also be able to exit the manager. To indicate to the user that the program is awaiting their input you should print a caret (>) followed by a space as a prompt. In the examples below the user input and expected output are shown interleaved, with the user input preceeded by the prompt. After receiving and executing the users command you should return to the initial input prompt so they can enter another command. Examples of a full interactive session are shown after the details of each command. To help you start this is what your main function should look like to loop over and handle a 6 users commands until they enter exit. You can use this as starter code for your program. Note that there is no cases loop in this assignment, just the user interaction loop. int main(){ std::string cmd; do { if (cmd == "exit"){ break; } else if (cmd == "list"){ // List contacts in the manager } else if (cmd == "add"){ // Add a contact to the manager } else if (cmd == "remove"){ // Remove a contact from the manager } else if (cmd == "find"){ // Find a contact in the manager } // Print a terminal prompt so the user knows // we're waiting for their input std::cout << "> "; } while (std::cin >> cmd); return 0; } Listing All Contacts When the user enters the list command and you should print out the first and last names of all the contacts in the manager. If there are no contacts in the manager you should just print no contacts. The contacts should be printed in alphabetical order, sorted by last name then by first name. This sorting will be done automatically for you if you build the key in the std::map properly. Example: The manager contains three people: Grace Hopper, Dennis Ritchie and Leslie Lamport > list Hopper, Grace Lamport, Leslie Ritchie, Dennis Example: The manager is empty. > list no contacts 7 Adding a Contact When the user enters the add command you should prompt them to enter the first name, last name, phone number and email address for the contact, one after the other. You should print a prompt each time so the user knows what the program is expecting them to input. First you should read the first name and print the prompt first name:, for the last name the prompt is last name:, for the phone number its phone number: and for the email address its e-mail:. If a contact with same first and last name is already in contact manager, then overwrite it with this new contact. Note that there is a trailing space after the colon printed for each prompt so the users input doesnt run right against the prompt. Example: The user wants to add Ada Lovelace as a contact. > add first name: Ada last name: Lovelace phone number: 555123456 e-mail: ada@lovelace.mathmail Removing a Contact When the user enters the remove command you should prompt them to enter the first and last name of the contact, with the same prompts used when adding a contact (first name: and last name:). If the contact is found they should be removed from the list and you should print a confirmation of the form removed FIRST LAST where FIRST and LAST are the first and last name of the contact that was removed. If no contact is found with the name entered you should print out contact not found. Example: The user wants to remove Richard Stallman, who is in the manager. > remove first name: Richard last name: Stallman removed Richard Stallman Example: The user wants to remove John McCarthy, who is not in the manager. > remove first name: John last name: McCarthy contact not found 8 Finding a Contact When the user enters the find command you should prompt them to enter the first and last name to search for, using the same prompt style as before. If the contact is found you should print out the full information about the contact (the name, phone number and email address). If the contact is not found you should print out contact not found. If a contact is found the contacts information should be printed out as: Name: FIRST LAST Phone Number: PHONENUMBER E-mail: EMAIL Where FIRST, LAST, PHONENUMBER and EMAIL correspond to the contacts data. Example: The user searches for Donald Knuth, who is in the manager. > find first name: Donald last name: Knuth Name: Donald Knuth Phone Number: 555898123 E-mail: knuth@donald.texmail Example: The user searches for Edsger Dijkstra, who is not in the manager. > find first name: Edsger last name: Dijkstra contact not found Exiting the Manager When the user enters the exit command your program should exit as done in the main function provided for you above. There is no example use case as the only thing your program should do after receiving the exit command is exit. Some Notes and Hints on std::map You should use a std::map to store the contact information where the keys will be some combination of their first and last name (as people can have the same first or last name, but not both) and the value will be your Contact struct. The actual values returned by the iterator over your map will be a std::pair where key would likely be std::string and Value would be a Contact. To access each element of the pair you can use first or second as shown below. 9 std::map my_map{{"hello", 4}, {"bob", 2}}; for (const auto &e : my_map){ // first accesses the key, second accesses the value std::cout << e.first << " mapped to " << e.second << " "; } This code will print: bob mapped to 2 hello mapped to 4 To insert into a map we can index it by the key with operator[]. If the key is not found a new entry is created using the default constructor and a reference returned (this is why we need a default constructor for Contact), if the key is found the existing entry will be returned. std::map my_map{{"hello", 4}, {"bob", 2}}; // Create key, value pair for joe and set it to 10 std::string joe = "joe"; my_map[joe] = 10; // Update the existing entry for hello my_map["hello"] = 5; for (const auto &e : my_map){ // first accesses the key, second accesses the value std::cout << e.first << " mapped to " << e.second << " "; } This code will print: bob mapped to 2 hello mapped to 5 joe mapped to 10 To find an entry by its key you can use find, see the interactive example on the documentation page for usage, and for how to see if the key was not found. Since the map will create the entry if its not found when indexing with operator[] you must use find to see if a key exists in the map. find returns an iterator so we can use find and erase to remove an element by its key since erase takes the iterator to the element that we want to remove. You can assume that you will receive valid input and commands and for simplicity that case does matter, for example Dennis Ritchie is a different person than dennis ritchie.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions