程序要求:
1.创建北京、上海两所学校(分析:通过学校类实例化两个学校实例)
2.创建Linux、python、go三个课程,Linux\go在北京开,Linux在上海开(创建Linux、python、go三门课程)
3.课程包含:周期、价格,通过学校来创建课程(分析:通过学校实例来创建课程,课程包含三个属性:名字、周期、价格)
4.通过学校来创建班级,班级关联课程、讲师(分析:通过学校实例来创建班级,班级要关联课程、讲师)
5.创建学员时,选择学校、关联班级(分析:选择学校后通过学校实例创建学员,学员关联班级)
6.创建讲师时要关联学校(分析:通过学校实例来创建讲师,并关联班级)
7.创建讲师角色时要关联学校提供的两个角色接口(同上)
8.学员视图:可以注册、交学费、选择班级
9.讲师视图:讲师可以管理自己的班级,上课选择班级、查看班级学员列表、修改所管理的学员的成绩
10.管理视图:创建讲师、创建班级、创建课程
11。上面的操作产生的数据都保存在文件里
由上述分析可知:需要创建5个类:学校、学员、班级、课程、讲师
5个类的对应关系:
一个学校对应多个班级、学员、课程、讲师
一个班级对应多个学员、一个课程、一个讲师
一个讲师对应多个班级、多个学员,(可通过班级找到学员、课程)
一个学员对应多个班级
有以上可知:可由学校类的属性中保存班级、学员、课程、讲师的实例字典,由班级类的属性中保
存学员实例字典,课程实例。由讲师类的属性中保存班级实例字典
功能需求:三个视图,即8、9、10所要求的功能
以下是程序的目录结构:
以下是程序的全部代码:
bin目录下的start
1 # -*- coding:utf-8 -*-
2 #!/user/bin/env.python
3 #Author:Mr Wu
4
5 import os,sys
6
7 BASE_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8 sys.path.insert(0,BASE_dir)
9 from core import main
10
11 if __name__ == '__main__':
12 obj = main.intersection_center()
13 obj.intersection()
View Code
conf目录下的settings
1 '''定义数据库的绝对路径'''
2 import os
3 if os.sep == "\\":
4 BASE_dir = "\\".join(os.path.abspath(os.path.dirname(__file__)).split("\\")[:-1])
5 database_dir = os.path.join(BASE_dir,"database")
6 else:
7 BASE_dir = "/".join(os.path.abspath(os.path.dirname(__file__)).split("/")[:-1])
8 database_dir = os.path.join(BASE_dir,"database")
9
10 school_db_file = os.path.join(database_dir,"school")
View Code
core目录下的main:
import os,sys,shelve
BASE_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,BASE_dir)
from modules import School
from conf import settings
class intersection_center(object):
'''开始交互'''
def __init__(self):
pass
def intersection(self):
while True:
print("\033[1;30m欢迎来到学生选课系统\033[0m\n"
"1:\033[1;31m管理视图\033[0m\n"
"2:\033[1;32m讲师视图\033[0m\n"
"3:\033[1;33m学员视图\033[0m\n"
"q:\033[1;34m退出\033[0m\n")
user_choice = input("\033[1;30m input your choice ID>>>:\033[0m")
if user_choice == "1":
manage_view()
elif user_choice == "2":
teacher_view()
elif user_choice == "3":
student_view()
elif user_choice == "q":
print("\033[1;30m感谢您使用学生信息管理系统\033[0m")
break
else:
print("\033[1;31m输入错误!\033[0m")
class manage_view(object):
'''管理视图'''
def __init__(self):
if os.path.exists(settings.school_db_file+".dat"): #shelve的.dat文件是用来存数据的
self.school_db = shelve.open(settings.school_db_file)
self.manage_intersection()
self.school_db.close()
else:
print("\033[1;31m系统提示:开始创建学校\033[0m")
self.create_school()
self.manage_intersection()
self.school_db.close()
def create_school(self):
'''创建学校'''
self.school_db = shelve.open(settings.school_db_file)
for i in range(2):
school_name = input("\033[1;30m input school name>>>:\033[0m")
school_address = input("\033[1;30m input school address>>>:\033[0m")
self.school_db[school_name] = School.School(school_name,school_address)
def manage_intersection(self):
'''管理视图交互'''
count = 0
print("\033[1;30m学校名称:\033[0m")
for key in self.school_db:
print(key)
while count < 3:
self.school_name = input("\033[1;30m输入要管理的学校名称>>>:\033[0m")
if self.school_name in self.school_db:
self.school = self.school_db[self.school_name]
while True:
print("\033[1;30m欢迎来到管理视图\033[0m\n"""
"1:\033[1;31m创建班级\033[0m\n"
"2:\033[1;32m创建课程\033[0m\n"
"3:\033[1;33m招聘讲师\033[0m\n"
"4:\033[1;34m查看班级\033[0m\n"
"5:\033[1;35m查看讲师\033[0m\n"
"6:\033[1;36m查看课程\033[0m\n"
"b:\033[1;37m返回\033[0m\n")
user_choice = input("\033[1;30m input your choice ID>>>:\033[0m")
if user_choice == "1":
self.create_class()
elif user_choice == "2":
self.create_course()
elif user_choice == "3":
self.create_teacher()
elif user_choice == "4":
self.show_class()
elif user_choice == "5":
self.show_teacher()
elif user_choice == "6":
self.show_course()
elif user_choice == "b":
print("\033[1;35m感谢您使用管理视图\033[0m")
break
else:
print("\033[1;31m输入错误!\033[0m")
break
else:
print("\033[1;31m输入错误,请重新输入\033[0m")
count += 1
def create_class(self):
'''创建班级'''
while True:
class_number = input("\033[1;30m输入班级编号>>>:\033[0m")
if class_number in self.school.classes:
print("\033[1;31m这个班级已经存在!\033[0m")
continue
else:
course_name = input("\033[1;30m输入关联的课程名称>>>:\033[0m")
if course_name in self.school.courses:
self.school.create_class(class_number,course_name)
print("\033[1;30m创建班级:\033[0m[\033[1;33m%s班]成功\033[0m,\033[1;30m开设课程:\033[0m[\033[1;33m%s\033[0m]"%(class_number,course_name))
if_continue = input("\033[1;30m是否继续创建班级? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
self.school_db.update({self.school_name:self.school})
break
else:
print("\033[1;31m输入错误!\033[0m")
else:
print("\033[1;31m此课程不存在,请先创建此课程!\033[0m")
break
def create_teacher(self):
'''招聘教师'''
while True:
tea_name = input("\033[1;30m输入讲师的名字>>>:\033[0m")
if tea_name in self.school.teachers:
print("\033[1;31m这个讲师已经存在!\033[0m")
continue
else:
tea_age = input("\033[1;30m输入讲师的年龄>>>:\033[0m")
tea_sex = input("\033[1;30m输入讲师的性别>>>:\033[0m")
tea_salary = input("\033[1;30m输入讲师的薪资>>>:\033[0m")
while True:
class_number_str = input("\033[1;30m输入关联的班级编号(格式:int,int...)>>>:\033[0m")
class_number_list = class_number_str.split(",")
count = 0
for class_number in class_number_list:
if class_number in self.school.classes:
count += 1
if count == len(class_number_list):
self.school.create_teacher(tea_name,tea_age,tea_sex,tea_salary,class_number_list)
print("讲师:[%s] 关联班级:[%s班]"%(tea_name,class_number_str))
break
else:
print("\033[1;31m关联的班级中有不存在的班级,请检查后重新输入!\033[0m")
if_continue = input("\033[1;30m是否继续招聘讲师? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
print("\033[1;30m招聘讲师:[%s]成功\033[0m \033[1;33m薪资:[%s]\033[0m"%(tea_name,tea_salary))
self.school_db.update({self.school_name:self.school})
break
else:
print("\033[1;31m输入错误!\033[0m")
break
def create_course(self):
'''创建课程'''
while True:
course_name = input("\033[1;30m输入课程名字>>>:\033[0m")
if course_name in self.school.courses:
print("\033[1;31m这个课程已经存在!\033[0m")
continue
else:
course_time = input("\033[1;30m输入课程周期>>>:\033[0m")
course_price = input("\033[1;30m输入课程价格>>>:\033[0m")
self.school.create_course(course_name,course_time,course_price)
print("\033[1;30m创建课程:\033[0m\033[1;33m[%s]成功\033[0m,\033[1;30m课程周期:\033[0m\033[1;33m[%s]\033[0m"%(course_name,course_time))
if_continue = input("\033[1;30m是否继续创建课程? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
self.school_db.update({self.school_name:self.school})
break
else:
print("\033[1;31m输入错误!\033[0m")
break
def show_class(self):
'''班级信息'''
while True:
self.school.show_classes()
if_continue = input("\033[1;30m是否继续查看班级信息? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
break
else:
print("\033[1;301输入错误!\033[0m")
def show_teacher(self):
'''讲师信息'''
while True:
self.school.show_teachers()
if_continue = input("\033[1;30m是否继续查看讲师信息? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
break
else:
print("\033[1;31m输入错误!\033[0m")
def show_course(self):
'''课程信息'''
while True:
self.school.show_courses()
if_continue = input("\033[1;30m是否继续查看课程信息? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
break
else:
print("\033[1;31m输入错误!\033[0m")
class teacher_view(object):
'''讲师视图'''
def __init__(self):
if os.path.exists(settings.school_db_file+".dat"):
self.school_db = shelve.open(settings.school_db_file)
self.teacher_intersection()
self.school_db.close()
else:
print("\033[1;31m抱歉学校还未建成!\033[0m")
def teacher_intersection(self):
'''讲师交互'''
count = 0
while count < 3:
self.school_name = input("\033[1;30m请输入您就职学校的名字>>>:\033[0m")
if self.school_name in self.school_db:
self.school = self.school_db[self.school_name]
tea_name = input("\033[1;30m输入您的名字>>>:\033[0m")
if tea_name in self.school.teachers:
self.teacher = self.school.teachers[tea_name]
while True:
print("\033[1;30m欢迎来到讲师视图\033[0m\n"
"1:\033[1;31m上课选择班级\033[0m\n"
"2:\033[1;32m查看学员列表\033[0m\n"
"b:\033[1;33m返回\033[0m\n")
user_choice = input("\033[1;30minput your choice ID>>>:\033[0m")
if user_choice == "1":
self.select_class()
elif user_choice == "2":
self.show_student()
elif user_choice == "b":
print("\033[1;33m感谢您使用讲师视图\033[0m")
break
else:
print("\033[1;31m输入错误!\033[0m")
break
else:
print("\033[1;31m输入错误!\033[0m")
count += 1
else:
print("\033[1;31m输入错误!\033[0m")
count += 1
def show_student(self):
'''查看学员列表'''
while True:
print("\033[1;30m班级列表:\033[0m")
for key in self.teacher.classes:
print("\033[1;36m班级编号:%s 开设课程:%s\033[0m"%(key,self.teacher.classes[key].course_obj.course_name))
class_number = input("\033[1;30m输入您想查看的班级编号>>>:\033[0m")
if class_number in self.teacher.classes:
class_obj = self.teacher.classes[class_number]
for key in class_obj.student_list:
stu_name = key
stu_age = class_obj.student_list[stu_name].stu_age
print("\033[1;30m学员名字:%s\033[0m \033[1;31m学员年龄:%s\033[0m"%(stu_name,stu_age))
if_continue = input("\033[1;30m是否继续查看学员列表? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
break
else:
print("\033[1;31m输入错误!\033[0m")
else:
print("\033[1;31m班级编号不存在!\033[0m")
def select_class(self):
'''上课选择班级'''
while True:
for class_number in self.teacher.classes:
class_obj = self.teacher.classes[class_number]
course_name = class_obj.course_obj.course_name
print("\033[1;30m班级编号:%s\033[0m \033[1;31m开设课程:m%s\033[0m"%(class_number,course_name))
select_class = input("\033[1;30m输入您想要上课的班级编号>>>:\033[0m")
if select_class in self.teacher.classes:
print("\033[1;35m讲师%s今天给%s班上课....\033[0m"%(self.teacher.tea_name,select_class))
if_continue = input("\033[1;30m是否重新选择上课班级? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
break
else:
print("\033[1;31m输入错误!\033[0m")
else:
print("\033[1;31m输入错误!\033[0m")
class student_view(object):
'''学员视图'''
def __init__(self):
if os.path.exists(settings.school_db_file+".dat"):
self.school_db = shelve.open(settings.school_db_file)
self.student_interaction()
self.school_db.close()
else:
print("\033[1;31m抱歉学校还未建成!\033[0m")
def student_interaction(self):
'''学生交互'''
while True:
print("\033[1;30m欢迎来到学生视图\033[0m\n"
"1:\033[1;31m注册\033[0m\n"
"2:\033[1;32m查看课程\033[0m\n"
"b:\033[1;33m返回\033[0m\n")
user_choice = input("\033[1;30m input your choice ID>>>:\033[0m")
if user_choice == "1":
self.registered_class()
elif user_choice == "2":
self.show_course()
elif user_choice == "b":
print("\033[1;35m感谢您使用学生视图\033[0m")
break
else:
print("\033[1;35m输入错误!\033[0m")
def registered_class(self):
'''注册学校、班级、缴费'''
while True:
print("\033[1;30m学校名称:\033[0m")
for key in self.school_db:
print(key)
self.school_name = input("\033[1;30m输入您想要注册的学校>>>:\033[0m")
if self.school_name in self.school_db:
self.school = self.school_db[self.school_name]
for key in self.school.classes:
class_obj = self.school.classes[key]
course_name = class_obj.course_obj.course_name
course_price = class_obj.course_obj.course_price
print("\033[1;33m班级编号:%s\033[0m \033[1;34m课程名称:%s\033[0m \033[1;35m课程价格:%s\033[0m"%(key,course_name,course_price))
class_number = input("\033[1;30m输入您想要注册的班级编号>>>:\033[0m")
if class_number in self.school.classes:
class_obj = self.school.classes[class_number]
course_money = class_obj.course_obj.course_price
stu_name = input("\033[1;30m输入您的名字>>>:\033[0m")
if stu_name in class_obj.student_list:
print("\033[1;31m您已经报名此班级,请选择其他班级\033[0m")
else:
stu_age = input("\033[1;30m输入您的年龄>>>:\033[0m")
stu_sex = input("\033[1;30m输入您的性别>>>:\033[0m")
if_pay = input("\033[1;30m您选中的课程费用为:%s,是否支付费用? Y/N>>>:\033[0m"%(course_money))
if if_pay == "Y":
print("\033[1;39m注册班级:[%s班]成功 , 学习课程:[%s]\033[0m"%(class_number,class_obj.course_obj.course_name))
elif if_pay == "N":
continue
else:
print("\033[1;31m输入错误!\033[0m")
self.school.create_student(stu_age,stu_sex,stu_name,class_number)
self.school_db.update({self.school_name:self.school})
break
else:
print("\033[1;31m不存在此班级编号!\033[0m")
else:
print("\033[1;31m不存在此学校!\033[0m")
def show_course(self):
'''查看课程信息'''
while True:
class_number = input("\033[1;30m输入您上课的班级编号>>>:\033[0m")
if class_number in self.school.classes:
class_obj = self.school.classes[class_number]
course_obj = class_obj.course_obj
course_name = course_obj.course_name
course_time = course_obj.course_time
course_price = course_obj.course_price
print("\033[1;32m课程:%s\033[0m \033[1;33m周期:%s\033[0m \033[1;34m价格:%s\033[0m"%(course_name,course_time,course_price))
if_continue = input("\033[1;30m是否继续查看课程信息? Y/N>>>:\033[0m")
if if_continue == "Y":
continue
elif if_continue == "N":
break
else:
print("\033[1;31m输入错误\033[0m")
else:
print("\033[1;31m不存在此班级编号!\033[0m")
View Code
modules目录:
Class:
1 class Class(object):
2 def __init__(self,class_number,course_obj):
3 self.class_number = class_number
4 self.course_obj = course_obj
5 self.student_list = {}
View Code
Teacher:
1 class Teacher(object):
2 def __init__(self,tea_name,tea_age,tea_sex,tea_salary):
3 self.tea_name = tea_name
4 self.tea_age = tea_age
5 self.tea_sex = tea_sex
6 self.tea_salary = tea_salary
7 self.classes = {}
8 def teacher_add_class(self,class_number,class_obj):
9 self.classes[class_number] = class_obj
View Code
School:
from modules import Course,Class,Student,Teacher
import pickle
class School(object):
'''学校类'''
def __init__(self,school_name,school_address):
self.school_name = school_name
self.school_address = school_address
self.teachers = {} #讲师实例
self.classes = {} #班级实例
self.courses = {} #课程实例
#self.students = {} #待扩展
def create_class(self,class_number,course_name):
'''创建班级、班级关联课程'''
class1 = Class.Class(class_number,self.courses[course_name])
self.classes[class_number] = class1
def create_teacher(self,tea_name,tea_age,tea_sex,tea_salary,class_number_list):
'''招聘讲师'''
teacher = Teacher.Teacher(tea_name,tea_age,tea_sex,tea_salary)
for class_number in class_number_list:
teacher.teacher_add_class(class_number,self.classes[class_number])
self.teachers[tea_name ] = teacher
def create_student(self,stu_age,stu_sex,stu_name,class_number):
'''创建学员,关联班级'''
student = Student.Student(stu_name,stu_age,stu_sex)
self.classes[class_number].student_list[stu_name] = student
def create_course(self,course_name,course_time,course_price):
'''创建课程'''
course = Course.Course(course_name,course_time,course_price)
self.courses[course_name] = course
def show_teachers(self):
'''查看教师信息'''
for key in self.teachers:
tea_obj = self.teachers[key]
print("名字:%s 年龄:%s 性别:%s 薪资:%s"%(tea_obj.tea_name,tea_obj.tea_age,tea_obj.tea_sex,tea_obj.tea_salary))
def show_classes(self):
'''查看班级信息'''
for key in self.classes:
class_obj = self.classes[key]
print("班级编号:%s 开设课程:%s"%(class_obj.class_number,class_obj.course_obj.course_name))
def show_courses(self):
'''查看课程信息'''
for key in self.courses:
course_obj = self.courses[key]
print("课程:%s 周期:%s 价格:%s"%(course_obj.course_name,course_obj.course_time,course_obj.course_price))
View Code
Course·:
class Course(object):
def __init__(self,course_name,course_time,course_price):
self.course_name = course_name
self.course_time = course_time
self.course_price = course_price
View Code
Student:
1 class Student(object):
2 def __init__(self,stu_name,stu_age,stu_sex):
3 self.stu_name = stu_name
4 self.stu_age = stu_age
5 self.stu_sex = stu_sex
View Code
以下是程序执行过程、结果:
开始界面:
管理视图:
创建课程:
创建班级:
招聘讲师:
查看班级:
查看讲师:
查看课程:
讲师视图:
上课选班级:
查看学员列表:
学员视图:
注册:
查看课程:
开发过程中遇到的问题:
1.数据存储问题:学校、班级、讲师、课程、学员之间的信息相互关联,如果将其分开存文件,那么如果有一处的信息变动,那么与之相关联的文件也要修改,这样做十分麻烦。
所以把班级实例、讲师实例、课程实例、学员实例都保存在学校实例中,这样只需要存学校一个文件即可
2.数据序列化问题:开始的时候我使用的是pickle序列化,这样在每个方法中都必须打开-关闭文件才能将文件中的数据更新,这样做十分麻烦。解决方法:使用shelve数据序列化,
在交互开始的时候打开文件,在交互结束的时候再关闭文件,在交互的过程中使用sellf.school_d.update({self.school_name:sellf.school})来更新文件中的数据,这样做十分简略