类来实现选课系统
选课系统:
功能介绍
-
管理员:
-
创建课程
-
创建学生
-
查看课程
-
查看学生
-
查看学生选课
-
退出程序
-
-
学生:
-
查看可选课程
-
查看已选课程
-
进行选课
-
修改密码
-
退出程序
-
细节问题
1、登录时,直接输入用户名和密码,要能根据不同角色(学生、管理员)进入不同功能选项。
2、要创建三个文件
-
course_file: 保存课程类的对象,用pickle格式保存。
-
login_file: 保存格式,用户名|密码|(学生类名/管理员类名)
-
student_file:保存学生类的对象,用pickle格式保存
3、创建学生时,要同时对login_file和student_file两个文件实现更新
代码:
import pickle
import sys
COURSE_PATH = 'course_file' #课程对象的文件路径
STUDENT_PATH = 'student_file'#学生对象的文件路径
LOGIN_PATH = 'login_file'#登录用户名密码,角色的文件路径
#读取course_file中的课程对象,读取student_file中的学生对象,list
def read_student_course(path):
lis = []
with open(path,'rb') as fp:
while 1:
try :
obj = pickle.load(fp)
lis.append(obj)
except EOFError:
break
return lis
#将更新的课程对象列表,重新写入到course_file
# 将更新后的学生对象列表,重新写到文件中student_file中 list
def write_student_course(lis,path):
with open(path,'wb')as fp:
for obj in lis:
pickle.dump(obj,fp)
return True
#读取Login_file文件,dic={'lhz':['lhz','123','student'],}
def read_login_file():
dic={}
with open(LOGIN_PATH,'r',encoding='utf-8')as fp:
for line in fp:
lis = line.strip().split('|')
dic[lis[0]]=lis
return dic
#将更新的用户密码字典写回,login_file文件
def write_login_file(dic):
with open(LOGIN_PATH,'w',encoding='utf-8') as fp:
for key in dic:
msg = '|'.join(dic[key]) +'\n'
fp.write(msg)
return True
#查看课表
def show_course():
lis = read_student_course(COURSE_PATH)
co =1
for obj in lis:
print(f"{co}、{obj}")#在Course类中写了 __str__
co+=1
class Course(object):
def __init__(self,name,permi,price,teacher):
self.name = name
self.permi = permi
self.price = price
self.teacher = teacher
def __str__(self):
msg = self.name+':'+str(self.permi)+','+str(self.price)+','+self.teacher
# msg = '123'
return msg
class Student(object):
opt_list = [
('查看课程','show_course'),(
