Aims of the request
solving problems using classes, lists of objects, stack, etc.
A Phone Book Management Program
In this question, you are going to use the class structures in Python to implement a simple phone book management program. Your program should display the contact records, assist the adding, removing, searching and updating records in the phone book.
The contact records are stored in a file called “contacts.txt” using the JSON format. As seen below, each line holds the information of a known contact including the name, phone number and email address. The information in this file is read into the program and stored (as a list of “Contact” objects) in an object of the “PhoneBook” class. When the program exists, it will automatically save the updated contact records into a file called “contacts2.txt”. Please DO NOT MODIFY the content of the “contacts.txt” file.
1 | ["John", "7589943", "john@amail.com"] |
You are provided with the following Python files in the ‘A1Q1Resource.zip’:
- Contact.py – contains the Contact class to store the information of a phone record. Please DO NOT MODIFY the content of this file.
- PhoneBook.py – contains the PhoneBook class (TO BE COMPLETED) so that it stores and manages the phone records.
- A1Q1abcd001.Py – contains the functions (TO BE COMPLETED) that display the menu and calls the methods defined in the PhoneBook class to fulfill the functions of the program.
You need to understand all the files, but the only files that you are allowed to modify are the PhoneBook.py and A1Q1abcd001.py files.
Complete the required methods accordingly described in the following 6 tasks to achieve the functions of the program. IMPORTANT: Test each function after you have completed it and make sure that the program runs as described.
Change the file name and UPI constant, and implement the get user input function
Rename the ‘A1Q1.py’ file into A1Q1 followed by your UPI, e.g., ‘A1Q1abcd001.py’. Assign your own UPI to the UPI constant variable defined in the display_intro() function of the ‘A1Q1abcd001.py’ file.
Implement the get_user_input(start,end) function inside the ‘A1Q1abcd001.py’ program, which receives an user choice of the menu option. The function should only accept a valid user input, i.e., a number between 1 and 6 (both inclusive). Any other numbers or value (e.g., non-digit characters) will be ignored. Its exact layout should have the same format as shown below.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22------------------------------------------------
-- A Phone Book Management Program by abcd001 --
------------------------------------------------
1. Look up a contact
2. Add a new contact
3. Change an existing contact
4. Delete a contact
5. Display all contacts
6. Quit the program
------------------------------------------------
Enter your choice: 8
Invalid menu option.
Please try again: E
Invalid menu option.
Please try again: 0
Invalid menu option.
Please try again: hello
Invalid menu option.
Please try again: 6
------------------------------------------------
Thank you!
------------------------------------------------
Search for an existing contact in the phone book
If the first option of the menu, i.e., ‘Look up a contact’, is selected, the program should allow the user to search for a known contact in the phone book. Its exact layout should have the same format as shown below.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37------------------------------------------------
-- A Phone Book Management Program by abcd001 --
------------------------------------------------
1. Look up a contact
2. Add a new contact
3. Change an existing contact
4. Delete a contact
5. Display all contacts
6. Quit the program
------------------------------------------------
Enter your choice: 1
------------------------------------------------
Enter the name: Kelly
Name: Kelly
Phone: 4344345
Email: kelly@bmail.com
------------------------------------------------
1. Look up a contact
2. Add a new contact
3. Change an existing contact
4. Delete a contact
5. Display all contacts
6. Quit the program
------------------------------------------------
Enter your choice: 1
------------------------------------------------
Enter the name: James
James is not found in the phone book.
------------------------------------------------
1. Look up a contact
2. Add a new contact
3. Change an existing contact
4. Delete a contact
5. Display all contacts
6. Quit the program
------------------------------------------------
Enter your choice:
To achieve the above task, you need to implement the find_a_record(), display_a_record() and search_a_record() functions inside the PhoneBook class.
The find_a_record(self, name)function locates the contact record that matches the given name from its parameter. The function returns the Contact object has the same name, or the value None if no matching record was found.
The display_a_record(self, item)function prints out a contact record in the format illustrated in the above example.
The search_a_record(self)fulfills the function of searching for a contact record with an user input name. The function requests for an user input, then it makes use of the find_a_record() to determine whether the input name exists in the phone book or not. If a matching name is found, it will make use of the display_a_record() function to print out the contact record. Otherwise, if no matching name found, it prints out the not-found message. The sample input/output are shown in the above example.
Add a new contact record to the phone book
If the second option of the menu, i.e., ‘Add a new contact’, is selected, the program should allow the user to add a new contact record to the phone book. Its exact layout should have the same format as shown below.
To achieve the above task, you should implement the add_a_record() method in the PhoneBook class.
The add_a_record(self) fulfills the function of adding a new contact record by user input. The function requests for an user input name, then it makes use of the find_a_record() to determine whether the input name already exists in the phone book or not. If no matching name is found, it will further asking input for the phone number and email address, then add a new Contact object to the phone book. Otherwise, if the input name already exists in the phone book, it prints out the error message. The sample input/output are shown in the above example.
Change an existing contact record
If the third option of the menu, i.e., ‘Change an existing contact’, is selected, the program should allow the user to update the phone number and email address of a known contact in the phone book. Its exact layout should have the same format as shown below.
To achieve the above task, you should implement the update_a_record() method in the PhoneBook class.
The update_a_record(self)fulfills the function of changing an existing contact record with user input. The function requests for a user input name, then it makes use of the find_a_record() to determine whether the input name already exists in the phone book or not. If a matching name is found, it will further asking input for the new phone number and email address, then change the corresponding Contact object in the phone book. Otherwise, if the input name does not exists in the phone book, it prints out the error message. The sample input/output are shown in the above example.
Remove an existing contact record
If the fourth option of the menu, i.e., ‘Delete a contact’, is selected, the program should allow the user to remove an existing contact record from the phone book. Its exact layout should have the same format as shown below.
To achieve the above task, you should implement the delete_a_record() method in the PhoneBook class.
The delete_a_record(self)fulfills the function of removing an existing contact record with user input. The function requests for a user input name, then it makes use of the find_a_record() to determine whether the input name already exists in the phone book or not. If a matching name is found, it will remove the corresponding Contact object in the phone book. Otherwise, if the input name does not exists in the phone book, it prints out the error message. The sample input/output are shown in the above example.
Display all the contacts records
If the fifth option of the menu, i.e., ‘Display all contacts’, is selected, the program should print out all the contact records in the phone book. Its exact layout should have the same format as shown below.
To achieve the above task, you should implement the display_records() function in the PhoneBook class.
The display_records(self)fulfills the function of printing out all contact records. The function will first sort the contact records by the name in an ascending order. It then iterate through the sorted phone book (i.e., the list self.__records) and makes use of the display_a_record() function to print out each contact record one by one in the required format. To sort the elements in a list structure, you can use the predefined function sort(). The sample input/output are shown in the above example.