选课系统
花了一晚上写的,可能还存在不足
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' {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' {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' {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}
'
f'学生密码:{student_obj.pwd}
'
f'学生学校:{student_obj.school}
'
f'学生课程:{student_obj.course}
'
f'学生成绩:{student_obj.score}
')
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' {course_name[:-4]}')
wait = input('输入任意键退出
')
#选择课程功能
@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' {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' {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()