@@ -18,10 +18,112 @@ def __str__(self):
18
18
class AddressBook :
19
19
20
20
def __init__ (self ):
21
- self .contacts = {}
21
+ """
22
+ Description :
23
+ Default constructor.
24
+ Resturns : address books dictionary
25
+ """
26
+ self .address_books = {}
27
+
28
+ def add_address_books (self ,address_book_name ):
29
+ """
30
+ Description :
31
+ This function creates a address book if it is already not present.
32
+ Parameters :
33
+ address_boo_name : address book name input from the user.
34
+ Returns :
35
+ none
36
+ prints message
37
+ """
38
+ if address_book_name in self .address_books :
39
+ print (f"Address Book with name '{ address_book_name } ' already exists." )
40
+ else :
41
+ self .address_books [address_book_name ] = {}
42
+ print (f"Address Book with name '{ address_book_name } ' is successfully created" )
43
+
44
+
45
+ def fetch_address_book (self ,address_book_name ):
46
+ """
47
+ Description :
48
+ This function feches address books from the address books dictionary.
49
+ Parameters :
50
+ address_bbok_name : name of the address book to fetch.
51
+ Returns :
52
+ address_book_value : contacts from address book
53
+ """
54
+ for address_book_key , address_book_value in self .address_books .items ():
55
+ if address_book_key == address_book_name :
56
+ return address_book_value
57
+ return None
58
+
59
+
60
+ def remove_address_book (self ,address_book_name ):
61
+ address_book = self .fetch_address_book (address_book_name )
62
+
63
+ if address_book :
64
+ self .address_books .pop (address_book_name )
65
+ print ("Removed Address Book successfully" )
66
+ else :
67
+ print (f"Address Book with name { address_book_name } not found" )
68
+
69
+
70
+ def update_address_book (self ,address_book_name ):
71
+ if address_book_name not in self .address_books :
72
+ print (f"Address Book with name '{ address_book_name } ' does not exists." )
73
+ else :
74
+ while True :
75
+ print ()
76
+ print ("--Contacts--" )
77
+ print ("-----------------------" )
78
+ print ("1.Add contact" )
79
+ print ("2.Update Contact" )
80
+ print ("3.Remove contact" )
81
+ print ("4.Print Contact" )
82
+ print ("5.Exit" )
83
+ print ("-----------------------" )
84
+ choice = int (input ("Enter choice: " ))
85
+ if choice == 1 :
86
+ self .add_contact (address_book_name )
87
+
88
+ elif choice == 2 :
89
+ first_name = input ("Enter first name to update: " )
90
+ self .update_contact (address_book_name ,first_name )
91
+
92
+ elif choice == 3 :
93
+ first_name = input ("Enter first name to remove: " )
94
+ self .remove_contact (address_book_name ,first_name )
95
+
96
+ elif choice == 4 :
97
+ self .display_contacts (address_book_name )
98
+
99
+ elif choice == 5 :
100
+ return False
101
+ else :
102
+ print ("[error] Selected option is wrong" )
103
+
104
+ # display address Book
105
+ def display_address_books (self ):
106
+ """
107
+ Description :
108
+ This function prints the contents from the address_books dictionary.
109
+ Parameters :
110
+ self : address_books dictionary which is declared in default constructor.
111
+ Returns :
112
+ none
113
+ prints address book name , contact from address book
114
+ """
115
+ for address_book_name , address_book in self .address_books .items ():
116
+ print ()
117
+ print (f"| AddressBook:{ address_book_name } |" )
118
+ print ("|------------------------------------------------------------------------------------------------------------------------------|" )
119
+ contact_number = 1
120
+ for contact_name , contact in address_book .items ():
121
+ print (f"| Contact { contact_number } : { contact_name } : { contact } |" )
122
+ contact_number += 1
123
+
22
124
23
125
# add contacts
24
- def add_contact (self ):
126
+ def add_contact (self , address_book_name ):
25
127
"""
26
128
Description :
27
129
This function passes the inputs from the user to conacts constructor and then appends the object of class contact to contacts list.
@@ -41,26 +143,27 @@ def add_contact(self):
41
143
phone_number = int (input ("Enter phone number: " ))
42
144
email_id = input ("Enter email id: " )
43
145
contact = Contact (first_name ,second_name ,address ,city ,state ,zip ,phone_number ,email_id )
44
- self .contacts [ first_name ]= contact
146
+ self .address_books [ address_book_name ][ first_name ] = contact
45
147
print ("Contact Added Succesfully" )
46
148
47
149
48
- def fetch_contact (self , first_name ):
150
+ def fetch_contact (self , address_book_name , first_name ):
49
151
"""
50
- Description:
51
- This function fetches a contact from the contacts list using the first name.
52
- Parameters:
53
- first_name: First name of the contact to retrieve.
54
- Returns:
55
- contact: The contact from the contacts list.
152
+ Descrption :
153
+ This function searches a contact from address book it it exist or not.
154
+ Parameters :
155
+ self : default constructor
156
+ address_book_name : name of the address_book for fetching
157
+ first_name : first name from the address book to fetch.
158
+
56
159
"""
57
- for contact_key , contact_value in self .contacts . items ():
58
- if contact_key == first_name :
59
- return contact_value
160
+ address_book = self .address_books . get ( address_book_name )
161
+ if address_book :
162
+ return address_book . get ( first_name )
60
163
return None
61
164
62
165
# update address Book
63
- def update_contact (self ,first_name ):
166
+ def update_contact (self ,address_book_name , first_name ):
64
167
"""
65
168
Description :
66
169
This function checks if the first name in present in contact
@@ -71,63 +174,65 @@ def update_contact(self,first_name):
71
174
none
72
175
prints update message.
73
176
"""
74
- contact = self .fetch_contact (first_name )
177
+ contact = self .fetch_contact (address_book_name , first_name )
75
178
if contact :
76
179
print ("Please enter contact details for update" )
77
- first_name = input ("Enter first name: " )
180
+ new_first_name = input ("Enter first name: " )
78
181
second_name = input ("Enter second name: " )
79
182
address = input ("Enter address: " )
80
183
city = input ("Enter city: " )
81
184
state = input ("Enter state: " )
82
185
zip = int (input ("Enter zip: " ))
83
186
phone_number = int (input ("Enter phone number: " ))
84
187
email_id = input ("Enter email id: " )
85
- new_contact = Contact (first_name ,second_name ,address ,city ,state ,zip ,phone_number ,email_id )
86
- self .contacts [first_name ] = new_contact
188
+ new_contact = Contact (new_first_name ,second_name ,address ,city ,state ,zip ,phone_number ,email_id )
189
+ self .address_books [address_book_name ][new_first_name ] = new_contact
190
+
191
+ if first_name != new_first_name :
192
+ del self .address_books [address_book_name ][first_name ]
193
+
87
194
print ("Contact updated successfully" )
88
195
else :
89
196
input ("Contact not found. Hit enter to continue" )
90
197
91
198
92
199
# remove contact from address Book
93
- def remove_contact (self ,first_name ):
200
+ def remove_contact (self ,address_book_name , first_name ):
94
201
"""
95
202
Description :
96
203
This function checks if the first name in present in contact
97
204
if it is present, take the inputs from user again ,find the index number and update the contact.
98
205
Parameters :
99
- self : self.contacts list.
206
+ self : self.addressbooks.
207
+ address_book_name : get name of addressbook
208
+ first_name : first name of the contact to remove.
100
209
Returns :
101
210
none
102
211
prints update message.
103
212
"""
104
- contact = self .fetch_contact (first_name )
213
+ contact = self .fetch_contact (address_book_name , first_name )
105
214
if contact :
106
- del self .contacts [first_name ]
215
+ del self .address_books [ address_book_name ] [first_name ]
107
216
print ("Contact removed successfully" )
108
217
else :
109
- input ("Contact not found. Hit enter to continue " )
218
+ print ("Contact not found" )
110
219
111
220
112
221
# display contacts from address Book
113
- def display_contact (self ):
114
- """
115
- Description :
116
- This function prints the object of class contact.
117
- Parameters :
118
- self : contacts dict containing multiple contacts
119
- Returns:
120
- none
121
- prints key value from contact object
122
- """
123
- # iterate over contacts dict to print each contact
124
- print ("|-------------------------------------------------------------------------------------------------|" )
125
- for key ,values in self .contacts .items ():
126
- print (f"{ key } \n [{ values } ]" )
222
+ def display_contacts (self , address_book_name ):
223
+ address_book = self .address_books .get (address_book_name )
224
+ if address_book :
225
+ print ("|-----------------------------------------------------------------|" )
226
+ print (f"| Contacts in '{ address_book_name } ' |" )
227
+ print ("|-----------------------------------------------------------------|" )
228
+ for contact in address_book .values ():
229
+ print (contact )
127
230
print ()
128
- print ("|-------------------------------------------------------------------------------------------------|" )
231
+ print ("|-----------------------------------------------------------------|" )
232
+ else :
233
+ print (f"contacts not found in '{ address_book_name } '" )
129
234
130
- input ("Contacts displayed, Hit Enter to continue!" )
235
+ input ("Contacts displayed. Hit Enter to continue!" )
131
236
132
237
133
238
0 commit comments