用文件读写的方式创建一个班级通讯录类,包含名字,班级,手机,家庭电话,邮箱,通讯地址,邮编,学号等信息,对学生的信息进行增加,删除,修改,查询,浏览等操作
时间: 2024-03-04 08:52:03 浏览: 63
好的,我可以为您提供一个 Python 的示例代码,实现班级通讯录类的增删改查等操作,具体如下:
```python
import os
class Student:
def __init__(self, name, clazz, phone, home_phone, email, address, postcode, student_id):
self.name = name # 姓名
self.clazz = clazz # 班级
self.phone = phone # 手机
self.home_phone = home_phone # 家庭电话
self.email = email # 邮箱
self.address = address # 通讯地址
self.postcode = postcode # 邮编
self.student_id = student_id # 学号
class ClassmateBook:
def __init__(self, filename):
self.filename = filename
def add_student(self, student):
with open(self.filename, 'a', encoding='utf-8') as f:
f.write(f'{student.name}\t{student.clazz}\t{student.phone}\t{student.home_phone}\t{student.email}\t{student.address}\t{student.postcode}\t{student.student_id}\n')
def delete_student(self, student_id):
with open(self.filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
with open(self.filename, 'w', encoding='utf-8') as f:
for line in lines:
if student_id not in line:
f.write(line)
def modify_student(self, student_id, new_student):
with open(self.filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
with open(self.filename, 'w', encoding='utf-8') as f:
for line in lines:
if student_id in line:
f.write(f'{new_student.name}\t{new_student.clazz}\t{new_student.phone}\t{new_student.home_phone}\t{new_student.email}\t{new_student.address}\t{new_student.postcode}\t{new_student.student_id}\n')
else:
f.write(line)
def search_student(self, student_id):
with open(self.filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if student_id in line:
name, clazz, phone, home_phone, email, address, postcode, _ = line.split('\t')
return Student(name, clazz, phone, home_phone, email, address, postcode, student_id)
return None
def browse_all_students(self):
with open(self.filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
name, clazz, phone, home_phone, email, address, postcode, student_id = line.split('\t')
print(f'姓名:{name}\t班级:{clazz}\t手机:{phone}\t家庭电话:{home_phone}\t邮箱:{email}\t通讯地址:{address}\t邮编:{postcode}\t学号:{student_id}')
book = ClassmateBook('classmate.txt') # 创建班级通讯录对象
# 添加学生信息
book.add_student(Student('张三', '1班', '13800138000', '010-12345678', 'zhangsan@qq.com', '北京市海淀区', '100000', '1001'))
book.add_student(Student('李四', '2班', '13900139000', '010-23456789', 'lisi@qq.com', '北京市朝阳区', '100001', '1002'))
book.add_student(Student('王五', '3班', '13600136000', '010-34567890', 'wangwu@qq.com', '北京市丰台区', '100002', '1003'))
# 浏览所有学生信息
book.browse_all_students()
# 查询学生信息
student = book.search_student('1002')
if student:
print(f'查询结果:姓名:{student.name}\t班级:{student.clazz}\t手机:{student.phone}\t家庭电话:{student.home_phone}\t邮箱:{student.email}\t通讯地址:{student.address}\t邮编:{student.postcode}\t学号:{student.student_id}')
else:
print('未找到该学生')
# 修改学生信息
book.modify_student('1002', Student('李四', '2班', '13900139000', '010-87654321', 'lisi@qq.com', '北京市朝阳区', '100001', '1002'))
# 删除学生信息
book.delete_student('1003')
# 再次浏览所有学生信息
book.browse_all_students()
```
上述代码中,我们首先定义了一个 `Student` 类来表示学生信息,然后定义了一个 `ClassmateBook` 类来表示班级通讯录,其中主要包含了增加、删除、修改、查询、浏览等操作。在具体实现时,我们使用了文件读写的方式来存储和读取学生信息,其中每一行包含了一个学生的所有信息,各字段之间使用制表符 `\t` 分隔。您可以根据实际情况,修改代码中的文件路径和字段名称等相关信息。
阅读全文