-
Notifications
You must be signed in to change notification settings - Fork 0
/
CONTACT BOOK
90 lines (80 loc) · 3.56 KB
/
CONTACT BOOK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class ContactBook:
def __init__(self):
self.contacts = {}
def add_contact(self, name, phone, email, address):
if name not in self.contacts:
self.contacts[name] = {'phone': phone, 'email': email, 'address': address}
print(f"Contact '{name}' added successfully.")
else:
print(f"Contact '{name}' already exists. Use update option to modify details.")
def view_contacts(self):
if not self.contacts:
print("Contact book is empty.")
else:
print("Contact List:")
for name, details in self.contacts.items():
print(f"Name: {name}, Phone: {details['phone']}, Email: {details['email']}, Address: {details['address']}")
def search_contact(self, keyword):
matching_contacts = [(name, details) for name, details in self.contacts.items() if keyword.lower() in name.lower() or keyword in details.values()]
if matching_contacts:
print("Matching Contacts:")
for name, details in matching_contacts:
print(f"Name: {name}, Phone: {details['phone']}, Email: {details['email']}, Address: {details['address']}")
else:
print("No matching contacts found.")
def update_contact(self, name, phone=None, email=None, address=None):
if name in self.contacts:
contact = self.contacts[name]
if phone:
contact['phone'] = phone
if email:
contact['email'] = email
if address:
contact['address'] = address
print(f"Contact '{name}' updated successfully.")
else:
print(f"Contact '{name}' not found. Use add option to create a new contact.")
def delete_contact(self, name):
if name in self.contacts:
del self.contacts[name]
print(f"Contact '{name}' deleted successfully.")
else:
print(f"Contact '{name}' not found.")
def contact_book_app():
contact_book = ContactBook()
while True:
print("\nContact Book Menu:")
print("1. Add Contact")
print("2. View Contacts")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Exit")
choice = input("Enter your choice (1-6): ")
if choice == '1':
name = input("Enter contact name: ")
phone = input("Enter phone number: ")
email = input("Enter email address: ")
address = input("Enter address: ")
contact_book.add_contact(name, phone, email, address)
elif choice == '2':
contact_book.view_contacts()
elif choice == '3':
keyword = input("Enter name or phone number to search: ")
contact_book.search_contact(keyword)
elif choice == '4':
name = input("Enter contact name to update: ")
phone = input("Enter new phone number (press enter to keep current): ")
email = input("Enter new email address (press enter to keep current): ")
address = input("Enter new address (press enter to keep current): ")
contact_book.update_contact(name, phone, email, address)
elif choice == '5':
name = input("Enter contact name to delete: ")
contact_book.delete_contact(name)
elif choice == '6':
print("Exiting Contact Book. Goodbye!")
break
else:
print("Invalid choice. Please enter a number from 1 to 6.")
if __name__ == "__main__":
contact_book_app()