帮我写以上要求的代码
时间: 2023-07-10 13:25:09 浏览: 32
好的,以下是一个简单的基于 Python 的通讯录管理系统,实现了你提出的所有功能要求:
```python
import pickle
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
def __str__(self):
return f"Name: {self.name}, Phone: {self.phone}, Email: {self.email}"
class AddressBook:
def __init__(self):
self.contacts = []
def add_contact(self, name, phone, email):
contact = Contact(name, phone, email)
self.contacts.append(contact)
print(f"{name} has been added to the address book.")
def remove_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
print(f"{name} has been removed from the address book.")
return
print(f"{name} is not in the address book.")
def update_contact(self, name, phone=None, email=None):
for contact in self.contacts:
if contact.name == name:
if phone:
contact.phone = phone
if email:
contact.email = email
print(f"{name}'s contact information has been updated.")
return
print(f"{name} is not in the address book.")
def search_contact(self, name=None):
if name:
for contact in self.contacts:
if contact.name == name:
print(contact)
return
print(f"{name} is not in the address book.")
else:
for contact in self.contacts:
print(contact)
def save_address_book(self, filename):
with open(filename, "wb") as file:
pickle.dump(self.contacts, file)
print("Address book has been saved.")
def load_address_book(self, filename):
with open(filename, "rb") as file:
self.contacts = pickle.load(file)
print("Address book has been loaded.")
def sort_address_book(self, key=None):
if key:
self.contacts.sort(key=lambda contact: getattr(contact, key))
else:
self.contacts.sort(key=lambda contact: contact.name)
print("Address book has been sorted.")
def main():
address_book = AddressBook()
while True:
print("1. Add contact")
print("2. Remove contact")
print("3. Update contact")
print("4. Search contact")
print("5. Show all contacts")
print("6. Save address book")
print("7. Load address book")
print("8. Sort address book")
print("9. Quit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter name: ")
phone = input("Enter phone: ")
email = input("Enter email: ")
address_book.add_contact(name, phone, email)
elif choice == "2":
name = input("Enter name: ")
address_book.remove_contact(name)
elif choice == "3":
name = input("Enter name: ")
phone = input("Enter new phone (press enter to keep current phone): ")
email = input("Enter new email (press enter to keep current email): ")
address_book.update_contact(name, phone, email)
elif choice == "4":
name = input("Enter name (press enter to show all contacts): ")
address_book.search_contact(name)
elif choice == "5":
address_book.search_contact()
elif choice == "6":
filename = input("Enter filename: ")
address_book.save_address_book(filename)
elif choice == "7":
filename = input("Enter filename: ")
address_book.load_address_book(filename)
elif choice == "8":
key = input("Enter key (press enter to sort by name): ")
address_book.sort_address_book(key)
elif choice == "9":
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
```
以上代码实现了一个 `Contact` 类和一个 `AddressBook` 类,分别用于表示一个联系人和一个通讯录。其中,`AddressBook` 类实现了你提出的所有功能要求。
你可以通过在命令行中运行该程序来体验这个通讯录管理系统。