Answer to Question #189237 in Python for Rania

Question #189237

Write a Python program using text files to create a Smart Phone book that collects contact details from the user. Contact details refer to the contact’s full name (first name and last name), phone number, date of birth and a category that the contact belongs to (Friends, Family, Work, Other). The Phone Book will display the following Menu and will support the corresponding functionality:

Enter your choice: 1: Add a new contact 2: Remove an existing contact 3: Look up a contact 4: Update a contact phone number 5: Display all contacts 6: Delete all contacts 7: Exit phonebook



1
Expert's answer
2021-05-07T06:06:00-0400
import pathlib
class Contact:
    def __init__(self):
        pass
    
    def __init__(self,fullName,phoneNumber,dateBirth,category):
        self.fullName = fullName
        self.phoneNumber = phoneNumber
        self.dateBirth = dateBirth
        self.category = category


    def getContact(self):
        self.fullName =input("Enter full name: ")
        self.phoneNumber =input("Enter phone number: ")
        self.dateBirth =input("Enter date of birth: ")
        self.category =input("Enter category: ")


    def displayContact(self):
        print(f"Full name: {self.fullName}")
        print(f"Phone number: {self.phoneNumber}")
        print(f"Date of birth: {self.dateBirth}")
        print(f"Category: {self.category}\n\n")


    def getPhoneNumber(self):
        return self.phoneNumber;
    def setPhoneNumber(self,phoneNumber):
        self.phoneNumber=phoneNumber;
    def toString(self):
        return self.fullName+"|"+self.phoneNumber+"|"+self.dateBirth+"|"+self.category;


def main():
    ch=""
    contacts=[]
    readContacts(contacts)
    while ch!="7":
        print("1: Add a new contact")
        print("2: Remove an existing contact")
        print("3: Look up a contact")
        print("4: Update a contact phone number")
        print("5: Display all contacts")
        print("6: Delete all contacts")
        print("7: Exit phonebook")
        ch=input("Your choice: ")
        if(ch=="1"):
            newContact=Contact("","","","")
            newContact.getContact()
            contacts.append(newContact)
            save(contacts)
        elif(ch=="2"):
            phoneNumber =input("Enter phone number to remove: ")
            isDeleted=False
            for c in contacts:
                if(c.getPhoneNumber()==phoneNumber):
                    contacts.remove(c)
                    isDeleted=True
                    break
            if isDeleted:
                print("\nThe contact has been deleted.\n")
                save(contacts)
            else:
                print("\nThe contact does not exist.\n")
                    
        elif(ch=="3"):
            print("")
            phoneNumber =input("Enter phone number to look up: ")
            isExist=False
            for c in contacts:
                if(c.getPhoneNumber()==phoneNumber):
                    c.displayContact()
                    isExist=True
                    break
            if isExist==False:
                print("\nThe contact does not exist.\n")
                
        elif(ch=="4"):
            phoneNumber =input("Enter phone number to update: ")
            index=-1
            counter=0
            for c in contacts:
                if(c.getPhoneNumber()==phoneNumber):
                    index=counter
                counter+=1
            if index!=-1:
                phoneNumber =input("Enter a new phone number: ")
                contacts[index].setPhoneNumber(phoneNumber)
                print("\nThe contact has been updated.\n")
                save(contacts)
            else:
                print("\nThe contact does not exist.\n")
        elif(ch=="5"):
            print("")
            for c in contacts:
                c.displayContact()
        elif(ch=="6"):
            contacts.clear()
            print("\nAll contacts have been deleted\n\n")
            save(contacts)
        elif(ch=="7"):
            ch="7"
        else:
            print("\nSelect a correct menu item.\n")


def save(contacts):
    f = open('contacts.txt', 'w')
    for c in contacts:
        f.write(f"{c.toString()}\n")
    
    f.close()  


def readContacts(contacts):
    FILE_NAME='contacts.txt'
    file = pathlib.Path(FILE_NAME)
    if file.exists():
        with open(FILE_NAME, 'r') as f:
            for line in f:
                values = line.split('|')
                contacts.append(Contact(values[0],values[1],values[2],values[3]))
    
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