选课系统
选课系统
选课系统项目分析
选课系统
角色:学校、学员、课程、讲师
要求:
- 创建北京、上海2所学校
- 创建linux , python , go三个课程 , linux\python在北京开, go在上海开
- 课程包含,周期,价格,通过学校创建课程
- 通过学校创建班级, 班级关联课程、讲师
- 创建学员时,选择学校,关联班级
- 创建讲师角色时要关联学校,
- 提供三个角色接口
7.1 学员视图, 可以登录,注册, 选择学校,选择课程,查看成绩
7.2 讲师视图, 讲师登录,选择学校,选择课程, 查看课程下学员列表 , 修改所管理的学员的成绩
7.3 管理视图,登录,注册,创建讲师, 创建班级,创建课程,创建学校 - 上面的操作产生的数据都通过pickle序列化保存到db文件夹下
架构设计
三层架构
展示层:
管理员功能
学员功能
讲师功能
接口层:
管理员接口
学员接口
讲师接口
数据处理层:
models.py 存储所有角色类
db_handler.py 处理数据增删改查
项目功能分析
管理员视图
注册
登录
创建学校
创建课程
创建讲师
讲师视图
登录
查看教授课程
选择教授课程
查看课程学生
评判学生分数
学员视图
注册
登录
选择学校
选择课程
查看分数
代码
core
点击查看src.py
from core import admin, lecturer, student
func_dict = {'1': admin.run_admin, '2': lecturer.run_lecturer, '3': student.run_student}
def initiate():
while True:
print("""
^^^^^^^^^^^^^^^^^^^^^选课系统^^^^^^^^^^^^^^^^^^^^^^^^^
1.管理员视图
2.讲师视图
3.学生视图
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
""")
choice = input('请输入功能编号>>>:').strip()
if choice == 'q':
return
if choice in func_dict:
func_dict[choice]()
else:
print('功能编号不存在')
if __name__ == '__main__':
initiate()
点击查看看admin_view.py
from interface import admin_interface
from lib import commom
login_status = {'username': None}
def register():
username = input('请设置您的管理员账号名>>>:').strip()
password = input('请设置您的密码>>>:').strip()
confirm_password = input('请确认您输入的密码>>>:').strip()
if password != confirm_password:
print('两次输入的密码不一致')
return
flag, msg = admin_interface.register_interface(username, password)
print(msg)
def login():
username = input('请输入您的管理员账号名>>>:').strip()
password = input('请输入您的密码>>>:').strip()
flag, msg = admin_interface.login_interface(username, password)
if flag:
print(msg)
login_status['username'] = username
return True
else:
print(msg)
return False
@commom.login_auth('admin')
def creat_school():
sch_name = input('请输入要添加的学校>>>:').strip()
sch_address = input('请输入学校地址>>>:').strip()
flag, msg = admin_interface.creat_school_interface(sch_name, sch_address, login_status.get('username'))
print(msg)
@commom.login_auth('admin')
def formulate_course():
flag, school_list = admin_interface.get_all_school_interface()
if not flag:
print('暂无学校')
return
for i, j in enumerate(school_list, start=1):
print(i, j)
school_num = input('请输入学校编号>>>:').strip()
if not school_num.isdigit():
return
school_num = int(school_num)
school_name = school_list[school_num - 1]
course_name = input('请输入需要添加的课程名称>>>:').strip()
course_price = input('请输入课程价格>>>:').strip()
course_period = input('请输入课程周期>>>:').strip()
flag, msg = admin_interface.formulate_course_interface(school_name, course_name, course_price, course_period, login_status.get('username'))
print(msg)
@commom.login_auth('admin')
def creat_lecturer():
username = input('请设置您创建的讲师账号名>>>:').strip()
password = input('请设置密码>>>:').strip()
flag, msg = admin_interface.creat_lecturer_interface(username, password, login_status.get('username'))
print(msg)
admin_func_dict = {'1': register, '2': login, '3': creat_school, '4': formulate_course, '5': creat_lecturer}
def run_admin():
while True:
print("""
=======================管理员界面==========================
1.注册
2.登录
3.创建学校
4.创建课程
5.创建讲师
========================================================
""")
choice = input('请输入功能编号').strip()
if choice == 'q':
return
if choice in admin_func_dict:
admin_func_dict[choice]()
else:
print('功能编号不存在')
if __name__ == '__main__':
run_admin()
点击查看lecturer_view.py
import interface.lecturer_interface
from lib import commom
login_status = {'username': None}
def login():
username = input('请输入您的账号名>>>:').strip()
password = input('请输入您的密码>>>:').strip()
flag, msg = interface.lecturer_interface.login_interface(username, password)
if flag:
print(msg)
login_status['username'] = username
return True
else:
print(msg)
return False
@commom.login_auth('lecturer')
def select_course():
school_list = interface.lecturer_interface.get_all_school_list()
for i, j in enumerate(school_list, start=1):
print(i, j)
school_num = input('请输入学校编号>>>:').strip()
if not school_num.isdigit():
return
school_num = int(school_num)
if school_num not in range(1, len(school_list) + 1):
print('编号超出范围')
return
school_name = school_list[school_num - 1]
flag, course_list = interface.lecturer_interface.get_all_course_list(school_name)
if not flag:
print('暂无课程')
return
for k, v in enumerate(course_list, start=1):
print(k, v)
course_num = input('请输入课程编号>>>:').strip()
if not course_num.isdigit():
print('输入有误')
return
course_num = int(course_num)
if course_num not in range(1, len(course_list) + 1):
return
course_name = course_list[course_num - 1]
flag, msg = interface.lecturer_interface.select_course_interface(course_name, login_status['username'])
print(msg)
@commom.login_auth('lecturer')
def observe_course():
print('查看教授课程')
flag, msg_or_course_list = interface.lecturer_interface.observe_course_interface(login_status['username'])
course_list = msg_or_course_list
for cid, course_name in enumerate(course_list, start=1):
print(cid, course_name)
@commom.login_auth('lecturer')
def observe_course_student():
print('查看课程学生')
flag, msg_or_course_list = interface.lecturer_interface.observe_course_interface(login_status['username'])
if not flag:
print(msg_or_course_list)
return
course_list = msg_or_course_list
for cid, course_name in enumerate(course_list, start=1):
print(cid, course_name)
course_num = input('请输入课程编号>>>:').strip()
if not course_num.isdigit():
print('输入有误')
return
course_num = int(course_num)
if course_num not in range(1, len(course_list) + 1):
return
course_name = course_list[course_num - 1]
flag, msg = interface.lecturer_interface.observe_course_student_interface(course_name)
print(msg)
@commom.login_auth('lecturer')
def judge_score():
print('评判分数')
course_list = interface.lecturer_interface.get_lecturer_course_list(login_status['username'])
for stuid, course_name in enumerate(course_list, start=1):
print(stuid, course_name)
course_num = input('请输入课程编号>>>:').strip()
if not course_num.isdigit():
print('输入有误')
return
course_num = int(course_num)
if course_num not in range(1, len(course_list) + 1):
return
select_course_name = course_list[course_num - 1]
stu_list = interface.lecturer_interface.get_course_all_student_list(select_course_name)
if not stu_list:
print('暂无学生选课')
return
for stuid, student_name in enumerate(stu_list, start=1):
print(stuid, student_name)
student_num = input('请输入学生编号>>>:').strip()
if not student_num.isdigit():
print('输入有误')
return
student_num = int(student_num)
if student_num not in range(1, len(stu_list) + 1):
return
select_student_name = stu_list[student_num - 1]
stu_score = input(f'请输入学生{select_student_name}该课程{select_course_name}成绩>>>:').strip()
flag, msg = interface.lecturer_interface.judge_score_interface(select_course_name, select_student_name, stu_score,
login_status['username'])
print(msg)
lecturer_func_dict = {'1': login, '2': observe_course, '3': select_course, '4': observe_course_student,
'5': judge_score}
def run_lecturer():
while True:
print("""
=======================讲师界面===========================
1.登录
2.查看教授课程
3.选择教授课程
4.查看课程学生
5.评判学生分数
========================================================
""")
choice = input('请输入功能编号').strip()
if choice == 'q':
return
if choice in lecturer_func_dict:
lecturer_func_dict[choice]()
else:
print('功能编号不存在')
点击查看student_view.py
from interface import student_interface
from lib import commom
login_status = {'username': None}
def register():
username = input('请设置您的学生账号名>>>:').strip()
password = input('请设置您的密码>>>:').strip()
confirm_password = input('请确认您输入的密码>>>:').strip()
if password != confirm_password:
print('两次输入的密码不一致')
return
flag, msg = student_interface.register_interface(username, password)
print(msg)
def login():
username = input('请输入您的学生账号名>>>:').strip()
password = input('请输入您的密码>>>:').strip()
flag, msg = student_interface.login_interface(username, password)
if flag:
print(msg)
login_status['username'] = username
return True
else:
print(msg)
return False
@commom.login_auth('student')
def select_school():
print('选择学校')
school_list = student_interface.get_all_school_list()
for i, j in enumerate(school_list, start=1):
print(i, j)
school_num = input('请输入学校编号>>>:').strip()
if not school_num.isdigit():
return
school_num = int(school_num)
if school_num not in range(1, len(school_list) + 1):
print('编号超出范围')
return
school_name = school_list[school_num - 1]
flag, msg = student_interface.select_school_interface(school_name, login_status['username'])
print(msg)
@commom.login_auth('student')
def select_course():
print('选择课程')
school_list = student_interface.get_student_all_school_list(login_status['username'])
for i, j in enumerate(school_list, start=1):
print(i, j)
school_num = input('请输入学校编号>>>:').strip()
if not school_num.isdigit():
return
school_num = int(school_num)
if school_num not in range(1, len(school_list) + 1):
print('编号超出范围')
return
school_name = school_list[school_num - 1]
flag, course_list = student_interface.get_all_course_list(school_name)
if not flag:
print('暂无课程')
return
for k, v in enumerate(course_list, start=1):
print(k, v)
course_num = input('请输入课程编号>>>:').strip()
if not course_num.isdigit():
print('输入有误')
return
course_num = int(course_num)
if course_num not in range(1, len(course_list) + 1):
return
course_name = course_list[course_num - 1]
flag, msg = student_interface.select_course_interface(course_name, login_status['username'])
print(msg)
@commom.login_auth('student')
def check_score():
print('查看分数')
course_list =student_interface.get_student_all_course_list(login_status['username'])
for cid, course_name in enumerate(course_list, start=1):
print(cid, course_name)
course_num = input('请输入课程编号>>>:').strip()
if not course_num.isdigit():
print('输入有误')
return
course_num = int(course_num)
if course_num not in range(1, len(course_list) + 1):
return
select_course_name = course_list[course_num - 1]
flag, msg = student_interface.check_score_interface(select_course_name, login_status['username'])
print(msg)
student_func_dict = {'1': register, '2': login, '3': select_school, '4': select_course,
'5': check_score}
def run_student():
while True:
print("""
=======================学生界面===========================
1.注册
2.登录
3.选择学校
4.选择课程
5.查看分数
========================================================
""")
choice = input('请输入功能编号').strip()
if choice == 'q':
return
if choice in student_func_dict:
student_func_dict[choice]()
else:
print('功能编号不存在')
conf
点击查看setting.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DB_DIR = os.path.join(BASE_DIR, 'db')
if not os.path.exists(DB_DIR):
os.mkdir(DB_DIR)
db
点击查看db_handler.py
import pickle
import os
from conf import settings
def save(obj, name):
file_path = os.path.join(settings.DB_DIR, obj.__class__.__name__)
if not os.path.exists(file_path):
os.mkdir(file_path)
obj_path = os.path.join(file_path, f'{name}')
with open(r'%s' % obj_path, 'wb') as f:
pickle.dump(obj, f)
def get_obj(cls, name):
file_path = os.path.join(settings.DB_DIR, cls.__name__)
obj_path = os.path.join(file_path, f'{name}')
# print(obj_path)
if os.path.exists(obj_path):
with open(r'%s' % obj_path, 'rb') as f1:
obj = pickle.load(f1)
return True, obj
return False, None
def get_all_obj_name_list(cls):
file_path = os.path.join(settings.DB_DIR, cls.__name__)
os.listdir(file_path)
return os.listdir(file_path)
点击查看models.py
from db import db_handler
class Baseclass:
def save(self, name):
db_handler.save(self, name)
@classmethod
def get_obj(cls, name) -> object:
return db_handler.get_obj(cls, name)
@classmethod
def get_all_obj_name_list(cls):
return db_handler.get_all_obj_name_list(cls)
class Admin(Baseclass):
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
self.save(name)
def creat_school(self, name, addr):
return School(name, addr)
def creat_lecturer(self, lecturer_name, pwd):
return Lecturer(lecturer_name, pwd)
def formulate_course(self, course_name, course_price, course_period):
return Course(course_name, course_price, course_period)
class School(Baseclass):
def __init__(self, sch_name, sch_address):
self.sch_name = sch_name
self.sch_address = sch_address
self.course_list = []
self.save(sch_name)
class Course(Baseclass):
def __init__(self, course_name, course_price, period):
self.course_name = course_name
self.course_price = course_price
self.period = period
self.stu_name_list = []
self.save(course_name)
class Lecturer(Baseclass):
def __init__(self, lecturer_name, password):
self.lecturer_name = lecturer_name
self.pwd = password
self.teach_course_list = []
self.save(lecturer_name)
class Student(Baseclass):
def __init__(self, stu_name, password):
self.lecturer_name = stu_name
self.pwd = password
self.school_list = []
self.score_dict = {}
self.course_list = []
self.save(stu_name)
interface
点击查看admin_interface
from db import models
import hashlib
def register_interface(username, password):
flag, msg = models.Admin.get_obj(username)
if flag:
return False, f'{username}管理员已存在'
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
hash_pwd = md5.hexdigest()
models.Admin(username, hash_pwd)
return True, f'{username}注册成功'
def login_interface(username, password):
flag, msg = models.Admin.get_obj(username)
if flag:
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
hash_pwd = md5.hexdigest()
if msg.pwd == hash_pwd:
return True, f'{username}登陆成功'
return False, '密码错误'
return False, '用户不存在'
def creat_school_interface(name, addr, admin_name):
is_school_obj, sch_obj = models.School.get_obj(name)
if is_school_obj:
return False, f'学校:{name}已存在'
flag, admin_obj = models.Admin.get_obj(admin_name)
if flag:
# print(admin_name, admin_obj)
admin_obj.creat_school(name, addr)
return True, f'管理员:{admin_name},创建了学校:{name},地点在{addr}'
def get_all_school_interface():
school_obj_list = models.School.get_all_obj_name_list()
if not school_obj_list:
return False, '当前没有创建任何学校'
return True, school_obj_list
def formulate_course_interface(school_name, course_name, course_price, course_period, admin_name):
full_course_name = f'{school_name}_{course_name}'
is_course_obj, course_obj = models.Course.get_obj(full_course_name)
if is_course_obj:
return False, f'当前课程{full_course_name}已存在'
flag, admin_obj = models.Admin.get_obj(admin_name)
if not flag:
return
admin_obj.formulate_course(full_course_name, course_price, course_period)
flag, school_obj = models.School.get_obj(school_name)
if not flag:
return
school_obj.course_list.append(full_course_name)
school_obj.save(school_name)
return True, f'管理员{admin_name}在学校{school_name}下创建了课程{course_name}'
def creat_lecturer_interface(lecturer_name, pwd, admin_name):
is_teacher_obj, t_obj = models.Lecturer.get_obj(lecturer_name)
if is_teacher_obj:
return False, f'讲师{lecturer_name}已存在'
flag, admin_obj = models.Admin.get_obj(admin_name)
if not flag:
return
md5 = hashlib.md5()
md5.update(pwd.encode('utf8'))
hash_pwd = md5.hexdigest()
admin_obj.creat_lecturer(lecturer_name, hash_pwd)
return True, f'管理员{admin_name}招募了讲师{lecturer_name}'
点击查看lecturer_interface
from db import models
import hashlib
def login_interface(username, password):
flag, msg = models.Lecturer.get_obj(username)
if flag:
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
hash_pwd = md5.hexdigest()
if msg.pwd == hash_pwd:
return True, f'{username}登陆成功'
return False, '密码错误'
return False, '用户不存在'
def select_course_interface(course_name, lct_name):
flag, lct_obj = models.Lecturer.get_obj(lct_name)
if flag:
lct_obj.teach_course_list.append(course_name)
lct_obj.save(lct_name)
return True, f'{lct_name}选课{course_name}成功'
def observe_course_interface(lct_name):
flag, lct_obj = models.Lecturer.get_obj(lct_name)
course_list = lct_obj.teach_course_list
if flag:
return True, course_list
return False, f'讲师{lct_name}没有讲授的课程'
def observe_course_student_interface(course_name):
flag, course_obj = models.Course.get_obj(course_name)
student_list = course_obj.stu_name_list
if flag:
if student_list:
return True, student_list
else:
return False, f'该课程{course_name}没有学生选择'
return False, f'该课程{course_name}不存在'
def judge_score_interface(course_name, stu_name, score, lct_name):
flag, stu_obj = models.Student.get_obj(stu_name)
if flag:
if stu_obj.score_dict[course_name]:
return False, f'该{stu_name}的{course_name}已经打过分'
stu_obj.score_dict[course_name] = score
stu_obj.save(stu_name)
return True, f'该{stu_name}的{course_name}得分{score}'
def get_all_school_list():
msg = models.School.get_all_obj_name_list()
return msg
def get_all_course_list(sch_name):
flag, sch_obj = models.School.get_obj(sch_name)
# print(flag, sch_obj)
if not flag:
return False, '无'
else:
course_list = sch_obj.course_list
return True, course_list
def get_lecturer_course_list(lct_name):
flag, lct_obj = models.Lecturer.get_obj(lct_name)
if flag:
course_list = lct_obj.teach_course_list
return course_list
def get_course_all_student_list(course_name):
course_obj = models.Course.get_obj(course_name)[1]
stu_list = course_obj.stu_name_list
return stu_list
点击查看student_interface
from db import models
import hashlib
def register_interface(username, password):
flag, msg = models.Student.get_obj(username)
if flag:
return False, f'{username}学生已存在'
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
hash_pwd = md5.hexdigest()
models.Student(username, hash_pwd)
return True, f'{username}注册成功'
def login_interface(username, password):
flag, msg = models.Student.get_obj(username)
if flag:
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
hash_pwd = md5.hexdigest()
if msg.pwd == hash_pwd:
return True, f'{username}登陆成功'
return False, '密码错误'
return False, '用户不存在'
def select_school_interface(school_name, stu_name):
flag, stu_obj = models.Student.get_obj(stu_name)
if flag:
if school_name in stu_obj.school_list:
return False, f'{school_name}以选过'
stu_obj.school_list.append(school_name)
stu_obj.save(stu_name)
return True, f'学生{stu_name}选择了学校{school_name}'
def select_course_interface(course_name, stu_name):
flag, stu_obj = models.Student.get_obj(stu_name)
if flag:
stu_obj.course_list.append(course_name)
stu_obj.score_dict[course_name] = None
stu_obj.save(stu_name)
flag, course_obj = models.Course.get_obj(course_name)
if flag:
course_obj.stu_name_list.append(stu_name)
course_obj.save(course_name)
return True, F'学生{stu_name}选择了课程{course_name}'
def check_score_interface(course_name, stu_name):
flag, stu_obj = models.Student.get_obj(stu_name)
if flag:
stu_score = stu_obj.score_dict.get(course_name)
# print(stu_score)
return True, F'学生{stu_name}课程{course_name}成绩为{stu_score}分'
def get_all_school_list():
msg = models.School.get_all_obj_name_list()
return msg
def get_all_course_list(sch_name):
flag, sch_obj = models.School.get_obj(sch_name)
if not flag:
return False, '无'
else:
course_list = sch_obj.course_list
return True, course_list
def get_student_all_course_list(stu_name):
flag, stu_obj = models.Student.get_obj(stu_name)
student_course_list = stu_obj.course_list
return student_course_list
def get_student_all_school_list(stu_name):
flag, stu_obj = models.Student.get_obj(stu_name)
student_school_list = stu_obj.school_list
return student_school_list
lib
点击查看commmom.py
def login_auth(user_type):
def outer(func_name):
def inner(*args, **kwargs):
from core import admin, lecturer, student
login_status_dict = {'admin': admin,
'lecturer': lecturer,
'student': student}
login_user = login_status_dict.get(user_type)
if login_user.login_status.get('username'):
res = func_name(*args, **kwargs)
return res
else:
print('%s账号未登录,请先登陆' % user_type)
if login_user.login():
res = func_name(*args, **kwargs)
return res
return inner
return outer
start文件
点击查看start.py
import os
import sys
from core import src
sys.path.append(os.path.dirname(__file__))
if __name__ == '__main__':
src.initiate()