Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

ALL IN PYTHON 1 Personal Contact Book Try/except will be nice, to process bad inputs gracefully, but not mandatory. The project is to make a

ALL IN PYTHON

1

Personal Contact Book

Try/except will be nice, to process bad inputs gracefully, but not mandatory.

The project is to make a PersonalContactBook class. A PersonalContactBook object must have an owner.

1. The owner is a string (= name). 2. You can also have an account number or anything else. 3. The owner must be set in the constructor, i.e. when an object is instantiated.

A PersonalContactBook object must have a list of contacts. A contact consists of a last name, first name, email address and phone number.

  1. You can declare a ContactItem class with the above data, else just store a contact as a list with the above data, its up to you.

  2. First name and last name are strings. Use strip() to remove all leading and trailing whitespace. They should be stored as capitalized strings.

  3. Phone number can be home and work numbers. If you do this, then home is mandatory and work is optional (= blank). Implement the phone number as a string xxx-yyy-zzzz (US format). No international codes.

  4. The email address is also a string, and must contain the @ character and must end with one of the following (.com, .edu, .gov, .net)

The list is initially empty, when a PersonalContactBook object is instantiated. The PersonalContactBook class should support the following functionality for the contacts.

  1. Add a new contact.

  2. Update info for an existing contact.

  3. Search/find if a (last name, first name) is in the list, if yes then print the contact info, else print a message to say not found.

  4. Remove/delete a contact. In this context, a contact is identified by (last name, first name).

  5. Sort the contact (i) by name (alphabetic by last name, then first name), (ii) by email address, (iii) by phone number. If you implement home and work phone numbers, sort by home number, then work number.

  6. You can also implement the ability to sort in reverse order if you wish.

  7. Print the contact book to the console. Write a display(self function to do this.

    Format the display nicely.

Think of other features you wish to implement, but dont go overboard.

I will write a main program to instantiate at least two PersonalComntactBook objects. Then try out the functions using each object.

3

Comments: Personal Contact Book

1. It might be helpful to declare a ContactItem class with the data: last name, first name, email address, phone number.

(a) The PersonalContactBook can contain a list of ContactItem objects.

(b) To test if two ContactItem objects are equal (or a match), we only compare the first and last names. To do that, implement the eq comparison operator. Perform a case-insensitive comparison.

def __eq__(self, other): if self.lastName.lower() == other.lastName.lower() and \

self.firstName.lower() == other.firstName.lower():

 return True else: 

return False

5

2. For the PersonalContactBook, I am not asking for a menu. I had in mind to create objects then call functions using them. The main program would have the following symbolic form:

 # create multiple objects pcb1 = PersonalContactBook( owner1 info ) pcb2 = PersonalContactBook( owner2 info ) etc 
 # invoke functions, see results # FUNCTIONS CAN BE CALLED IN ANY ORDER NOT JUST THE ORDER BELOW 

pcb1.addContact( ... ) pcb1.findContact( .. ) pcb1.updateContact( .. ) pcb1.removeContact( .. ) pcb1.displayContacts( .. )

pcb1.sortContactsByName( ... ) pcb1.sortContactsByEmail( ... ) etc

similarly for pcb2 
# invoke this multiple times 
# call this after adding/updating/deleting contacts, # sorting, etc 

6

3. How to validate an email address? (a) What is the shortest acceptable email string?

  1. The string must contain "@" and end with one of the following (.com, .edu, .gov, .net)

  2. How about this: @.com No good.

  3. There must be a name and a host, at least a@b.com, i.e. minimum seven characters.

  4. Lesson #1: reject if len(email) < 7.

  5. Hence uppercase endings such as .COM, .EDU, .GOV, .NET are acceptable.

  6. Lesson #2: convert the email string to lower case before validating it.

  7. After that, reject if the string does not end with one of the following (.com, .edu,

    .gov, .net)

(c) What about the "@" character? The email string must contain one "@" character.

  1. Lesson #3: the first character must not be "@"

  2. Lesson #4: there must be EXACTLY ONE "@" character in the email string.

  3. Lesson #5: the host cannot be empty: reject the email string if

    email[-5] == "@" and email[-4] == "."

4. How to update a contact?

  1. (a) First of all, the contact must already exist, so searching for (lastName, firstName) must yield a match, else nothing to do.

  2. (b) But suppose we find a match, how to update its information?

  3. (c) We could prompt the user which field do you wish to update, and what is its new value? but this could be tedious and/or complicated to implement.

  4. (d) Here is a possible alternative idea.

    1. WriteamethodupdateContact(self, lastName, firstName, newFirstName, newLastName, newEmail, newPhone).

    2. Use lastName, firstName to find a contact item, if not found then do nothing.

    3. The other update fields are all strings.

    4. The idea is that if an update string is empty, dont update that field.

    5. Update only the fields of the nonempty update strings.

    6. Warning: use strip() to remove leading and trailing whitespace, so that if a

      user enters all blanks. e.g. " " then strip() will reduce it to an empty string.

    7. Suppose pcb is a PersonalContactBook object.

      Example #1:

      There is a contact Smith, Alice and we wish to update the email address pcb.updateContact("Smith", "Alice", "", "", newEmail, "")

      Example #2:

      There is a contact Jones, Bob and we wish to update the first name to Robert and also the phone number. pcb.updateContact("Jones", "Bob", "", "Robert", "", newPhone)

    8. Warning: before changing the last name and/or first name, check that the updated name is not in the contact list, else we will have a duplicate, which is not allowed.

    9. Hence in the example #2, we must check if Jones, Robert already exists in the contact list, and reject the update if it does.

(e) This is not so different from what we do in an iPhone, for example.

  1. We first find the contact we wish to update.

  2. We type in the values of the fields we wish to update, then click save or a similar button.

  3. We can update multiple fields at the same time (= one function call).

  4. We dont change the values of the fields we dont wish to update.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions