#phone.txt Donald Trump 1600 Pennsylvania Ave NW Washington DC 20500 USA 202-456-1414 trump@whitehouse.gov Greg Abbott 1010 Colorado St. Austin TX 78701 USA 512-463-5516 abbott@governor_state.tx.gov #Please
#phone.txt
Donald Trump 1600 Pennsylvania Ave NW Washington DC 20500 USA 202-456-1414 trump@whitehouse.gov Greg Abbott 1010 Colorado St. Austin TX 78701 USA 512-463-5516 abbott@governor_state.tx.gov
#Please complete the code in Python.
The purpose of this assignment is to build a phone book that will store contact information of people that you know. The contact information will be the address, phone number, and e-mail address. In this phone book you can add, delete, lookup, and update information about a person using their name as the key. The data structure that you will be using will be a dictionary.
All the names of the people and their contact information will be stored in a file called phone.txt. Each person will have the following pieces of information associated with him or her:
name
street
city
state
zip
country
phone number
e-mail address
Each piece of information (i.e. name, street, etc.) will be stored in the file on separate lines. There will be a blank line at the end of the contact information of each person.
This program is menu driven. When the program is initiated, a function opens the file phone.txt reads the data from the file creates a ContactInfo object and stores that object as the value in a dictionary where the name of the person is the key. The menu choices are:
add a person
delete a person
search for a person
update the information of a person
save and quit the program
The opening screen for the phone book will appear as shown below:
Phone Book 1. Add a Person 2. Delete a Person 3. Search for a Person 4. Update Information on a Person 5. Quit Enter your selection:
The user will make a menu selection by typing a number between 1 and 5. If the user selects a number other than a number in that range then you will state that it is an invalid selection and ask the user to select again. You will keep prompting until the user either makes a valid selection or quits.
Each valid selection will be handled by a separate function. After your program has handled menu items 1 through 4, the same menu should reappear for additional requests by the user. The handling of each menu item is explained in detail below. When the user selects menu item 5, the appropriate function is called and then the program prints a message thanking the user.
Thank you for using the phone book.
1. Add a Person: This function will prompt the user to enter the following data:
Enter name: Enter street: Enter city: Enter state: Enter zip: Enter country: Enter phone number: Enter e-mail address:
You will create a ContactInfo object and add that to the dictionary with the name as key. Here are special cases that you need to consider:
If the name already exists, alert the user and return from the function.
If the name is not entered, do nothing.
If any other data item is not entered, create the ContactInfo object with a blank string for that field.
2. Delete a Person: Prompt the user to enter the name.
Enter name:
If the name exists, delete the the person from the dictionary. Write a message to that effect.
So-and-So was deleted from the phone book.
If the name does not exist write a message to that effect.
So-and-So does not exist in the phone book.
3. Search for a Person: Prompt the user to enter the name.
Enter name:
If the name exists, write the name and the contact information in a neat format. If the name does not exist write a message to that effect.
So-and-So does not exist in the phone book.
4. Update Information on a Person: This function will prompt the user to enter the information below. The user should enter a valid name and any other piece or pieces of information that he wants to update. He may choose not to enter any data field that has not changed. For example, if only the e-mail address has changed for a person, then the user will enter only the name and the e-mail address and leave everything blank.
Enter name: Enter street: Enter city: Enter state: Enter zip: Enter country: Enter phone number: Enter e-mail address:
If the name exists, retrieve the ContactInfo object from the dictionary for that person. For those fields that are not blank change it in the ContactInfo object and store it back in the dictionary. If the name does not exist, write a message to that effect.
So-and-So does not exist in the phone book.
5. Quit: In this function, you will open the file phone.txt for writing (not appending). You will write out all the key-value pairs in the file in exactly the same format that you read the data in. You will close the file after writing.
Before you turn your assignment in, you must thoroughly test each of the functionality. Here is a list of tests that you must perform and your program should pass each test.
Add at least 2 people to your phone book.
Quit and restart the program.
Search for a person that you just added. Your program should print out the information on that person.
Search for a person you know is not in the phone book. Your program should print out that that person was not found.
Update the information of a person in the phone book.
Search for the same person in the phone book so that you can see that the information has been correctly updated.
Delete a person that you know is there from the phone book.
Search for that person and the program should print out that that person could not be found.
class ContactInfo (object): # constructor def __init__ (self, street, city, state, zip_code, country, phone, email): self.street = street self.city = city self.state = state self.zip_code = zip_code self.country = country self.phone = phone self.email = email # string representation of Contact Info def __str__(self): return (str(self.street)) + " " + (str(self.city)) + " " + (str(self.state)) + " " + (str(self.zip_code)) + " " + (str(self.country)) + " " + (str(self.phone)) + " " + (str(self.email)) # Define global dictionary to hold all the contact information phone_book = {} #import collections #phone_book = collections.OrderedDict() # This function adds the contact information of a new person in the # dictionary def add_person(): pass # Prompt the user to enter the name of the new person # Check if name exists in phone book. If it does print a message # to that effect and return # Prompt the user to enter the required contact information street = input("Enter street: ") city = input("Enter city: ") state = input("Enter state: ") zip_code = input("Enter zip: ") country = input("Enter country: ") phone = input("Enter phone: ") email = input("Enter e-mail: ") # Create the ContactInfo object contactObj = ContactInfo (street, city, state, zip_code, country, phone, email) # Add the name and the contact information to the phone dictionary # Print message that the information was added successfully # This function deletes an existing person from the phone dictionary def delete_person(): pass # Prompt the user to enter the name of the person # If the name exists in phone book delete it. # Print message as to the action. # This function updates the information of an existing person def update_person(): pass # Prompt the user to enter the name of the person # Check if name exists in phone book. If it does prompt # the user to enter the required information. # Write a message as to the action # This function prints the contact information of an existing person def search_person(): pass # Prompt the user to enter the name of the person # Check if name exists in phone book. If it does print the # information in a neat format. # If the name does not exist print a message to that effect. # This function open the file for writing and writes out the contents # of the dictionary. def save_quit(): pass # Open file for writing # Iterate through the dictionary and write out the items in the file # Close file # Print message # This function prints the menu, prompts the user for his/her selection # and returns it. def menu(): print() print("1. Add a Person") print() print("2. Delete a Person") print() print("3. Search for a Person") print() print("4. Update Information on a Person") print() print("5. Quit") print() user_input = eval(input("Enter your selection: ")) if user_input < 1 or user_input > 7: raise ValueError("Invalid Input") # creates an error so it goes to the expect part of the main else: return user_input # This function opens the file for reading, reads the contact information # for each person and adds it to the dictionary. def create_phone_book(): # Open file for reading in_file = open ("./phone.txt", "r") # Read first line (name) line = in_file.readline() line = line.strip() #print(line) # Loop through the entries for each person contact_info_list = [] while (line != ""): name = line print(name) #phone_book[name] # Read street line = in_file.readline() line = line.strip() street = line print(street) #phone_book[name] = line # Read city line = in_file.readline() line = line.strip() print(line) city = line #phone_book[name] = line # Read state line = in_file.readline() line = line.strip() print(line) state = line #phone_book[name] = line # Read zip-code line = in_file.readline() line = line.strip() print(line) zip = line #phone_book[name] = line # Read country line = in_file.readline() line = line.strip() print(line) country = line #phone_book[name] = line # Read phone number line = in_file.readline() line = line.strip() print(line) phone = line #phone_book[name] = line # Read e-mail address line = in_file.readline() line = line.strip() print(line) email = line #phone_book[name] = line # Read blank line line = in_file.readline() line = line.strip() print(line) #phone_book[name] = line # Read first line of the next block of data line = in_file.readline() line = line.strip() #print(line) # Create ContactInfo object contactObj = ContactInfo(street, city, state, zip, country, phone, email) print(contactObj) # Add to phone dictionary #phone[name] = contactObj # Close file print(phone_book) in_file.close() def main(): # Read file and create phone book dictionary create_phone_book() # Print logo print("Phone Book") # Print menu and get selection selection = menu() while selection != 5: if selection == 1: add_person() if selection == 2: delete_person() if selection == 3: search_person() if selection == 4: update_person() # Process request, print menu and prompt again and again # until the user types 5 to quit. # Save, print goodbye message, quit # This line above main is for grading purposes. It will not affect how # your code will run while you develop and test it. # DO NOT REMOVE THE LINE ABOVE MAIN if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started