小型通讯录管理程序设计的类的设计
时间: 2024-01-22 16:18:14 浏览: 62
通讯录程序设计
4星 · 用户满意度95%
一个小型通讯录管理程序可以由以下几个类构成:
1. Contact 类:表示一个联系人,包含联系人的姓名、电话号码、电子邮件地址等属性,以及操作这些属性的方法。
2. AddressBook 类:表示整个通讯录,包含多个 Contact 对象,以及操作这些对象的方法,如添加、删除、修改、查找等。
3. FileIO 类:表示文件读写操作,负责将通讯录保存到文件或从文件中读取通讯录。
4. ConsoleUI 类:表示控制台界面,负责与用户交互,接收用户输入并显示输出。
其中,Contact 类可以定义如下:
```python
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
def __str__(self):
return f"{self.name}\t{self.phone}\t{self.email}"
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_phone(self):
return self.phone
def set_phone(self, phone):
self.phone = phone
def get_email(self):
return self.email
def set_email(self, email):
self.email = email
```
AddressBook 类可以定义如下:
```python
class AddressBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
def remove_contact(self, contact):
self.contacts.remove(contact)
def find_contact_by_name(self, name):
for contact in self.contacts:
if contact.get_name() == name:
return contact
return None
def find_contact_by_phone(self, phone):
for contact in self.contacts:
if contact.get_phone() == phone:
return contact
return None
def find_contact_by_email(self, email):
for contact in self.contacts:
if contact.get_email() == email:
return contact
return None
def __str__(self):
result = ""
for contact in self.contacts:
result += str(contact) + "\n"
return result
```
FileIO 类可以定义如下:
```python
class FileIO:
@staticmethod
def load_address_book(filename):
address_book = AddressBook()
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
parts = line.strip().split("\t")
name = parts[0]
phone = parts[1]
email = parts[2]
contact = Contact(name, phone, email)
address_book.add_contact(contact)
return address_book
@staticmethod
def save_address_book(address_book, filename):
with open(filename, "w") as f:
for contact in address_book.contacts:
f.write(str(contact) + "\n")
```
ConsoleUI 类可以定义如下:
```python
class ConsoleUI:
def __init__(self):
self.address_book = None
def print_menu(self):
print("请选择操作:")
print("1. 新建联系人")
print("2. 删除联系人")
print("3. 修改联系人")
print("4. 查找联系人")
print("5. 显示所有联系人")
print("6. 保存并退出")
def run(self):
self.address_book = FileIO.load_address_book("address_book.txt")
while True:
self.print_menu()
choice = input("请选择操作:")
if choice == "1":
name = input("请输入姓名:")
phone = input("请输入电话号码:")
email = input("请输入电子邮件地址:")
contact = Contact(name, phone, email)
self.address_book.add_contact(contact)
elif choice == "2":
name = input("请输入姓名:")
contact = self.address_book.find_contact_by_name(name)
if contact:
self.address_book.remove_contact(contact)
else:
print("联系人不存在!")
elif choice == "3":
name = input("请输入姓名:")
contact = self.address_book.find_contact_by_name(name)
if contact:
phone = input("请输入电话号码:")
email = input("请输入电子邮件地址:")
contact.set_phone(phone)
contact.set_email(email)
else:
print("联系人不存在!")
elif choice == "4":
keyword = input("请输入关键词:")
found = False
for contact in self.address_book.contacts:
if keyword in str(contact):
print(contact)
found = True
if not found:
print("联系人不存在!")
elif choice == "5":
print(self.address_book)
elif choice == "6":
FileIO.save_address_book(self.address_book, "address_book.txt")
break
else:
print("无效操作!")
```
以上就是一个简单的通讯录管理程序的类设计。可以根据需要进行修改和扩展。
阅读全文