• 选课系统


    readme

    本周作业:综合应用面向对象

    角色:学校、学员、课程、讲师
    要求:
    1. 创建北京、上海 2 所学校
    2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
    3. 课程包含,周期,价格,通过学校创建课程
    4. 通过学校创建班级, 班级关联课程、讲师
    5. 创建学员时,选择学校,关联班级
    5. 创建讲师角色时要关联学校,
    6. 提供两个角色接口
    6.1 学员视图, 可以注册, 交学费, 选择班级,
    6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
    6.3 管理视图,创建讲师, 创建班级,创建课程

    7. 上面的操作产生的数据都通过pickle序列化保存到文件里

    1、需求分析
    - 管理员功能
    - 管理员注册
    - 管理员登录
    - 创建学校
    - 创建班级(先选择校区)
    - 班级名称
    - 班级绑定的校区
    - 创建课程(课程绑定给班级,先选择班级)
    - 课程周期
    - 课程价格
    - 创建老师
    - 老师名称
    - 老师密码
    - 学生的功能
    - 注册
    - 登录
    - 交学费
    - 选择校区
    - 选择课程
    - 查看分数
    - 老师的功能
    - 登录
    - 查看授课课程
    - 选择授课课程
    - 查看课程下学生
    - 修改学生分数



    2、程序的架构设计
    - ATM + 购物车
    - 三层架构
    - 视图层: 与用户交互的
    - 接口层: 核心的业务逻辑
    - 数据层: 处理数据的,增删改查
    - json格式的数据
    - {"key":"value"}类型数据
    - Python
    - 字典类型: ---> {'key': "value"}

    - 选课系统
    - 三层架构
    - 视图层: 与用户交互的
    - 接口层: 核心的业务逻辑
    - 数据层: 处理数据的,增删改查
    - pickle格式的数据
    - bytes类型数据

    - Python
    - 对象: ---> object

    settngs

    import os

    BASE_PATH = os.path.dirname(
    os.path.dirname(__file__)
    )

    DB_PATH = os.path.join(
    BASE_PATH, 'db'
    )

    ADMIN_PATH = os.path.join(
    BASE_PATH, 'db','Admin'
    )

    CLASSES_PATH = os.path.join(
    BASE_PATH, 'db','Classes'
    )

    COURSE_PATH = os.path.join(
    BASE_PATH, 'db','Course'
    )

    TEACHER_PATH = os.path.join(
    BASE_PATH, 'db','Teacher'
    )

    SCHOOL_PATH = os.path.join(
    BASE_PATH, 'db','School'
    )

    STUDENT_PATH = os.path.join(
    BASE_PATH, 'db','Student'
    )
    admin
    from interface import admin_interface
    from lib import common
    Login_admin_user=None
    # 注册
    def register():
    while True:
    username = input('请输入用户名: ').strip()
    password = input('请输入密码: ').strip()
    re_password = input('请确认密码: ').strip()
    if password == re_password:
    # 调用接口,进行注册
    flag, msg = admin_interface.register_interface(
    username, password
    )
    if flag:
    print(msg)
    break
    else:
    print(msg)
    else:
    print('两次密码不一致!')

    # 登录
    def login():
    while True:
    username = input('请输入用户名: ').strip()
    password = input('请输入密码: ').strip()
    flag, msg=admin_interface.login_interface(username, password)
    if flag:
    print(msg)
    global Login_admin_user
    Login_admin_user = username
    break
    else:
    print(msg)

    # 创建学校
    @common.auth_admin
    def create_school():
    while True:
    school_name = input('请输入学校名称: ').strip()
    school_address = input('请输入学校地址: ').strip()
    flag, msg=admin_interface.create_school_intreface(school_name,school_address)
    if flag:
    print(msg)
    break
    else:
    print(msg)



    # 创建班级
    @common.auth_admin
    def create_classes():
    while True:
    class_name = input('请输入班级名称: ').strip()
    flag, msg = admin_interface.create_classes_interface(class_name,)
    if flag:
    print(msg)
    break
    else:
    print(msg)


    # 创建课程
    @common.auth_admin
    def create_course():
    while True:
    course_name = input('请输入课程名称: ').strip()
    course_cycle = input('请输入课程周期: ').strip()
    course_price = input('请输入课程价格: ').strip()
    flag, msg =admin_interface.create_course_interface(course_name,course_cycle,course_price)
    if flag:
    print(msg)
    break
    else:
    print(msg)

    # 创建老师
    @common.auth_admin
    def create_teacher():
    while True:
    teacher_name = input('请输入教师姓名: ').strip()
    password = input('请输入密码: ').strip()
    teacher_age = input('请输入教师年龄: ').strip()
    teacher_salary = input('请输入教师薪资: ').strip()
    teacher_level = input('请输入教师等级: ').strip()
    flag, msg = admin_interface.create_teacher_interface(teacher_name,password,teacher_age,teacher_salary,teacher_level)
    if flag:
    print(msg)
    break
    else:
    print(msg)

    func_dic = {
    '1': register,
    '2': login,
    '3': create_school,
    '4': create_classes,
    '5': create_course,
    '6': create_teacher
    }


    # 管理员视图
    def admin_view():
    '''
    - 管理员功能
    - 管理员注册
    - 管理员登录
    - 创建学校
    - 创建班级(先选择校区)
    - 班级名称
    - 班级绑定的校区
    - 创建课程(课程绑定给班级,先选择班级)
    - 课程周期
    - 课程价格
    - 创建老师
    - 老师名称
    - 老师密码
    '''
    while True:
    print('''
    1.管理员注册
    2.管理员登录
    3.创建学校
    4.创建班级
    5.创建课程
    6.创建老师
    ''')

    choice = input('请选择功能编号: ').strip()
    if choice == 'q':
    break

    if choice not in func_dic:
    continue

    func_dic.get(choice)()
    src
    from core import admin, teacher, student

    func_dic = {
    '1': admin.admin_view,
    '2': student.student_view,
    '3': teacher.teacher_view,
    }


    # 主视图
    def run():
    while True:
    print('''
    1.管理员功能
    2.学生功能
    3.老师功能
    ''')

    choice = input('请选择功能编号: ').strip()
    if choice == 'q':
    break
    if choice not in func_dic:
    continue

    func_dic.get(choice)()
    student
    from interface import student_interface
    from lib import common
    from db import db_handler
    Login_student_user=None
    def register():
    while True:
    username = input('请输入用户名: ').strip()
    password = input('请输入密码: ').strip()
    re_password = input('请确认密码: ').strip()
    if password == re_password:
    # 调用接口,进行注册
    flag, msg = student_interface.register_interface(
    username, password
    )
    if flag:
    print(msg)
    break
    else:
    print(msg)
    else:
    print('两次密码不一致!')
    def login():
    while True:
    username = input('请输入用户名: ').strip()
    password = input('请输入密码: ').strip()
    flag, msg = student_interface.login_interface(username, password)
    if flag:
    print(msg)
    global Login_student_user
    Login_student_user = username
    break
    else:
    print(msg)

    @common.student_admin
    def pay_tuition():
    while True:
    course_list = db_handler.show_courselist()
    course_name = input('请输入您要缴费的课程:').strip()
    if course_name not in course_list:
    continue
    cost, msg = student_interface.course_price(course_name)
    print(msg)
    money=input('请输入金额:').strip()
    if float(money) >= float(cost):
    flag,msg= student_interface.pay_interface(Login_student_user)
    if flag:
    print(msg)
    break
    else:
    print(msg)
    else:
    print('金额不足!')

    @common.student_admin
    def change_school():
    school_list=student_interface.change_school_interface()
    print(school_list)
    while True:
    choice = input('请输入您要选择的学校编号(从0开始):').strip()
    choice = int(choice)
    if choice not in range(len(school_list)):
    continue
    else:
    schoolname=school_list[choice]
    student_obj=student_interface.add_school_student(Login_student_user)
    student_obj.school=schoolname
    student_obj.save()
    break

    @common.student_admin
    def change_course():
    course_list,user_obj = student_interface.change_course_interface(Login_student_user)
    print(course_list)
    while True:
    choice = input('请输入您要选择的课程编号(从0开始):').strip()
    choice = int(choice)
    if choice not in range(len(course_list)):
    continue
    else:
    coursename=course_list[choice]
    user_obj.course.update({coursename:0})
    user_obj.save()
    course_obj=student_interface.add_course_student(coursename)
    if Login_student_user in course_obj.student:
    print('当前学生已选择过该课程')
    break
    else:
    course_obj.student.append(Login_student_user)
    course_obj.save()
    break

    @common.student_admin
    def check_fraction():
    course_list=student_interface.check_fraction_interface(Login_student_user)
    print(course_list)
    func_dic = {
    '1': register,
    '2': login,
    '3': pay_tuition,
    '4': change_school,
    '5': change_course,
    '6': check_fraction
    }
    # 学生视图
    def student_view():
    '''
    - 学生的功能
    - 注册
    - 登录
    - 交学费
    - 选择校区
    - 选择课程
    - 查看分数
    '''
    while True:
    print('''
    1.注册
    2.登录
    3.交学费
    4.选择校区
    5.选择课程
    6.查看分数
    ''')

    choice = input('请选择功能编号: ').strip()
    if choice == 'q':
    break

    if choice not in func_dic:
    continue

    func_dic.get(choice)()
    teacher
    from interface import teacher_interface
    from lib import common
    Login_teacher_user = None
    def login():
    while True:
    username = input('请输入用户名: ').strip()
    password = input('请输入密码: ').strip()
    flag, msg = teacher_interface.login_interface(username, password)
    if flag:
    print(msg)
    global Login_teacher_user
    Login_teacher_user = username
    break
    else:
    print(msg)
    @common.teacher_admin
    def check_course():
    course=teacher_interface.check_course_interface()
    print(course)

    @common.teacher_admin
    def change_course():
    course_list,user_obj = teacher_interface.change_course_interface(Login_teacher_user)
    print(f'课程列表为:{course_list}')
    print(f'当前已选课程为:{user_obj.course}')
    while True:
    choice = input('请输入您要教授的课程编号(从0开始):').strip()
    choice = int(choice)
    if choice not in range(len(course_list)):
    continue
    else:
    if course_list[choice] in user_obj.course:
    print('当前课程已经选择,请选择其他课程')
    break
    else:
    user_obj.course.append(course_list[choice])
    user_obj.save()
    print('选课成功!')
    choice=input('继续选课则输入任意字符,输入q则退出:')
    if choice == 'q':
    break
    else:
    continue

    @common.teacher_admin
    def check_student():
    course_name = input('请输入您要查看的课程:').strip()
    student_list= teacher_interface.check_student_interface(course_name)
    print(student_list)

    @common.teacher_admin
    def change_fraction():
    course_name = input('请输入需要修改的课程:').strip()
    student_name=input('请输入需要修改的学生:').strip()
    stu_obj,fraction=teacher_interface.change_fraction_interface(course_name,student_name)
    while True:
    r_fraction=input('请输入修改后的分数:').strip()
    if r_fraction.isdigit():
    stu_obj.course[course_name] = r_fraction
    stu_obj.save()
    break
    else:
    print('请重新输入:')
    continue

    func_dic = {
    '1': login,
    '2': check_course,
    '3': change_course,
    '4': check_student,
    '5': change_fraction
    }
    # 教师视图
    def teacher_view():
    '''
    - 老师的功能
    - 登录
    - 查看授课课程
    - 选择授课课程
    - 查看课程下学生
    - 修改学生分数
    '''
    while True:
    print('''
    1.登录
    2.查看授课课程
    3.选择授课课程
    4.查看课程下学生
    5.修改学生分数
    ''')

    choice = input('请选择功能编号: ').strip()
    if choice == 'q':
    break

    if choice not in func_dic:
    continue

    func_dic.get(choice)()
    db_handler
    import os, pickle
    from conf import settings

    # 保存数据
    def save(obj):
    user_dir = os.path.join(
    settings.DB_PATH, obj.__class__.__name__,
    )
    if not os.path.isdir(user_dir):
    os.mkdir(user_dir)

    # 拼接文件 路径
    user_path = os.path.join(
    user_dir, obj.user
    )

    # 保存对象数据
    with open(user_path, 'wb') as f:
    pickle.dump(obj, f)


    # 查看数据
    def select(cls, username):
    # 拼接保存数据的文件夹路径
    user_path = os.path.join(
    settings.DB_PATH, cls.__name__,username
    )

    # 判断如果不存在 路径,直接不返回
    if os.path.exists(user_path):
    # print(user_path)
    # 保存对象数据
    with open(user_path, 'rb') as f:
    obj = pickle.load(f)
    return obj

    def show_courselist():
    course_dir = settings.COURSE_PATH
    if not course_dir:
    return None
    else:
    course_list = []
    for course in os.listdir(course_dir):
    course_list.append(course)
    return course_list

    def show_schoollist():
    school_dir = settings.SCHOOL_PATH
    if not school_dir:
    return None
    else:
    school_list = []
    for school in os.listdir(school_dir):
    school_list.append(school)
    return school_list
    models
    '''
    用于存放类的
    '''
    from db import db_handler

    class Base:
    def save(self):
    db_handler.save(self)

    @classmethod
    def select(cls, user):
    # 传递类名与用户名给db_handler里的select
    obj = db_handler.select(cls, user)
    return obj

    class Admin(Base):

    def __init__(self, user, pwd):
    self.user = user
    self.pwd = pwd
    # 调用类,即保存数据
    self.save()

    class Student(Base):

    def __init__(self, user, pwd):
    self.user = user
    self.pwd = pwd
    self.locked = False
    self.school = None
    self.course = {}
    self.save()

    class School(Base):

    def __init__(self, school_name,school_address):
    self.user = school_name
    self.address = school_address
    self.classes_list = []
    self.save()

    def create_classes(self, classes_name):
    self.classes_list.append(classes_name)
    cls_obj = Classes(classes_name)
    return cls_obj

    class Classes(Base):

    def __init__(self, class_name):
    self.user = class_name
    self.course_list = []
    self.save()

    def create_course(self, course_name, course_cycle,course_price):
    self.course_list.append(course_name)
    course_obj = Course(course_name, course_cycle,course_price)
    return course_obj

    class Course(Base):
    def __init__(self, course_name,course_cycle,course_price):
    self.user = course_name
    self.cycle = course_cycle
    self.price = course_price
    self.student = []
    self.save()

    class Teacher(Base):

    def __init__(self, teacher_name,password,teacher_age,teacher_salary,teacher_level):
    self.user = teacher_name
    self.pwd = password
    self.age = teacher_age
    self.salary = teacher_salary
    self.level = teacher_level
    self.course = []
    self.save()
    admin_interface
    '''
    管理员接口
    '''
    from db import models

    def register_interface(user, pwd):
    user_obj = models.Admin.select(user)

    # 用户存在
    if user_obj:
    return False, '用户已存在'

    # 2) 不存在,在调用类创建对象,并保存数据
    # 通过models来调用数据层
    models.Admin(user, pwd)

    # 3.返回注册成功
    return True, '注册成功'

    def login_interface(user, pwd):
    user_obj = models.Admin.select(user)
    if not user_obj:
    return False, '用户不存在'
    else:
    if pwd == user_obj.pwd:
    return True, '登录成功!'
    return False, '密码错误!'

    def create_school_intreface(school_name,school_address):
    school_obj=models.School.select(school_name)
    if school_obj:
    return False,'学校已存在'
    models.School(school_name,school_address)
    return True,'学校已创建'

    def create_classes_interface(class_name):
    class_obj=models.Classes.select(class_name)
    if class_obj:
    return False,'班级已存在'
    models.Classes(class_name,)
    return True,'班级已创建'

    def create_course_interface(course_name,course_cycle,course_price):
    course_obj=models.Course.select(course_name)
    if course_obj:
    return False,'课程已存在'
    models.Course(course_name,course_cycle,course_price)
    return True,'课程已创建'

    def create_teacher_interface(teacher_name,password,teacher_age,teacher_salary,teacher_level):
    teacher_obj=models.Teacher.select(teacher_name)
    if teacher_obj:
    return False,'教师已存在'
    models.Teacher(teacher_name,password,teacher_age,teacher_salary,teacher_level)
    return True,'教师已创建'
    student_interface
    from db import models
    from db import db_handler
    def register_interface(user, pwd):
    user_obj = models.Student.select(user)
    if user_obj:
    return False, '用户已存在'
    models.Student(user, pwd)
    return True, '注册成功'

    def login_interface(user, pwd):
    user_obj = models.Student.select(user)
    if not user_obj:
    return False, '用户不存在'
    else:
    if pwd == user_obj.pwd:
    return True, '登录成功!'
    return False, '密码错误!'

    def course_price(coursename):
    course_obj = models.Course.select(coursename)
    cost = course_obj.price
    return cost,f'课程{coursename}的价格为{cost}'

    def pay_interface(username):
    student_obj = models.Student.select(username)
    if student_obj.locked:
    return False, '您已交过费用!'
    student_obj.locked = True
    student_obj.save()
    return True, '缴费成功!'

    def change_school_interface():
    school_list = db_handler.show_schoollist()
    return school_list

    def add_school_student(username):
    student_obj=models.Student.select(username)
    return student_obj

    def change_course_interface(username):
    course_list = db_handler.show_courselist()
    user_obj = models.Student.select(username)
    return course_list, user_obj

    def add_course_student(coursename):
    course_obj=models.Course.select(coursename)
    return course_obj

    def check_fraction_interface(username):
    user_obj = models.Student.select(username)
    course_list=user_obj.course
    return course_list
    teacher_interface
    from db import models
    from db import db_handler
    def login_interface(user, pwd):
    user_obj = models.Teacher.select(user)
    if not user_obj:
    return False, '用户不存在'
    else:
    if pwd == user_obj.pwd:
    return True, '登录成功!'
    return False, '密码错误!'

    def check_course_interface():
    course_list=db_handler.show_courselist()
    return course_list

    def change_course_interface(username):
    user_obj = models.Teacher.select(username)
    course_list = db_handler.show_courselist()
    return course_list,user_obj



    def check_student_interface(course_name):
    cou_obj = models.Course.select(course_name)
    student_list=cou_obj.student
    return student_list

    def change_fraction_interface(course_name,student_name):
    stu_obj=models.Student.select(student_name)
    fraction=stu_obj.course[course_name]
    return stu_obj,fraction
    common
    def auth_admin(func):
    from core import admin
    def inner(*args,**kwargs):
    if admin.Login_admin_user:
    res=func(*args,**kwargs)
    return res
    else:
    print('您尚未登录,无法使用该功能!')
    admin.login()
    return inner

    def teacher_admin(func):
    from core import teacher
    def inner(*args,**kwargs):
    if teacher.Login_teacher_user:
    res=func(*args,**kwargs)
    return res
    else:
    print('您尚未登录,无法使用该功能!')
    teacher.login()
    return inner

    def student_admin(func):
    from core import student
    def inner(*args, **kwargs):
    if student.Login_student_user:
    res = func(*args, **kwargs)
    return res
    else:
    print('您尚未登录,无法使用该功能!')
    student.login()

    return inner
    start
    import os, sys

    sys.path.append(
    os.path.dirname(__file__)
    )

    from core import src


    if __name__ == '__main__':
    src.run()
  • 相关阅读:
    jq 自定义动画案例
    jq 左右轮播图案例
    hdu-5728 PowMod(数论)
    UVA-11892(组合游戏)
    UVA-12293(组合游戏)
    LA-5059(组合游戏)
    hdu-5724 Chess(组合游戏)
    hdu-5750 Dertouzos(数论)
    hdu-5748 Bellovin(LIS)
    hdu-5747 Aaronson(水题)
  • 原文地址:https://www.cnblogs.com/0B0S/p/12700867.html
Copyright © 2020-2023  润新知