Python 基于列表实现的通讯录管理系统(有完整源码)_python通讯录管理系统
目录
通讯录管理系统
PersonInformation类
ContactList类
main函数
程序的运行流程
完整代码
运行示例
通讯录管理系统
这是一个基于文本的界面程序,用户可以通过命令行与之交互,它使用了CSV文件来存储和读取联系人信息,这使得数据可以持久化保存。此外,程序还提供了一些基本的输入验证,以确保输入的数据是有效的。
它包含了两个类PersonInformation
类用于创建和管理个人信息对象,而ContactList
类则用于管理这些对象的集合。
PersonInformation
类
这个类用于存储一个人的基本信息,包括姓名、性别、年龄、电话和住址。它有一个__str__
方法,用于定义当对象被转换为字符串时的表现形式。
class PersonInformation: def __init__(self, name, gender, age, telephone, address): self.name = name self.gender = gender self.age = age self.telephone = telephone self.address = address def __str__(self): return f\"名字: {self.name}, 性别: {self.gender}, 年龄: {self.age}, 电话: {self.telephone}, 住址: {self.address}\"
ContactList
类
这个类用于管理一个联系人列表。它提供了以下功能:
- 添加联系人(
add_contact
) - 删除联系人(
remove_contact
) - 查找联系人(
find_contact
) - 修改联系人信息(
modify_contact
) - 显示所有联系人(
show_contacts
) - 保存联系人到CSV文件(
save_contacts
) - 从CSV文件加载联系人(
load_contacts
)
class ContactList: def __init__(self): self.arr = [] def add_contact(self, person): self.arr.append(person) self.save_contacts() def is_empty(self): if not self.arr: print(\"当前联系人列表为空!\") def remove_contact(self, name): person = self.find_contact(name) if person: self.arr.remove(person) self.save_contacts() print(\"删除成功!\") self.is_empty() else: print(\"此联系人不存在!\") def show_contacts(self): print(\'通讯录:\') for person in self.arr: print(person) self.is_empty() def find_contact(self, name): for person in self.arr: if person.name == name: return person return None def modify_contact(self, old_name, new_info): person = self.find_contact(old_name) if person: person.name = new_info.name person.gender = new_info.gender person.age = new_info.age person.telephone = new_info.telephone person.address = new_info.address self.save_contacts() print(\"修改成功!\\n \", person) else: print(\"此联系人不存在!\") def save_contacts(self, filename=\"contacts.csv\"): with open(filename, \'w\', newline=\'\', encoding=\'utf-8\') as f: writer = csv.writer(f) writer.writerow([\'姓名\', \'性别\', \'年龄\', \'电话\', \'住址\']) for person in self.arr: writer.writerow([person.name, person.gender, person.age, person.telephone, person.address]) def load_contacts(self, filename=\"contacts.csv\"): if not os.path.isfile(filename): with open(filename, \'w\', newline=\'\', encoding=\'utf-8\') as f: writer = csv.writer(f) writer.writerow([\'姓名\', \'性别\', \'年龄\', \'电话\', \'住址\']) with open(filename, \'r\', newline=\'\', encoding=\'utf-8\') as f: reader = csv.reader(f) next(reader) for row in reader: if len(row) == 5: name, gender, age, telephone, address = row self.add_contact(PersonInformation(name, gender, age, telephone, address)) else: print(f\"跳过不完整的联系人记录: {row}\")
menu
函数
这个函数用于显示程序的菜单选项,让用户可以选择执行不同的操作。
main
函数
这是程序的主入口,它首先创建一个ContactList
实例,然后进入一个无限循环,不断显示菜单并根据用户的选择调用相应的方法。当用户选择退出时,程序会结束。
程序的运行流程
- 程序启动后,首先加载已有的联系人信息。
- 显示菜单,等待用户输入选择。
- 根据用户的选择执行相应的操作:
- 添加联系人:输入新的联系人信息并添加到列表中。
- 删除联系人:输入要删除的联系人姓名,然后从列表中删除。
- 查找联系人:输入要查找的联系人姓名,并显示其信息。
- 修改联系人:输入要修改的联系人姓名,然后输入新的信息进行修改。
- 显示所有联系人:列出当前所有的联系人信息。
- 退出通讯录:退出程序。
完整代码
import os, csvclass PersonInformation: def __init__(self, name, gender, age, telephone, address): self.name = name self.gender = gender self.age = age self.telephone = telephone self.address = address def __str__(self): return f\"名字: {self.name}, 性别: {self.gender}, 年龄: {self.age}, 电话: {self.telephone}, 住址: {self.address}\"class ContactList: def __init__(self): self.arr = [] def add_contact(self, person): self.arr.append(person) self.save_contacts() def is_empty(self): if not self.arr: print(\"当前联系人列表为空!\") def remove_contact(self, name): person = self.find_contact(name) if person: self.arr.remove(person) self.save_contacts() print(\"删除成功!\") self.is_empty() else: print(\"此联系人不存在!\") def show_contacts(self): print(\'通讯录:\') for person in self.arr: print(person) self.is_empty() def find_contact(self, name): for person in self.arr: if person.name == name: return person return None def modify_contact(self, old_name, new_info): person = self.find_contact(old_name) if person: person.name = new_info.name person.gender = new_info.gender person.age = new_info.age person.telephone = new_info.telephone person.address = new_info.address self.save_contacts() print(\"修改成功!\\n \", person) else: print(\"此联系人不存在!\") def save_contacts(self, filename=\"contacts.csv\"): with open(filename, \'w\', newline=\'\', encoding=\'utf-8\') as f: writer = csv.writer(f) writer.writerow([\'姓名\', \'性别\', \'年龄\', \'电话\', \'住址\']) for person in self.arr: writer.writerow([person.name, person.gender, person.age, person.telephone, person.address]) def load_contacts(self, filename=\"contacts.csv\"): if not os.path.isfile(filename): with open(filename, \'w\', newline=\'\', encoding=\'utf-8\') as f: writer = csv.writer(f) writer.writerow([\'姓名\', \'性别\', \'年龄\', \'电话\', \'住址\']) with open(filename, \'r\', newline=\'\', encoding=\'utf-8\') as f: reader = csv.reader(f) next(reader) for row in reader: if len(row) == 5: name, gender, age, telephone, address = row self.add_contact(PersonInformation(name, gender, age, telephone, address)) else: print(f\"跳过不完整的联系人记录: {row}\")def menu(contact_list): print() print(\"*********************************\") print(\"** 1、增加联系人 2、删除联系人 **\") print(\"** 3、查找联系人 4、修改联系人 **\") print(\"** 5、展示联系人 0、退出通讯录 **\") print(\"*********************************\")def main(): contact_list = ContactList() contact_list.load_contacts() while True: menu(contact_list) name, gender, age, telephone, address = [\"\"]*5 input_choice = input(\"请输入您的选择:\") if input_choice == \'1\': while len(name)<2: name = input(\"请输入您要添加的联系人的姓名:\\n\") if contact_list.find_contact(name): print(f\'此姓名[{name}]已存在!\') break while gender not in (\'男\',\'女\',\'1\',\'0\'): gender = input(\"请输入性别(1-男, 0-女):\\n\") if gender==\'1\': gender = \'男\' elif gender==\'0\': gender = \'女\' while not age.isnumeric(): age = input(\"请输入年龄:\\n\") while telephone is None or len(telephone)!=11 or not telephone.startswith(\'1\'): telephone = input(\"请输入手机号:\\n\") while len(address)<6: address = input(\"请输入住址(字数不少于6):\\n\") contact_list.add_contact(PersonInformation(name, gender, age, telephone, address)) print(\"联系人添加成功!\") elif input_choice == \'2\': name = input(\"请输入您要删除的联系人的名字:\\n\") contact_list.remove_contact(name) elif input_choice == \'3\': name = input(\"请输入您要查找的联系人的名字:\\n\") person = contact_list.find_contact(name) print(person if person else \"此联系人不存在!\") elif input_choice == \'4\': old_name = input(\"请输入您要修改的联系人的名字:\\n\") new_info = PersonInformation(*([\"\"]*5)) person = contact_list.find_contact(old_name) if person: print(\'待修改联系人:(直接回车保留原字段)\\n \', person) while len(new_info.name)<2: new_info.name = input(\"请输入新的名字:\\n\") if new_info.name==\"\": new_info.name = person.name elif contact_list.find_contact(name): print(f\'此姓名[{name}]已存在,退出修改!\') break while new_info.gender not in (\'男\',\'女\',\'1\',\'0\'): new_info.gender = input(\"请输入性别(1-男, 0-女):\\n\") if new_info.gender==\'1\': new_info.gender = \'男\' elif new_info.gender==\'0\': new_info.gender = \'女\' elif new_info.gender==\"\": new_info.gender = person.gender while not new_info.age.isnumeric(): new_info.age = input(\"请输入年龄:\\n\") if new_info.age==\"\": new_info.age = person.age while len(new_info.telephone)!=11 or not new_info.telephone.startswith(\'1\'): new_info.telephone = input(\"请输入新的手机号:\\n\") if new_info.telephone==\"\": new_info.telephone = person.telephone while len(new_info.address)<6: new_info.address = input(\"请输入新的住址(字数不少于6):\\n\") if new_info.address==\"\": new_info.address = person.address contact_list.modify_contact(old_name, new_info) elif input_choice == \'5\': contact_list.show_contacts() elif input_choice == \'0\': print(\"成功退出通讯录!\") break else: print(\"输入错误,请重新选择!\")if __name__ == \"__main__\": main()
运行示例
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:5
通讯录:
当前联系人列表为空!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:1
请输入您要添加的联系人的姓名:
Hann
请输入性别(1-男, 0-女):
1
请输入年龄:
51
请输入手机号:
13962600000
请输入住址(字数不少于6):
江苏省昆山市
联系人添加成功!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:5
通讯录:
名字: Hann, 性别: 男, 年龄: 51, 电话: 13962600000, 住址: 江苏省昆山市
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:1
请输入您要添加的联系人的姓名:
Hann
此姓名[Hann]已存在!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:1
请输入您要添加的联系人的姓名:
YangCheng
请输入性别(1-男, 0-女):
1
请输入年龄:
22
请输入手机号:
13962611111
请输入住址(字数不少于6):
江苏省昆山市
联系人添加成功!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:5
通讯录:
名字: Hann, 性别: 男, 年龄: 51, 电话: 13962600000, 住址: 江苏省昆山市
名字: YangCheng, 性别: 男, 年龄: 22, 电话: 13962611111, 住址: 江苏省昆山市
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:4
请输入您要修改的联系人的名字:
Hann
待修改联系人:(直接回车保留原字段)
名字: Hann, 性别: 男, 年龄: 51, 电话: 13962600000, 住址: 江苏省昆山市
请输入新的名字:
HannYang
请输入性别(1-男, 0-女):
请输入年龄:
请输入新的手机号:
请输入新的住址(字数不少于6):
修改成功!
名字: HannYang, 性别: 男, 年龄: 51, 电话: 13962600000, 住址: 江苏省昆山市
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:5
通讯录:
名字: HannYang, 性别: 男, 年龄: 51, 电话: 13962600000, 住址: 江苏省昆山市
名字: YangCheng, 性别: 男, 年龄: 22, 电话: 13962611111, 住址: 江苏省昆山市
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:3
请输入您要查找的联系人的名字:
hannyang
此联系人不存在!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:3
请输入您要查找的联系人的名字:
HannYang
名字: HannYang, 性别: 男, 年龄: 51, 电话: 13962600000, 住址: 江苏省昆山市
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:2
请输入您要删除的联系人的名字:
hann
此联系人不存在!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:2
请输入您要删除的联系人的名字:
HannYang
删除成功!
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:5
通讯录:
名字: YangCheng, 性别: 男, 年龄: 22, 电话: 13962611111, 住址: 江苏省昆山市
*********************************
** 1、增加联系人 2、删除联系人 **
** 3、查找联系人 4、修改联系人 **
** 5、展示联系人 0、退出通讯录 **
*********************************
请输入您的选择:0
成功退出通讯录!
完