Answer to Question #198514 in Python for Andu

Question #198514

Project 1: Phonebook

Implement a program that manages a smartphone phonebook. Each contact might have the following data: name, surname, birthday and a set of Phone Numbers (to the same contact we may assign several phone numbers). The data are stored into a json file. The application should manage the following characteristics: create and add new contact the book ,remove existing contact (contacts are identified by name) , show all the contact , show the list of persons among your contact the has the birthdate

Changes need to be stored back in the file. The above functionalities are implemented through methods of a class named PhoneBook. The application interacts with the user through a menu where the user is asked for the next action to undertake (e.g., 1-Add new contact, 2- Remove Existing contact, etc.) Put comments in your code


1
Expert's answer
2021-05-26T15:31:49-0400
import pathlib
import json


class PhoneBook:
    def __init__(self):
        self.name = ""
        self.surname = ""
        self.birthday  = ""
        self.numberPhoneNumbers=0
        self.phoneNumbers = []
        
    
    def getContact(self):
        self.name =input("Enter the name: ")
        self.surname =input("Enter the surname: ")
        self.birthday =input("Enter date of birth: ")
        self.numberPhoneNumbers =int(input("Enter the number of phone numbers: "))
        for i in range(0,self.numberPhoneNumbers):
            phoneNumber=input("Enter phone number: ")
            self.phoneNumbers.append(phoneNumber)
        print("")


    def displayContact(self):
        print(f"The name: {self.name}")
        print(f"The surname: {self.surname}")
        print(f"Date of birth: {self.birthday}")
        print(f"Phone numbers: ")
        for i in range(0,self.numberPhoneNumbers):
            print(self.phoneNumbers[i])
            
    def getFullName(self):
        return self.name+" "+ self.surname




    def addPhoneNumber(self,phoneNumber):
        self.phoneNumbers.append(phoneNumber)




def main():
    choice=""
    phoneBooks={}
    
    while choice!="4":
        print("1 - Add a new contact")
        print("2 - Remove Existing contact")
        print("3 - Show all the contact") 
        print("4 - Exit phonebook")
        choice=input("Your choice: ")
        if(choice=="1"):
            newContact=PhoneBook()
            newContact.getContact()
            phoneBooks[newContact.getFullName()]=newContact
            savePhoneBooks(phoneBooks)
        elif(choice=="2"):
            fullName =input("Enter the contact name and surname to remove: ")
            isDeleted=False
            if fullName in phoneBooks:
                del phoneBooks[fullName]
                print("\nThe contact has been deleted.\n")
                savePhoneBooks(phoneBooks)
            else:
                print("\nThe contact does not exist.\n")
                    
        elif(choice=="3"):
            print("")
            for key, value in phoneBooks.items():
                value.displayContact()
                
        elif(choice=="4"):
            pass
        else:
            print("\nSelect a correct menu item.\n")


def savePhoneBooks(phoneBooks):
    FILE_NAME='phoneBooks.json'
    with open(FILE_NAME, "w") as outfile:
        for key, value in phoneBooks.items():
            json.dump(value.__dict__, outfile)
            


               
        
    
main()



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS