-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactbook.py
83 lines (68 loc) · 2.75 KB
/
contactbook.py
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
class Contact:
def __init__(self, name, phone, email, address):
self.name = name
self.phone = phone
self.email = email
self.address = address
class ContactBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
def view_contacts(self):
for contact in self.contacts:
print(f"Name: {contact.name}, Phone: {contact.phone}")
def search_contact(self, search_term):
results = []
for contact in self.contacts:
if search_term.lower() in contact.name.lower() or search_term in contact.phone:
results.append(contact)
return results
def update_contact(self, old_name, new_contact):
for i, contact in enumerate(self.contacts):
if contact.name == old_name:
self.contacts[i] = new_contact
def delete_contact(self, name):
for i, contact in enumerate(self.contacts):
if contact.name == name:
del self.contacts[i]
def main():
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: ")
if choice == '1':
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email: ")
address = input("Enter address: ")
new_contact = Contact(name, phone, email, address)
contact_book.add_contact(new_contact)
elif choice == '2':
contact_book.view_contacts()
elif choice == '3':
search_term = input("Enter name or phone number to search: ")
results = contact_book.search_contact(search_term)
for contact in results:
print(f"Name: {contact.name}, Phone: {contact.phone}")
elif choice == '4':
old_name = input("Enter the name of the contact to update: ")
name = input("Enter new name: ")
phone = input("Enter new phone number: ")
email = input("Enter new email: ")
address = input("Enter new address: ")
new_contact = Contact(name, phone, email, address)
contact_book.update_contact(old_name, new_contact)
elif choice == '5':
name = input("Enter the name of the contact to delete: ")
contact_book.delete_contact(name)
elif choice == '6':
break
if __name__ == '__main__':
main()