zoukankan      html  css  js  c++  java
  • 选课系统

    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()
    bin中strat.py

     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")
    conf中settings.py

    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("
    33[34;0m欢迎进入FG课程系统33[0m
    "
                      "1 学生视图
    "
                      "2 教师视图
    "
                      "3 学校管理视图
    "
                      "4 退出学员管理系统
    ")
                user_choice = input("33[34;0m请输入您要登录的视图:33[0m")
                if user_choice == '1':
                    Manage_student()
                elif user_choice == '2':
                    Manage_teacher()
                elif user_choice == '3':
                    Manage_school()
                elif user_choice == '4':
                    print("33[34;1m感谢使用学员管理系统,退出33[0m")
                    break
                else:
                    print("33[31;1m请输入正确的选项33[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("
    欢迎来到老男孩%s校区
    "
                              "添加课程 add_course
    "
                              "增加班级 add_class
    "
                              "招聘讲师 add_teacher
    "
                              "查看课程 check_course
    "
                              "查看班级 check_class
    "
                              "查看讲师 check_teacher
    "
                              "退出程序 exit"% self.school_obj.school_name)
                        user_func = input('''33[34;0m输入要操作的命令:33[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('''33[34;0m输入要添加课程的名称:33[0m''').strip()
            course_price = input('''33[34;0m输入要添加课程的价格:33[0m''').strip()
            course_time = input('''33[34;0m输入要添加课程的时长:33[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('''33[34;0m输入要添加班级的名称:33[0m''').strip()
            course_name = input('''33[34;0m输入要关联的课程:33[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('''33[34;0m输入要招聘教师的名称:33[0m''').strip()
            teacher_salary = input('''33[34;0m输入教师的薪资:33[0m''').strip()
            teacher_class = input('''33[34;0m输入要关联的班级:33[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("33[32;1m欢迎下次使用学员管理系统33[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("33[31;1m数据库文件不存在,请先创建学校33[0m")
                exit()
    
        def run_manage(self):
            print("
    欢迎进入学员视图")
            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('''33[34;0m输入学生的姓名:33[0m''').strip()
                student_age = input('''33[34;0m输入学生的年龄:33[0m''').strip()
                self.school_obj.show_class_course()
                class_choice = input('''33[34;0m输入上课的班级:33[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("33[31;1m数据库文件不存在,请先创建学校33[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('''33[34;0m输入登录讲师的姓名:33[0m''').strip()
                while True:
                    if teacher_name in self.school_obj.school_teacher:
                        print("
    欢迎来到教师中心
    "
                              "查看班级 check_class
    "
                              "退出程序 exit" )
                        user_func = input('''33[34;0m输入要操作的命令:33[0m''').strip()
                        if hasattr(self, user_func):
                            getattr(self, user_func)(teacher_name)
                    else:
                        print("33[31;1m讲师不存在33[0m")
    
        def check_class(self,teacher_name):
            self.school_obj.show_teacher_classinfo(teacher_name)
    
        def exit(self):
            self.school_db.close()
            sys.exit("33[32;1m欢迎下次使用学员管理系统33[0m")
    
    
    #有了这段代码可以不要strat.py启动程序,所有两者只要一个
    #obj =Manage_center()
    #obj.run()
    core中main.py

    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.py 
    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
    course.py
    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	价格:%s	周期:%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	关联课程:%s33[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	关联课程:%s	价格:%s	周期:%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	薪资:%s	关联班级:%s33[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	关联课程:%s	学员:%s33[0m" % (class_obj.class_name, class_obj.class_courese.course_name,  student_list))
    school.py
    class Student(object):
        '''学生类,包含姓名,年龄'''
        def __init__(self,student_name,student_age):
            self.student_name = student_name
            self.student_age = student_age
    student.py
    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
    teacher.py
  • 相关阅读:
    Android 11 的5个新安全和隐私功能,不更新会面临被黑客入侵风险
    SSL / TLS新加密漏洞,黑客是这样读取敏感数据,看看修复方法!
    蓝牙新漏洞,黑客可以轻松控制你的手机,全球数亿台设备受影响
    Visa信用卡漏洞,新的PIN验证黑客可绕过,并进行非接触式付款
    思科是如何遭黑客入侵?原因:XSS缺陷到RCE缺陷
    WhatsApp现神秘漏洞,黑客可远程查看聊天记录
    郭盛华:免受网络黑客攻击的5个技巧
    Apache服务器现3个致命漏洞,黑客可远程监听
    centos 下安装python3 的MySQLdb
    MySQL创建用户与授权及删除
  • 原文地址:https://www.cnblogs.com/yifugui/p/7390663.html
Copyright © 2011-2022 走看看