选课系统
选课系统:
角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
readme:
功能:1.学生入口
2.讲师入口
3.管理入口
学生入口:1.选择班级
2.选择课程
3.交学费
4.查看成绩
讲师入口:1.查看班级
查看班级:a.查看班级成员
b.修改学员成绩
2.管理班级:a.加入班级
b.退出班级
管理入口:1.创建讲师
2.创建班级
3.创建课程
结构:Subject:bin ---enter.py ...程序入口,执行此文件进入程序
core ---main.py ...创建模板类和方法
---admin_enter.py ...管理员入口
---student_enter.py ...学生入口
---teacher_enter.py ...讲师入口
---test.py ...查看所有文件信息里的值
conf ---data.py ...数据读写的方法
---school.info 学校信息数据文件,存储格式:{school_name:{"address": str, "city": str}}
---student.info 学生信息数据文件,存储格式:{school_name:
{student_name:
{"pwd":self.pwd,"age": self.age, "sex": self.sex,"score":{"语文":90,"数学":80},"course": [], "classes": None,"fee_sign":0}}}
未做加入考试成绩功能,所有在创建学生的时候默认给予成绩"score":{"语文":90,"数学":80},"fee_sign":0为缴费标志,0为未交费,1为已缴费
---teacher.info 老师信息数据文件,存储格式:{school_name:
{teacher_name:
{"pwd":self.pwd,"age": self.age, "sex": self.sex,"course": [], "classes": []}}}
---classes.info 班级信息数据文件,存储格式:{school_name:
{classes_name:
{"teacher":[],"student":[],"course":[]}}}
---course.info 课程信息数据文件,存储格式:{school_name:
{course_name:
{"cycle": cycle, "price": price,"teacher": [], "student": [], "classes": []}}}
使用方法:
1.先执行enter.py进入登录界面
2.选择管理,先创建讲师或者课程,再创建班级,因为在创建班级时默认需要加入讲师和课程
3.进入学生接口注册学生,注册后登录选择加入班级和课程,班级只会加入一个,第二次加入会替换第一次,课程可以加入多个。加入课程后可以交费。
4.进入讲师入口
问题:
班级创建时添加课程和老师,只能添加一个,添加多个老师需进入讲师入口添加班级,添加多个课程功能未实现。
学生添加一个课程后完成缴费,再添加一个课程再缴费会提示学费已结清。
流程图:
代码:
bin:
入口:enter.py
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import os import sys PATH=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,PATH) import core import conf #存学校的内存对象 school_list=[] school_bj=core.main.School("北京学校","五里屯","北京") school_sh=core.main.School("上海学校","黄浦江","上海") school_bj.get_school() school_sh.get_school() school_list.append(school_bj) school_list.append(school_sh) INPUT_DICT={"1":core.student_enter.student_enter, "2":core.teacher_enter.teacher_enter, "3":core.admin_enter.admin_enter} INFO='''\033[34;1m--*--Welcome To Subject System--*-- 1.学生 2.讲师 3.管理 9.退出\033[0m ''' core.main.login_face(INFO,INPUT_DICT,school_list)
core:
创建模板类和方法:main.py
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import conf import core class School(object): def __init__(self,name,address,city,): self.name=name self.address=address self.city=city def get_school(self): # 创建学校,并存入school.info school_dict = conf.data.read() school_dict[self.name] = {"address": self.address, "city": self.city} conf.data.write(school_dict) def get_class(self): # 创建班级 # 数据存入class.info:{学校名称:{班级名称:{"teacher":[],"studenr":[],"course":[]}}} class_dict=conf.data.read("classes.info") null_dict = {} MODEL={"teacher":[],"student":[],"course":[]} class_name=input("\033[32;0m请输入班级名称:\033[0m").strip() if self.name not in class_dict.keys(): null_dict[class_name]=MODEL class_dict[self.name]=null_dict else: class_dict[self.name][class_name]=MODEL #创建的时候加入课程和教师 data_list=[self.name,class_name] conf.data.write(class_dict,"classes.info") view=core.student_enter.Student_view(data_list,"classes") view.pick("course.info","course") view.pick("teacher.info", "teacher") print("\33[31;0m创建成功!\33[0m") def get_course(self): # 创建课程,存储方式与班级相同 course_dict = conf.data.read("course.info") null_dict = {} course_name=input("\033[32;0m请输入课程名称:\033[0m").strip() cycle=input("\033[32;0m请输入课程周期(单位:月)\033[0m").strip() price=input("\033[32;0m请输入课程价格(单位:元)\033[0m").strip() MODEL={"cycle": cycle, "price": price, "teacher": [], "student": [], "classes": []} if self.name not in course_dict.keys(): null_dict[course_name] =MODEL course_dict[self.name] = null_dict else: course_dict[self.name][course_name]=MODEL conf.data.write(course_dict, "course.info") print("\33[31;0m创建成功!\33[0m") class People(object): #人类 def __init__(self,name,age,sex,pwd): self.name=name self.age=age self.sex=sex self.pwd=pwd #数据存储格式 self.MODEL = {"pwd":self.pwd,"age": self.age, "sex": self.sex} def get_people(self,school_obj,data_address): ''' 创建学校里任务的模板 :param school_obj: 学校的对象 :param data_address: 存储文件名称 :return: ''' people_dict = conf.data.read(data_address) null_dict = {} if school_obj.name not in people_dict.keys(): null_dict[self.name] = self.MODEL people_dict[school_obj.name] = null_dict else: people_dict[school_obj.name][self.name] = self.MODEL conf.data.write(people_dict, data_address) print("\33[31;0m创建成功!\33[0m") class Teacher(People): #老师 def __init__(self,name,age,sex,pwd): super(Teacher,self).__init__(name,age,sex,pwd) # 数据存储格式 self.MODEL = {"pwd":self.pwd,"age": self.age, "sex": self.sex,"course": [], "classes": []} class Students(People): #学生 def __init__(self,name,age,sex,pwd): super(Students,self).__init__(name,age,sex,pwd) # 数据存储格式 fee:学费状态,0为未交,1为已缴 self.MODEL = {"pwd":self.pwd,"age": self.age, "sex": self.sex,"score":{"语文":90,"数学":80}, "course": [], "classes": None,"fee_sign":0} def login_face(info,input_dict,args=None): #界面展示功能模板 INPUT_DICT = input_dict while True: INFO = info print(INFO) num = input("\033[32;0m请输入序号选择入口:\033[0m") if num=="0": break elif num=="9": exit() elif num in INPUT_DICT.keys(): INPUT_DICT[num](args) else: print("\033[31;1m输入错误,请重新输入\033[0m") def pick_school(obj): ''' 该方法用于选择学校时调用 :param obj: obj为学校的对象列表 :return: obj[num-1]选择后的学校对象 ''' for i in obj: print("\33[34;1m%s.%s\33[0m"%(obj.index(i)+1,i.name)) while True: num=input("\033[32;0m请选择学校序号:\033[0m") if num.isdigit(): num=int(num) if num in range(1,len(obj)+1): return obj[num-1] else: print("\033[31;1m输入错误\033[0m") else: print("\033[31;1m输入错误\033[0m") def auth(obj,datafile): ''' 登录验证 :param obj: obj为学校的对象列表 :param datafile: datafile为存储数据的文件名 :return: data_list=[school_obj.name,name],登录时选择的学校名称和登录人员名称 ''' school_obj=pick_school(obj) while True: name = input("\033[32;0m请输入姓名:\033[0m").strip() pwd = input("\033[32;0m请输入密码:\033[0m").strip() data_dict=conf.data.read(datafile) if data_dict: if name in data_dict[school_obj.name].keys() and \ pwd==data_dict[school_obj.name][name]["pwd"]: print("\033[32;0mWelcome,%s\033[0m"%name) data_list=[school_obj.name,name] #返回学校名称和登录人名称 return data_list else: print("\033[31;1m账号或密码错误\033[0m") else: print("\033[31;1m没有数据,请先注册!\033[0m") return []
管理入口:admin_enter.py
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import core import conf def admin_enter(obj): #管理员入口 INPUT_DICT={"1":c_teather,"2":c_class,"3":c_course} INFO='''\033[34;1m1.创建讲师 2.创建班级 3.创建课程 0.返回上一层 9.退出\033[0m''' core.main.login_face(INFO,INPUT_DICT,obj) def c_teather(obj): #创建老师 school_obj = core.main.pick_school(obj) name = input("\033[32;0m名字:\033[0m").strip() age = input("\033[32;0m年龄:\033[0m").strip() sex = input("\033[32;0m性别:\033[0m").strip() pwd=input("\033[32;0m密码:\033[0m").strip() teacher_obj=core.main.Teacher(name,age,sex,pwd) teacher_obj.get_people(school_obj,"teacher.info") def c_class(obj): #创建班级 school_obj = core.main.pick_school(obj) school_obj.get_class() def c_course(obj): #创建课程 school_obj = core.main.pick_school(obj) school_obj.get_course()
学生入口:student_enter.py
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import core import conf def student_enter(obj): #学生入口 INPUT_DICT = {"1": login, "2": logon} INFO = '''\033[34;1m1.注册 2.登录 0.返回上一层 9.退出\033[0m ''' core.main.login_face(INFO, INPUT_DICT,obj) def login(obj): #注册 school_obj = core.main.pick_school(obj) name = input("\033[32;0m名字:\033[0m").strip() age = input("\033[32;0m年龄:\033[0m").strip() sex = input("\033[32;0m性别:\033[0m").strip() pwd = input("\033[32;0m密码:\033[0m").strip() student_obj = core.main.Students(name, age, sex,pwd) student_obj.get_people(school_obj, "student.info") print("请登录:") logon(obj) def logon(obj): #登录 data_list=core.main.auth(obj,"student.info") if data_list: view = Student_view(data_list,"student") INFO = '''\033[34;1m1.选择班级 2.选择课程 3.交学费 4.查看成绩 0.返回上一层 9.退出\033[0m''' while True: print(INFO) num = input("\033[32;0m请输入序号选择入口:\033[0m") if num == "0": break elif num == "9": exit() elif num=="1": view.pick("classes.info","classes") elif num=="2": view.pick("course.info", "course") elif num =="3": view.pay("course.info","course") elif num =="4": view.get_mark() else: print("\033[31;1m输入错误,请重新输入\033[0m") else: return class Student_view(object): #学生视图类 def __init__(self,data_list,info_name): ''' :param data_list: 调用core里的auto登录验证时会返回学校名称和登录名称的列表 :param info_name: 打开文件的文件名称 ''' self.school_name=data_list[0] self.name=data_list[1] self.info_name=info_name self.student_data = conf.data.read("%s.info"%self.info_name) def pick(self,datafile,choose): ''' pick():选择的接口,比如学生老师选择班级,选择课程,生成班级时选择老师,选择课程都调用此方法 选择后双向添加,比如向班级里加入课程,在classes.info中"course"会加入选择的课程, 同时在course.info中的"classes"也会加入该班级 :param datafile: 信息文件名称 :param choose: 选择类型("class"或者"course") :return: ''' data=conf.data.read(datafile) count=0 key_dict={} try: for key in data[self.school_name]: count+=1 print("\033[34;1m%s.%s\033[0m"%(count,key)) key_dict[count]=key num=input("\033[32;0m选择需要加入的序号:\033[0m").strip() if num.isdigit(): num=int(num) if num in key_dict.keys(): if type(self.student_data[self.school_name][self.name][choose]) is list: self.student_data[self.school_name][self.name][choose].append(key_dict[num]) else: self.student_data[self.school_name][self.name][choose]=key_dict[num] if type(data[self.school_name][key_dict[num]][self.info_name]) is list: data[self.school_name][key_dict[num]][self.info_name].append(self.name) else: data[self.school_name][key_dict[num]][self.info_name] = (self.name) conf.data.write(self.student_data,"%s.info"%self.info_name) conf.data.write(data,datafile) except KeyError as e: print("请先创建讲师和课程") def pay(self,datafile,choose): #支付学费 if self.student_data[self.school_name][self.name]["fee_sign"]==0: course_list=self.student_data[self.school_name][self.name][choose] data = conf.data.read(datafile) fee=0 for i in course_list: fee=fee+int(data[self.school_name][i]["price"]) print("\033[32;0m课程总学费为:%s\033[0m"%fee) num=input("\033[32;0m请输入缴费金额:\033[0m").strip() if num.isdigit(): num=int(num) if num==fee: print("\033[31;1m缴费成功\033[0m") self.student_data[self.school_name][self.name]["fee_sign"] =1 conf.data.write(self.student_data,"student.info") else: print("\033[31;1m输入错误\033[0m") else: print("\033[31;1m输入错误\033[0m") else: print("\033[31;1m学费已结清\033[0m") def get_mark(self): #查看成绩 score_dict=self.student_data[self.school_name][self.name]["score"] for key,values in score_dict.items(): print("%s:%s"%(key,values)) print()
讲师入口:teacher_enter.py
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import core import conf def teacher_enter(obj): teacher_list=core.main.auth(obj,"teacher.info") if teacher_list: INPUT_DICT={"1":see,"2":manage} INFO='''\033[34;1m1.查看班级 2.管理班级 0.返回上一层 9.退出\033[0m''' core.main.login_face(INFO, INPUT_DICT,teacher_list) else: return def see(teacher_list): INPUT_DICT = {"1": see_student, "2": alter} INFO = '''\033[34;1m1.查看班级成员 2.修改学员成绩 0.返回上一层 9.退出\033[0m''' core.main.login_face(INFO, INPUT_DICT, teacher_list) def choose_class(teacher_list): #选择班级的模板,返回班级列表 #teacher_list:调用core.main.auto()登录验证方法返回的学校姓名和登录名称的列表 school_name = teacher_list[0] teacher_name = teacher_list[1] teacher_data = conf.data.read("teacher.info") class_list=teacher_data[school_name][teacher_name]["classes"] for i in class_list: print("\033[34;1m%s.%s\033[0m" % (class_list.index(i)+1, i)) return class_list def see_student(teacher_list): #查看班级成员 class_list=choose_class(teacher_list) num = input("\033[32;0m选择查看的班级序号:\033[0m").strip() if num.isdigit(): num = int(num) if num in range(1,len(class_list)+1): class_data = conf.data.read("classes.info") student_list=class_data[teacher_list[0]][class_list[num-1]]["student"] if student_list: for i in student_list: print("\033[34;1m%s.%s\033[0m" % (student_list.index(i) + 1, i)) print() else: print("\033[31;1m未加入学生\033[0m") return student_list else: print("\033[31;1m输入错误\033[0m") return [] else: print("\033[31;1m输入错误\033[0m") return [] def alter(teacher_list): #修改学生成绩 student_list=see_student(teacher_list) num=input("\033[32;0m选择修改的学生序号:\033[0m").strip() if num.isdigit(): num = int(num) if num in range(1, len(student_list) + 1): student_data = conf.data.read("student.info") score_dict=student_data[teacher_list[0]][student_list[num-1]]["score"] for key,values in score_dict.items(): print("\033[34;1m%s:%s\033[0m"%(key,values)) course=input("\033[32;0m输入需要修改的课程名称:\033[0m") score=input("\033[32;0m修改后的成绩:\033[0m") score_dict[course]=score conf.data.write(student_data,"student.info") print("\033[31;1m修改成功\033[0m") else: print("\033[31;1m输入错误\033[0m") else: print("\033[31;1m输入错误\033[0m") def manage(teacher_list): #管理班级 INPUT_DICT = {"1": join, "2": quit} INFO = '''\033[34;1m1.加入班级 2.退出班级 0.返回上一层 9.退出\033[0m''' core.main.login_face(INFO, INPUT_DICT, teacher_list) def join(teacher_list): #加入班级 view = core.student_enter.Student_view(teacher_list, "teacher") view.pick("classes.info", "classes") def quit(teacher_list): ''' 退出班级,移除teacher.info中“classes”:[]中的班级, 同时移除classes.info中该班级里"teacher":[]中的该老师 :param teacher_list: 调用core.main.auto()登录验证方法返回的学校姓名和登录名称的列表 :return: ''' class_list = choose_class(teacher_list) print(class_list) num = input("\033[32;0m选择想退出的班级序号:\033[0m").strip() if num.isdigit(): num = int(num) if num in range(1, len(class_list) + 1): teacher_data = conf.data.read("teacher.info") class_data=conf.data.read("classes.info") teacher_data[teacher_list[0]][teacher_list[1]]["classes"].remove(class_list[num-1]) class_data[teacher_list[0]][class_list[num-1]]["teacher"].remove(teacher_list[1]) conf.data.write(teacher_data,"teacher.info") conf.data.write(class_data, "classes.info") print("\033[31;1m删除成功\033[0m") else: print("\033[31;1m输入错误\033[0m") else: print("\033[31;1m输入错误\033[0m")
测试:test.py
import os import sys PATH=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,PATH) import conf school_data=conf.data.read("school.info") classes_data=conf.data.read("classes.info") course_data=conf.data.read("course.info") teacher_data=conf.data.read("teacher.info") student_data=conf.data.read("student.info") print("school_data:",school_data) print("classes_data:",classes_data) print("course_data:",course_data) print("teacher_data:",teacher_data) print("student_data:",student_data)
conf:
数据读写:data.py
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import pickle import os PATH=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+os.sep+"conf" def write(obj,name="school.info"): pickle.dump(obj,open("%s%s%s"%(PATH,os.sep,name), "wb")) def read(name="school.info"): if os.path.exists("%s%s%s" % (PATH, os.sep,name)): school_dict=pickle.load(open("%s%s%s"%(PATH,os.sep,name), "rb")) return school_dict else: return {}