(6) Display all the contacts records. marks] If the fifth option of the menu, ie
ID: 3746814 • Letter: #
Question
(6) Display all the contacts records. marks] If the fifth option of the menu, ie., '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. -- 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 COMPSCI 105 S2 C-Assignment One 8 of 13 6. Quit the program Enter your choice: 5 List of Contacts: Record #1: Name: John Phone: 7599943 Email: john@amail.com Record #2: Name: Kelly Phone: 4344345 Email: kelly@bmail.com Record #3: Name: Nicky Phone: 9774104 Email: nickyecmail.com Record #4: Name: Sam Phone: 5723943 Email: sam@dimail.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: To achiere 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 ie, the list self. recorda) 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 shonn in the abore example.Explanation / Answer
from typing import List
class PhoneBook:
def __init__(self,name:str,Phone:str,Email:str):# a readymade class which contains all records of phonebook listed one by one
self.name = name;
self.Phone = Phone;
self.Email = Email;
class PBS:
def display_a_record(self,name:str,Phone:str,Email:str): # a method to list a record in phone book
print("Name : " + name)
print("Phone : "+ Phone)
print("Email : "+Email)
def display_records(self,listOfContacts:List[PhoneBook]):# a method to sort all the records of the phonebook by name and list the contact one by one
listOfContacts.sort(key=lambda x: x.name, reverse=False)
print("List of contacts:")
i=0
for contact in listOfContacts:
i=i+1
print("Record #"+str(i)+":")
self.display_a_record(contact.name,contact.Phone,contact.Email)
if __name__ == '__main__':
listOfContacts: List[PhoneBook] = []
listOfContacts.append(PhoneBook("Rock", "98128321321", "rock@nic.in"))
listOfContacts.append(PhoneBook("Amanda", "9812124621", "amanda@nic.in"))
listOfContacts.append(PhoneBook("Nicky", "8774104", "nicky@cmail.com"))
obj=PBS()
#switch(1) ##Switch your choice to 5
obj.display_records(listOfContacts)#use your test case =5,to get the required result.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.