本周作业:综合应用面向对象
角色:学校、学员、课程、讲师
要求:
- 创建北京、上海 2 所学校 -->管理员创建学校
- 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
- 课程包含,周期,价格,通过学校创建课程
- 创建讲师
- 创建学员时,选择学校,关联班级
- 创建讲师角色时要关联学校,
- 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择课程(等同于选择班级)
6.2 讲师视图, 讲师可管理自己的课程, 上课时选择班级,
查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程 - 上面的操作产生的数据都通过pickle序列化保存到文件里
1.需求分析
-管理视图
- 1.注册
- 2.登录
- 3.创建学校
- 4.创建课程(先选择学校)
- 5.创建讲师
-学员视图
- 1.注册
- 2.登录
- 3.选择校区
- 4.选择课程(先选择校区,再选择校区中的某一门课)
- 学生选择课程,课程也选择学生
- 5.查看分数
- 6.交学费 studeng_obj.pay ->false
-讲师视图
- 1.登录
- 2.查看教授课程
- 3.选择教授课程
- 4.查看课程下的学生
- 5.修改学生分数
2.程序的架构设计
- 三层架构
- 用户视图层
- 用于与用户进行交互的
- 小的逻辑判断,比如注册功能中的密码判断
- core
- src.py
- admin.py
- admin_view
- student.py
- student_view
- teacher.py
- teacher_view
- 逻辑接口层
- 核心业务逻辑的处理
- interface
- 数据处理层
- 做数据的处理,数据的增删改查
- db
- db_handler.py
- ATM+购物车
- 存放json格式的数据
- dict -> json
- models.py
conf文件夹下
#settings.py
import os
BASE_PATH = os.path.dirname(
os.path.dirname(__file__)
)
DB_PATH = os.path.join(
BASE_PATH,'db'
)
core文件夹下
#admin.py
from interface import admin_interface,common_interface
from lib import common
admin_info = {
'user': None
}
def admin_register():
while 1:
print("注册...")
username = input("请输入账户:").strip()
password = input("请输入密码:").strip()
re_pwd = input("请再次输入密码:").strip()
if password == re_pwd:
flag,msg = admin_interface.admin_register_interface(
username,password
)
if common.get_flag(flag, msg) == 1: break
else:
print("密码不一致")
def admin_login():
while 1:
print("登录...")
username = input("请输入账户:").strip()
password = input("请输入密码:").strip()
flag, msg = admin_interface.admin_login_interface(
username, password
)
if flag:
print(msg)
admin_info['user'] = username
break
else:
print(msg)
@common.login_auth('admin')
def admin_creat_school():
while 1:
print("创建学校...")
name = input("请输入学校的名称:").strip()
addr = input("请输入学校的地址:").strip()
flag,msg = admin_interface.admin_creat_school_interface(
name,addr,admin_info['user']
)
if common.get_flag(flag,msg) == 1 :break
@common.login_auth('admin')
def admin_creat_course():
while 1:
print("创建课程...")
flag,school_list = common_interface.get_all_data("School")
if not flag:
print(school_list)
print("请选择学校:")
flag,school_name = common.choice_list(school_list)
if not flag:
continue
name = input("请输入课程的名称:").strip()
flag, msg = admin_interface.admin_creat_course_interface(
school_name, name, admin_info['user']
)
if common.get_flag(flag, msg) == 1: break
@common.login_auth('admin')
def admin_creat_teacher():
while 1:
print("创建老师...")
name = input("请输入老师的名称:").strip()
flag, msg = admin_interface.admin_creat_teacher_interface(
name, admin_info['user']
)
if common.get_flag(flag, msg) == 1: break
func_admin_dir = {
'1':admin_register,
'2':admin_login,
'3':admin_creat_school,
'4':admin_creat_course,
'5':admin_creat_teacher,
}
def admin_view():
while True:
print(
"""
- 1.注册
- 2.登录
- 3.创建学校
- 4.创建课程(先选择学校)
- 5.创建讲师
"""
)
choice = input('请输入功能编号: ').strip()
if choice == 'q':
admin_info['user'] = None
break
if choice not in func_admin_dir:
print('输入有误,请重新输入!')
continue
func_admin_dir.get(choice)()
#src.py
from core import admin
from core import student
from core import teacher
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 not in func_dic:
print('输入有误,请重新输入!')
continue
func_dic.get(choice)()
#student.py
"""
"""
from interface import student_interface,common_interface
from lib import common
student_info = {
'user': None
}
def student_register():
while 1:
print("注册...")
username = input("请输入账户:").strip()
password = input("请输入密码:").strip()
re_pwd = input("请再次输入密码:").strip()
if password == re_pwd:
flag,msg = student_interface.student_register_interface(
username,password
)
if flag:
print(msg)
break
else:
print(msg)
else:
print("密码不一致")
def student_login():
while 1:
print("登录...")
username = input("请输入账户:").strip()
password = input("请输入密码:").strip()
flag, msg = student_interface.student_login_interface(
username, password
)
if flag:
print(msg)
student_info['user'] = username
break
else:
print(msg)
@common.login_auth('student')
def student_choice_school():
while 1:
flag,school_list = common_interface.get_all_data("School")
if not flag:
print(school_list)
break
flag ,school_name = common.choice_list(school_list)
if not flag:
continue
flag,msg = student_interface.student_add_school_interface(
school_name,student_info['user']
)
if flag:
print(msg)
break
else:
print(msg)
break
@common.login_auth('student')
def student_choice_course():
#选择课程
while 1:
flag,course_list = student_interface.get_school_course_interface(
student_info['user']
)
if not flag:
print(course_list)
break
print("学校开设的相关课程:")
flag,course_name = common.choice_list(course_list)
if not flag:
continue
flag,msg = student_interface.student_add_course_interface(
course_name, student_info['user']
)
if common.get_flag(flag,msg) == 1:break
@common.login_auth('student')
def student_check_score():
#查看分数
msg = student_interface.student_check_score_interface(
student_info['user']
)
print(msg)
func_student_dir = {
'1':student_register,
'2':student_login,
'3':student_choice_school,
'4':student_choice_course,
'5':student_check_score,
}
def student_view():
while True:
print(
"""
- 1.注册
- 2.登录功能
- 3.选择校区
- 4.选择课程(先选择校区,再选择校区中的某一门课程)
- 5.查看分数
"""
)
choice = input('请输入功能编号: ').strip()
if choice == 'q':
student_info['user'] = None
break
if choice not in func_student_dir:
print('输入有误,请重新输入!')
continue
func_student_dir.get(choice)()
#teacher.py
from interface import teacher_interface,common_interface
from lib import common
teacher_info = {
'user': None
}
def teacher_login():
while 1:
print("登录...")
username = input("请输入账户:").strip()
password = input("请输入密码:").strip()
flag, msg = teacher_interface.teacher_login_interface(
username, password
)
if flag:
print(msg)
teacher_info['user'] = username
break
else:
print(msg)
@common.login_auth('teacher')
def teacher_check_course():
#查看教授课程
flag,course_list = teacher_interface.teacher_check_course_interface(
teacher_info["user"]
)
print(course_list)
@common.login_auth('teacher')
def teacher_choice_course():
#选择教授课程
while 1:
flag,course_list = common_interface.get_all_data('Course')
if not flag:
print("没有课程数据,请联系管理员")
print("所有课程列表:")
flag, course_name = common.choice_list(course_list)
if not flag:
continue
flag,msg = teacher_interface.teacher_add_course_interface(
course_name,teacher_info["user"]
)
if common.get_flag(flag,msg) == 1:break
@common.login_auth('teacher')
def teacher_check_course_student():
#查看课程下学生
while 1:
flag,course_list = teacher_interface.teacher_check_course_interface(
teacher_info["user"]
)
if not flag:
return "没有课程数据,请先选课"
print("您的课程列表:")
flag,course_name = common.choice_list(course_list)
if not flag:
continue
flag,msg = teacher_interface.teacher_check_course_student_interface(
course_name,teacher_info["user"]
)
if common.get_flag(flag,msg) == 1:break
@common.login_auth('teacher')
def teacher_update_student_score():
#修改学生分数
while 1:
flag,course_list = teacher_interface.teacher_check_course_interface(
teacher_info["user"]
)
if not flag:
return "没有课程数据,请先选课"
print("您的课程列表:")
flag,course_name = common.choice_list(course_list)
if not flag:
continue
flag,student_list = teacher_interface.teacher_check_course_student_interface(
course_name,teacher_info["user"]
)
if not flag:
print(student_list)
continue
print("所选课程的学生列表:")
flag, student_name = common.choice_list(student_list)
if not flag:
continue
score = input("请输入修改后的分数").strip()
if not score.isdigit():
print("请输入数字")
continue
score = int(score)
if score < 0 or score>100:
print("输入数字非法")
continue
flag,msg = teacher_interface.teacher_update_student_score_interface(
student_name,score,course_name,teacher_info["user"]
)
if common.get_flag(flag,msg) == 1:break
func_teacher_dir = {
'1':teacher_login,
'2':teacher_check_course,
'3':teacher_choice_course,
'4':teacher_check_course_student,
'5':teacher_update_student_score,
}
def teacher_view():
while True:
print(
"""
- 1.登录
- 2.查看教授课程
- 3.选择教授课程
- 4.查看课程下学生
- 5.修改学生分数
"""
)
choice = input('请输入功能编号: ').strip()
if choice == 'q':
teacher_info['user'] = None
break
if choice not in func_teacher_dir:
print('输入有误,请重新输入!')
continue
func_teacher_dir.get(choice)()
db文件夹下
#db_handler.py
import os
from conf import settings
import pickle
def select(cls,username):
class_name = cls.__name__
user_dir_path = os.path.join(
settings.DB_PATH,class_name
)
if not os.path.exists(user_dir_path):
os.mkdir(user_dir_path)
else:
user_path = os.path.join(
user_dir_path, f'{username}.pkl'
)
if os.path.exists(user_path):
with open(user_path, 'rb') as f:
obj = pickle.load(f)
return obj
def save(obj):
class_name = obj.__class__.__name__
user_dir_path = os.path.join(
settings.DB_PATH,class_name
)
if not os.path.exists(user_dir_path):
os.mkdir(user_dir_path)
user_path = os.path.join(
user_dir_path,f'{obj.name}.pkl'
)
with open(user_path,'wb')as f:
pickle.dump(obj,f)
#models.py
'''
专门用于管理所有类
'''
from db import db_handler
class Base:
def save(self):
db_handler.save(self)
@classmethod
def select(cls,username):
obj = db_handler.select(cls,username)
return obj
class Admin(Base):
def __init__(self,name,pwd):
self.name = name
self.pwd = pwd
self.save()
#创建学校
def creat_school(self,name,addr):
school_obj = School(name,addr)
school_obj.save()
#创建课程
def creat_course(self,school_obj,name):
course_obj = Course(name)
course_obj.save()
school_obj.course_list.append(name)
school_obj.save()
def creat_teacher(self,name,pwd):
teacher_obj = Teacher(name,pwd)
teacher_obj.save()
class School(Base):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.course_list = []
# self.save()
class Course(Base):
def __init__(self,name):
self.name = name
self.student_list =[]
# self.save()
class Student(Base):
def __init__(self,name,pwd):
self.name = name
self.pwd = pwd
self.school = None
self.course_name = []
self.score = {}
self.save()
#添加学校
def add_school(self,school_name):
self.school=school_name
self.save()
#添加课程
def add_course(self,course_name):
self.course_name.append(course_name)
self.score[course_name] = 0
self.save()
course_obj = Course.select(course_name)
course_obj.student_list.append(self.name)
course_obj.save()
class Teacher(Base):
def __init__(self,name,pwd):
self.name = name
self.pwd = pwd
self.course_list =[]
#显示所有课程
def show_course_list(self):
return self.course_list
#添加课程
def add_course(self,course_name):
self.course_list.append(course_name)
self.save()
#查询课程下的学生
def check_course_student(self,course_name):
course_obj = Course.select(course_name)
return course_obj.student_list
#更新学生课程成绩
def update_student_score(self,student_name,score,course_name):
student_obj = Student.select(student_name)
student_obj.score[course_name] = score
student_obj.save()
interface文件夹下
#admin_interface.py
from db import models
def admin_register_interface(username,pwd):
obj = models.Admin.select(username)
if not obj:
admin_obj = models.Admin(username,pwd)
# admin_obj.save()
return True,'注册成功'
else:
return False,'用户已存在'
def admin_login_interface(username,pwd):
obj = models.Admin.select(username)
if not obj:
return False,'用户不存在'
else:
if obj.pwd == pwd:
return True,"登录成功"
else:
return False,'密码错误'
def admin_creat_school_interface(name,addr,admin_name):
obj = models.School.select(name)
if obj:
return False,'所建学校已存在'
admin_obj = models.Admin.select(admin_name)
admin_obj.creat_school(name,addr)
return True,f'[{name}]学校创建成功!'
def admin_creat_course_interface(school_name,course_name,admin_name):
obj = models.School.select(school_name)
if course_name in obj.course_list:
return False,'课程已存在'
admin_obj = models.Admin.select(admin_name)
admin_obj.creat_course(obj,course_name)
return True, f'[{course_name}] 课程创建成功, 绑定给[{school_name}]校区!'
def admin_creat_teacher_interface(teacher_name,admin_name,teacher_pwd='123'):
teacher_obj = models.Teacher.select(teacher_name)
if teacher_obj:
return False,'老师已存在'
admin_obj = models.Admin.select(admin_name)
admin_obj.creat_teacher(teacher_name,teacher_pwd)
return True,f'[{teacher_name}]老师创建成功!'
#common_interface.py
from conf import settings
import os
from db import models
def get_all_data(type):
type_path = os.path.join(
settings.DB_PATH,type
)
if not type_path:
return False,f"没有{type},请联系管理员"
type_names = os.listdir(type_path)
type_list = []
for v in type_names:
type_list.append(v.split(".")[0])
return True, type_list
#student.py
from db import models
def student_register_interface(name,pwd):
obj = models.Student.select(name)
if not obj:
student_obj = models.Student(name, pwd)
# student_obj.save()
return True, '注册成功'
else:
return False, '用户已存在'
def student_login_interface(name,pwd):
obj = models.Student.select(name)
if not obj:
return False, "信息不存在,请联系管理员"
if obj.pwd == pwd:
return True, f"{name}学生,登录成功"
else:
return False, '密码错误'
def student_add_school_interface(school_name,name):
student_obj = models.Student.select(name)
if not student_obj.school:
student_obj.add_school(school_name)
return True,"学校选择成功"
else:
return False,'学校已选择,请勿重复选择'
def get_school_course_interface(name):
student_obj = models.Student.select(name)
if not student_obj.school:
return False,"请先选择学校"
school_obj = models.School.select(student_obj.school)
return True,school_obj.course_list
def student_add_course_interface(course_name,name):
student_obj = models.Student.select(name)
if course_name in student_obj.course_name:
return False, '该课程已经选择过了!'
student_obj.add_course(course_name)
return True, f'[{course_name}] -- 课程添加成功!'
def student_check_score_interface(name):
student_obj = models.Student.select(name)
if student_obj.score:
return student_obj.score
#teacher_interface.py
from db import models
def teacher_login_interface(name,pwd):
obj = models.Teacher.select(name)
if not obj:
return False,"信息不存在,请联系管理员"
if obj.pwd == pwd:
return True,f"{name}老师,登录成功"
else:
return False,'密码错误'
def teacher_check_course_interface(name):
obj = models.Teacher.select(name)
course_list = obj.show_course_list()
if not course_list:
return False,"您还未教授课程"
else:
return True,course_list
def teacher_add_course_interface(course_name,name):
obj = models.Teacher.select(name)
if course_name in obj.course_list:
return False,'该课程已被选择,请勿重复添加'
obj.add_course(course_name)
return True,f'{name}老师,选了{course_name}课'
def teacher_check_course_student_interface(course_name,name):
obj = models.Teacher.select(name)
res_list = obj.check_course_student(course_name)
if not res_list:
return False,"没有学生选择这门课"
return True,res_list
def teacher_update_student_score_interface(student_name,score,course_name,name):
obj = models.Teacher.select(name)
obj.update_student_score(student_name,score,course_name)
return True,f'成功将{student_name}的{course_name}修改为{score}分'
lib文件夹下
#common.py
def login_auth(role):
'''
:param role: admin,student,teacher
:return:
'''
from core import admin,student,teacher
def inner(func):
def warpper(*args,**kwargs):
if role =='admin':
if admin.admin_info['user']:
res = func(*args,**kwargs)
return res
else:
admin.admin_login()
elif role == 'student':
if student.student_info['user']:
res = func(*args,**kwargs)
return res
else:
student.student_login()
elif role == 'teacher':
if teacher.teacher_info['user']:
res = func(*args,**kwargs)
return res
else:
teacher.teacher_login()
return warpper
return inner
#选择流程
def choice_list(list):
for index, v in enumerate(list):
print(f'编号:{index},值:{v}')
choice = input("请选择:").strip()
if not choice.isdigit():
print("请输入数字")
return False,None
choice = int(choice)
if choice not in range(len(list)):
print("非法输入")
return False,None
name = list[choice]
return True, name
def get_flag(flag,msg):
if flag:
print(msg)
return 1
else:
print(msg)
start.py
'''
程序入口
'''
import os,sys
sys.path.append(
os.path.dirname(__file__)
)
from core import src
if __name__ == '__main__':
src.run()