选课系统
1,文件夹解析:
2,内容:
2.1
import os # BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = "\\".join(os.path.abspath(os.path.dirname(__file__)).split("\\")[:-1]) database_path = os.path.join(BASE_DIR,"db") school_db_file = os.path.join(database_path,"school") from core import main if __name__ == '__main__': obj = main.Manage_center() obj.run()
2.2:前面strat,py 中写了 可以省略这一步
import os BASE_DIR = "\\".join(os.path.abspath(os.path.dirname(__file__)).split("\\")[:-1]) database_path = os.path.join(BASE_DIR,"db") school_db_file = os.path.join(database_path,"school")
2.3
import os import sys import shelve from conf import settings from modules.school import School '''装饰器登录验证''' auth_status = {'user': None,'status': False} def wrapper(func): def inner(*args, **kwargs): count = 1 while count <= 3: if auth_status['status']: ret = func(*args, **kwargs) # index/home ret None return ret else: # 获取用户名和密码 name = input('请输入你的用户名:').strip() pwd = input('请输入您的密码:').strip() # with open(text_f, 'r', encoding='utf-8') as f: # dic = eval(f.read()) # 读入文件 # if name in dic and pwd == dic[name]: # 判断输入的用户名和密码是否读入的文件中包含 if name=='fugui' and pwd=='123': print('登录成功!') auth_status['user'] = name auth_status['status'] = True ret = func(*args, **kwargs) return ret else: print('登录失败!') count += 1 return inner class Manage_center: # def __init__(self): # pass # @wrapper def run(self): while True: print("\n\033[34;0m欢迎进入FG课程系统\033[0m\n" "1 学生视图\n" "2 教师视图\n" "3 学校管理视图\n" "4 退出学员管理系统\n") user_choice = input("\033[34;0m请输入您要登录的视图:\033[0m") if user_choice == '1': Manage_student() elif user_choice == '2': Manage_teacher() elif user_choice == '3': Manage_school() elif user_choice == '4': print("\033[34;1m感谢使用学员管理系统,退出\033[0m") break else: print("\033[31;1m请输入正确的选项\033[0m") class Manage_school(object): '''学校管理视图''' def __init__(self): if os.path.exists(settings.school_db_file+".dat"): #shelve会生成三个文件,其中有.dat结尾 self.school_db = shelve.open(settings.school_db_file) #打开学校数据库文件 self.run_manage() #运行管理视图 self.school_db.close() #关闭数据库文件 else: print("\33[31;1m系统信息:初始化数据库\33[0m") self.initialize_school() #初始化数据库 self.run_manage() self.school_db.close() def initialize_school(self): '''实例化两个学校北京/广州''' self.school_db = shelve.open(settings.school_db_file) self.school_db['北京oldboyedu'] = School('北京oldboyedu', '中国.北京') self.school_db['广州老男孩教育'] = School('广州老男孩教育', '中国.广州') def run_manage(self): '''运行学校管理视图 ''' while True: for key in self.school_db: print("学校名称:",key) choice_school = input("\33[34;0m输入选择管理的学校名:\33[0m").strip() if choice_school in self.school_db: self.choice_school = choice_school self.school_obj = self.school_db[choice_school] while True: print("\n欢迎来到老男孩%s校区\n" "添加课程 add_course\n" "增加班级 add_class\n" "招聘讲师 add_teacher\n" "查看课程 check_course\n" "查看班级 check_class\n" "查看讲师 check_teacher\n" "退出程序 exit"% self.school_obj.school_name) user_func = input('''\033[34;0m输入要操作的命令:\033[0m''').strip() if hasattr(self,user_func): getattr(self,user_func)() else: print("\33[31;1m输入错误:请输入正确的学校名\33[0m") def add_course(self): course_name = input('''\033[34;0m输入要添加课程的名称:\033[0m''').strip() course_price = input('''\033[34;0m输入要添加课程的价格:\033[0m''').strip() course_time = input('''\033[34;0m输入要添加课程的时长:\033[0m''').strip() if course_name in self.school_obj.school_course: #判断课程是否已经添加过 print("\33[32;1m课程存在\33[0m") self.school_obj.create_course(course_name, course_price, course_time) print("\33[32;1m课程更新完成\33[0m") else: self.school_obj.create_course(course_name,course_price,course_time) print("\33[32;1m课程添加成功\33[0m") self.school_db.update({self.choice_school: self.school_obj}) #更新数据库数据 def add_class(self): class_name = input('''\033[34;0m输入要添加班级的名称:\033[0m''').strip() course_name = input('''\033[34;0m输入要关联的课程:\033[0m''').strip() if class_name not in self.school_obj.school_class: if course_name in self.school_obj.school_course: course_obj = self.school_obj.school_course[course_name] self.school_obj.create_class(class_name,course_obj) self.school_db.update({self.choice_school: self.school_obj}) # 更新数据库数据 print("\33[32;1m班级创建成功\33[0m") else: print("\33[31;1m系统错误:关联的课程不存在\33[0m") else: print("\33[31;1m系统错误:班级已经存在\33[0m") def add_teacher(self): teacher_name = input('''\033[34;0m输入要招聘教师的名称:\033[0m''').strip() teacher_salary = input('''\033[34;0m输入教师的薪资:\033[0m''').strip() teacher_class = input('''\033[34;0m输入要关联的班级:\033[0m''').strip() if teacher_class in self.school_obj.school_class: #判断班级是否存在 class_obj = self.school_obj.school_class[teacher_class] #获取班级名对应的实例 if teacher_name not in self.school_obj.school_teacher: #判断招聘教师是否存在,不存在创建,存在更新 self.school_obj.create_teacher(teacher_name,teacher_salary,teacher_class,class_obj) print("\33[32;1m新讲师招聘成功\33[0m") else: self.school_obj.update_teacher(teacher_name, teacher_class, class_obj) print("\33[32;1m讲师已经存在,信息更新完成\33[0m") self.school_db.update({self.choice_school: self.school_obj}) # 更新数据库数据 else: print("\33[31;1m系统错误:关联的班级不存在\33[0m") def check_course(self): self.school_obj.show_course() def check_class(self): self.school_obj.show_class() def check_teacher(self): self.school_obj.show_teacher() def exit(self): self.school_db.close() sys.exit("\033[32;1m欢迎下次使用学员管理系统\033[0m") class Manage_student(object): '''学生视图''' def __init__(self): if os.path.exists(settings.school_db_file + ".dat"): # shelve会生成三个文件,其中有.dat结尾 self.school_db = shelve.open(settings.school_db_file) # 打开学校数据库文件 self.run_manage() # 运行管理视图 self.school_db.close() # 关闭数据库文件 else: print("\033[31;1m数据库文件不存在,请先创建学校\033[0m") exit() def run_manage(self): print("\n欢迎进入学员视图") for key in self.school_db: print("学校名称:", key) choice_school = input("\33[34;0m输入选择注册的学校名:\33[0m").strip() if choice_school in self.school_db: self.choice_school = choice_school self.school_obj = self.school_db[choice_school] student_name = input('''\033[34;0m输入学生的姓名:\033[0m''').strip() student_age = input('''\033[34;0m输入学生的年龄:\033[0m''').strip() self.school_obj.show_class_course() class_choice = input('''\033[34;0m输入上课的班级:\033[0m''').strip() if class_choice in self.school_obj.school_class: self.school_obj.create_student(student_name,student_age,class_choice) self.school_db.update({self.choice_school: self.school_obj}) # 更新数据库数据 print("\33[32;1m学生注册成功\33[0m") else: print("\33[31;1m系统错误:输入的班级不存在\33[0m") else: print("\33[31;1m系统错误:输入的学校不存在\33[0m") class Manage_teacher(object): '''教师视图''' def __init__(self): if os.path.exists(settings.school_db_file + ".dat"): # shelve会生成三个文件,其中有.dat结尾 self.school_db = shelve.open(settings.school_db_file) # 打开学校数据库文件 self.run_manage() # 运行管理视图 self.school_db.close() # 关闭数据库文件 else: print("\033[31;1m数据库文件不存在,请先创建学校\033[0m") exit() def run_manage(self): for key in self.school_db: print("学校名称:", key) choice_school = input("\33[34;0m输入选择学校名:\33[0m").strip() if choice_school in self.school_db: self.choice_school = choice_school self.school_obj = self.school_db[choice_school] teacher_name = input('''\033[34;0m输入登录讲师的姓名:\033[0m''').strip() while True: if teacher_name in self.school_obj.school_teacher: print("\n欢迎来到教师中心\n" "查看班级 check_class\n" "退出程序 exit" ) user_func = input('''\033[34;0m输入要操作的命令:\033[0m''').strip() if hasattr(self, user_func): getattr(self, user_func)(teacher_name) else: print("\033[31;1m讲师不存在\033[0m") def check_class(self,teacher_name): self.school_obj.show_teacher_classinfo(teacher_name) def exit(self): self.school_db.close() sys.exit("\033[32;1m欢迎下次使用学员管理系统\033[0m") #有了这段代码可以不要strat.py启动程序,所有两者只要一个 #obj =Manage_center() #obj.run()
2.4 db文件不用实现配置,运行程序自动添加
2.5在modules中的五个角色文件
class Class(object): '''班级类,包含名称,课程,学生''' def __init__(self,class_name,course_obj): self.class_name = class_name self.class_courese = course_obj self.class_student = {}
class Course(): '''定义课程类,包含名称,价格,周期''' def __init__(self,course_name,course_price,course_time): self.course_name = course_name self.course_price = course_price self.course_time = course_time
from modules.course import Course from modules.classs import Class from modules.teacher import Teacher from modules.student import Student class School(object): '''学校类,包含名称,地址,课程,班级,教师''' def __init__(self,school_name,school_addr): self.school_name = school_name self.school_addr = school_addr self.school_course = {} #学校所有的课程实例 self.school_class = {} self.school_teacher = {} #self.school_student = {} def create_course(self,course_name,course_price,course_time): '''创建课程''' course_obj = Course(course_name,course_price,course_time) self.school_course[course_name] = course_obj def show_course(self): '''查看课程信息''' for key in self.school_course: course_obj = self.school_course[key] print("\33[32;1m课程:%s\t价格:%s\t周期:%s月\33[0m"%(course_obj.course_name,course_obj.course_price, course_obj.course_time,)) def create_class(self,class_name,courese_obj): '''创建班级''' class_obj = Class(class_name,courese_obj) self.school_class[class_name] = class_obj def show_class(self): for key in self.school_class: class_obj = self.school_class[key] print("\33[32;1m班级:%s\t关联课程:%s\33[0m" % (class_obj.class_name, class_obj.class_courese.course_name)) def show_class_course(self): for key in self.school_class: class_obj = self.school_class[key] course_obj = class_obj.class_courese print("\33[32;1m班级:%s\t关联课程:%s\t价格:%s\t周期:%s月\33[0m" % (class_obj.class_name, course_obj.course_name, course_obj.course_price,course_obj.course_time)) def create_teacher(self,teacher_name, teacher_salary,class_name,class_obj): '''创建讲师''' teacher_obj = Teacher(teacher_name, teacher_salary) teacher_obj.teacher_add_class(class_name,class_obj) self.school_teacher[teacher_name] = teacher_obj def update_teacher(self,teacher_name,class_name,class_obj): '''更新教师信息''' teacher_obj = self.school_teacher[teacher_name] teacher_obj.teacher_add_class(class_name,class_obj) def show_teacher(self): '''查看讲师信息''' for key in self.school_teacher: teacher_obj = self.school_teacher[key] class_list = [] for i in teacher_obj.teacher_calss: class_list.append(i) print("\33[32;1m讲师:%s\t薪资:%s\t关联班级:%s\33[0m" % (teacher_obj.teacher_name, teacher_obj.teacher_salary, class_list )) def create_student(self,student_name,student_age,class_choice): '''注册学生''' student_obj = Student(student_name,student_age) #生成学生实例 class_obj = self.school_class[class_choice] #获取学生所注册班级的实例对象 class_obj.class_student[student_name]=student_obj #班级实例里添加学生信息 self.school_class[class_choice] = class_obj #学校班级字典更新 def show_teacher_classinfo(self,teacher_name): teacher_obj = self.school_teacher[teacher_name] for i in teacher_obj.teacher_calss: class_obj = self.school_class[i] student_list = [] for k in class_obj.class_student: student_list.append(k) print("\33[32;1m班级:%s\t关联课程:%s\t学员:%s\33[0m" % (class_obj.class_name, class_obj.class_courese.course_name, student_list))
class Student(object): '''学生类,包含姓名,年龄''' def __init__(self,student_name,student_age): self.student_name = student_name self.student_age = student_age
class Teacher(object): '''讲师类,定义teacher_name,teacher_salary,包含teacher_class''' def __init__(self, teacher_name, teacher_salary): self.teacher_name = teacher_name self.teacher_salary = teacher_salary self.teacher_calss = [] #班级列表 [s14,15] def teacher_add_class(self,class_name,class_obj): self.teacher_calss[class_name] = class_obj
转载请注来源:https://github.com/fugui0310