-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact Book.txt
115 lines (94 loc) · 4 KB
/
Contact Book.txt
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
Python:Contact Book
class ContactBook:
def __init__(self):
self.contacts = []
def add_contact(self, name, phone_number, email, address):
contact = {
'name': name,
'phone_number': phone_number,
'email': email,
'address': address
}
self.contacts.append(contact)
print(f"Contact '{name}' added successfully!\n")
def view_contact_list(self):
if not self.contacts:
print("Contact book is empty.\n")
else:
print("Contact List:")
for i, contact in enumerate(self.contacts, start=1):
print(f"{i}. {contact['name']} - {contact['phone_number']}")
print()
def search_contact(self, search_query):
results = []
for contact in self.contacts:
if (
search_query.lower() in contact['name'].lower() or
search_query in contact['phone_number']
):
results.append(contact)
return results
def update_contact(self, contact_index, new_phone_number, new_email, new_address):
contact = self.contacts[contact_index]
contact['phone_number'] = new_phone_number
contact['email'] = new_email
contact['address'] = new_address
print("Contact updated successfully!\n")
def delete_contact(self, contact_index):
deleted_contact = self.contacts.pop(contact_index)
print(f"Contact '{deleted_contact['name']}' deleted successfully!\n")
def save_to_file(self, filename='contacts.json'):
with open(filename, 'w') as file:
json.dump(self.contacts, file)
def load_from_file(self, filename='contacts.json'):
if json.load(open(filename, 'r')):
self.contacts = json.load(open(filename, 'r'))
def main():
contact_book = ContactBook()
while True:
print("CONTACT BOOK")
print("1. Add Contact")
print("2. View Contact List")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Save and Exit")
choice = input("Enter your choice (1-6): ")
if choice == '1':
name = input("Enter contact name: ")
phone_number = input("Enter phone number: ")
email = input("Enter email address: ")
address = input("Enter address: ")
contact_book.add_contact(name, phone_number, email, address)
elif choice == '2':
contact_book.view_contact_list()
elif choice == '3':
search_query = input("Enter contact name or phone number to search: ")
results = contact_book.search_contact(search_query)
if results:
print("Search Results:")
for contact in results:
print(f"{contact['name']} - {contact['phone_number']}")
print()
else:
print("No matching contacts found.\n")
elif choice == '4':
contact_book.view_contact_list()
contact_index = int(input("Enter the number of the contact to update: ")) - 1
new_phone_number = input("Enter new phone number: ")
new_email = input("Enter new email address: ")
new_address = input("Enter new address: ")
contact_book.update_contact(contact_index, new_phone_number, new_email, new_address)
elif choice == '5':
contact_book.view_contact_list()
contact_index = int(input("Enter the number of the contact to delete: ")) - 1
contact_book.delete_contact(contact_index)
elif choice == '6':
contact_book.save_to_file()
print("Contact book saved. Exiting...")
break
else:
print("Invalid choice. Please enter a number between 1 and 6.\n")
if __name__ == "__main__":
main()