main
main.py
"""
主要的控制
"""
from student_manger import *
if __name__ == '__main__':
sms = StudentMangerSystem()
sms.start()
学生类
student.py
"""
定义一个学生类
"""
class Student(object):
def __init__(self, stu_id, stu_name, stu_age):
self.stu_id = stu_id
self.stu_name = stu_name
self.stu_age = stu_age
def __str__(self):
return '| '+self.stu_id.ljust(5)+self.stu_name.ljust(8)+self.stu_age.ljust(2)+' |'
if __name__ == '__main__':
s = Student('1', 'tom', '12')
print(s)
功能类
student_manger.py
"""
实现增删改查
"""
from student import *
class StudentMangerSystem(object):
def __init__(self):
# 定义一个字典存取数据,存取的方式,以学会的id为key,stu为values
self.students = {}
self.__load_info()
def start(self):
print('系统开始运行')
while True:
self.__show_menu()
select_id = input('请输入你要进行的操作:')
if select_id.isdigit():
s_id = int(select_id)
if 0 <= s_id <= 6:
self.__operation(select_id)
else:
print('输入的超出范围')
else:
print('输入非法的字符请重新输入')
# 操作调用函数
def __operation(self, select_id):
print(f'操作{select_id}')
menthod_dict = {'1': self.__add_stu,
'2': self.__search_stu,
'3': self.__modify_stu_info,
'4': self.__del_stu,
'5': self.__show_all_stu_info,
'6': self.__write_info,
'0': exit}
menthod = menthod_dict[select_id]
if select_id == '2' or select_id == '3' or select_id == '4':
stu_id = input('请输入你要进行操作的学生id:')
menthod(stu_id)
elif select_id == '0':
menthod(0)
else:
menthod()
# 菜单方法
def __show_menu(self):
print('*' * 15)
print('学生管理系统')
print('1.添加学生')
print('2.查找学生')
print('3.更改学生')
print('4.删除学生')
print('5.显示全部')
print('6.保存信息')
print('0.退出系统')
print('*' * 15)
# 添加学生:
def __add_stu(self):
# 信息的输入
stu_info = self.__input_stu_info()
# 生成一个学生实例接收
stu = Student(stu_info[0], stu_info[1], stu_info[2])
# 接收信息,保存
self.students[stu.stu_id] = stu
print(self.students)
# 查找:
def __search_stu(self, stu_id):
stu = None
if stu_id in self.students:
stu = self.students[stu_id]
self.__show_stu_info(stu)
else:
print(f'该学号为:{stu_id}的学生不存在')
return stu
# 修改
def __modify_stu_info(self, stu_id):
# 找到要修改的学生
stu = self.__search_stu(stu_id)
# 判断是否找到
if stu:
new_stu_info = self.__input_stu_info()
stu.stu_id = new_stu_info[0]
stu.stu_name = new_stu_info[1]
stu.stu_age = new_stu_info[2]
print('修改完成')
self.__show_stu_info(stu)
else:
print('该学生不存在')
# 删除学会信息
def __del_stu(self, stu_id):
stu = self.__search_stu(stu_id)
if stu:
self.students.pop(stu_id)
# 显示全部
def __show_all_stu_info(self):
for stu in self.students.values():
print(stu)
# 打印单个学生的信息
def __show_stu_info(self, stu):
print(stu)
# 输入学生信息
def __input_stu_info(self):
stu_id = input('请输入学生的id:')
stu_name = input('请输入学生的name:')
stu_age = input('请输入学生的age:')
return stu_id, stu_name, stu_age
# 文件的操作
# 写入文件
def __write_info(self):
file_r = open('data', 'w')
for stu_id in self.students:
stu = self.students[stu_id]
stu_s = stu.stu_id+' '+stu.stu_name+' '+stu.stu_age+'\n'
file_r.write(stu_s)
file_r.close()
def __load_info(self):
file_r = None
try:
file_r = open('data', 'r')
except Exception as e:
print(e, '文件不存在')
else:
content = file_r.readlines()
for stu_info in content:
split_info = stu_info.split()
stu = Student(split_info[0], split_info[1], split_info[2])
self.students[split_info[0]] = stu
finally:
if file_r:
file_r.close()