一、作业要求:
选课系统:
角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
二、程序目录说明
学生选课系统/
|-- bin/
| |-- __init__.py
| |-- start.py #程序启动的主入口
|
|-- conf/
| |-- __init__.py
| |-- setting.py #配置文件
|
|-- db/
| |-- admin/ #管理员数据文件夹
| |-- charge/ #学生缴费数据文件夹
| |-- classes/ #班级数据文件夹
| |-- course/ #课程数据文件夹
| |-- school/ #学校数据文件夹
| |-- score/ #学生成绩数据文件夹
| |-- student/ #学生数据文件夹
| |-- teacher/ #老师数据文件夹
|
|-- lib/
| |-- __init__.py
| |-- common.py #公共函数
| |-- main.py #各大类
|
|-- log/
| |-- log_sys.log #日志文件
|
|-- src/
| |-- __init__.py
| |-- admin.py #管理员系统
| |-- func.py #主逻辑函数
| |-- student.py #学生系统
| |-- teacher.py #老师系统
|
|-- 流程图
|-- README.txt
三、程序已实现功能
程序实现了以下功能:
1、创建管理员(已有管理员:japhi、alex;密码都是123)
2、管理员创建老师(已有老师:李杰、林海峰、武佩奇)
3、管理员创建班级(已有班级:go1班、python1班、linux1班)
4、管理员创建课程(已有课程:go、python、linux)
5、管理员查看老师、班级、课程信息
6、老师修改学生成绩
7、老师查看班级和学生信息
8、学生注册及缴费(已有学生:alex、张三、李四、王五、赵六)
9、数据序列号保存并读取
四、流程图
五、作业代码
1、bin文件夹下程序启动主入口start.py:
import os,sys frame = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"src") sys.path.append(frame) from func import sys_func if __name__ == '__main__': sys_func()
2、conf文件夹下配置文件seetings.py:
import os,sys basdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #定义环境变量 sys.path.append(basdir) log_dir = os.path.join(basdir,"log")
3、lib文件夹下公共函数common.py:
import logging,os,pickle,sys,uuid frame = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(frame) from conf import setting def sys_logging(content,levelname): ''' 程序记录日志函数 :param content: 日志的内容 :param levelname: 日志的等级 :return: none ''' _filename = os.path.join(setting.log_dir,"log_sys.log") log = logging.getLogger(_filename) logging.basicConfig(filename=_filename,level=logging.INFO,format='%(asctime)s-%(levelname)s-%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') if levelname == 'debug': logging.debug(content) elif levelname == 'info': logging.info(content) elif levelname == 'warning': logging.warning(content) elif levelname == 'error': logging.error(content) elif levelname == 'critical': logging.critical(content) def printer(msg,msg_type): ''' 程序不同信息打印的字体颜色 :param msg: 打印信息 :param msg_type: 打印信息的类型 :return: none ''' if msg_type == "info": show_msg = " 33[1;33m%s 33[0m"%msg elif msg_type == "error": show_msg = " 33[1;31m%s 33[0m"%msg elif msg_type == "msg": show_msg = " 33[1;37m%s 33[0m"%msg else: show_msg = " 33[1;32m%s 33[0m"%msg print(show_msg.center(60,'-')) sys_logging(msg,msg_type)
主类代码main.py:
import os,pickle,sys,time frame = os.path.dirname(os.path.abspath(__file__)) sys.path.append(frame) from common import printer class BasicObj(object): ''' 基类 ''' def save(self): basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) file_path = os.path.join(os.path.join(os.path.join(basedir,"db"),self.form),self.name) with open(file_path,"wb") as f: pickle.dump(self,f) @staticmethod def gain_data(name): basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) file_path = os.path.join(os.path.join(basedir,"db"),name) # print(file_path) res_list = [] for file in os.listdir(file_path): filename = os.path.join(file_path,file) res_list.append(pickle.load(open(filename,"rb"))) return res_list class School(BasicObj): ''' 学校类 ''' def __init__(self,form,name,city): self.form = form self.name = name self.city = city self.create_time = time.strftime('%Y-%m-%d') class Student(BasicObj): ''' 学生类 ''' def __init__(self,form,school,classes,name,psd): self.form = form self.school = school self.classes = classes self.name = name self.psd = psd self.create_time = time.strftime('%Y-%m-%d') class Teacher(BasicObj): ''' 老师类 ''' def __init__(self,form,name,level): self.form = form self.name = name self.level = level self.create_time = time.strftime('%Y-%m-%d') class Course(BasicObj): ''' 课程类 ''' def __init__(self,form,name,price,period,school): self.form = form self.name = name self.price = price self.period = period self.school = school self.create_time = time.strftime('%Y-%m-%d') class Classes(BasicObj): ''' 班级类 ''' def __init__(self,form,school,teacher,course,name): self.form = form self.school = school self.teacher = teacher self.course = course self.name = name class Admin(BasicObj): ''' 管理员类 ''' def __init__(self,form,name,psd): self.form = form self.name = name self.psd = psd self.create_time = time.strftime('%Y-%m-%d') @staticmethod def login(): ''' 管理员登陆系统 :return: 登陆成功返回True,否则返回False ''' sign = False ad_name = input("请输入您的用户名>>>").strip() ad_psd = input("请输入密码>>>").strip() admin_dic = {} for obj in BasicObj.gain_data("admin"): admin_dic[obj.name] = obj.psd if admin_dic.get(ad_name) == ad_psd: printer("登陆成功","info") sign = True else: printer("用户名或密码错误","error") return sign class StudentScore(BasicObj): ''' 学生分数类 ''' def __init__(self,form,name,score): self.form = form self.name = name self.score = score class StudentCharge(BasicObj): ''' 学生缴费类 ''' def __init__(self,form,name,charge): self.form = form self.name = name self.charge = charge
4、src文件下管理员逻辑函数admin.py:
import os,sys frame = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"lib") sys.path.append(frame) from common import printer from main import BasicObj,Admin,Teacher,Course,Classes def exist_parse(data,list): ''' 对用户数的data是否存在于实例文件进行判断函数 :param data: 用户输入的数据 :param list: 用户输入的数据类型对应的实例对象列表 :return: 信号 ''' sign = True begin_list = [] for obj in list: begin_list.append(obj.name) if data not in begin_list: printer("您输入的[%s]不存在"%data,"a") sign = False return sign def exist_school(data,list): ''' 对用户选择的校区是否存在进行判断的函数 :param data: 用户输入的校区 :param list: 校区实例对象列表 :return: 信号 ''' sign = True begin_list = [] for obj in list: begin_list.append(obj.city) if data not in begin_list: printer("您输入的[%s]不存在"%data,"a") sign = False return sign def exist(data,list): ''' 对用户输入的数据是否存在实例对象进行判断的函数 :param data: 用户输入的数据 :param list: 实例对象列表 :return: 信号 ''' begin_list = [] sign = True for obj in list: begin_list.append(obj.name) if data in begin_list: printer("[%s]已存在"%data,"error") sign =False return sign def creat_teacher(): ''' 创建老师实例对象函数 :return: none ''' teacher_list = BasicObj.gain_data("teacher") while True: printer("创建老师信息","info") name = input("请输入您创建的老师的名字>>>") sign = exist(name,teacher_list) if sign: level = input("请输入您创建的老师的等级>>>") teacher0 = Teacher("teacher",name,level) teacher0.save() data = "姓名[%s]-等级[%s]"%(name,level) printer(data,"info") break else: continue def view_teacher(): ''' 查看老师信息的函数 :return: none ''' teacher_list = BasicObj.gain_data("teacher") printer("----------老师信息----------","info") for obj in teacher_list: teacher_info = "姓名[%s]-等级[%s]-注册时间[%s]"%(obj.name,obj.level,obj.create_time) printer(teacher_info,"info") def creat_class(): ''' 创建班级函数 :return: none ''' class_list = BasicObj.gain_data("classes") teacher_list = BasicObj.gain_data("teacher") school_list = BasicObj.gain_data("school") course_list = BasicObj.gain_data("course") while True: printer("创建班级信息", "info") school_data() school = input("请选择校区>>>") sign0 = exist_school(school,school_list) if sign0: teacher_data() teacher = input("请选择授课老师>>>") sign1 = exist_parse(teacher,teacher_list) if sign1: course_data() course = input("请选择班级课程>>>") sign2 = exist_parse(course,course_list) if sign2: name = input("请输入您创建的班级名称>>>") class0 = Classes("classes",school,teacher,course,name) sign3 = exist(name,class_list) if sign3: class0.save() data = "校区[%s]-老师[%s]-课程[%s]-班级[%s]" % (school,teacher,course,name) printer(data, "info") break def view_class(): ''' 查看班级信息函数 :return: none ''' class_list = BasicObj.gain_data("classes") if len(class_list) != 0: printer("----------班级信息----------", "info") for obj in class_list: class_info = "校区[%s]-老师[%s]-课程[%s]-班级[%s]"%(obj.school,obj.teacher,obj.course,obj.name) printer(class_info,"info") else: printer("暂无班级信息","a") def creat_course(): ''' 创建课程函数 :return: none ''' course_list = BasicObj.gain_data("course") school_list = BasicObj.gain_data("school") while True: printer("创建课程信息", "info") school_data() school= input("请选择校区>>>") sign0 = exist_school(school,school_list) if sign0: name = input("请输入您创建的课程的名字>>>") sign1 = exist(name,course_list) if sign1: price = input("请输入您创建的课程的价格>>>") period = input("请输入您创建的课程的周期>>>") course0 = Course("course",name,price,period,school) course0.save() data = "姓名[%s]-价格[%s]-周期[%s]" % (name,price,period) printer(data, "info") break def view_course(): ''' 查看课程函数 :return: none ''' course_list = BasicObj.gain_data("course") printer("----------课程信息----------", "info") for obj in course_list: course_info = "课程名[%s]-价格[%s]-周期[%s]-校区[%s]"%(obj.name,obj.price,obj.period,obj.school) printer(course_info,"info") def show_msg(): ''' 打印管理员功能选择以及处理输入端哦选择 :return: none ''' tag = False while not tag: choice = {"2": creat_teacher, "1": view_teacher,"4": creat_class, "3": view_class,"6": creat_course, "5": view_course} msg = '''----------管理员系统---------- 1、查看老师 2、创建老师 3、查看班级 4、创建班级 5、查看课程 6、创建课程 ''' printer(msg, "info") choose = input("请输入您选择>>>").strip() if choose.isdigit() and 0 < int(choose) <= len(choice): choice[choose]() else: printer("您输入的选择有误","error") def admin_serve(): ''' 管理员系统主函数 :return: none ''' sign = Admin.login() if sign: show_msg() def school_data(): ''' 打印学校实例对象信息 :return: none ''' school_list = BasicObj.gain_data("school") for obj in school_list: printer("学校[oldboy]-校区[%s]"%obj.city,"msg") def classes_data(): ''' 打印班级实例对象信息 :return: none ''' class_list = BasicObj.gain_data("classes") for obj in class_list: printer("可选班级:%s"%(obj.name),"msg") def teacher_data(): ''' 打印老师实例对象信息 :return: none ''' teacher_list = BasicObj.gain_data("teacher") for obj in teacher_list: printer("老师[%s]-等级[%s]"%(obj.name,obj.level),"msg") def course_data(): ''' 打印课程实例对象信息 :return: none ''' course_list = BasicObj.gain_data("course") for obj in course_list: printer("课程名[%s]-价格[%s]-周期[%s]-校区[%s]"%(obj.name,obj.price,obj.period,obj.school),"msg")
func.py 逻辑主函数:
import os,sys frame = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"lib") sys.path.append(frame) from common import printer from admin import admin_serve from teacher import teacher_serve from student import student_serve from main import BasicObj,Admin def initialize(): ''' 管理员初始化函数 :return: none ''' admin_list = BasicObj.gain_data("admin") list_name = [] for obj in admin_list: list_name.append(obj.name) admin_name = input("请输入用户名>>>").strip() if admin_name in list_name: printer("您输入的用户名已存在","error") else: psd = input("请输入密码>>>").strip() admin0 = Admin("admin",admin_name,psd) printer("管理员创建成功","info") admin0.save() def sys_func(): ''' 程序的逻辑判断主函数 :return: ''' choice = {"1":admin_serve,"2":teacher_serve,"3":student_serve,"4":initialize} msg = '''----------欢迎光临选课系统---------- 1、管理员登陆 2、老师登陆 3、学生登陆 4、管理员初始化 ''' while True: printer(msg,"info") choose = input("请输入您选择>>>").strip() if choose.isdigit() and 0 < int(choose) <= len(choice): choice[choose]() else: printer("您输入的选择有误","error") # sys_func()
student.py 学生系统逻辑函数:
import os,sys,time frame = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"lib") sys.path.append(frame) from common import printer from main import Student,BasicObj,StudentScore,StudentCharge from admin import exist_parse,exist_school,school_data,teacher_data,course_data,classes_data def fuck(name): ''' 对学生是否缴费进行判断的函数 :param name: 学生名字 :return: none ''' tag = False charge_list = BasicObj.gain_data("charge") for obj in charge_list: if obj.name == name : if int(obj.charge) != 0: tag = True return tag def goto_class(): ''' 学生上课函数 :return: none ''' choose = input("是否开始上课(Y/N)>>>").strip() if choose == "y": info = '''您正在上课中。。。。。 1、上厕所 2、结束上课 ''' while True: printer(info,"info") what = input("你要干嘛>>>").strip() if what == "1": printer("上完记得冲厕所","msg") elif what == "2":break def student_msg(student): ''' 学生信息以及缴费函数 :param student: 学生名字 :return: none ''' student_list = BasicObj.gain_data("student") class_list = BasicObj.gain_data("classes") course_list = BasicObj.gain_data("course") for obj1 in student_list: if obj1.name == student: classes_info = obj1.classes for obj2 in class_list: if obj2.name == classes_info: course_info = obj2.course for obj3 in course_list: if obj3.name == course_info: printer("您的班级信息", "info") printer("校区[%s]-老师[%s]-课程[%s]" % (obj2.school, obj2.teacher, obj2.course), "info") printer("您的课程信息","info") printer("名称[%s]-价格[%s]-周期[%s]"%(obj3.name,obj3.price,obj3.period),"info") sign = fuck(student) if not sign: choose = input("您还未缴纳费用,请选项是否缴费(Y/N)>>>").strip() if choose == "y": price = input("请输入您要缴纳的费用>>>") if int(price) == int(obj3.price): printer("缴费成功","info") charge0 = StudentCharge("charge",student,obj3.price) charge0.save() goto_class() elif int(price) < int(obj3.price): printer("你输入的费用不够,缴费失败","error") elif int(price) > int(obj3.price): left_money = int(price) - int(obj3.price) printer("缴费成功,您缴纳了%s,还剩下%s"%(obj3.price,left_money),"info") charge0 = StudentCharge("charge", student, obj3.price) charge0.save() goto_class() else: printer("缴费失败", "error") else: printer("您已缴费,可以开始上课","info") goto_class() def student_regist(student): ''' 学生登录注册函数 :param student: 学生名字 :return: none ''' school_list = BasicObj.gain_data("school") class_list = BasicObj.gain_data("classes") while True: name = input("请输入您的名字>>>").strip() school_data() school = input("请选择您入学的校区>>>").strip() if exist_school(school,school_list): classes_data() classes = input("请输入您要入学的班级>>>").strip() if exist_parse(classes,class_list): psd = input("请输入您的密码>>>").strip() student0 = Student("student",school,classes,name,psd) student0.save() charge = 0 charge0 = StudentCharge("charge",name,charge) charge0.save() score = 0 student_score0 = StudentScore("score",name,score) student_score0.save() data = "校区[%s]-班级[%s]-姓名[%s]" % (school,classes,name) printer("注册成功","error") printer(data,"info") student_msg(student) break def student_serve(): ''' 学生系统主函数 :return: none ''' student_dic = {} student_list = BasicObj.gain_data("student") for obj in student_list: student_dic[obj.name] = obj.psd student = input("请输入您的姓名>>>").strip() psd = input("请输入密码>>>").strip() if student_dic.get(student) == psd: student_msg(student) else: printer("您尚未注册,请先注册","a") student_regist(student)
teacher.py 老师系统逻辑函数:
import os,sys,pickle frame = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"lib") sys.path.append(frame) from common import printer from main import Teacher,BasicObj,StudentScore from admin import exist_parse,school_data,teacher_data,course_data def check_info(data,list): ''' 老师信息在班级的处理函数 :param data: 老师的名字 :param list: 班级对象实例列表 :return: 老师的班级列表 ''' res = [] for obj in list: if obj.teacher == data: res.append(obj) return res def class_manage(teacher): ''' 班级管理函数 :param teacher: 老师的名字 :return: 信号和老师班级列表 ''' sign = False class_list = BasicObj.gain_data("classes") res = check_info(teacher,class_list) printer("班级信息","info") if len(res) != 0: for i in res: printer("校区[%s]-老师[%s]-课程[%s]-班名[%s]"%(i.school,i.teacher,i.course,i.name),"info") sign = True else: printer("您还没有自己的班级,请先创建班级","error") sign = False return sign,res def attend_class(teacher): ''' 老师选择上课班级的函数 :param teacher:老师名字 :return: none ''' sign,res = class_manage(teacher) class_name = [] if sign : while True: choose = input("请输入您要选择上课的班级名>>>").strip() for i in res: class_name.append(i.name) if choose in class_name: printer("选择上课班级成功","info") break else: printer("您输入的班级不存在","error") def view_student(teacher): ''' 查看班级学生的函数 :param teacher: 老师名字 :return: none ''' class_list = BasicObj.gain_data("classes") course_list = BasicObj.gain_data("course") student_list = BasicObj.gain_data("student") for obj1 in class_list: if obj1.teacher == teacher: class_name = obj1.name for obj2 in student_list: if obj2.classes == class_name: printer("%s学生:%s"%(obj2.classes,obj2.name),"info") def modify_score(teacher): ''' 修改学生成绩的函数 :param teacher: 老师名字 :return: none ''' class_list = BasicObj.gain_data("classes") student_list = BasicObj.gain_data("student") score_list = BasicObj.gain_data("score") stu_list = [] for obj1 in class_list: if obj1.teacher == teacher: class_name = obj1.name for obj2 in student_list: if obj2.classes == class_name: stu_list.append(obj2.name) for obj3 in score_list: if obj3.name in stu_list: printer("学生%s:%s分"%(obj3.name,obj3.score),"info") # for i in stu_list: # printer("学生%s:0分"%i,"info") st = input("请输入您要更改的学生的分数>>>") if st in stu_list: sc = input("请输入您修改的成绩(0-100)>>>>") if sc.isdigit and 0 <= int(sc) <= 100: st_score = StudentScore("score",st,int(sc)) st_score.save() printer("修改成功","info") else: printer("您输入的分数错误","error") else: printer("您输入的学生不存在","error") def teacher_msg(teacher): ''' 老师系统功能函数选项 :param teacher: 老师名字 :return: none ''' choice = {"2": attend_class, "1": class_manage, "4": modify_score, "3": view_student} msg = '''----------老师系统---------- 1、管理班级 2、选择班级 3、查看学生 4、修改成绩 5、返回 ''' tag = False while not tag: printer(msg, "info") choose = input("请输入您的功能选项>>>").strip() if choose.isdigit() and 0 < int(choose) <= len(choice): choice[choose](teacher) elif choose == "5": tag = True else: printer("您输入的选择有误", "error") def teacher_serve(): ''' 老师系统主函数 :return: none ''' teacher_list = BasicObj.gain_data("teacher") teacher_data() teacher = input("请输入您的姓名>>>").strip() sign = exist_parse(teacher,teacher_list) if sign: teacher_msg(teacher)
六、界面显示