选课系统<基于面向过程\对象>

2020-04-15 00:09:28

程序目录:

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


DB_PATH=os.path.join(BASE_PATH,'db')
settings.py
from interface import admin_interface
from lib import common
from interface import commin_interface

user_info={
    'user':None
}
# -管理员注册
def register():
    while True:
        username=input('>>:').strip()
        pwd=input('>>:').strip()
        re_pwd=input('>>:').strip()
        if pwd == re_pwd:
            flag,msg=admin_interface.register_interface(username,pwd)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('两次密码不一致')
# -管理员登录
def login():
    while True:
        username=input('>>:').strip()
        pwd=input('>>:').strip()
        flag,msg=commin_interface.login_interface(username,pwd,user_type='admin')
        if flag:
            user_info['user']=username
            print(msg)
            break
        else:
            print(msg)
# -创建学校
@common.auth('Admin')
def create_school():
    while True:
        school_name=input('>>:').strip()
        school_address=input('>>:').strip()
        flag,msg=admin_interface.crete_school_interface(school_name,school_address,user_info.get('user'))
        if flag:
            print(msg)
            break
        else:
            print(msg)

# -创建课程
@common.auth('Admin')
def create_class():
    while True:
        flag,school_list_msg=commin_interface.get_all_school_list()
        if not flag:
            print(school_list_msg)
            break
        for index,school_name in enumerate(school_list_msg):
            print(f'编号:{index}名称:{school_name}')
        choice=input('选择学校>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice=int(choice)
        if choice not in range(len(school_list_msg)):
            print('没有该编号')
            continue
        school_name=school_list_msg[choice]
        course_name=input('创建课程名:').strip()
        flag,msg=admin_interface.create_course_interface(school_name,course_name,user_info.get('user'))
        if flag:
            print(msg)
            break
        else:
            print(msg)


# -创建老师
@common.auth('Admin')
def create_teacher():
    while True:
        teacher_name=input('>>:').strip()
        flag,msg=admin_interface.create_teacher_interface(teacher_name,user_info.get('user'))
        if flag:
            print(msg)
            break
        else:
            print(msg)


func_dic={
    '1':register,
    '2':login,
    '3':create_school,
    '4':create_class,
    '5':create_teacher,
}
def run():
    while True:
        print('''
        1-管理员注册
        2-管理员登录
        3-创建学校
        4-创建课程
        5-创建老师
        ''')
        choice=input('>>:').strip()
        if choice == 'q':
            user_info['user']=None
            break
        if choice not in func_dic:
            continue

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





func_dic={
    '1':admin.run,
    '2':student.run,
    '3':teacher.run,

}



def run():
    while True:
        print('''
        1、管理员功能
        2、学生功能
        3、老师功能
        ''')
        choice=input('>>:').strip()
        if choice not in func_dic:
            continue
        func_dic.get(choice)()
src.py
from lib import common
from interface import student_interfacce
from interface import commin_interface
user_info={
    'user':None
}
# -注册
def register():
    while True:
        username=input('>>:').strip()
        pwd=input('>>:').strip()
        re_pwd=input('>>:').strip()
        if pwd == re_pwd:
            flag,msg=student_interfacce.regisiter_interface(username,pwd)
            if flag:
                print(msg)
                break
            else:
                print(msg)
# -登录
def login():
    while True:
        username=input('>>:').strip()
        pwd=input('>>:').strip()
        falg,msg=commin_interface.login_interface(username,pwd,user_type='student')
        if falg:
            user_info['user']=username
            print(msg)
            break
        else:
            print(msg)
# -选择校区
@common.auth('Student')
def choice_school():
    while True:
        #1.打印校区列表
        flag,school_list=commin_interface.get_all_school_list()
        if not flag:
            print(school_list)
            break
        for index,school_name in enumerate(school_list):
            print(f'编号{index}:学校名:{school_name}')
        choice=input('>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice=int(choice)
        if choice not in range(len(school_list)):
            print('没有该编号')
            continue
        school_name=school_list[choice]
        flag,msg=student_interfacce.choice_school(school_name,user_info.get('user'))
        if flag:
            print(msg)
            break
        else:
            print(msg)
            break

# -选择课程
@common.auth('Student')
def choice_class():
    while True:
        #1获取学生选择的校区下的课程
        flag,course_list=student_interfacce.get_school_course(user_info.get('user'))
        if not flag:
            print(course_list)
            break
        for index,course_name in enumerate(course_list):
            print(f'编号:{index}课程名称:{course_name}')
        choice=input('>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice=int(choice)
        if choice not in range(len(course_list)):
            print('没有该编号')
            continue
        course_name=course_list[choice]
        flag,msg=student_interfacce.choice_course(course_name,user_info.get('user'))
        if flag:
            print(msg)
            break
        else:
            print(msg)


        pass
# -查看成绩
@common.auth('Student')
def check_score():
    flag,score_dic=student_interfacce.check_score(user_info.get('user'))
    if flag:
        print(score_dic)
    else:
        print(score_dic)



func_dic={
    '1':register,
    '2':login,
    '3':choice_school,
    '4':choice_class,
    '5':check_score,
}
def run():
    while True:
        print('''
        1-注册
        2-登录
        3-选择校区
        4-选择班级
        5-查看成绩
        ''')
        choice=input('>>:').strip()
        if choice == 'q':
            user_info['user'] = None
            break
        if choice not in func_dic:
            continue

        func_dic.get(choice)()
student.py
from lib import common
from interface import commin_interface
from interface import teacher_interface
user_info={
    'user':None
}
# -登录
def login():
    while True:
        username=input('>>:').strip()
        pwd=input('>>:').strip()
        flag,msg=commin_interface.login_interface(username,pwd,user_type='teacher')
        if flag:
            user_info['user']=username
            print(msg)
            break
        else:
            print(msg)
# -查看教授课程
@common.auth('Teacher')
def check_course():
    flag,course_list=teacher_interface.check_course_interface(user_info.get('user'))
    if flag:
        print(course_list)
    else:
        print(course_list)
# -选择教授课程
@common.auth('Teacher')
def choice_course():
    while True:
        #1.先选择校区
        flag,school_list=commin_interface.get_all_school_list()
        if not flag:
            print(school_list)
            break
        for index,school_name in enumerate(school_list):
            print(f'编号{index}:学校名:{school_name}')
        choice=input('>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice=int(choice)
        if choice not in range(len(school_list)):
            print('没有该编号')
            continue
        school_name=school_list[choice]
        #2.选择课程
        flag,course_list=commin_interface.check_course(school_name)
        if not flag:
            print(course_list)
            break
        for index2,course_name in enumerate(course_list):
            print(f'编号:{index2}课程名称:{course_name}')
        choice2=input('>>:').strip()
        if not choice2.isdigit():
            print('请输入纯数字')
            continue
        choice2=int(choice2)
        if choice2 not in range(len(course_list)):
            print('没有该编号')
            continue
        course_name=course_list[choice2]
        flag,msg=teacher_interface.choice_course_interface(course_name,user_info.get('user'))
        if flag:
            print(msg)
            break
        else:
            print(msg)
# -查看课程学员
@common.auth('Teacher')
def check_student():
    while True:
        #1.先选择查看老师下的课程
        flag,course_list=teacher_interface.check_course_interface(user_info.get('user'))
        if not flag:
            print(course_list)
            break
        for index,course_name in enumerate(course_list):
            print(f'编号:{index}课程名:{course_name}')
        choice=input('>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice=int(choice)
        if choice not in range(len(course_list)):
            print('没有该编号')
            continue
        course_name=course_list[choice]
        #2.查看课程下的学生
        flag1,student_list=teacher_interface.check_student_interface(course_name,user_info.get('user'))
        if flag:
            print(student_list)
            break
        else:
            print(student_list)
# -修改学生成绩
@common.auth('Teacher')
def change_score():
    while True:
        # 1.先选择查看老师下的课程
        flag, course_list = teacher_interface.check_course_interface(user_info.get('user'))
        if not flag:
            print(course_list)
            break
        for index, course_name in enumerate(course_list):
            print(f'编号:{index}课程名:{course_name}')
        choice = input('>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice = int(choice)
        if choice not in range(len(course_list)):
            print('没有该编号')
            continue
        course_name = course_list[choice]
        # 2.查看课程下的学生
        flag1, student_list = teacher_interface.check_student_interface(course_name, user_info.get('user'))
        if not flag1:
            print('该课程没有学生')
            continue
        for index,student_name in enumerate(student_list):
            print(f'编号:{index}姓名:{student_name}')
        choice=input('>>:').strip()
        if not choice.isdigit():
            print('请输入纯数字')
            continue
        choice=int(choice)
        if choice not in range(len(student_list)):
            print('没有该编号')
            continue
        student_name=student_list[choice]
        score=input('修改后的分数:').strip()
        msg=teacher_interface.change_score_interface(student_name,course_name,score,user_info.get('user'))
        print(msg)

func_dic={
    '1':login,
    '2':check_course,
    '3':choice_course,
    '4':check_student,
    '5':change_score,
}
def run():
    while True:
        print('''
        1-登录
        2-查看教授课程
        3-选择教授课程
        4-查看课程学员
        5-修改学生成绩
        ''')
        choice=input('>>:').strip()
        if choice == 'q':
            user_info['user'] = None
            break
        if choice not in func_dic:
            continue

        func_dic.get(choice)()
teacher.py
from conf import settings
import os,pickle
def save(obj):
    user_dir=os.path.join(settings.DB_PATH,obj.__class__.__name__)
    if not os.path.exists(user_dir):
        os.mkdir(user_dir)
    user_path=os.path.join(user_dir,obj.name)
    with open(user_path,'wb') as f:
        pickle.dump(obj,f)

def select(cls,name):
    user_dir=os.path.join(settings.DB_PATH,cls.__name__)
    user_path=os.path.join(user_dir,name)
    if os.path.exists(user_path):
        with open(user_path,'rb') as f:
            obj=pickle.load(f)
            return obj
db_handler.py
from db import db_handler

class Base:
    def save(self):
        db_handler.save(self)
    @classmethod
    def select(cls,name):
        admin_obj=db_handler.select(cls,name)
        return admin_obj
class Admin(Base):
    def __init__(self,name,pwd):
        self.name=name
        self.pwd=pwd
    def create_school(self,school_name,school_address):
        school_obj=School(school_name,school_address)
        school_obj.save()
    def create_course(self,school_obj,course_name):
        course_obj=Course(course_name)
        course_obj.save()
        school_obj.course_list.append(course_name)
        school_obj.save()
    def create_teacher(self,teacher_name,teacher_pwd):
        teacher_obj=Teacher(teacher_name,teacher_pwd)
        teacher_obj.save()

class School(Base):
    def __init__(self,name,address):
        self.name=name
        self.address=address
        self.course_list=[]


class Course(Base):
    def __init__(self,name):
        self.name=name
        self.student_list=[]
class Student(Base):
    def __init__(self,name,pwd):
        self.name=name
        self.pwd=pwd
        self.school=None
        self.course_list=[]
        self.score_dic={}
    def add_school(self,school_name):
        self.school=school_name
        self.save()
    def add_course(self,course_name):
        self.course_list.append(course_name)
        self.save()
        course_obj=Course(course_name)
        course_obj.student_list.append(self.name)
        course_obj.save()
        self.score_dic[course_name]=0
        self.save()


class Teacher(Base):
    def __init__(self,name,pwd):
        self.name=name
        self.pwd=pwd
        self.course_list=[]

    def check_course(self):
        return self.course_list

    def choice_course(self,course_name):
        self.course_list.append(course_name)
        self.save()

    def check_student(self,course_name):
        course_obj=Course.select(course_name)
        student_list=course_obj.student_list
        return student_list

    def change_score(self,student_name,course_name,score,):
        student_obj=Student.select(student_name)
        student_obj.score_dic[course_name]=score
        student_obj.save()
modles.py
from db import modles
#注册
def register_interface(username,pwd):
    admin_obj=modles.Admin.select(username)
    if admin_obj:
        return False,'用户已存在'
    admin_obj=modles.Admin(username,pwd)
    admin_obj.save()
    return True,'注册成功'



def crete_school_interface(school_name,school_address,user):
    school_obj=modles.School.select(school_name)
    if school_obj:
        return False,'该学校已存在'
    admin_obj=modles.Admin.select(user)
    admin_obj.create_school(school_name,school_address)
    return True,'创建成功'

def create_course_interface(school_name,course_name,user):
    school_obj=modles.School.select(school_name)
    if course_name in school_obj.course_list:
        return False,'课程已存在'
    admin_obj=modles.Admin.select(user)
    admin_obj.create_course(school_obj,course_name)
    return True,'创建成功'

def create_teacher_interface(name,user,pwd='123'):
    teacher_obj=modles.Teacher.select(name)
    if  teacher_obj:
        return False,'该老师已存在'
    admin_obj=modles.Admin.select(user)
    teacher_obj=admin_obj.create_teacher(name,pwd)
    return True,'创建成功'
admin_interface.py
from conf import settings
import os
from db import modles
#获取所有学校列表
def get_all_school_list():
    school_path=os.path.join(settings.DB_PATH,'School')
    if os.path.exists(school_path):
        school_list=os.listdir(school_path)
        return True,school_list
    return False,'没有学校'

#所有用户登录功能
def login_interface(name,pwd,user_type):
    if user_type == 'admin':
        obj=modles.Admin.select(name)
    elif user_type == 'student':
        obj=modles.Student.select(name)
    elif user_type == 'teacher':
        obj=modles.Teacher.select(name)
    else:
        return False,'没有权限'
    if obj:
        if pwd == obj.pwd:
            return True,'登录成功'
        return False,'密码错误'
    return False,'用户不存在'

#查看课程接口
def check_course(school_name):
    school_obj=modles.School.select(school_name)
    course_list=school_obj.course_list
    if course_list:
        return True,course_list
    return False,'没有课程,请联系管理员'
common_interface.py
from db import modles
#注册接口
def regisiter_interface(username,pwd):
    student_obj=modles.Student.select(username)
    if student_obj:
        return False,'该用户已存在'
    student_obj=modles.Student(username,pwd)
    student_obj.save()
    return True,'注册成功'



#选择校区接口
def choice_school(school_name,student_name):
    student_obj=modles.Student.select(student_name)
    if student_obj.school:
        return False,'已选择学校'
    student_obj.add_school(school_name)
    return True,'选择学校成功'

#选择课程接口
def choice_course(course_name,student_name):
    student_obj=modles.Student.select(student_name)
    if course_name in student_obj.course_list:
        return False,'该课程已存在'
    student_obj.add_course(course_name)
    return True,'选择课程成功'


#查看分数接口
def check_score(student_name):
    student_obj=modles.Student.select(student_name)
    score_dic=student_obj.score_dic
    if score_dic:
        return True,score_dic
    return False,'没有成绩,联系老师'

#获取学生已选择的校区下的课程
def get_school_course(student_name):
    student_obj=modles.Student.select(student_name)
    school_name=student_obj.school
    school_obj=modles.School.select(school_name)
    course_list=school_obj.course_list
    if course_list:
        return True,course_list
    return False,'该学校没有课程'
student_interface.py
from db import modles


#查看课程列表接口
def check_course_interface(teacher_name):
    teacher_obj=modles.Teacher.select(teacher_name)
    course_list=teacher_obj.check_course()
    if course_list:
        return True,course_list
    return False,'没有课程'

#选择课程接口
def choice_course_interface(course_name,teacher_name):
    teacher_obj=modles.Teacher.select(teacher_name)
    course_list=teacher_obj.course_list
    if course_name in course_list:
        return False,'该课程已存在'
    teacher_obj.choice_course(course_name)
    return True,'选择课程成功'

#查看课程下的学生接口
def check_student_interface(course_name,teacher_name):
    teacher_obj=modles.Teacher.select(teacher_name)
    student_list=teacher_obj.check_student(course_name)
    if student_list:
        return True,student_list
    return False,'该课程没有学生'

#修改学生分数接口
def change_score_interface(student_name,course_name,score,teacher_name):
    teacher_obj=modles.Teacher.select(teacher_name)
    teacher_obj.change_score(student_name,course_name,score,)
    return '修改成功'
teacher_interface.py
# 登录认证装饰器
def auth(role):
    def login_auth(func):
        from core import admin,student,teacher
        def inner(*args,**kwargs):
            if role == 'Admin':
                if admin.user_info.get('user'):
                    res=func(*args,**kwargs)
                    return res
                else:
                    print('请登录,再进行操作')
                    admin.login()
                    func()
            if role == 'Student':
                if student.user_info.get('user'):
                    res=func(*args,**kwargs)
                    return res
                else:
                    print('请登录,再进行操作')
                    student.login()
                    func()
            if role == 'Teacher':
                if teacher.user_info.get('user'):
                    res=func(*args,**kwargs)
                    return res
                else:
                    print('请登录,再进行操作')
                    teacher.login()
                    func()
        return inner
    return login_auth
common.py
#软件使用说明
本周作业:综合应用面向对象

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

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

-需求分析
    -管理员视图
        -管理员注册
        -管理员登录
        -创建学校
        -创建班级
        -创建课程
        -创建老师
    -学生视图
        -注册
        -登录
        -选择校区
        -选择班级
        -交学费
        -查看成绩
    -老师视图
        -登录
        -查看教授课程
        -选择教授课程
        -查看课程学员
        -修改学生成绩
-项目的架构设计
    三层架构
        -视图层
            -core
                -src.py主视图
                -admin.py管理员视图
                -student.py学生视图
                -teacher.py老师视图
        -接口层
            -interface
                -admin_interface.py管理员接口
                -student_interface.py学生接口
                -teacher_interface.py老师接口

        -数据处理层
            -db
                -modles.py 存放类
                -da_handler.py保存\查看数据

    3.分任务开发
    4.测试
    5.上线
readme.md
import os,sys
sys.path.append(os.path.dirname(__file__))

from core import src
if __name__ == '__main__':
    src.run()
start.py

 

猜你喜欢

转载自www.cnblogs.com/lqb666/p/12702517.html