一、需求分析(课程与班级合为一体)
-管理员视图
-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. HTTP 400 Bad Request 错误。

  2. Linux-RAID类型介绍、创建、彻底删除

    一.RAID(磁盘阵列) 1.1.RAID概念 RAID简称为独立冗余磁盘阵列,把多块独立的物理硬盘按不同的方式组合起来形成一个硬盘组(逻辑硬盘),从而提供比单个硬盘更高的存储性能和提供数据备份技术, ...

  3. Apache Hudi 设计与架构解读

    1. 简介 Apache Hudi(简称:Hudi)允许您在现有的hadoop兼容存储之上存储大量数据,同时提供两种原语,使得除了经典的批处理之外,还可以在数据湖上进行流处理. 这两种原语分别是: 1 ...

  4. NC51178 没有上司的舞会

    题目链接 题目 题目描述 Ural大学有N名职员,编号为1~N. 他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司. 每个职员有一个快乐指数,用整数 \(Hi\) 给出,其中 \(1\le ...

  5. NVME CLI 命令使用

    1.下载地址https://github.com/linux-nvme/nvme-cli2.安装unzip nvme-cli-master.zipcd nvme-cli-master.zipmake ...

  6. java 从零开始手写 redis(十)缓存淘汰算法 LFU 最少使用频次

    前言 java从零手写实现redis(一)如何实现固定大小的缓存? java从零手写实现redis(三)redis expire 过期原理 java从零手写实现redis(三)内存数据如何重启不丢失? ...

  7. AIR32F103(二) Linux环境和LibOpenCM3项目模板

    目录 AIR32F103(一) 合宙AIR32F103CBT6开发板上手报告 AIR32F103(二) Linux环境和LibOpenCM3项目模板 AIR32F103(三) Linux环境基于标准外 ...

  8. Gerrit 笔记

    Gerrit 通过git push后增加一个中间状态, 来完成代码审批环节, 因此在git commit的时候增加了一个change id, 并且push到定制的target, 在push之后, 需要 ...

  9. SpringBoot下Akka的简单使用

    SpringBoot下Akka的简单使用 Akka框架实现一个异步消息传输,通过定义演员来处理业务逻辑. 首先引入依赖 <!-- akka --> <dependency> & ...

  10. 学习go语言编程之工程管理

    Go命令行工具 安装了Go语言的安装包后,就直接自带Go命令行工具. # 查看当前安装的Golang版本 go version # 查看go命令行工具的帮助信息 go help Go命令行工具可以完成 ...