Almost everyone these days has some type of contact set for managing the phone numbers and email addresses of friends, co-workers, and acquaintances. Some use Google contacts( contacts.google.com ), others use the contacts app on their phone, and some still even prefer old-fashioned paper-and-pencil organization.
In this assignment, you will implement a very simple contact organizer application in Python.
You should write a program called contact_manager.py
.
Using this application, you will be able to request contact information, as well as add new contacts to the contact manager.
You are NOT allowed to use dictionaries or lists (with some exceptions). The exception is that you MAY use lists when it comes to reading in content from a file and splitting the lines, but you may not use them for storing the contacts for later access. You may also use a list when it comes to printing out the contacts, in sorted order Thus, you should use sets and tuples.
Specifically, I recommend storing the contacts in a set of tuples. The set will contain all of the contacts. Each contact will be represented by a tuple, and each tuple should contain exactly three values: name, email, and phone number. Your program should not allow a contact with the same name, email, and phone number to appear twice. Using a set can help you maintain this property of the contacts!
The contacts should also be able to be loaded from a file called contacts.txt
.
contacts.txt
will have a file format that looks like the following:
Bill Jones | bjon@gmail.com | 520-999-8765
Sam Dekker | dekker@apple.com | 123-456-7890
Janet Keller | kel@yahoo.com | 456-2356
James Jamie | jjamie@gmail.com | 435-234-2334
The precise names, emails, and phone numbers may vary.
Also, the number of contacts in the file may vary.
You should not assume that the file has any particular number of contacts in it, but you can assume that the file exists.
You can assume that the contacts.txt
file is in the same directory as the program you are implementing (contact_manager.py
).
When contact_manager.py
begins, it should read in and save all of the contact names, emails, and phone numbers into the program.
Since you won’t know how many contacts are in the file ahead-of-time, you should use a data structure to store the contacts.
You should store the contacts in a set of tuples. Each tuple within the set should have the name, email, and phone number, in that order. Using a set will automatically guarantee that no duplicate contact name+email+number will be able to be stored.
After optionally reading in all of the contacts from the contacts file, your program should repeatedly prompt the user to enter in commands. Your program should support three main types of commands.
The user can enter a command of the form show contacts with A B
, where A
is either name
, email
, or phone
, and B
is the name or email or phone to search for.
For instance, the user could type show contacts with name Indiana Jones
, where A
is name
and B
is Indiana Jones
.
Or, the user could type show contacts with email a@gmail.com
where A
would be email
and B
would be a@gmail.com
.
If the command that a user types begins with show contacts with
, then you can assume that the A
and B
components exist.
You should loop through the set of contacts and print out all of the ones where the name, email, or phone matches the search string.
Contacts should be reported with the following format:
C's contact info:
email: A
phone: B
However, you should replace C, A, and B with the actual name, email, and phone number. If there are no matching contacts, then print
None
In order to pass the test cases, you’ll need to print the matching contacts in sorted order. In order to do so:
matches = {. . .}
for contact in sorted(matches):
# print each contact
Using that sorted
function in the loop will give you the contacts in sorted order.
After processing the command, the program should prompt the user for the next command afterward.
When add contact
is typed, the program will continue to prompt the user for three additional values: The name, email, and number of the contact.
Once it prompts the user for these three things, the program will add this new contact to the set of contact info that are stored in the program.
Below is an example of what this should look like:
> add contact
name: X
email: Y
phone: Z
contact added!
This is what it would look like, but the X
, Y
, and Z
values will be custom values entered by the user.
Each time an add contact
command is processed, a new contact should be added to the set.
You do not need to validate that X
, Y
, and Z
are any particular type of input.
If the user types exit
(all lower case) then the program should print an exit message and end.
For example:
> exit
Goodbye!
Complete the following feature for extra credit.
When the user requests the program to exit, the program should save the contents of the contact set back into the contacts.txt
file.
This is important so that your program will remember all of the newly-added contacts when it runs again in the future.
If contacts were added via the add contact
command, these must be saved into the contacts.txt
file, using the format described earlier.
If any other command is entered, your program should print Huh?
and then prompt the user for another command.
I will be providing a lot of example runs/output on the diff-testing tool on the class website. You should take care to make sure that the output of your program very closely matches the example runs that I post. That will be the best way to make sure you are implementing the program correctly.
Several test-cases on the diff-testing tool on the class website. The text of each test will have multiple parts.
contacts.txt
should be from the get-go (before you run the program)Note that for some of the test cases, the ordering of contacts does not need to match perfectly in some cases. This is because order is not guaranteed for elements in a set!
It is due on Tuesday, 12/1/2020 at 7:00pm.
You should put all of your code in to a python file named contact_manager.py
.
Submit contact_manager.py
on Gradescope.