面向对象程序设计------学校中心系统

 

角色:学校, 学员, 课程,讲师

要求:

  1. 创建北京,上海2所学校
  2. 创建linux,python,go3个课程,Linux\python 在北京开设
  3. 课程包括name,价格,周期
  4. 班级关联课程,讲师
  5. 创建学员时,选择学校
  6. 创建讲师角色时要关联学校
  7. 上面的操作产生的数据都通过Pickle序列化保存到文件里

实现功能:

  1. 创建课程 (1.查看当前已有课程  2.创建课程)
  2. 创建讲师(1.查看当前已有课程 2.创建讲师)
  3. 学员注册(1.查看当前已注册的学员 2.学员注册)
  4. 还可以创建班级,关联课程和学员(这个程序未写,另一个写了,太绕,不想加了)
import pickle
import os

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

__db_main = BASE_DIR + r"\database\main_dict"
__db_stu_ = BASE_DIR + r"\database\stu_dict"


# 初始化数据库
def init_database():
    bj = School("北京", "北京市")
    sh = School("上海", "上海市")
    if not os.path.exists(__db_main):
        dict = {bj: {}, sh: {}}
        open_file(__db_main, 'wb', dict)

    if not os.path.exists(__db_stu_):
        dict = {bj: {}, sh: {}}
        open_file(__db_stu_, 'wb', dict)


# 数据库写入,读出
def open_file(file, mode, *args):
    if mode == "wb":
        dict = args
        with open(file, "wb") as f:
            f.write(pickle.dumps(dict))

    elif mode == "rb":
        with open(file, "rb") as f:
            dict = pickle.loads(f.read())
        return dict

在main_dict文件中数据的存储方式是{bj:{course:{"teacher_name":teacher,"teacher_name":teacher}}} ,里面的bj,course和teacher都是实例化对象

在stu_dict文件中数据的存储方式也是这样

 功能实现:

# 创建学校
class School(object):

    def __init__(self, name, addr):
        self.name = name
        self.addr = addr

    def print_school(self):
        print("学校名:【%s】\t地址:【%s】" % (self.name, self.addr))

    def create_course(self, dict, course, file):
        # 数据库添加课程
        dict[self][course] = {}  # 原本数据库=={school:{}},现在则是=={school:{course:{}}}
        open_file(file, "wb", dict)

    # 数据库添加讲师
    def create_teacher(self, dict, course, teacher, file):
        dict[self][course] = {"teacher": teacher}
        open_file(file, "wb", dict)

    # 数据库添加学生
    def create_student(self, dict, course, name, student, file):
        dict[self][course][name] = student
        open_file(file, "wb", dict)


# 创建班级
class Grade(object):

    def __init__(self, name, course, teacher):
        self.name = name
        self.course = course
        self.teacher = teacher

    def print_grade(self):
        # 查看班级信息
        print("班级:【%s】\t课程:【%s】\t讲师:【%s】"
              % (self.name, self.course, self.teacher))


# 创建课程
class Course(object):

    def __init__(self, name, price, time):
        self.name = name
        self.price = price
        self.time = time

    def print_course(self):
        print("课程:【%s】\t价格:【¥%s】\t周期:【%s个月】"
              % (self.name, self.price, self.time))


# 创建讲师
class Teacher(object):
    def __init__(self, name, age, course):
        self.course = course
        self.name = name
        self.age = age

    def print_teacher(self):
        print('课程【%s】\t讲师【%s】' % (self.course, self.name))


# 创建学生
class Student(object):

    def __init__(self, name, age, course):
        self.name = name
        self.age = age
        self.course = course

    def print_student(self):
        print('课程【%s】\t学生【%s】' % (self.course, self.name))


# 打印信息函数:
def print_info(dict, mode):
    dict_info = {}

    if dict:
        for key in dict:
            if mode == "school":
                key.print_school()

            elif mode == "course":
                key.print_course()

            elif mode == "teacher":
                dict[key].print_teacher()  # dict == {"teacher":teacher}

            if type(key) != str:  # key值不是字符串
                dict_info[key.name] = key
    else:
        print(dict_info)

    return dict_info


# 创建课程具体操作
def create_courses():
    dict_main = open_file(__db_main, "rb")[0]  # 主字典
    print(dict_main)  # 不加[0],dict_main是个元组类型,并不是想的字典类型(原因不明)
    dict_school = print_info(dict_main, "school")  # 打印学校信息
    print(dict_school)

    school_name = input("请选择你要进入的学校:")

    if school_name in dict_school:
        school = dict_school[school_name]  # school 就是类School的实例化对象
        choice = options(list_course)  # 打印选项
        if choice == "1":
            print_info(dict_main[school], "course")

        elif choice == "2":
            print("\33[32;0m学校【%s】目前已经有的课程\33[0m".center(40, "-") % school.name)
            # 打印课程信息
            dict_course = print_info(dict_main[school], "course")
            while True:
                if_cont = input("\n\33[34;0m是否要创建课程 【y】创建 【b】退出\33[0m:")
                if if_cont == "y":
                    course_name = input("\33[34;0m输入要创建的课程\33[0m:").strip()
                    if course_name not in dict_course:
                        price = input("\33[34;0m输入课程 【%s】 的价格\33[0m:" % course_name)
                        time = input("\33[34;0m输入课程 【%s】 的周期(月)\33[0m:" % course_name)
                        course = Course(course_name, price, time)  # 创建课程
                        school.create_course(dict_main, course, __db_main)  # 关联课程和学校
                        school.create_course(dict_main, course, __db_stu_)
                        dict_course = print_info(dict_main[school], "course")

                    else:
                        print("\33[31;0m错误:当前课程 【%s】 已经存在\33[0m" % course_name)
                elif if_cont == "b":
                    break


# 创建讲师具体操作
def create_teachers():
    dict_main = open_file(__db_main, "rb")[0]  # 主字典
    dict_school = print_info(dict_main, "school")  # 打印学校信息
    school_name = input("请选择你要进入的学校:")

    if school_name in dict_school:
        school = dict_school[school_name]  # 得到实例化对象school
        choice = options(list_teacher)

        if choice == "1":  # 打印课程与讲师的关系
            print("\33[32;0m学校【%s】目前已经有的课程与讲师\33[0m".center(40, "-") % school.name)
            for key in dict_main[school]:  # key == 实例化course
                dict_teacher = dict_main[school][key]

                if dict_teacher == {}:  # 等于空字典说明还未创建teacher这个对象(就是还没有往数据库添加过讲师)
                    teacher = Teacher("None", "None", key.name)
                    teacher.print_teacher()
                else:
                    teacher = dict_teacher["teacher"]
                    teacher.print_teacher()

        elif choice == "2":
            if_cont = input("\n\33[34;0m是否要招聘讲师 【y】招聘 【b】退出\33[0m:")
            if if_cont == "y":
                teacher_name = input("\33[34;0m输入要招聘讲师的名字\33[0m:").strip()
                teacher_age = input("\33[34;0m输入要招聘讲师的年龄\33[0m:").strip()
                course_name = input("\33[34;0m输入讲师【%s】要授课的课程\33[0m:" % teacher_name).strip()

                # dict_course = {"课程名":course,"":course1}
                dict_course = print_info(dict_main[school], "course")
                if course_name in dict_course:
                    course = dict_course[course_name]  # 获取实例化course
                    # dict_teacher = print_info(dict_main[school][course])
                    if teacher_name not in dict_main[school][course]:
                        teacher = Teacher(teacher_name, teacher_age, course_name)
                        school.create_teacher(dict_main, course, teacher, __db_main)
                        print("创建成功")
                    else:
                        print("\33[31;0m错误:教师【%s】已经被聘用\33[0m" % teacher_name)
                else:
                    print("\33[31;0m错误:课程【%s】不存在,请先创建课程\33[0m" % course_name)


# 创建学员(注册)
def create_students():
    dict_main = open_file(__db_stu_, "rb")[0]  # 主字典
    dict_school = print_info(dict_main, "school")  # 打印学校信息
    school_name = input("请选择你要进入的学校:")

    if school_name in dict_school:
        school = dict_school[school_name]  # 得到实例化对象school
        choice = options(list_student)

        if choice == "1":  # 打印课程与学生的关系
            print("\33[32;0m学校【%s】目前已经有的课程与学生\33[0m".center(40, "-") % school.name)
            # 下面是有课程的情况
            for key in dict_main[school]:  # key == 实例化course
                dict_student = dict_main[school][key]
                print(dict_student)
                if dict_student == {}:
                    print(dict_student)
                else:
                    for key in dict_student:
                        dict_student[key].print_student()

        elif choice == "2":
            if_cont = input("\n\33[34;0m是否要进行学员注册 【y】招聘 【b】退出\33[0m:")
            if if_cont == "y":

                student_name = input("\33[34;0m输入名字\33[0m:").strip()
                student_age = input("\33[34;0m输入年龄\33[0m:").strip()
                course_name = input("\33[34;0m输入要学习的课程\33[0m:").strip()

                # dict_course = {"课程名":course,"":course1}
                # 打印课程信息
                dict_course = print_info(dict_main[school], "course")
                if course_name in dict_course:
                    course = dict_course[course_name]  # 获取实例化course
                    if_cont1 = input("\n\33[34;0m是否要进行缴费 【y】缴费 【b】退出\33[0m:")
                    if if_cont1 == "y":
                        money = input("请支付相关金额:")
                        if money == course.price:
                            if student_name not in dict_main[school][course]:
                                student = Student(student_name, student_age, course_name)
                                school.create_student(dict_main, course, student_name, student, __db_stu_)
                                print("创建成功")
                            else:
                                print("\33[31;0m错误:学生【%s】已经注册\33[0m" % student_name)
                else:
                    print("\33[31;0m错误:课程【%s】不存在,请先创建课程\33[0m" % course_name)


def options(mode_list):
    # 打印可选择的操作模式,并返回选择值
    for i, v in enumerate(mode_list):
        print(i + 1, v)
    choice = input("\33[34;0m选择要进入模式\33[0m:")
    return choice

主程序:



def start():
    while True:
        print("\33[35;1m欢迎进入选课系统\33[0m".center(50, "*"))
        choice = options(list_main)
        if choice == "1":
            create_courses()  # 创建课程
        if choice == "2":
            create_teachers()  # 创建讲师
        if choice == "3":
            create_students()  # 创建学员
        if choice == "4":
            break


if __name__ == '__main__':
    init_database()
    list_main = ["课程中心", "讲师中心", "学员注册", "退出"]
    list_course = ["查看当前已有的课程", "创建课程"]
    list_teacher = ["查看当前已有的讲师", "创建讲师"]
    list_student = ["查看当前已有学生信息", "学员注册"]
    start()

                                   不论代码的质量,第一次写了300行的代码,纪念一下!!!

                                          (っ•̀ω•́)っ✎⁾⁾ 我爱学习

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值