Answer to Question #297651 in Python for Chinna

Question #297651

Develop a custom class called Contact(Name, Dob, Phonenumber) which has methods such as Read() and Display() with the following criterions. Read Name (Text: maximum length 50), Dob(Date: after 01/01/2000) and



Phone numbers(Integer) from user and some of these accepting only a specific format with the four-digit area and six-digit local codes accepted as ten digits, or separated into blocks using hyphens or spaces, and with the area code optionally enclosed in parentheses.



For example, all of these are valid: 0426-123-456, (0432) 123456 and 0432



123456.



Read the phone numbers from console and for each one echo the number in the form "(999) 999 9999" or report an error for any that are invalid, or that don't have exactly ten digits.



Show how the object creations and manipulations are done for 'N' valid



Contacts.



Finally store the contacts in a separate .csv file

1
Expert's answer
2022-02-14T13:25:01-0500
import csv

class Contact:
    def __init__(self, name, dob, phoneNumber):
        self.name = name
        self.dob = dob
        self.phoneNumber = phoneNumber

    def read(self):
        num = self.phoneNumber
        if num.isdigit() and len(num) == 10:
            return f'({phoneNumber[4:7]}) {phoneNumber[7:10]} {phoneNumber[0:4]}'
        elif '-' in num:
            number = num.split('-')
            count = ''
            for c in (number):
                count += str(len(c))
            if count == '433':
                return f'({number[1]}) {number[2]} {number[0]}'
        elif '(' in num and ')' in number:
            number = num.split(' ')
            count = ''
            for c in (number):
                count += str(len(c))
            if count == '46':
                return  f'({number[1][0:3]}) {number[1][3:6]} {number[0]}'
        else:
            return 'Invalid phone number '

    def display(self):
        return f"{self.name} {self.dob} {self.phoneNumber}"

n = 2;

contacts = []

for i in range(n):
    print('Enter name: ')
    name = input()
    print('Enter Date of Birth')
    dob = input()
    print('Enter phone number')
    phoneNumber = input()

    contact = Contact(name, dob, phoneNumber)
    contacts.append(contact)

    print(contact.read())

with open('contacts.csv', mode='w') as csv_file:
    fieldnames = ['name', 'dob', 'phoneNumber']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    for contact in contacts:
        writer.writerow({'name': contact.name, 'dob': contact.dob, 'phoneNumber': contact.phoneNumber})

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