第六周作业-选课系统
需求
选课系统
角色:学校、学员、课程、讲师
要求:
1.创建北京、上海2所学校
2.创建linux、python、go 3个课程。linux/python在北京开,go在上海开
3.课程包含:周期,价格,通过学校创建课程
4.通过学校创建班级,班级关联课程、讲师
5.创建学员时,选择学校,关联班级
5.1创建讲师角色时要关联学校
6.提供2个角色接口
6.1学员视图,可以注册,交学费,选择班级
6.2讲师视图,讲师可管理自己的班级,上课时选择班级,查看班级学员列表,修改所管理的学员的成绩
6.3管理视图,创建讲师,创建班级,创建课程
7.上面的操作产生的数据都通过pickle序列化保存到文件里
程序实现
1.逻辑图
2.主程序
2.1 modules文件夹下:
class Class(object): """班级类""" def __init__(self,class_name,course_obj): """属性:班级名,课程,所关联的学生对象""" self.class_name = class_name self.class_course = course_obj self.class_student = {} #学生对象
class Course(object): """课程类""" def __init__(self,course_name,course_price,course_period): """属性:课程名,课程价格,课程周期""" self.course_name = course_name self.course_price = course_price self.course_period = course_period
#!/usr/bin/env python # -*- coding:utf-8 -*- from modules.class_ import Class from modules.course_ import Course from modules.student_ import Student from modules.teacher_ import Teacher class School(object): """学校类""" def __init__(self,name,addr): """属性:学校名称,地址,课程,班级,讲师,学生""" self.name = name self.addr = addr self.sch_course = {} #关联的课程 self.sch_class = {} #关联的班级 self.sch_teacher = {} #关联的讲师 #self.sch_student = {} #关联的学生 def create_course(self,course_name,course_price,course_period): """1.创建课程对象 2.根据课程名称为key,课程对象为value来建立对应关系""" course_obj = Course(course_name,course_price,course_period) self.sch_course[course_name] = course_obj def show_course(self): for course in self.sch_course: course_obj = self.sch_course[course] print("课程名称\33[31;1m[%s]\33[0m\t课程价格\33[31;1m[%s]\33[0m\t课程周期\33[31;1m[%s]\33[0m个月"%(course_obj.course_name,course_obj.course_price,course_obj.course_period)) def show_class(self): for class_name in self.sch_class: class_obj = self.sch_class[class_name] print("班级名称\33[31;1m[%s]\33[0m\t关联课程\33[31;1m[%s]\33[0m\t"%(class_obj.class_name,class_obj.class_course.course_name)) def create_class(self,class_name,course_obj): class_obj = Class(class_name,course_obj) self.sch_class[class_name]= class_obj def create_teacher(self,teach_name,teach_age,teach_sex,teach_salary,class_name,class_obj): teacher_obj = Teacher(teach_name,teach_age,teach_sex,teach_salary) teacher_obj.add_teach_class(class_name,class_obj) self.sch_teacher[teach_name] = teacher_obj def show_teacher(self): for teach_name in self.sch_teacher: teacher_obj = self.sch_teacher[teach_name] class_list = [] for i in teacher_obj.teach_class: class_list.append(i) print("讲师姓名\33[31;1m[%s]\33[0m\t讲师年龄\33[31;1m[%s]\33[0m\t讲师性别\33[31;1m[%s]\33[0m\t讲师工资\33[31;1m[%s]\33[0m\t关联班级\33[31;1m[%s]\33[0m\t"%(teacher_obj.teach_name,teacher_obj.teach_age,teacher_obj.teach_sex,teacher_obj.teach_salary,class_list)) def create_student(self,name,sex,age,class_name): #1.创建学生对象 student_obj = Student(name,sex,age) # self.sch_student[name] = student_obj # 2.建立学生和班级的关联关系 class_obj = self.sch_class[class_name] self.sch_class[class_name].class_student[name] = student_obj #3.更新班级信息 def show_teacher_stud_info(self,teach_name): teach_obj = self.sch_teacher[teach_name] for t in teach_obj.teach_class: class_obj = self.sch_class[t] stud_list = [] for j in class_obj.class_student: stud_list.append(j) print("班级名称\33[31;1m[%s]\33[0m\t课程\33[31;1m[%s]\33[0m\t学生\33[31;1m[%s]\33[0m"%(class_obj.class_name,class_obj.class_course.course_name,stud_list)) def modify_student_score(self,sch_teach_name): #1.查出该讲师所对应的班级 #2.查出该班级的学生 #3.输入学生姓名,如果存在,则调用modify_score方法进行修改 #4.保存 teach_obj = self.sch_teacher[sch_teach_name] for i in teach_obj.teach_class: class_obj = self.sch_class[i] for j in class_obj.class_student: student_obj = class_obj.class_student[j] print("学生\33[31;1m[%s]\33[0m的成绩为\33[31;1m[%s]\33[0m\t" % (student_obj.name, student_obj.score)) stud_name = input("请输入要修改的学生姓名:") if stud_name in class_obj.class_student: stud_score = input("请输入要修改学生\33[31;1m[%s]\33[0m成绩:" % stud_name) class_obj.class_student[stud_name].modify_score(stud_score) print("\33[31;1m[%s]\33[0m成绩更新成功!"%stud_name) else: print("学生\33[31;1m[%s]\33[0m不存在!"%stud_name)
class Student(object): """学员类""" def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age self.score = 0 #修改分数 def modify_score(self,new_score): self.score = new_score
class Teacher(object): """讲师类""" def __init__(self,teach_name,teach_age,teach_sex,teach_salary): """属性:姓名,年龄,性别,工资,所关联的班级""" self.teach_name = teach_name self.teach_age =teach_age self.teach_sex = teach_sex self.teach_salary = teach_salary self.teach_class = {} #教授的班级,保存为字典 #讲师和班级之间建立关系 def add_teach_class(self,class_name,class_obj): self.teach_class[class_name]= class_obj
2.2 database文件夹下:
默认为空,自动生成
2.3 core文件夹下:
import os,sys,shelve from conf import settings from modules.school_ import School class Center(object): def run(self): #退出的标记 exit_flag = False menu = u""" \033[34;1m 1.学生视图 2.讲师视图 3.学校视图 q.退出\033[0m """ while not exit_flag: print(menu) user_option = input("请输入要管理的视图或按q退出:") if user_option == "1": Student_view() elif user_option == "2": Teacher_view() elif user_option == "3": School_view() elif user_option == "q": break else: print("输入的选项不正确,请重新输入!") class School_view(object): def __init__(self): if os.path.exists(settings.school_file + '.db'): self.school_file = shelve.open(settings.school_file) self.school_manager() self.school_file.close() else: print("没有学校和班级的数据,请先创建!") self.init_school() self.school_manager() self.school_file.close() def init_school(self): self.school_file = shelve.open(settings.school_file) self.school_file["北京"] = School("北京总部","中国北京") self.school_file["上海"] = School("上海分部", "中国上海") def school_manager(self): while True: for sch_name in self.school_file: print("学校名称\33[32;1m[%s]\33[0m"%sch_name) sch_option = input("请输入要管理的学校名称:").strip() if sch_option in self.school_file: self.sch_option = sch_option self.school_obj = self.school_file[sch_option] while True: menu = """ 欢迎来到老男孩\33[31;1m[%s]\33[0m校区 添加课程 add_course 添加班级 add_class 添加讲师 add_teacher 查看班级 show_class 查看课程 show_course 查看讲师 show_teacher 退出 exit """%(sch_option) print(menu) user_choice = input("请选择以上操作:").strip() if hasattr(self,user_choice): getattr(self,user_choice)() def add_course(self): #1.输入对象信息 #2.判断,有课程就提示,没有课程就调用school的方法去创建课程 course_name = input("请输入课程名称:").strip() course_price = input("请输入课程价格:").strip() course_period = input("请输入课程周期:").strip() if course_name in self.school_obj.sch_course: print("该课程已经存在!") else: self.school_obj.create_course(course_name,course_price,course_period) print("\33[31;1m[%s]\33[0m课程添加成功!"%course_name) self.school_file.update({self.sch_option:self.school_obj}) def add_class(self): #1.输入对象的信息 #2.判断,如果班级不存在且课程存在,才可以创建新的班级 #3.否则提示报错 class_name =input("请输入班级名称:").strip() class_course = input("请输入课程:").strip() if class_name not in self.school_obj.sch_class: if class_course in self.school_obj.sch_course: course_obj = self.school_obj.sch_course[class_course] self.school_obj.create_class(class_name,course_obj) self.school_file.update({self.sch_option:self.school_obj}) print("班级\33[31;1m[%s]\33[0m创建成功!"%class_name) else: print("课程不存在!") else: print("班级已经存在!") def add_teacher(self): teach_name = input("请输入讲师的姓名:") teach_age = input("请输入讲师的年龄:") teach_sex = input("请输入讲师的性别:") teach_salary = input("请输入讲师的薪水:") teach_class = input("请输入讲师教授的班级:") if teach_class in self.school_obj.sch_class: class_obj = self.school_obj.sch_class[teach_class] if teach_name not in self.school_obj.sch_teacher: self.school_obj.create_teacher(teach_name,teach_age,teach_sex,teach_salary,teach_class,class_obj) print("讲师\33[31;1m[%s]\33[0m招聘成功!"%teach_name) self.school_file.update({self.sch_option:self.school_obj}) else: print("请先创建班级!") def show_class(self): self.school_obj.show_class() def show_course(self): self.school_obj.show_course() def show_teacher(self): self.school_obj.show_teacher() def exit(self): self.school_file.close() sys.exit("感谢您使用本学校管理系统!") class Teacher_view(object): def __init__(self): if os.path.exists(settings.school_file+ '.db'): self.school_file = shelve.open(settings.school_file) self.teacher_manager() self.school_file.close() else: print("没有学校和班级的数据,请先创建学校") exit() def teacher_manager(self): while True: for sch_name in self.school_file: print("学校名称\33[31;1m[%s]\33[0m"%sch_name) sch_option = input("请输入要管理的学校名称:").strip() if sch_option in self.school_file: self.sch_option = sch_option self.school_obj = self.school_file[sch_option] sch_teach_name = input("请输入讲师姓名:").strip() if sch_teach_name in self.school_obj.sch_teacher: while True: menu = """ 欢迎讲师\33[32;1m[%s]\33[0m来到老男孩\33[33;1m[%s]\33[0m校区 查看学生信息 show_stud 修改学生成绩 modi_stud_scr 退出 exit """%(sch_teach_name,sch_option) print(menu) user_choice = input("请选择以上操作:").strip() if hasattr(self,user_choice): getattr(self,user_choice)(sch_teach_name) else: print("讲师不存在!") def show_stud(self,sch_teach_name): self.school_obj.show_teacher_stud_info(sch_teach_name) def modi_stud_scr(self,sch_teach_name): self.school_obj.modify_student_score(sch_teach_name) self.school_file.update({self.sch_option: self.school_obj}) def exit(self,sch_teach_name): sys.exit("感谢您使用本讲师管理系统!") class Student_view(object): def __init__(self): if os.path.exists(settings.school_file + '.db'): self.school_file = shelve.open(settings.school_file) self.student_manager() self.school_file.close() else: print("没有学校和班级的数据,请先创建学校") exit() def student_manager(self): # 1.判断数据文件是否存在 # 2.输入学校,班级,课程 # 3.验证输入的是否存在 # 4.存在,更新文件,完成学生的注册 while True: for sch_name in self.school_file: print("学校名称\33[31;1m[%s]\33[0m" % sch_name) sch_option = input("请输入要注册的学校名称:").strip() if sch_option in self.school_file: self.sch_option = sch_option self.school_obj = self.school_file[sch_option] sch_stud_name = input("请输入要注册的学生姓名:").strip() sch_stud_sex = input("请输入要注册的学生性别:").strip() sch_stud_age = input("请输入要注册的学生年龄:").strip() self.school_obj.show_class() for i in self.school_obj.sch_class: if sch_stud_name not in self.school_obj.sch_class[i].class_student: sch_stud_class = input("请输入要您要学习的班级:").strip() if sch_stud_class in self.school_obj.sch_class: self.school_obj.create_student(sch_stud_name,sch_stud_sex,sch_stud_age,sch_stud_class) self.school_file.update({self.sch_option: self.school_obj}) print("学生注册成功!") while True: menu = """ 欢迎学生\33[31;1m[%s]\33[0m来到老男孩\33[31;1m[%s]\33[0m校区 退出 exit """ % (sch_stud_name, sch_option) print(menu) user_choice = input("请选择以上操作:").strip() if hasattr(self, user_choice): getattr(self, user_choice)() else: print("班级不存在,请重新输入!") else: print("学生已经存在!") else: print("学校不存在!") def exit(self): sys.exit("感谢您使用本学生管理系统!")
2.4 conf文件夹下:
import os,sys,platform if platform.system() == "Windows": BASE_DIR = "\\".join(os.path.dirname(os.path.abspath(__file__)).split("\\")[:-1]) database_path = os.path.join(BASE_DIR,"database") else: BASE_DIR = "/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1]) database_path = os.path.join(BASE_DIR, "database") school_file = os.path.join(database_path,"school")
2.5 bin文件夹下:
#!/usr/bin/env python # -*- coding:utf-8 -*- import os,sys,platform if platform.system() == "Windows": BASE_DIR = "\\".join(os.path.dirname(os.path.abspath(__file__)).split("\\")[:-1]) else: BASE_DIR = "/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1]) sys.path.insert(0,BASE_DIR) from core import main if __name__ == '__main__': obj = main.Center() obj.run()
2.6选课系统文件夹下:
博客地址:http://www.cnblogs.com/Mr-hu/ 程序运行步骤: 北京校区:课程(python:alex和linux:oldboy) 上海校区:课程go:peiqi 根据菜单进行选择增删改查功能。 本程序介绍: 1.学生视图:可以注册学生,选择上课班级 2.讲师视图:可以查看学生信息,修改学生成绩 3.学校管理视图:可以添加课程,添加班级,添加讲师,查看班级,查看课程,查看讲师
3.程序运行
3.1学生视图:
3.2讲师视图:
3.3学校视图: