DEV Community

Param Mehrotra
Param Mehrotra

Posted on

Create Contact Book

Hi guys. From my second project, I created a contact book which helps you save details about people you know. For this project, I limited myself to 4 details of the contact: Name, Phone, Email ID, Address.

So, for the contact book. I needed to store my details somewhere, so this data can be easily retrievable. So, just like my previous project, I stored this data of contacts into a csv file Contact_Book.csv.

Now, that I had the contact book set up, I needed to create three functions for the user to interact with the contact book. create_new_contact, update_exisiting_contact, find_contact.

In the function create_new_contact,
I took in four values: Name, Phone, Email ID, Address and wrote it into the csv file.

In the function retrieving_contact ,I took in two variables finder_keys and finder_values each of it is a set. The finder_keys contains the type of data i have. i.e. name,phone,etc. The finder_values contains the data i have in the same order the keys are in in finder_keys.I then iterated through each contact in the contact book and converted each string into an array.I then saw if each datum in the contact is the same as the datum i want for a particular type of data. If so, then i append this contact into a set containing all the contacts I want and in the ens it will return the contact.

In the function updating_existing_contact, i took in 4 sets of values,
1) finder_keys: which contains the types of data i have to identify the contacts i want to update
2) finder_values: which contains the data i have to identify the contacts i want to update
3) tbc_keys: which contain the type of data i want to change for each contact
4) new_values: which contain the new values i want to put into the contact

I then wrote all the contacts I am not updating and the updated contacts into a new csv file and renamed that file to the original one.

In the function find_contact, i took in two sets of values, finder_keys and finder_values. Then i iterated through the csv file. And found if the value in the line and the position of each finder_key is the same as the corrospoding finder_value. If all the finder_values match with the contact. I append the contact into a set found_contact and return the set at the end.

I then created a user_interface to allow the contact book user to easily interact with the book.

Finally, i unittested each of the functions using custom impits and custom outputs to ensure all were working well.

Top comments (0)