老男孩Day7作业:选课系统

1、作业需求:
角色:学校、学员、课程、讲师

1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程 
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校, 
6. 提供两个角色接口
7. 学员视图, 可以注册, 交学费, 选择班级,
8. 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 ,修改所管理的学员的成绩 
9. 管理视图,创建讲师, 创建班级,创建课程
10. 上面的操作产生的数据都通过pickle序列化保存到文件里

2、流程图

3、目录结构

 

4、代码区

bin目录下的start.py程序执行文件

# -*- coding:utf-8 -*-
# Author:D.Gray
import os,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from core import main
if __name__ == '__main__':
    start = main.Count()
    start.run()
start.py

 conf目下的setting.py系统配置文件

# -*- coding:utf-8 -*-
# Author:D.Gray
import os,pickle
#数据处理路径
#BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = "\\".join(os.path.abspath(os.path.dirname(__file__)).split("\\")[:-1])
data_path = os.path.join(BASE_DIR,"databases")
#print(data_path)

#数据文件名
school_file = os.path.join(data_path,"school")
#print(school_file)
setting.py

core目录下的main.py主目录程序文件

# -*- coding:utf-8 -*-
# Author:D.Gray
import os,shelve,sys
from conf import setting
from src.school import School
from src.classroom import Classroom
from src.course import Course
from src.student import Student
from src.teacher import Teacher

class Count(object):
    '''
    主目录运行类
    '''
    exit_flag = False
    def run(self):
        menu = '''            1、学生视图
            2、教师视图
            3、学校视图'''
        while True:
            print("\033[35;1m欢迎来的选课系统CRM\033[0m".center(50,"="))
            print(menu)
            user_option = input("\033[34;1m请选择视图...按Q退出>>>:\033[0m").strip()
            if user_option == "1":
                Student_view()
            elif user_option == "2":
                Teacher_view()
            elif user_option == "3":
                School_view()
            elif user_option.capitalize() == "Q":
                break
            else:
                print("\033[31;1m请选择有效视图\033[0m")

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_manager()
            self.school_file.close()
        else:
            print("\033[31;1m无数据文件请先创建学校\033[0m")
            exit()

    def teacher_manager(self):
        '''
        定义一个讲师管理模块
        :return:
        '''
        while True:
            for sch_name in self.school_file:
                print("学校名称【%s】"%sch_name)
            sch_option = input('请选择管理的学校按b返回>>>:').strip()
            if sch_option in self.school_file:
                self.sch_option = sch_option
                self.sch_obj = self.school_file[sch_option]
                print("学校地址【%s】"%self.sch_obj.sch_addr)
                teaname_option = input('请输入登录讲师姓名>>>:').strip()
                while True:
                    if teaname_option in self.sch_obj.sch_teacher:
                        menu = '''                        欢迎【%s】来到教师中心
                        1、查看讲师信息  show_teacher
                        2、查看班级学员  show_classroom
                        3、退出    exit
                        '''%teaname_option
                        print(menu)
                        menu_option = input('请输入操作命令按b返回>>>:').strip()
                        if hasattr(self,menu_option):
                            getattr(self,menu_option)(teaname_option)
                        elif menu_option == 'b':
                            break
                        else:
                            print('\033[31;1m请输入有效操作命令\033[0m')
                    else:
                        print("\033[31;1m讲师不存在请先创建\033[0m")
                        break
            elif sch_option == "b":
                break
            else:
                print('\033[31;1m请输入有效学校名\033[0m')

    def show_teacher(self,*args):
        '''
        查看班级模块
        :return:
        '''
        self.sch_obj.show_teacher(*args)
        if len(self.sch_obj.sch_classroom) == 0:
            print('\033[31;1m该讲师暂无班级\033[0m')

    def show_classroom(self,teaname_option):
        self.sch_obj.show_tea_clsinfo(teaname_option)
        if len(self.sch_obj.sch_classroom) == 0:
            print('\033[31;1m该讲师暂无班级\033[0m')

    def exit(self,*args):
        '''
        退出程序模块
        :return:
        '''
        self.school_file.close()
        exit("\033[32;1m欢迎下次使用学员管理系统\033[0m")


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_manager()
            self.school_file.close()
        else:
            print("\033[31;1m没有数据文件请先创建\033[0m")
            self.init_school()
            self.school_manager()
            self.school_file.close()

    def init_school(self):
        '''
        创建学校的模块
        :return:
        '''
        self.school_file = shelve.open(setting.school_file)
        self.school_file["北京"] = School('北京',"北京总校")
        self.school_file["上海"] = School("上海","上海分校")

    def school_manager(self):
        '''
        定义一个学校管理模块
        :return:
        '''
        while True:
            for sch_name in self.school_file:
                print("学校名称【%s】"%(sch_name))
            sch_option = input("请选择管理校区按b返回>>>:").strip()
            if sch_option in self.school_file:
                self.sch_option = sch_option
                self.sch_obj = self.school_file[sch_option]
                print("学校地址【%s】"%self.sch_obj.sch_addr)
                while True:
                    menu = u'''                        欢迎来到老男孩【%s】校区
                        1、添加课程  add_course
                        2、添加班级  add_classroom
                        3、添加讲师  add_teacher
                        4、查看班级  show_classroom
                        5、查看课程  show_course
                        6、查看讲师  show_teacher
                        7、退出    exit
                    ''' %(self.sch_obj.sch_name)
                    print(menu)
                    menu_option = input("请选择以上操作命令按b返回>>>:").strip()
                    if hasattr(self,menu_option):
                        getattr(self,menu_option)()
                    else:
                        print("\033[31;1m请输入有效命令\033[0m")
            elif sch_option == 'b':
                break
            else:
                print('\033[31;1m请选择正确的学校名\033[0m')

    def add_course(self):
        '''
        添加课程模块
        :return:
        '''
        add_course_name = input("请输入课程名称>>>:")
        add_course_pice = input("请输入课程价格>>>:")
        add_course_time = input('请输入课程周期>>>:')
        if add_course_name in self.sch_obj.sch_course:
            print('\033[31;1m课程已存在\033[0m')
            # self.sch_obj.create_course(add_course_name,add_course_pice,add_course_time)
            # print("课程更新成功")
        else:
            self.sch_obj.create_course(add_course_name,add_course_pice,add_course_time)
            print('\033[32;1m课程添加成功\033[0m')
        self.school_file.update({self.sch_option:self.sch_obj})

    def add_classroom(self):
        '''
        添加教师模块
        :return:
        '''
        add_cla_name = input("请输入班级名称>>>:")
        add_clacourse_name = input("请输入关联课程名称>>>:")
        if add_cla_name not in self.sch_obj.sch_classroom:  #判断需添加的教室名称 不在学校教师方法中
            if add_clacourse_name in self.sch_obj.sch_course:   #判断 需添加的教室课程名称 在学校课程中
                class_course_obj = self.sch_obj.sch_course[add_clacourse_name]  #将需添加的课程名称 添加到班级中
                self.sch_obj.create_classroom(add_cla_name,class_course_obj)
                print('\033[32;1m班级添加成功\033[0m')
                self.school_file.update({self.sch_option:self.sch_obj})
            else:
                print("\033[31;1m课程不存在\033[0m")
        else:
            print("\033[31;1m班级已存在\033[0m")

    def add_teacher(self):
        '''
        添加教师模块
        :return:
        '''
        add_teacher_name = input('请输入招聘老师姓名>>>:').strip()
        add_teacher_slary = input('请输入老师薪资>>>:').strip()
        teacher_class_name = input('请输入老师关联班级>>>:').strip()
        if teacher_class_name in self.sch_obj.sch_classroom:   #判断输入的班级名称是否在sch_classroom中
            teacher_class_obj = self.sch_obj.sch_classroom[teacher_class_name]  #将输入的班级名称添加到学校教师字典中
            if add_teacher_name not in self.sch_obj.sch_teacher: #判断输入讲师名称是否在sch_teacher中
                self.sch_obj.create_teacher(add_teacher_name,add_teacher_slary,teacher_class_name,teacher_class_obj)
                #在添加讲师模块中加入 add_teacher_name,add_teacher_salary,teacher_class_name,teacher_class_obj
                print("\33[32;1m新讲师招聘成功\33[0m")
            else:
                self.sch_obj.update_teacher(add_teacher_name,teacher_class_name,teacher_class_obj)
                print("\33[32;1m讲师已经存在,信息更新完成\33[0m")
            self.school_file.update({self.sch_option:self.sch_obj})
        else:
            print("\33[31;1m系统错误:关联的班级不存在\33[0m")

    def show_course(self):
        '''
        查看课程模块
        :return:
        '''
        self.sch_obj.show_course()
        if len(self.sch_obj.sch_course) == 0:
            print("\033[31;1m该校区暂无课程\033[0m")

    def show_classroom(self):
        '''
        查看教室模块
        :return:
        '''
        self.sch_obj.show_classroom()
        if len(self.sch_obj.sch_classroom) == 0:
            print("\033[31;1m该校区暂无班级\033[0m")

    def show_teacher(self):
        '''
        查看教师模块
        :return:
        '''
        self.sch_obj.show_teacher()
        if len(self.sch_obj.sch_teacher) == 0:
            print("\033[31;1m该校区暂无教师\033[0m")

    def exit(self):
        '''
        退出程序模块
        :return:
        '''
        self.school_file.close()
        exit("\033[32;1m欢迎下次使用学员管理系统\033[0m")


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_manager()
            self.school_file.close()
        else:
            exit('数据文件不存在请先创建学校')

    def student_manager(self):
        while True:
            for sch_name in self.school_file:
                print('学校名称【%s】'%sch_name)
            sch_option = input("请选择注册学校按b返回>>>:").strip()
            if sch_option in self.school_file:
                self.sch_option = sch_option
                self.sch_obj = self.school_file[sch_option]
                print("学校地址:【%s】"%self.sch_obj.sch_addr)
                while True:
                    menu = '''                      欢迎来到【%s】校区
                    1、查看课程  show_course
                    2、查看班级  show_classroom
                    3、注册班级课程    add_stu
                    4、查看学生班级    show_stu_classroom
                    5、退出    exit
                    '''%self.sch_obj.sch_name
                    print(menu)
                    menu_option = input('请输入操作命令按b返回>>>:').strip()
                    if hasattr(self,menu_option):
                        getattr(self,menu_option)()
                    elif menu_option == 'b':
                        break
                    else:
                        print('\033[31;1m请输入有效操作命令\033[0m')
            elif sch_option == 'b':
                break
            else:
                print('\033[31;1m学校不存在\033[0m')

    def add_stu(self):
        add_stu_name = input('请输入注册学生姓名>>>:').strip()
        add_stu_age = input('请输入注册学生年龄>>>:').strip()
        add_stu_sex = input('请输入注册学生性别>>>:').strip()
        add_stu = input('请输入注册班级>>>:').strip()
        if add_stu in self.sch_obj.sch_classroom:
            if add_stu_name not in self.sch_obj.sch_student:
                self.sch_obj.create_student(add_stu_name,add_stu_age,add_stu_sex,add_stu)
                print("\033[32;1m学生注册成功\033[0m")
                self.school_file.update({self.sch_option:self.sch_obj})
            else:
                print("\033[31;1m该学生已注册\033[0m")
        else:
            print("\033[31;1m该班级不存在\033[0m")

    def show_course(self):
        self.sch_obj.show_course()
        if len(self.sch_obj.sch_course) == 0:
            print('\033[31;1m该校区暂无课程\033[0m')

    def show_classroom(self):
        self.sch_obj.show_classroom()
        if len(self.sch_obj.sch_classroom) == 0:
            print('\033[31;1m该校区暂无班级\033[0m')

    def show_stu_classroom(self):
        self.sch_obj.show_stu_classroom()

    def exit(self):
        self.school_file.close()
        exit("欢迎下次继续使用课程管理系统")
mian.py

databases目录下的数据库文件(系统自动生成)

�csrc.school
School
q )�q}q(X
   sch_classroomq}q(X   s14qcsrc.classroom
Classroom
q)�q}q(X   cls_nameq    hX   cls_course_objq
csrc.course
Course
q)�q}q
(X   course_nameqX   pythonqX   course_priceqX   13000qX   course_timeqX       2018-2019qubX   cls_studentq}qubX   s15qh)�q}q(h    hh
hh}qubuX
   sch_courseq}qhhsX   sch_studentq}qX   sch_addrqX   北京总校qX   sch_teacherq }q!X   sch_nameq"X   北京q#ub.                                                        �csrc.school
School
q )�q}q(X
   sch_classroomq}qX
   sch_courseq}qX   sch_studentq}qX   sch_addrq    X   上海分校q
X   sch_teacherq}qX   sch_nameq
X   上海qub.                                                                                                                                                                                                                                                                                                                                           �csrc.school
School
q )�q}q(X   sch_studentq}qX   sch_addrqX   北京总校qX
   sch_classroomq}q(X   s15q    csrc.classroom
Classroom
q
)�q}q(X   cls_studentq
}qX   jayqcsrc.student
Student
q)�q}q(X   stu_ageqX   24qX   stu_nameqhX   stu_sexqX   男qubsX   cls_nameqh    X   cls_course_objqcsrc.course
Course
q)�q}q(X   course_timeqX       2018-2019qX   course_priceqX   13000q X   course_nameq!X   pythonq"ububX   s14q#h
)�q$}q%(h
}q&(X   maryq'h)�q(}q)(hX   32q*hh'hX   nvq+ubX   kyoq,h)�q-}q.(hX   23q/hh,hX   女q0ubuhh#hhubuX   sch_nameq1X   北京q2X
   sch_courseq3}q4h"hsX   sch_teacherq5}q6X   alexq7csrc.teacher
Teacher
q8)�q9}q:(X   tea_nameq;h7X   tea_salq<X   40000q=X       tea_classq>}q?(X   s15q@hX   s14qAh$uubsub.
school.dat

src目录下的教室类文件

# -*- coding:utf-8 -*-
# Author:D.Gray
class Classroom(object):
    def __init__(self,cls_name,cls_course_obj):
        self.cls_name = cls_name
        self.cls_course_obj = cls_course_obj
        self.cls_student = {}
classroom.py

src目录下的课程类文件

# -*- coding:utf-8 -*-
# Author:D.Gray
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
course.py

src目录下的学校类文件

# -*- coding:utf-8 -*-
# Author:D.Gray
import os
from src.course import Course
from src.classroom import Classroom
from src.teacher import Teacher
from src.student import Student

class School(object):
    def __init__(self,sch_name,sch_addr):
        self.sch_name = sch_name
        self.sch_addr = sch_addr
        self.sch_teacher = {}
        self.sch_classroom = {}
        self.sch_course = {}
        self.sch_student = {}

    def create_course(self,course_name,course_price,course_time):
        #创建课程对象
        #根据课程名称为key,课程对象为value来建立对应关系
        sch_course_obj = Course(course_name,course_price,course_time)
        self.sch_course[course_name] = sch_course_obj

    def show_course(self):
        '''
        查看课程
        :return:
        '''
        for course_key in self.sch_course:
            show_course_obj = self.sch_course[course_key]
            print("\033[32;1m课程名称【%s】\t课程价格【%s】\t课程周期【%s】\033[0m"
                  % (show_course_obj.course_name,show_course_obj.course_price,show_course_obj.course_time))

    # def modify_course(self,**kwargs):
    # 根据key取出课程对象
    # for course in self.sch_course:
    #     course_obj = self.sch_course[course]
    # 修改course_obj的每个值
    # 保存

    def create_classroom(self,claroom_name,course_obj):
        #添加教师
        sch_classroom_obj = Classroom(claroom_name,course_obj)
        self.sch_classroom[claroom_name] = sch_classroom_obj

    def show_classroom(self):
        #查看教师
        for claroom_key in self.sch_classroom:
            show_claroom_obj = self.sch_classroom[claroom_key]
        for teacher_key in self.sch_teacher:
            teacher_name_obj = self.sch_teacher[teacher_key]
            print("\033[32;1m班级名称【%s】\t班级课程【%s】\t主讲教师【%s】\033[0m"%(show_claroom_obj.cls_name,
                                                    show_claroom_obj.cls_course_obj.course_name
                                                                   ,teacher_name_obj.tea_name))

    def create_teacher(self,sch_tea_name,sch_tea_sal,tea_class_name,class_obj):
        '''
        添加老师
        :param sch_tea_name:
        :param sch_tea_sal:
        :param tea_class_name:
        :param class_obj:
        :return:
        '''
        sch_teacher_obj = Teacher(sch_tea_name,sch_tea_sal)
        sch_teacher_obj.add_tea_class(tea_class_name,class_obj)
        self.sch_teacher[sch_tea_name] = sch_teacher_obj

    def show_teacher(self,*args):
        '''
        查看教师信息
        :return:
        '''
        for show_teacher_key in self.sch_teacher:
            show_teacher_obj = self.sch_teacher[show_teacher_key]
            class_list = []
            for tea_class_key in show_teacher_obj.tea_class:
                class_list.append(tea_class_key)
            print("\033[32;1m讲师名称【%s】讲师薪资【%s】关联班级【%s】\033[0m"
                  %(show_teacher_obj.tea_name,
                    show_teacher_obj.tea_sal,class_list))

    def update_teacher(self,update_tea_name,update_class_name,update_class_obj):
        '''
        更新教师信息
        :param update_tea_name: 更新教师姓名
        :param update_class_name: 更新教师班级名称
        :param update_class_obj: 更新教师班级内容
        :return:
        '''
        update_teacher_obj = self.sch_teacher[update_tea_name]
        update_teacher_obj.add_tea_class(update_class_name,update_class_obj)

    def create_student(self,sch_stu_name,sch_stu_age,sch_stu_sex,cls_name):
        '''
        创建学生
        :param sch_stu_name:学生姓名
        :param sch_stu_age: 学生年龄
        :param sch_stu_sex: 学生性别
        :param cls_name: 班级名称
        :return:
        '''
        sch_stu_obj = Student(sch_stu_name,sch_stu_age,sch_stu_sex)
        stu_class_obj = self.sch_classroom[cls_name]
        stu_class_obj.cls_student[sch_stu_name] = sch_stu_obj
        self.sch_classroom[cls_name] = stu_class_obj

    def show_stu_classroom(self):
        #学生视图查看班级学员信息
        for classroom_name_key in self.sch_classroom:
            cls_name_obj = self.sch_classroom[classroom_name_key]
            stu_list = []
            for cls_stu_name in cls_name_obj.cls_student:
                stu_list.append(cls_stu_name)
            print("\033[32;1m班级名称【%s】\t班级课程【%s】\t学员【%s】\033[0m"
                  %(cls_name_obj.cls_name,cls_name_obj.cls_course_obj.course_name,stu_list))

    def show_tea_clsinfo(self,tea_name):
        '''
        教师视图查看班级 学员信息
        :param tea_name:
        :return:
        '''
        show_teainfo_obj = self.sch_teacher[tea_name]
        for cls_info in show_teainfo_obj.tea_class:
            cls_info_obj = self.sch_classroom[cls_info]
            stu_list = []
            for stu_info in cls_info_obj.cls_student:
                stu_list.append(stu_info)
            print('\033[32;1m班级【%s】\t关联课程【%s】\t学员【%s】\033[0m'%(cls_info_obj.cls_name,
                                              cls_info_obj.cls_course_obj.course_name,stu_list))
school.py

src目录下的学生类文件

# -*- coding:utf-8 -*-
# Author:D.Gray
class Student(object):
    def __init__(self,stu_name,stu_age,stu_sex):
        self.stu_name = stu_name
        self.stu_age = stu_age
        self.stu_sex = stu_sex
student.py

src目录下的讲师类文件

# -*- coding:utf-8 -*-
# Author:D.Gray
class Teacher(object):
    def __init__(self,tea_name,tea_sal):
        self.tea_name = tea_name
        self.tea_sal = tea_sal
        self.tea_class = {}

    def add_tea_class(self,tea_class_name,tea_class_obj):
        self.tea_class[tea_class_name] = tea_class_obj
teacher.py

 

posted @ 2018-02-23 10:06  空s蝉灬  阅读(1806)  评论(1编辑  收藏  举报