欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

选课系统

程序员文章站 2023-01-01 12:45:33
选课系统 1.程序框架 2.文件夹建立 3.api 1.common_api.py 4.bil 1.common.py 5.conf 1.setting.py 6.core 1.admin.py 2.src.py 3.student.py 4.teacher.py 7.db 1.common_mod ......

选课系统

花了一晚上写的,可能还存在不足

1.程序框架

选课系统

2.文件夹建立

d:/选课系统
|___api
|    |___common_api.py
|___bil
|    |___common.py
|___conf
|    |___setting.py
|___core
|    |___admin.py
|    |___src.py
|    |___student.py
|    |___teacher.py
|___db
|    |___common_modles.py
|    |___modles.py
|___starts.py
|___test.py
|___选课系统.jpg
#写的思路,先写视图,再根据功能需求写接口,路径什么的都丢入setting.py,生成的人物的类丢modles.py,公共的课程学习等类丢入common_modles.py,后面写功能就是写逻辑了

3.api

1.common_api.py

import pickle
import os
def save_obj(obj):

    path = os.path.dirname(os.path.dirname(__file__))
    file_path = os.path.join(path,'db',obj.__class__.__name__)

    if not os.path.exists(file_path):
        os.mkdir(file_path)
    obj_path = os.path.join(file_path,f'{obj.name}.pkl')

    if not os.path.exists(obj_path):
        with open(obj_path,'wb') as fw:
            pickle.dump(obj,fw)
        return true
    with open(obj_path, 'wb') as fw:
        pickle.dump(obj, fw)
    return false

4.bil

1.common.py

from conf.setting import long_dict
import pickle
import os

def user(user):

    def login_deco(func):

        def wrapper(*args, **kwargs):

            if long_dict[user]: func()
            else: print('请先登入')

        return wrapper

    return login_deco



def read_obj(objtype,name):

    path = os.path.dirname(os.path.dirname(__file__))
    file_path = os.path.join(path,'db',objtype)

    if not os.path.exists(file_path):
        os.mkdir(file_path)
    obj_path = os.path.join(file_path,f'{name}.pkl')

    if  os.path.exists(obj_path):
        with open(obj_path, 'rb') as fr:
            read_obj = pickle.load(fr)
        return read_obj
    return false

5.conf

1.setting.py

import os
long_dict = {'student':none,'teacher':none,'admin':none}

path = os.path.dirname(os.path.dirname(__file__))
student_file_path = os.path.join(path,'db','student')
teacher_file_path = os.path.join(path,'db','teacher')
admin_file_path = os.path.join(path,'db','admin')
course_file_path = os.path.join(path,'db','course')
school_file_path = os.path.join(path,'db','school')

6.core

1.admin.py

from conf.setting import *
from bil.common import user,read_obj
from db.modles import admin,teacher,course,school

#注册功能
def register():
    print('欢迎使用注册功能')
    while true:
        print('输入q退出')
        register_name = input('请输入你的名字')
        register_pwd = input('请输入你的密码')

        if register_name=='q' or register_pwd =='q':
            print('退出成功')
            return false

        admin = admin(register_name,register_pwd)

        if admin.save():
            print('注册成功')
            return true
        else:
            print('账户已存在')
            continue

#登入功能
def login():
    print('欢迎使用登入功能')
    while true:
        print('输入q退出')
        login_name = input('请输入你的名字')
        login_pwd = input('请输入你的密码')

        if login_name == 'q' or login_pwd == 'q':
            print('退出成功')
            return false

        if not os.path.exists(admin_file_path):
            os.mkdir(admin_file_path)

        path = os.path.join(admin_file_path,f'{login_name}.pkl')
        if not os.path.exists(path):
            print('账号不存在')
            continue

        info_obj = read_obj('admin',login_name)

        if info_obj.pwd == login_pwd:
            print('登入成功')
            long_dict['admin'] = login_name

            return true
        else:
            print('密码错误')
            continue



#创建学习功能
@user('admin')
def found_school():
    print('欢迎使用创建学校功能')
    while true:

        print('输入q退出')
        name = input('请输入学校的名字')
        pwd = input('请输入学校的地址')

        if name == 'q' or pwd == 'q':
            print('退出成功')
            return false

        obj = school(name, pwd)

        if obj.save():
            print('创建成功')
            return true
        else:
            print('账户已存在')
            continue

#创建老师功能
@user('admin')
def found_teacher():
    print('欢迎使用创建老师功能')
    while true:

        print('输入q退出')
        name = input('请输入老师的名字')
        pwd = input('请输入你的密码')

        if name=='q' or pwd =='q':
            print('退出成功')
            return false

        obj = teacher(name,pwd)

        if obj.save():
            print('创建成功')
            return true
        else:
            print('账户已存在')
            continue

#创建课程功能
@user('admin')
def found_course():
    print('欢迎使用创建课程功能')
    while true:

        print('输入q退出')
        name = input('请输课程的名字')


        if name == 'q' :
            print('退出成功')
            return false

        obj = course(name)

        if obj.save():
            print('创建成功')
            return true
        else:
            print('已存在')
            continue

#功能字典
action ={
    '1':register,
    '2':login,
    '3':found_school,
    '4':found_teacher,
    '5':found_course,
}

#终端显示功能字典
msg = '''
------------------
|    1:注册账号   |
|    2:登入账号   |
|    3:创建学校   |
|    4:创建老师   |
|    5:创建课程   |
|    q:退出功能   |
------------------
'''


#管理员视图运行
def admin_action():
    while true:

        print(msg)
        chiose = input('请选择')

        if chiose == 'q': print('退出成功') ; break
        elif chiose in action: action[chiose]()
        else:print('请好好输入') ; continue

2.src.py

from core.student import student_action
from core.teacher import teacher_action
from core.admin import admin_action
action ={
    '1':student_action,
    '2':teacher_action,
    '3':admin_action,
}

#终端显示功能字典
msg = '''
------------------
|    1:学生       |
|    2:老师       |
|    3:管理员     |
|    q:退出功能   |
------------------
'''


#主界面视图运行
def run():
    while true:

        print(msg)
        chiose = input('请选择')

        if chiose == 'q': print('退出成功') ; break
        elif chiose in action: action[chiose]()
        else:print('请好好输入') ; continue

3.student.py

from conf.setting import *
from bil.common import user,read_obj
from db.modles import student,course

#注册功能
def register():
    print('欢迎使用注册功能')
    while true:
        print('输入q退出')
        register_name = input('请输入你的名字')
        register_pwd = input('请输入你的密码')

        if register_name=='q' or register_pwd =='q':
            print('退出成功')
            return false

        obj = student(register_name,register_pwd)

        if obj.save():
            print('注册成功')
            return true
        else:
            print('账户已存在')
            continue
#登入功能
def login():
    print('欢迎使用登入功能')
    while true:
        print('输入q退出')
        login_name = input('请输入你的名字')
        login_pwd = input('请输入你的密码')

        if login_name == 'q' or login_pwd == 'q':
            print('退出成功')
            return false

        if not os.path.exists(student_file_path):
            os.mkdir(student_file_path)

        path = os.path.join(student_file_path, f'{login_name}.pkl')
        if not os.path.exists(path):
            print('账号不存在')
            continue

        info_obj = read_obj('student', login_name)

        if info_obj.pwd == login_pwd:
            print('登入成功')
            long_dict['student'] = login_name

            return true
        else:
            print('密码错误')
            continue

#选择学校功能
@user('student')
def choose_school():
    print('欢迎使用选择学校功能')
    while true:
        print('输入q退出')

        if not os.path.exists(school_file_path):
            os.mkdir(school_file_path)

        lis = os.listdir(school_file_path)
        if lis == []:
            print('没有学校,请让管理员创建学校')
            return false

        while true:
            print('学校名称:')
            for school_name in lis:
                print(f'\t\t{school_name[:-4]}')

            name = input('请输入学校的名字')

            if name == 'q':
                print('退出成功')
                return false

            if read_obj('school',name):

                obj_student = read_obj('student',long_dict['student'])
                obj_student.school = name
                obj_student.save()
                print('选择学校成功')
                return true

            else:
                print('学校不存在')
                continue


#选择课程功能
@user('student')
def choose_course():
    print('欢迎使用选择课程功能')
    print('输入q退出')

    if not os.path.exists(course_file_path):
        os.mkdir(course_file_path)

    lis = os.listdir(course_file_path)
    if lis == []:
        print('没有课程,请让管理员创建课程')
        return false

    while true:
        print('课程名字:')
        for course_name in lis:
            print(f'\t\t{course_name[:-4]}')

        name = input('请输入学校的名字')

        if name == 'q':
            print('退出成功')
            return false

        if read_obj('course', name):

            obj_student = read_obj('student', long_dict['student']) #type:student
            obj_student.course.append(name)
            obj_student.save()

            course_obj = read_obj('course', name) #type:course
            obj_student.add_course(course_obj)
            course_obj.save()

            print('选择课程成功')
            return true

        else:
            print('学校不存在')
            continue

#查看成绩功能
@user('student')
def check_score():
    print('欢迎使用查看成绩功能')
    obj_student = read_obj('student', long_dict['student']) #type:student

    if obj_student.score :
        print(f'你的成绩是{obj_student.score }')
        return true
    print('你没有成绩')

#功能字典
action ={
    '1':register,
    '2':login,
    '3':choose_school,
    '4':choose_course,
    '5':check_score,
}

#终端显示功能字典
msg = '''
------------------
|    1:注册账号   |
|    2:登入账号   |
|    3:选择学校   |
|    4:选择课程   |
|    5:查看成绩   |
|    q:退出功能   |
------------------
'''


#学生视图运行
def student_action():
    while true:

        print(msg)
        chiose = input('请选择')

        if chiose == 'q': print('退出成功') ; break
        elif chiose in action: action[chiose]()
        else:print('请好好输入') ; continue


if __name__ == '__main__':
    student_action()

4.teacher.py

from conf.setting import *
from bil.common import user,read_obj
from db.modles import student,course


#登入功能
def login():
    print('欢迎使用登入功能')
    while true:
        print('输入q退出')
        login_name = input('请输入你的名字')
        login_pwd = input('请输入你的密码')

        if login_name == 'q' or login_pwd == 'q':
            print('退出成功')
            return false

        if not os.path.exists(teacher_file_path):
            os.mkdir(teacher_file_path)

        path = os.path.join(teacher_file_path, f'{login_name}.pkl')
        if not os.path.exists(path):
            print('账号不存在')
            continue

        info_obj = read_obj('teacher', login_name)

        if info_obj.pwd == login_pwd:
            print('登入成功')
            long_dict['teacher'] = login_name

            return true
        else:
            print('密码错误')
            continue

#查看学生
@user('teacher')
def check_student():
    print('欢迎使用查看学生功能')

    print('输入q退出')

    if not os.path.exists(student_file_path):
        os.mkdir(student_file_path)

    lis = os.listdir(student_file_path)
    if lis == []:
        print('没有学生')
        return false

    while true:
        print('学生账号:')
        for course_name in lis:
            print(f'\t\t{course_name[:-4]}')

        name = input('请输入学生的名字')

        if name == 'q':
            print('退出成功')
            return false

        if read_obj('student', name):
            student_obj = read_obj('student', name) #type:student
            print(f'学生账号:{student_obj.name}\n'
                  f'学生密码:{student_obj.pwd}\n'
                  f'学生学校:{student_obj.school}\n'
                  f'学生课程:{student_obj.course}\n'
                  f'学生成绩:{student_obj.score}\n')
            return true
        else:
            print('学生账号不存在')


#查看课程
@user('teacher')
def check_course():
    print('欢迎使用查看课程功能')


    if not os.path.exists(course_file_path):
        os.mkdir(course_file_path)

    lis = os.listdir(course_file_path)
    if lis == []:
        print('没有课程')
        return false
    else:
        print('课程名称:')
        for course_name in lis:
            print(f'\t\t{course_name[:-4]}')

    wait = input('输入任意键退出\n')

#选择课程功能
@user('teacher')
def choose_course():
    print('欢迎使用选择课程功能')

    print('输入q退出')

    if not os.path.exists(course_file_path):
        os.mkdir(course_file_path)

    lis = os.listdir(course_file_path)
    if lis == []:
        print('没有课程')
        return false
    else:
        print('课程名称:')
        for course_name in lis:
            print(f'\t\t{course_name[:-4]}')

    name = input('请输入课程的名字')

    if name == 'q':
        print('退出成功')
        return false

    if read_obj('course', name):

        obj_teacher = read_obj('teacher', long_dict['teacher'])
        obj_teacher.course.append(name)
        obj_teacher.save()

        course_obj = read_obj('course', name)  # type:course
        obj_teacher.add_course(course_obj)
        course_obj.save()

        print('课程选择成功')

    else:
        print('课程不存在')


#修改成绩
@user('teacher')
def revision_score():
    print('欢迎使用修改成绩功能')

    print('输入q退出')

    if not os.path.exists(student_file_path):
        os.mkdir(student_file_path)

    lis = os.listdir(student_file_path)
    if lis == []:
        print('没有学生')
        return false
    else:
        print('学生名称:')
        for course_name in lis:
            print(f'\t\t{course_name[:-4]}')

    name = input('请输入学生的名字')

    if name == 'q':
        print('退出成功')
        return false

    if read_obj('student', name):

        student_obj = read_obj('student', name)   #type:student

        score = input('请输入学生的成绩')

        student_obj.score =  score

        student_obj.save()

    else:
        print('学生不存在')
#功能字典
action ={
    '1':login,
    '2':check_student,
    '3':check_course,
    '4':choose_course,
    '5':revision_score,
}

#终端显示功能字典
msg = '''
------------------
|    1:登入账号   |
|    2:查看学生   |
|    3:查看课程   |
|    4:选择课程   |
|    5:修改成绩   |
|    q:退出功能   |
------------------
'''


#老师视图运行
def teacher_action():
    while true:

        print(msg)

        chiose = input('请选择')

        if chiose == 'q': print('退出成功') ; break
        elif chiose in action: action[chiose]()
        else:print('请好好输入') ; continue

7.db

1.common_modles.py

'''
基类:
    保存


学校类:姓名、地址、课程列表
    添加课程

课程类:姓名、学生列表
    添加学生
    添加老师
'''
from api.common_api import save_obj

#基类
class base:

    #存对象
    def save(self):
        return save_obj(self)



#定义学校
class school(base):

    def __init__(self,name,addr):
        self.name = name
        self.addr = addr
        self.course = []
        self.teacher = []
        self.student = []

    def add_course(self,course):
        self.course.append(course)

#定义课程
class course(base):

    def __init__(self, name):
        self.name = name
        self.studet_list = []
        self.teacher_list = []

    def add_studet(self,student):
        self.studet_list.append(student)

    def add_teacher(self,teacher):
        self.teacher_list.append(teacher)

2.modles.py

'''
基类:
    保存


管理员类:姓名、密码
    创建学校
    创建课程
    创建老师

老师类:姓名、密码、课程列表
    添加课程
    修改分数

学生类:姓名、密码、学校、课程列表、分数
    获取学校
    选择学校
    添加课程
'''
from db.common_modles import *

#创建学生
class student(base):

    def __init__(self,name,pwd):
        self.name = name
        self.pwd = pwd
        self.school = none
        self.course = []
        self.score = none

    def check_school(self):
        return self.school

    def choose_school(self,obj):
        self.school = obj.name

    def add_course(self,course):
        course.add_studet(self)


class teacher(base):

    def __init__(self,name,pwd):
        self.name = name
        self.pwd = pwd
        self.course = []
        self.school = none



    def revision_score(self,student,score):
        student.score = score

    def add_course(self,course):
        course.add_teacher(self)


class admin(base):

    def __init__(self,name,pwd):
        self.name = name
        self.pwd = pwd

    def found_school(self,name,addr):
        school(name,addr)

    def found_teacher(self,name,pwd):
        teacher(name,pwd)

    def found_course(self,name):
        course(name)

8.starts.py(运行程序)

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

9.test.py(生成第2里面的树状图)

import os
def dirstree():
    path = os.path.dirname(__file__)
    print(path)
    lis = os.listdir(path)
    if lis != none:
        for a in lis:
            print(f'|___{a}')
            if '.' not in a:
                a_path = os.path.join(path,a)
                lis_2 = os.listdir(a_path)
                if lis_2 != none:
                    for b in lis_2:
                        if b != '__pycache__':
                            print(f'|    |___{b}')
dirstree()