Question
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.
-
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.
-
First name and last name are strings. Use strip() to remove all leading and trailing whitespace. They should be stored as capitalized strings.
-
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.
-
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.
-
Add a new contact.
-
Update info for an existing contact.
-
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.
-
Remove/delete a contact. In this context, a contact is identified by (last name, first name).
-
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.
-
You can also implement the ability to sort in reverse order if you wish.
-
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?
-
The string must contain "@" and end with one of the following (.com, .edu, .gov, .net)
-
How about this: @.com No good.
-
There must be a name and a host, at least a@b.com, i.e. minimum seven characters.
-
Lesson #1: reject if len(email) < 7.
-
Hence uppercase endings such as .COM, .EDU, .GOV, .NET are acceptable.
-
Lesson #2: convert the email string to lower case before validating it.
-
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.
-
Lesson #3: the first character must not be "@"
-
Lesson #4: there must be EXACTLY ONE "@" character in the email string.
-
Lesson #5: the host cannot be empty: reject the email string if
email[-5] == "@" and email[-4] == "."
4. How to update a contact?
-
(a) First of all, the contact must already exist, so searching for (lastName, firstName) must yield a match, else nothing to do.
-
(b) But suppose we find a match, how to update its information?
-
(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.
-
(d) Here is a possible alternative idea.
-
WriteamethodupdateContact(self, lastName, firstName, newFirstName, newLastName, newEmail, newPhone).
-
Use lastName, firstName to find a contact item, if not found then do nothing.
-
The other update fields are all strings.
-
The idea is that if an update string is empty, dont update that field.
-
Update only the fields of the nonempty update strings.
-
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.
-
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)
-
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.
-
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.
-
We first find the contact we wish to update.
-
We type in the values of the fields we wish to update, then click save or a similar button.
-
We can update multiple fields at the same time (= one function call).
-
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started