-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonebook.py
More file actions
93 lines (82 loc) · 3.52 KB
/
phonebook.py
File metadata and controls
93 lines (82 loc) · 3.52 KB
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
91
92
93
class Contact:
def __init__(self, name, phone_number, email, address):
self.name = name
self.phone_number = phone_number
self.email = email
self.address = address
class ContactBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
print(f"Contact {contact.name} added successfully.")
def view_contacts(self):
if not self.contacts:
print("No contacts found.")
else:
print("\nContact List:")
for contact in self.contacts:
print(f"{contact.name} - {contact.phone_number}")
def search_contact(self, keyword):
results = [contact for contact in self.contacts if keyword.lower() in contact.name.lower() or keyword in contact.phone_number]
if not results:
print("No matching contacts found.")
else:
print("\nSearch Results:")
for contact in results:
print(f"{contact.name} - {contact.phone_number}")
def update_contact(self, name, new_phone_number, new_email, new_address):
for contact in self.contacts:
if contact.name == name:
contact.phone_number = new_phone_number
contact.email = new_email
contact.address = new_address
print(f"Contact {name} updated successfully.")
return
print(f"Contact {name} not found.")
def delete_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
print(f"Contact {name} deleted successfully.")
return
print(f"Contact {name} not found.")
def main():
contact_book = ContactBook()
while True:
print("\nContact Management System")
print("1. Add Contact")
print("2. View Contacts")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Quit")
choice = input("Enter your choice (1-6): ")
if choice == "1":
name = input("Enter the contact's name: ")
phone_number = input("Enter the contact's phone number: ")
email = input("Enter the contact's email: ")
address = input("Enter the contact's address: ")
new_contact = Contact(name, phone_number, email, address)
contact_book.add_contact(new_contact)
elif choice == "2":
contact_book.view_contacts()
elif choice == "3":
keyword = input("Enter the name or phone number to search: ")
contact_book.search_contact(keyword)
elif choice == "4":
name = input("Enter the name of the contact to update: ")
new_phone_number = input("Enter the new phone number: ")
new_email = input("Enter the new email: ")
new_address = input("Enter the new address: ")
contact_book.update_contact(name, new_phone_number, new_email, new_address)
elif choice == "5":
name = input("Enter the name of the contact to delete: ")
contact_book.delete_contact(name)
elif choice == "6":
print("Exiting the Contact Management System. Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 6.")
if __name__ == "__main__":
main()