作业需求:
角色:学校、学员、课程、讲师、完成下面的要求
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
思路(简单整理)
关联关系:
1.学校-班级
2. 班级-课程-讲师
3.讲师-学校
4.学生-学校-班级-(这里隐藏了一个关联关系就是班级还关联这-课程-讲师)
分析后得出5个类,学校,课程,班级,讲师,学生,创建这5个类,然后用学校类将关联关系理清,然后用三个视图调用不同对象进行操作。
简单流程图:
README:
作者:yaobin
版本:示例版本 v0.1 mmp版本 :(
程序介绍:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
文件目录
---选课系统 #主程序目录
│ README
│ __init__.py
│
├─bin
│ Course_Selecting.py #主程序
│ __init__.py
│
├─conf
│ │ setting.py #配置文件
│ │ __init__.py
│
├─core
│ │ main.py #主逻辑交互程序
│ │ school_view.py #学校视图
│ │ student_view.py #学生视图
│ │ teacher_view.py #老师视图
│ │ __init__.py
│
├─db
│ 1.py
│ school.bak
│ school.dat #数据db
│ school.dir
│
├─logs
│ __init__.py
│
├─src
│ │ class_classroom.py #教室类
│ │ class_course.py #课程类
│ │ class_school.py #学校类
│ │ class_student.py #学生类
│ │ class_teacher.py #老师类
│ │ __init__.py
│
└─test
__init__.py
代码部分:
bin / Course_Selecting.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) #添加环境变量
from conf import setting
from core.main import Admin
if __name__ == '__main__':
Admin.run('')
conf / setting.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import os,time,shelve,sys
from src.class_school import School
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) #添加环境变量
data_path = os.path.join(BASE_DIR+'db')
school_file = os.path.join(data_path,'school')
core / main.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) #添加环境变量
from conf import setting
from core.school_view import School_view
from core.teacher_view import Teacher_view
from core.student_view import Student_view
class Admin(object):
def run(self):
exit_flag =False
menu = u'''
33[32;1m
1.学生视图
2.老师视图
3.学校视图
4.退出 33[0m
'''
while not exit_flag:
print(menu)
user_option = input('请输入要管理的视图,输入q退出>>>: ')
if user_option == '1':
Student_view()
elif user_option == '2':
Teacher_view()
elif user_option == '3':
School_view()
elif user_option == 'q' or user_option =='4':
sys.exit()
else:
print('输入的选项不正确,请重新输入')
school_view.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) #添加环境变量
from conf import setting
from src.class_school import School
class School_view(object):
def __init__(self):
if os.path.exists(setting.school_file+ '.dat'):
self.school_file = shelve.open(setting.school_file)
self.school_manger()
self.school_file.close()
else:
print('没有学校和班级的数据,请先创建')
self.init_school()
self.school_manger()
self.school_file.close()
def init_school(self):
self.school_file = shelve.open(setting.school_file)
self.school_file['北京'] = School('总校','北京')
self.school_file['上海'] = School('总校','上海')
def school_manger(self):
while True:
for school_name in self.school_file:
print('学校名称:%s'%school_name)
school_option = input('请输入要管理的学校名称>>>:').strip()
if school_option in self.school_file:
self.school_option = school_option
self.school_obj = self.school_file[school_option]
'''
如果是北京就等于self.school_obj[北京]
file[北京] =School('总校','北京')
所以当下文出现了self.school_obj.school_course =School('总校','北京').school_course
等同于调用了School().school_course的属性是
'''
while True:
menu = '''
欢迎来到 Python%s校区
添加课程 add_course
添加班级 add_classroom
添加讲师 add_teacher
查看班级 show_classroom
查看课程 show_classroom
查看讲师 show_teacher
修改讲师 add_teacher
退出 exit
'''%school_option
print(menu)
user_choice = input('请选择以上操作>>>:').strip()
if hasattr(self,user_choice):
getattr(self,user_choice)()
else:
print("没有这个操作,请重新输入!!!!")
pass
def add_course(self):
course_name = input('请输入课程名称>>>:').strip()
course_price = input('请输入课程价格>>>:').strip()
course_time = input('请输入课程周期[默认为周]>>>:').strip()
if course_name in self.school_obj.school_course:
print('课程已经存在')
else:
self.school_obj.create_course(course_name, course_price, course_time) #相当于School.create_course(course_name, course_price, course_time)
print('%s课程添加成功' % course_name)
self.school_file.update({self.school_option:self.school_obj})
self.school_file.close()
def add_classroom(self):
class_name = input('请输入班级名称>>>:').strip()
class_course = input('请输入课程>>>:').strip()
if class_name not in self.school_obj.sch_class_room: #sch_class_room
if class_name not in self.school_obj.school_course:
course_obj = self.school_obj.school_course[class_course]
self.school_obj.create_classroom(class_name,course_obj)
self.school_file.update({self.school_option:self.school_obj})
self.school_file.close()
print('班级创建成功')
else:
print('课程不存在')
else:
print('班级已经存在')
def show_classroom(self):
for classroom in self.school_obj.sch_class_room:
class_obj = self.school_obj.sch_class_room[classroom]
print('班级名称:%s 课程:%s'
% (class_obj.class_name,class_obj.course_obj.course_name))
def add_teacher(self):
teacher_name = input('请输入讲师姓名>>>:')
teacher_age = input('请输入讲师年龄>>>:')
teacher_gender = input('请输入讲师性别>>>:')
teacher_salary = input('请输入讲师薪水>>>:')
class_name = input('请输入授课班级>>>:')
if class_name in self.school_obj.sch_class_room: # sch_class_room
class_obj = self.school_obj.sch_class_room[class_name]
if teacher_name not in self.school_obj.sch_teacher:
self.school_obj.create_teacher(teacher_name,teacher_age,teacher_gender,teacher_salary,class_name,class_obj)
print('讲师创建成功,请确认是否已经签完合同')
else:
self.school_obj.modify_teacher({})
print('修改讲师成功')
self.school_file.update({self.school_option:self.school_obj})
self.school_file.close()
else:
print('请先创建班级')
def show_teacher(self):
for teacher_name in self.school_obj.sch_teacher:
teacher_obj = self.school_obj.sch_teacher[teacher_name]
for t in teacher_obj.teacher_classroom:
class_obj = self.school_obj.sch_class_room[t]
stu_list = []
for j in class_obj.class_student:
stu_list.append(j)
print('教师姓名:%s 教师所在班级:%s 教授课程:%s 课程学员:%s' %(teacher_obj.teacher_name
,t,class_obj.course_obj.course_name,class_obj.class_student))
def exit(self):
sys.exit('程序退出')
student_view.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import os, sys, logging, shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) # 添加环境变量
from conf import setting
from src.class_school import School
from core.school_view import School_view
class Student_view(object):
def __init__(self):
if os.path.exists(setting.school_file+ '.dat'):
self.school_file = shelve.open(setting.school_file)
self.student_manger()
self.school_file.close()
else:
print('没有数据,请联系管理员选择分校')
sys.exit()
def student_manger(self):
while True:
print('欢迎注册IT学院我们的分校有:')
for school_name in self.school_file:
print(' Sean[%s]分校' %school_name)
school_option = input('请输入您要学习的分校地址>>>:').strip()
if school_option in self.school_file:
self.school_option = school_option
self.school_obj = self.school_file[school_option]
else:
print('输入有误程序退出')
sys.exit()
for classroom in self.school_obj.sch_class_room:
class_obj = self.school_obj.sch_class_room[classroom]
print('欢迎来到%s分校我们的班级有:%s 课程有:%s'
% (school_option, class_obj.class_name, class_obj.course_obj.course_name))
student_option = input('请输入学生姓名进行操作[PS:注册也在这里哟]>>>:').strip()
if student_option in self.school_obj.sch_student:
self.student_option = student_option
self.student_obj = self.school_obj.sch_student[self.student_option]
else:
print('[%s]这位童鞋'%student_option,'您是否进行注册?')
user_choice = input('请注册/退出(y/n)>>>:')
if user_choice == 'y':
self.student_option = student_option
self.enroll()
elif user_choice == 'n':
sys.exit()
else:
print('输入有误,请重新开始 = 。= ')
self.student_manger()
while True:
menu = '''
欢迎来到 Python[%s]校区[%s]童鞋
缴纳学费 pay
选择教室/课程 choice_class
查看讲师 view_teacher
退出 exit
''' %(school_option,student_option)
print(menu)
user_choice = input('请选择以上操作>>>:').strip()
if hasattr(self, user_choice):
getattr(self, user_choice)()
else:
print("没有这个操作,请重新输入!!!!")
pass
'''注册'''
def enroll(self): # 课程、班级
student_name = self.student_option
student_gender = input('%s童鞋请输入性别>>>:' % student_name)
student_age = input('%s童鞋请输入年纪>>>:' % student_name)
class_name = input('%s童鞋请输入教室(PS:教室已经和课程关联)>>>:' % student_name)
student_score = None
if class_name in self.school_obj.sch_class_room:
class_obj = self.school_obj.sch_class_room[class_name]
self.school_obj.create_student(student_name, student_gender, student_age, student_score, class_name)
self.school_file.update({self.school_option: self.school_obj})
self.school_file.close()
print('%s童鞋注册成功,您选择的课程是%s,教室为%s'
% (student_name, class_obj.course_obj.course_name, class_obj.class_name))
else:
print('创建失败教室不存在')
'''缴纳学费'''
def pay(self): # 根据教室缴纳学费
student_name = self.student_option
class_name = input('%s童鞋请输入您所在的教室(PS:教室已经和课程关联)>>>:' % student_name)
if class_name in self.school_obj.sch_class_room: # sch_class_room
class_obj = self.school_obj.sch_class_room[class_name]
if class_obj.course_obj.course_name in self.school_obj.school_course:
course_obj = self.school_obj.school_course[class_obj.course_obj.course_name]
print('所在分校:%s 课程名称:%s 课程价格:%s 课程学习计划:%s周'
% (self.school_obj.school_addr, course_obj.course_name, course_obj.course_price, course_obj.course_time))
'''选择班级'''
def choice_class(self): # 根据学习日期选择班级
student_name = self.student_option
student_gender = self.student_obj.student_gender
student_age = self.student_obj.student_age
class_name = input('%s童鞋选择教室(PS:教室已经和课程关联)>>>:' % student_name)
student_score = 0
# class_name ='room'
if class_name in self.school_obj.sch_class_room: # sch_class_room
class_obj = self.school_obj.sch_class_room[class_name]
self.school_obj.create_student(student_name, student_gender, student_age, student_score, class_name)
self.school_file.update({self.school_option: self.school_obj})
self.school_file.close()
print('%s更改教室成功,您选择的课程是%s,教室为%s'
% (student_name, class_obj.course_obj.course_name, class_obj.class_name))
else:
print('创建失败教室不存在')
'''查看讲师'''
def view_teacher(self):
for teacher_name in self.school_obj.sch_teacher:
teacher_obj = self.school_obj.sch_teacher[teacher_name]
for t in teacher_obj.teacher_classroom:
class_obj = self.school_obj.sch_class_room[t]
stu_list = []
for j in class_obj.class_student:
stu_list.append(j)
print('教师姓名:%s 教师所在班级:%s 教授课程:%s 学生有%s'
% (teacher_obj.teacher_name,t, class_obj.course_obj.course_name,stu_list))
'''退出'''
def exit(self):
sys.exit('感谢使用再见')
teacher_view.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) #添加环境变量
from conf import setting
from src.class_school import School
class Teacher_view(object):
def __init__(self):
if os.path.exists(setting.school_file+ '.dat'):
self.school_file = shelve.open(setting.school_file)
self.teacher_manger()
self.school_file.close()
else:
print('没有教师数据,请先创建学校')
sys.exit()
def teacher_manger(self):
while True:
print('欢迎进入IT学院讲师视图:')
for school_name in self.school_file:
print('讲师分校名:%s' % school_name)
school_option = input('请输入您所在分校名>>>:').strip()
if school_option in self.school_file:
self.school_option = school_option
self.school_obj = self.school_file[school_option]
else:
print('您的输入有误')
break
teacher_option = input('请输入讲师姓名进行管理>>>:').strip()
if teacher_option in self.school_obj.sch_teacher:
self.teacher_option = teacher_option
self.teacher_obj = self.school_obj.sch_teacher[self.teacher_option]
print('欢迎讲师%s进入讲师管理系统以下是您的讲师信息:'
'
讲师姓名:%s 讲师年龄:%s 讲师性别:%s 讲师工资:%s'
%(self.teacher_obj.teacher_name,self.teacher_obj.teacher_name
,self.teacher_obj.teacher_age,self.teacher_obj.teacher_gender,self.teacher_obj.teacher_salary))
else:
print('没有[%s]这位讲师'%teacher_option)
sys.exit()
while True:
menu = '''
欢迎来到 Python[%s]校区[%s]讲师
选择教室 management_class
查看班级 view_class
查看学员 view_student_list
修改成绩 revise_student_grades
退出 exit
''' %(school_option,teacher_option)
print(menu)
user_choice = input('请选择以上操作>>>:').strip()
if hasattr(self, user_choice):
getattr(self, user_choice)()
else:
print("没有这个操作,请重新输入!!!!")
pass
'''管理自己的班级'''
def management_class(self):
for classroom in self.teacher_obj.teacher_classroom:
class_obj = self.teacher_obj.teacher_classroom[classroom] #教室对象
print('%s您所在的班级为:%s 教授的课程是%s:' % (self.teacher_obj.teacher_name,
class_obj.class_name,class_obj.course_obj.course_name)) # 讲师对应教室和课程
user_choice = input('是否选择要上课的教室y/n>>>:').strip()
if user_choice == 'y':
class_name = input('请输入要上课的教室>>>:').strip()
class_obj = self.school_obj.sch_class_room[class_name]
self.school_obj.create_teacher(self.teacher_obj.teacher_name, self.teacher_obj.teacher_age,
self.teacher_obj.teacher_gender,self.teacher_obj.teacher_salary,class_name,
class_obj)
print('讲师选择教室对应的课程成功')
self.school_file.update({self.school_option:self.school_obj})
self.school_file.close()
elif user_choice == 'n':
print('不对班级进行管理....请重新选择您的操作!!!>>>:')
pass
else:
print('输入不正确')
pass
'''上课选择班级和管理班级'''
def view_class(self): # 上课选择班级,
for classroom in self.teacher_obj.teacher_classroom:
class_obj = self.teacher_obj.teacher_classroom[classroom] # 教室对象
print('%s您所在的班级为:%s 教授的课程是%s:' % (self.teacher_obj.teacher_name,
class_obj.class_name, class_obj.course_obj.course_name)) # 讲师对应教室和课程
'''查看学员列表'''
def view_student_list(self): # 查看学员列表
for classroom in self.teacher_obj.teacher_classroom:
class_obj = self.teacher_obj.teacher_classroom[classroom] # 教室对象
stu_list = []
for j in class_obj.class_student:
stu_list.append(j)
print('%s您所在的班级为:%s 教授的课程是%s 您班级里的学生有%s:' % (self.teacher_obj.teacher_name,
class_obj.class_name, class_obj.course_obj.course_name,
stu_list)) # 讲师对应教室和课程有问题
'''修改成绩'''
def revise_student_grades(self):
self.view_student_list()
for classroom in self.teacher_obj.teacher_classroom:
class_obj = self.school_obj.sch_class_room[classroom] # 教室对象
student_name = input('请讲师修改成绩的学生的姓名>>>:').strip()
if student_name in class_obj.class_student:
student_obj = class_obj.class_student[student_name]
print('%s童鞋成绩为%s 课程:%s 教室:%s'
%(student_name, student_obj.student_score, class_obj.course_obj.course_name, class_obj.class_name))
student_gender = student_obj.student_gender
student_age = student_obj.student_age
new_score = input('请讲师修改成绩>>>:').strip()
self.school_obj.create_student
(student_name, student_gender, student_age, new_score,class_obj.class_name)
self.school_file.update({self.school_option: self.school_obj})
self.school_file.close()
print('%s童鞋修改成功,他的成绩为%s,他的课程是%s,教室为%s'
% (student_name, new_score, class_obj.class_name, class_obj.course_obj.course_name))
else:
print('没有这个童鞋>>>>>!!!!')
def exit(self):
sys.exit('[%s]讲师感谢使用再见'%self.teacher_obj.teacher_name)
db
logs
src / class_classroom.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import time
'''班级类'''
class Class_room(object):
def __init__(self,class_name,course_obj):
self.class_name = class_name
self.course_obj = course_obj
self.class_student = {}
class_course.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
'''课程类'''
import time
class Course(object):
def __init__(self,course_name,course_price,course_time):
self.course_name = course_name
self.course_price = course_price
self.course_time = course_time
class_school.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import time,os,shelve,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR) #添加环境变量
from src.class_teacher import Teacher
from src.class_course import Course
from src.class_classroom import Class_room
from src.class_student import Student
'''学校类'''
class School(object):
def __init__(self,school_name,school_addr):
self.school_name = school_name
self.school_addr = school_addr
self.school_course = {}
self.sch_class_room ={}
self.sch_teacher = {}
self.sch_student = {}
'''创建课程'''
def create_course(self,course_name,course_price,course_time):
course_obj = Course(course_name,course_price,course_time) #创建课程对象
self.school_course[course_name] = course_obj #根据课程名为key,课程对象为value创建对应关系传进了init self.school_course
'''展示课程'''
def show_course(self):
for course in self.school_course:
course_obj = self.school_course[course]
print(course_obj.__dict__) #是否需要添加学校
print('所在分校:%s 课程名称:%s 课程价格:%s 课程学习计划:%s周'
%(self.school_addr,course_obj.course_name,course_obj.course_price,course_obj.course_time))
'''修改课程'''
def modify_course(self): #可能会继续追加、保留
for course in self.school_course:
course_obj = self.school_course[course]
course_obj.course_name= input('修改课程名称>>>:')
course_obj.course_price = input('修改课程价格>>>:')
course_obj.course_time = input('修改课程周期>>>:')
'''创建教室'''
def create_classroom(self,class_name,course_obj): #创建classroom、关联课程
class_obj = Class_room(class_name,course_obj) #创建课程对象
self.sch_class_room[class_name] = class_obj #根据教室名为key,课程对象为value创建对应关系传进了init self.school_course
'''展示教室'''
def show_classroom(self):
for classroom in self.sch_class_room:
class_obj = self.sch_class_room[classroom]
#应该是班级名称、对应课程、对应老师、对应的student、保留
print('班级名称:%s 课程:%s'
% (class_obj.class_name,class_obj.course_obj))
print(class_obj.__dict__)
'''修改教室'''
def modify_course(self): #可能会继续追加、保留
for classroom in self.sch_class_room:
class_obj = self.sch_class_room[classroom]
x = time.strftime("%Y%m%d%H%M%S", time.localtime()) #按照根据时间修改教室省去输入
class_obj.course_obj = input('请慎重输入课程名称>>>:')
y = time.strftime("%Y%m%d%H%M%S", time.localtime())
class_obj.class_name= (y+"_"+ class_obj.course_obj)
'''创建教师'''
def create_teacher(self,teacher_name,teacher_age,teacher_gender,teacher_salary,class_name,class_obj):
teacher_obj = Teacher(teacher_name,teacher_age,teacher_gender,teacher_salary)
teacher_obj.add_tech_classroom(class_name,class_obj) #讲师关联教室
self.sch_teacher[teacher_name] = teacher_obj
'''查看教师'''
def show_teacher(self):
for teacher_name in self.teacher:
teacher_obj=self.teacher[teacher_name]
print('老师姓名:%s','老师年龄:%s','老师性别:%s','老师工资:%s'
%(teacher_obj.teacher_name,teacher_obj.teacher_age,teacher_obj.teacher_gender,teacher_obj.teacher_salary))
'''修改老师'''
def modify_teacher(self):
for teacher_name in self.teacher:
teacher_obj=self.teacher[teacher_name]
teacher_obj.teacher_name = input('请输入修改的老师姓名>>>:')
teacher_obj.teacher_age = input('请输入修改的老师年龄>>>:')
teacher_obj.teacher_gender = input('请输入修改的老师性别>>>:')
teacher_obj.teacher_salary = input('请输入修改的老师工资>>>:')
'''创建学生'''
def create_student(self,student_name,student_gender,student_age,student_score,class_name):
#创建学生对象
student_obj = Student(student_name,student_gender,student_age,student_score)
self.sch_student[student_name] = student_obj
#建立学生和班级的关联关系
class_obj = self.sch_class_room[class_name]
class_obj.class_student[student_name] = student_obj
#更新班级信息
self.sch_class_room[class_name] =class_obj
class_student.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
class Student(object):
def __init__(self,student_name,student_gender,student_age,student_score):
#学生性命,性别,年龄,分数
self.student_name = student_name
self.student_gender = student_gender
self.student_age = student_age
self.student_score = student_score
self.student_classroom = {}
class_teacher.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
class Teacher(object):
def __init__(self,teacher_name,teacher_age,teacher_gender
,teacher_salary):
self.teacher_name = teacher_name
self.teacher_age = teacher_age
self.teacher_gender = teacher_gender
self.teacher_salary = teacher_salary
self.teacher_classroom = {}
def add_tech_classroom(self,class_name,class_obj):
self.teacher_classroom[class_name] = class_obj