一、需求分析(课程与班级合为一体)
-管理员视图
-1.注册
-2.登录
-3.创建学校
-4.创建课程(先选择学校)
-5.创建讲师
-学员视图
-1.注册
-2.登录功能
-3.选择校区
-4.选择课程(先选择校区,再选择校区中的某一门课程)
- 学生选择课程,课程也选择学生
-5.查看分数
-讲师视图
-1.登录
-2.查看教授课程
-3.选择教授课程
-4.查看课程下学生
-5.修改学生分数
二、程序的架构设计
  -conf
    -setting.py
  -core
    -src.py
    -admin.py
    -student.py
    -teacher.py
  -interface
    -admin_interface.py
    -student_interface.py
    -teacher_interface.py
    -common_interface.py
  -db
    -models.py
    -db_handler.py
  -lib
    -common.py
  -start.py
核心三层架构设计:用户视图层、逻辑接口层、数据处理层
难点是数据处理层的models.py中的几个类关系处理好
 
-conf
    -setting.py
import os

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

DB_PATH = os.path.join(
BASE_PATH, 'db'
) print(DB_PATH)
-core
    -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.老师功能
---------------end----------------
''') choice = input('请输入功能编号:').strip() if choice == 'q':
break if choice not in func_dic:
print('输入有误,请重新输入!')
continue func_dic.get(choice)()
    -admin.py
from interface import admin_interface
from interface import common_interface
from lib import common admin_info = {'user': None} # 管理员注册
def register(): while True:
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
re_password = input('请输入密码:').strip() if password == re_password:
# 调用接口层,管理员注册接口
flag, msg = admin_interface.admin_register_interface(
username, password
)
if flag:
print(msg)
break
else:
print(msg)
else:
print('两次密码输入不一样,请重新输入!') # 管理员登录
def login():
while True:
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip() # 1.调用管理员登录接口
flag, msg = common_interface.login_interface(
username, password, user_type='admin'
)
if flag:
print(msg)
# 记录当前用户登录状态
# 可变类型不需要global
admin_info['user'] = username
break
else:
print(msg)
register() # 管理员创建学校
@common.auth('admin')
def create_school():
while True:
# 1.让用户输入学校的名称与地址
school_name = input('请输入学校名称:').strip()
school_addr = input('请输入学校地址: ').strip()
# 2.调用接口,保存学校
flag, msg = admin_interface.admin_creat_school_interface(
# 学校名、学校地址、创建学校
school_name, school_addr, admin_info.get('user')
)
if flag:
print(msg)
break
else:
print(msg) # 管理员创建课程
@common.auth('admin')
def create_course():
while True:
# 1.让管理员先选择学校
flag, school_list_or_msg = common_interface.get_all_school_interface()
if not flag:
print(school_list_or_msg)
break
else:
for index, school_name in enumerate(school_list_or_msg):
print('编号:{} 学校名:{}'.format(index, school_name)) choice = input('请输入学校编号:').strip() if not choice.isdigit():
print('请输入数字')
continue choice = int(choice) if choice not in range(len(school_list_or_msg)):
print('请输入正确编号!')
continue
# 获取选择后的学校名字
school_name = school_list_or_msg[choice]
# 2.选择学校后,再输入课程名称
course_name = input('请输入需要创建的课程名称:').strip()
# 3.调用创建课程接口,让管理员去创建课程
flag, msg = admin_interface.admin_create_course_interface(
# 传递学校的目的,是为了关联课程
school_name, course_name, admin_info.get('user')
)
if flag:
print(msg)
break
else:
print(msg) # 管理员创建老师
@common.auth('admin')
def create_teacher():
while True:
# 1.让管理员创建老师
teacher_name = input('请输入老师名称:').strip()
# 调用创建老师接口,让管理员创建老师
flag, msg = admin_interface.admin_create_teacher_interface(teacher_name, admin_info.get('user'))
if flag:
print(msg)
break
else:
print(msg)
pass func_dict = {
'1': register,
'2': login,
'3': create_school,
'4': create_course,
'5': create_teacher
} # 管理员视图函数
def admin_view():
while True:
print('''
-1.注册
-2.登录
-3.创建学校
-4.创建课程
-5.创建讲师
''') choice = input('请输入功能编号:').strip() if choice == 'q':
break if choice not in func_dict:
print('输入有误,请重新输入!')
continue func_dict.get(choice)()
    -student.py
from lib import common
from interface import student_interface
from interface import common_interface student_info = {'user': None} # 学生注册
def register():
while True:
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
re_password = input('请输入密码:').strip() if password == re_password:
# 调用接口层,管理员注册接口
flag, msg = student_interface.student_register_interface(
username, password
)
if flag:
print(msg)
break
else:
print(msg)
break
else:
print('两次密码输入不一样,请重新输入!') # 学生登录
def login():
while True:
username = input('请输入账号:').strip()
password = input('请输入密码:').strip() # 如果账号存在,登录成功
# 如果账号不存在,请重新注册
flag, msg = common_interface.login_interface(
username, password, user_type='student')
if flag:
print(msg)
student_info['user'] = username
break
else:
print(msg)
register() # 学生选择校区
@common.auth('student')
def choice_school():
while True:
# 1、获取所有学校,让学生选择
flag, school_list = common_interface.get_all_school_interface()
if not flag:
print(school_list)
break for index, school_name in enumerate(school_list):
print('编号:{} 学校名:{}'.format(index, school_name)) # 2、让学生输入学校编号
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]
# 3、开始调用学生选择学校接口
flag, msg = student_interface.add_school_interface(
school_name, student_info.get('user')
) if flag:
print(msg)
break
else:
print(msg) # 学生选择课程
@common.auth('student')
def choice_course():
while True:
# 1、先获取"当前学生"所在学校的课程列表
flag, course_list = student_interface.get_course_list_interface(
student_info['user']
)
# 2、打印课程列表,并让用户选择课程
if not flag:
print(course_list)
break
else:
for index, course_name in enumerate(course_list):
print('编号:{} 课程名称:{} !'.format(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] # 3、调用学生选择课程接口
flag, msg = student_interface.add_course_interface(
course_name, student_info['user']
)
if flag:
print(msg)
break
else:
print(msg) # 查看分数
@common.auth('student')
def check_score():
score = student_interface.check_score_interface(
student_info['user']
) if not score:
print('没有选择课程!') print(score) func_dict = {
'1': register,
'2': login,
'3': choice_school,
'4': choice_course,
'5': check_score
} def student_view():
while True:
print('''
-1.注册
-2.登录功能
-3.选择校区
-4.选择课程
-5.查看分数
''') choice = input('请输入功能编号:').strip() if choice == 'q':
break if choice not in func_dict:
print('输入有误,请重新输入!')
continue func_dict.get(choice)()
    -teacher.py
from lib import common
from interface import common_interface
from interface import teacher_interface teacher_info = {'user': None} # 老师登录
def login():
while True:
username = input('请输入账号:').strip()
password = input('请输入密码:').strip() # 如果账号存在,登录成功
# 如果账号不存在,请重新注册
flag, msg = common_interface.login_interface(
username, password, user_type='teacher')
if flag:
print(msg)
teacher_info['user'] = username
break
else:
print(msg) # 查看教授课程
@common.auth('teacher')
def check_course():
flag, course_list_from_tea = teacher_interface.check_course_interface(
teacher_info['user']
)
if flag:
print(course_list_from_tea)
else:
print(course_list_from_tea) # 选择教授课程
@common.auth('teacher')
def choose_course():
while True:
# 1、先打印所有学校,并选择学校
flag, school_list = common_interface.get_all_school_interface()
if not flag:
print(school_list)
break for index, school_name in enumerate(school_list):
print('编号:{}---学校名称:{}'.format(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、从选择的学校中获取所有的课程
flag2, course_list = teacher_interface.choice_school_interface(
school_name) if not flag2:
print(course_list)
break for index2, course_name in enumerate(course_list):
print('编号:{}---课程:{}'.format(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]
# 3、调用选择教授课程接口,将该课程添加到老师课程列表中
flag3, msg = teacher_interface.add_teacher_course(
course_name, teacher_info['user']
) if flag3:
print(msg)
break
else:
print(msg) # 查看课程下学生
@common.auth('teacher')
def check_stu_from_course():
while True:
# 1、选择课程
flag, course_list = teacher_interface.check_course_interface(
teacher_info['user']
)
if not flag:
print(course_list)
break
for index, course_name in enumerate(course_list):
print('编号:{}---课程:{}'.format(index, course_name)) choice = input('请输入编号:') 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、查看课程下的学生
flag, student_list = teacher_interface.check_stu_from_course(
course_name, teacher_info['user']
)
if flag:
print(student_list)
break
else:
print(student_list) # 修改学生课程分数
@common.auth('teacher')
def change_score_from_student():
# 1、上一步的基础上选择出学生
while True:
# 1、选择课程
flag, course_list = teacher_interface.check_course_interface(
teacher_info['user']
)
if not flag:
print(course_list)
break
for index, course_name in enumerate(course_list):
print('编号:{}---课程:{}'.format(index, course_name)) choice = input('请输入编号:') 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、查看课程下的学生
flag, student_list = teacher_interface.check_stu_from_course(
course_name, teacher_info['user']
)
if not flag:
print(student_list)
break for index, student_name in enumerate(student_list):
print('编号:{}---学生:{}'.format(index, student_name)) choice = input('请输入编号:') if not choice.isdigit():
print('输入有误')
continue choice = int(choice) if choice not in range(len(student_list)):
print('输入有误')
continue student_name = student_list[choice] # 2、调用修改学生分数接口修改分数
change_score = input('请输入修改的分数').strip() if not change_score.isdigit():
print('输入有误')
continue change_score = int(change_score) flag, msg = teacher_interface.change_score_from_student_interface(
course_name, student_name, change_score, teacher_info['user']
) if flag:
print(msg)
break func_dict = {
'1': login,
'2': check_course,
'3': choose_course,
'4': check_stu_from_course,
'5': change_score_from_student
} def teacher_view():
while True:
print('''
-1.登录
-2.查看教授课程
-3.选择教授课程
-4.查看课程下学生
-5.修改学生分数
''')
choice = input('请输入功能编号:').strip() if choice == 'q':
break if choice not in func_dict:
print('输入有误,请重新输入!')
continue func_dict.get(choice)()

逻辑接口层省略

数据处理层(设计好几个类的关系)

-db
    -models.py
from db import db_handler

# 父类,让所有子类都继承select 与 save 方法
class Base:
# 查看数据--->登录、查看数据库
@classmethod
def select(cls, username): # Admin,username
obj = db_handler.select_data(cls, username)
return obj # 保存数据----》注册、保存、更新数据
def save(self):
# 让db_handle中的save_data帮我保存对象数据
db_handler.save_data(self) # 管理员类
class Admin(Base):
# 调用类的时候触发
# username,password
def __init__(self, user, pwd):
# 给当前对象赋值
self.user = user
self.pwd = pwd # 创建学校
def create_school(self, school_name, school_addr):
# 该方法内部来调用学校类实例化的得到对象,并保存
school_obj = School(school_name, school_addr)
school_obj.save() # 创建课程
def create_course(self, school_obj, course_name):
# 1.该方法内部调用课程类实例化的得到对象,并保存
course_obj = Course(course_name)
course_obj.save()
# 2.获取当前学校对象,并将课程添加到课程列表中
school_obj.course_list.append(course_name)
# 3.更新学校数据
school_obj.save() # 创建讲师
def create_teacher(self, teacher_name, admin_name, teacher_pwd='123'):
# 1.调用老师类,实例化得到老师对象,并保存
teacher_obj = Teacher(teacher_name, teacher_pwd)
teacher_obj.save()
pass # 学校类
class School(Base):
def __init__(self, name, addr):
# 必须写self.user,因为db_handler里面的save_data统一规范
self.user = name
self.addr = addr
# 课程列表:每所学校都应该有相应的课程
self.course_list = [] class Student(Base):
def __init__(self, name, pwd):
self.user = name
self.pwd = pwd
# 每个学生只能有一个校区
self.school = None
# 一个学生可以选择多门课程
self.course_list = []
# 学生课程分数
self.score = {} # {'course_name:0} def add_school(self, school_name):
self.school = school_name
self.save() def add_course(self, course_name):
# 1、学生课程列表添加课程
self.course_list.append(course_name)
# 2、给学生选择的课程设置默认分数
self.score[course_name] = 0
self.save()
# 2、学生选择的课程对象,添加学生
course_obj = Course.select(course_name)
course_obj.student_list.append(
self.user
)
course_obj.save() class Course(Base):
def __init__(self, course_name):
# 必须写self.user,因为db_handler里面的save_data统一规范
self.user = course_name
# 课程列表:每所学校都应该有相应的课程
self.student_list = [] class Teacher(Base):
def __init__(self, teacher_name, teacher_pwd):
self.user = teacher_name
# self.pwd需要统一
self.pwd = teacher_pwd
self.course_list_from_tea = [] # 老师添加课程方法
def add_teacher_course(self, course_name):
self.course_list_from_tea.append(course_name)
self.save() # 老师参看课程方法
def get_course(self, course_name):
course_obj = Course.select(course_name)
student_list = course_obj.student_list
return student_list # 老师修改学生分数方法
def change_score(self, course_name, student_name, change_score):
student_obj = Student.select(student_name)
student_obj.score[course_name] = change_score
student_obj.save()
    -db_handler.py
import os
import pickle
from conf import settings # 保存数据
def save_data(obj):
# 1.获取对象的保存文件夹路径
# 以类名当做文件夹的名字
# obj.__class__:获取当前对象的类
# obj.__class__.__name__:获取类的名字
class_name = obj.__class__.__name__
user_dir_path = os.path.join(
settings.DB_PATH, class_name
) # 2.判断文件夹是否存在,不存在则创建文件夹
if not os.path.exists(user_dir_path):
os.mkdir(user_dir_path) # 3.拼接当前用户的pickle文件路径,以用户名作为文件名
user_path = os.path.join(
user_dir_path, obj.user # 当前用户名字
)
# 4.打开文件,保存对象,通过pickle
with open(user_path, 'wb') as f:
pickle.dump(obj, f) # 查看数据
def select_data(cls, username): # 类,username
# 由cls类获取类名
class_name = cls.__name__
user_dir_path = os.path.join(
settings.DB_PATH, class_name
) # 2.判断文件夹是否存在,不存在则创建文件夹
if not os.path.exists(user_dir_path):
os.mkdir(user_dir_path) # 3.拼接当前用户的pickle文件路径,以用户名作为文件名
user_path = os.path.join(
user_dir_path, username # 当前用户名字
) # 4.判断文件如果存在,再打开,并返回,若不存在,则代表用户不存在
if os.path.exists(user_path):
# 5.打开文件,获取对象
with open(user_path, 'rb') as f:
obj = pickle.load(f)
return obj

python面向对象(选课系统)的更多相关文章

  1. [ python ] 面向对象 - 选课系统

    根据源程序进行改写:    原程序地址:http://www.cnblogs.com/lianzhilei/p/5985333.html  如有侵权立即删除.    感谢原作者将完整的代码提供参考.  ...

  2. Python作业-选课系统

    目录 Python作业-选课系统 days6作业-选课系统: 1. 程序说明 2. 思路和程序限制 3. 选课系统程序目录结构 4. 测试帐户说明 5. 程序测试过程 title: Python作业- ...

  3. python之选课系统详解[功能未完善]

    作业需求 思路:1.先写出大体的类,比如学校类,学生类,课程类--   2.写出类里面大概的方法,比如学校类里面有创建讲师.创建班级-- 3.根据下面写出大致的代码,并实现其功能       遇到的困 ...

  4. python编辑选课系统

    一.需求分析 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 3. 课程包含,周期,价格,通过学校创建课 ...

  5. Python作业选课系统(第六周)

    作业需求: 角色:学校.学员.课程.讲师.完成下面的要求 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 ...

  6. Python 28 选课系统的讲解

    1.首先我们要对每一个新的项目有一个明确的思路,脑子是好东西,但是好记性不如烂笔头,所以,要把能想到的都写下来 2.然后就是创建项目的整体结构框架,比如说:conf ( 配置文件 ) .core (  ...

  7. 一个简单的python选课系统

    下面介绍一下自己写的python程序,主要是的知识点为sys.os.json.pickle的模块应用,python程序包的的使用,以及关于类的使用. 下面是我的程序目录: bin是存放一些执行文件co ...

  8. python实现学生选课系统 面向对象的应用:

    一.要求: 选课系统 管理员: 创建老师:姓名.性别.年龄.资产 创建课程:课程名称.上课时间.课时费.关联老师 使用pickle保存在文件 学生: 学生:用户名.密码.性别.年龄.选课列表[].上课 ...

  9. python 面向对象 class 老男孩选课系统

    要求:1. 创建北京.上海 2 所学校 class2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. 课程包含,周期,价格,通过学校创建课 ...

  10. python基础-10 程序目录结构 学生选课系统面向对象练习

    一 程序目录结构 1 bin文件夹 二进制文件.代码程序  2 conf 配置文件  3 帮助文档  4 头文件库文件等 二 学生选课系统部分代码 未完待续 1 包内的__init__.py文件 在包 ...

随机推荐

  1. FreeSWITCH在session上执行特定dialplan

    操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 日常开发中,会遇到需要在已存在的session上执行特定拨号方案的情况,今天整理下这方面的内容,我将从以下几个方面进 ...

  2. CF1902

    A 只要不是全 \(1\) 即可. B 二分完成天数. C \(x\) 取差的 \(gcd\),\(a_{n+1}\) 见缝插针. D 用一个 map 记录按原始操作序列,要走到 \((x,y)\) ...

  3. 源码剖析Spring依赖注入:今天你还不会,你就输了

    在之前的讲解中,我乐意将源码拿出来并粘贴在文章中,让大家看一下.然而,我最近意识到这样做不仅会占用很多篇幅,而且实际作用很小,因为大部分人不会花太多时间去阅读源码. 因此,从今天开始,我将采取以下几个 ...

  4. Centos7安装MySQL5.7和Redis6.0流水账

    安装mysql 使用rpm包安装 yum remove mariadb-libs.x86_64 yum install perl rpm -ivh mysql-community-common-5.7 ...

  5. Java I/O 教程(五) BufferedOutputStream 类

    Java BufferedOutputStream Class Java BufferedOutputStream class 用于缓冲一个输出流 其内部使用缓冲区存储数据,可以更有效率的往流中写入数 ...

  6. [BUUCTF][Web][极客大挑战 2019]EasySQL 1

    打开靶机对应的url 界面显示需要输入账号和密码 分别在两个输入框尝试加单引号尝试是否有sql注入的可能,比如 123' 发现两个框可以注入,因为报了个错误信息 You have an error i ...

  7. Taro兼容h5的一些小问题

    背景:先做了小程序,现在需要兼容h5 问题一:Image组件mode属性设置为aspectFill在h5上没效果 解决方法:给img加样式 object-fit: cover (例子如下) // js ...

  8. django学习第六天---shell指令,单表基于双下划线的模糊查询,distinct注意点,字段的choices属性,url反向解析,orm多表操作创建表

    shell指令 命令 python manage.py shell 在Terminal,执行上面这个指令会进入到python解释器环境中,并且加载了我们当前django项目配置环境,所以可以在当前sh ...

  9. 【八股cover#2】CPP语法 Q&A与知识点

    CPP语法 Q&A与知识点 简历cover 1.熟练使用C的指针应用及内存管理 指针与引用的区别 指针是一个存储地址的变量,可以有多级,可以为空,并且在初始化后可以改变指向: 引用是原变量的别 ...

  10. 第一百零九篇:基本数据类型(String类型)

    好家伙, 本篇内容为<JS高级程序设计>第三章学习笔记   1.String类型 字符串类型是最常用的几个基本类型之一 字符串可以使用双引号,单引号以及反引号(键盘左Tab上面那个)标示 ...