基于python的通讯录管理系统 要求1.提供增加、删除,修改通讯录的功能。2.提供单个查询,查询所有通讯录的功能。3.能够保存通讯录信息到本地文件,退出等功能。4.实现按照姓名首字母排序,或者按照特殊要求(如学号)排序。使用命令行界面
时间: 2024-02-03 20:14:13 浏览: 63
好的,下面是一个基于 Python 的通讯录管理系统的示例代码,它满足了你的所有要求:
```python
import json
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
class AddressBook:
def __init__(self):
self.contacts = []
def add_contact(self, name, phone, email):
contact = Contact(name, phone, email)
self.contacts.append(contact)
def delete_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
def modify_contact(self, name, new_phone, new_email):
for contact in self.contacts:
if contact.name == name:
contact.phone = new_phone
contact.email = new_email
def search_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None
def sort_contacts(self, key):
self.contacts.sort(key=lambda contact: getattr(contact, key))
def display_contacts(self):
for contact in self.contacts:
print(f"Name: {contact.name}, Phone: {contact.phone}, Email: {contact.email}")
def save_to_file(self, filename):
with open(filename, "w") as f:
json.dump([vars(contact) for contact in self.contacts], f)
def load_from_file(self, filename):
with open(filename, "r") as f:
data = json.load(f)
for contact_data in data:
contact = Contact(**contact_data)
self.contacts.append(contact)
if __name__ == "__main__":
address_book = AddressBook()
while True:
print("1. Add contact")
print("2. Delete contact")
print("3. Modify contact")
print("4. Search contact")
print("5. Display all contacts")
print("6. Sort contacts")
print("7. Save to file")
print("8. Load from file")
print("9. Exit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email: ")
address_book.add_contact(name, phone, email)
elif choice == "2":
name = input("Enter name: ")
address_book.delete_contact(name)
elif choice == "3":
name = input("Enter name: ")
new_phone = input("Enter new phone number: ")
new_email = input("Enter new email: ")
address_book.modify_contact(name, new_phone, new_email)
elif choice == "4":
name = input("Enter name: ")
contact = address_book.search_contact(name)
if contact:
print(f"Name: {contact.name}, Phone: {contact.phone}, Email: {contact.email}")
else:
print("Contact not found")
elif choice == "5":
address_book.display_contacts()
elif choice == "6":
key = input("Enter key to sort by: ")
address_book.sort_contacts(key)
elif choice == "7":
filename = input("Enter filename: ")
address_book.save_to_file(filename)
elif choice == "8":
filename = input("Enter filename: ")
address_book.load_from_file(filename)
elif choice == "9":
break
else:
print("Invalid choice")
```
在这个示例代码中,我们首先定义了两个类,`Contact` 和 `AddressBook`。`Contact` 类代表一个通讯录条目,包含姓名、电话和电子邮件三个属性。`AddressBook` 类代表整个通讯录,包含了增加、删除、修改、查询、排序、显示、保存和加载通讯录等功能。
在主程序中,我们使用一个 while 循环来不断接收用户的输入,根据用户的选择调用相应的方法。这些方法都是在 `AddressBook` 类中定义的。其中最重要的是 `save_to_file` 和 `load_from_file` 方法,它们可以将通讯录保存到本地文件和从本地文件加载通讯录。
你可以在命令行中运行这个程序,并按照提示逐步操作。例如,输入 1 可以添加一个新的通讯录条目,输入 5 可以显示所有通讯录条目,输入 7 可以将通讯录保存到文件中,输入 8 可以从文件中加载通讯录。你也可以根据需要自己修改这个程序,添加更多的功能或者改进现有的功能。
阅读全文