码迷,mamicode.com
首页 > 编程语言 > 详细

python选课系统完整版

时间:2018-08-31 12:36:06      阅读:736      评论:0      收藏:0      [点我收藏+]

标签:class   显示   说明   NPU   位置   creat   分享图片   学生   ict   

一、需求

角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程 
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校, 
6. 提供两个角色接口
7. 学员视图, 可以注册, 交学费, 选择班级,
8. 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩 
9. 管理视图,创建讲师, 创建班级,创建课程

10. 上面的操作产生的数据都通过pickle序列化保存到文件里

 

二、代码

技术分享图片
import os, pickle

BASR_DIR = os.path.dirname(os.path.abspath(__file__))
__school_db = os.path.join(BASR_DIR, school_db)
__teacher_db = os.path.join(BASR_DIR, teacher_db)


def init_database():
    if not os.path.exists(__school_db):
        bj = School(北京, 北京沙河)
        sh = School(上海, 上海市)
        dic = {bj: [], sh: []}
        file_opration(__school_db, wb, dic)
    if not os.path.exists(__teacher_db):
        dic = {}
        file_opration(__teacher_db, wb, dic)


def file_opration(file, mode, *args):
    if mode == wb:
        with open(file, mode)as f:
            f.write(pickle.dumps(args[0]))
    if mode == rb:
        with open(file, mode) as f:
            return pickle.loads(f.read())


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

    def msg_display(self):
        print(校区名称:\033[1;32m%s\033[0m 校区位置:\033[1;32m %s\033[0m % (self.name, self.addr))

    def create_course(self, course_obj, school_dict, file):
        school_dict[self].append(course_obj)
        file_opration(file, wb, school_dict)


class Course:
    def __init__(self, name, price, time):
        self.name = name
        self.price = price
        self.time = time
        self.classes = {}

    def msg_display(self):
        print(【课程】:\033[1;32m%s\033[0m 【价格】:\033[1;32m%s\033[0m 【周期】:\033[1;32m%s\033[0m % (
        self.name, self.price, self.time))

    def create_class(self, class_obj):
        self.classes[class_obj.name] = class_obj


class Class:
    def __init__(self, name):
        self.name = name
        self.students = {}
        self.teachers = {}


class Teacher:
    def __init__(self, name):
        self.name = name


class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age


def main_center():
    flag = True
    while flag:
        print(********\033[1;35m欢迎来到管理员系统\033[0m!**********)
        choice = input(输入1:增加课程\n输入2:增加班级\n输入3:增加教师\n输入4:返回\n).strip()
        school_dict = file_opration(__school_db, rb)
        if choice == 1:
            for i in school_dict:
                i.msg_display()
            while 1:
                school_choice = input("请输入需要创建课程的校区名称:\n")
                if school_choice == 北京 or school_choice == 上海:
                    for i in school_dict:
                        if i.name == school_choice:
                            school_obj = i
                    course_name = input("请输入课程名称\n")
                    price = input("请输入课程价格\n")
                    time = input("请输入课程周期\n")
                    course_obj = Course(course_name, price, time)
                    school_obj.create_course(course_obj, school_dict, __school_db)
                    print("\033[1;35m增加课程成功\033[0m")
                    break
                else:
                    print("\033[1;35m别瞎几把乱输!输入学校名称,懂??\033[0m")

        elif choice == 2:
            for i in school_dict:
                i.msg_display()
            while flag:
                school_choice = input("请输入需要创建班级的校区名称:\n")
                if school_choice == 北京 or school_choice == 上海:
                    for i in school_dict:
                        if i.name == school_choice:
                            school_obj = i
                    if not school_dict[school_obj]:
                        print("\033[1;35m你脑袋在想啥?没建课程,就想创班级??\033[0m")
                        break
                    print(******\033[1;32m%s校区已有的课程如下:\033[0m****** % (school_choice))
                    course_name_list = []
                    for i in school_dict[school_obj]:
                        course_name_list.append(i.name)
                        i.msg_display()
                    while 1:
                        course_name = input("请输入课程名称\n")
                        if course_name in course_name_list:
                            for i in school_dict[school_obj]:
                                if i.name == course_name:
                                    if i.classes:
                                        print("%s课程已有的班级如下:" % (i.name))
                                        for j in i.classes:
                                            print(j)
                                    else:
                                        print("该课程该没有班级,请创建。")
                                    class_name = input("请输入要新建的班级名称:\n")
                                    new_class_obj = Class(class_name)
                                    i.create_class(new_class_obj)
                                    file_opration(__school_db, wb, school_dict)
                                    print("\033[1;32m创建成功!\033[0m")
                                    flag = False
                                    break
                            break
                        else:
                            print("\033[1;35m输入已有课程的名字!understand?\033[0m")


                else:
                    print("\033[1;35m别瞎几把乱输!输入学校名称,懂??\033[0m")

        elif choice == 3:
            flag = True
            for i in school_dict:
                i.msg_display()
            while flag:
                school_choice = input("请输入需要招聘教师的校区名称:\n")
                if school_choice == 北京 or school_choice == 上海:
                    for i in school_dict:
                        if i.name == school_choice:
                            school_obj = i
                    if not school_dict[school_obj]:
                        print("\033[1;35m你脑袋在想啥?课程都没有,招老师去干吗??\033[0m")
                        break
                    print(******\033[1;32m%s校区已有的课程如下:\033[0m****** % (school_choice))
                    course_name_list = []
                    for i in school_dict[school_obj]:
                        course_name_list.append(i.name)
                        i.msg_display()
                    flag2 = True
                    while flag2:
                        course_name = input("请输入需要招聘教师的课程名称\n")
                        if course_name in course_name_list:
                            for i in school_dict[school_obj]:
                                if i.name == course_name:
                                    if i.classes:
                                        print("%s课程已有的班级如下:" % (i.name))
                                        for j in i.classes:
                                            print(j)
                                    else:
                                        print("该课程该没有班级,请创建后再进行教师招聘。")
                                        flag2 = False
                                        flag = False
                                        break
                                    while 1:
                                        class_name = input("请输入要招聘教师的班级名称:\n").strip()
                                        teacher_name = input("请输入教师名字:\n")
                                        teacher_obj = Teacher(teacher_name)
                                        if class_name in i.classes:
                                            i.classes[class_name].teachers[teacher_name] = teacher_obj
                                            file_opration(__school_db, wb, school_dict)
                                            print("\033[1;32m招聘成功!\033[0m")
                                        else:
                                            print("\033[1;35m输入班级名称!!!\033[0m")
                                        flag = False
                                        break
                            break
                        else:
                            print("\033[1;35m输入已有课程的名字!understand?\033[0m")


                else:
                    print("\033[1;35m别瞎几把乱输!输入学校名称,懂??\033[0m")
        elif choice == 4:
            break
        else:
            print("\033[1;35m别瞎几把乱输!输入1、2、3,懂??\033[0m")


def studet_center():
    print(********\033[1;35m欢迎来到学生系统\033[0m!**********)
    school_dict = file_opration(__school_db, rb)
    while 1:
        choice = input(输入1:报名\n输入2:返回\n).strip()
        if choice == 1:
            student_name = input("请输入您的姓名:\n").strip()
            student_age = input("请输入您的年龄:\n").strip()
            student_obj = Student(student_name, student_age)
            flag = True
            for i in school_dict:
                i.msg_display()
            while flag:
                school_choice = input("请输入需要要报名的校区名称:\n")
                if school_choice == 北京 or school_choice == 上海:
                    for i in school_dict:
                        if i.name == school_choice:
                            school_obj = i
                    if not school_dict[school_obj]:
                        print("\033[1;35m你脑袋在想啥?课程都没有,报啥名??\033[0m")
                        break
                    print(******\033[1;32m%s校区已有的课程如下:\033[0m****** % (school_choice))
                    course_name_list = []
                    for i in school_dict[school_obj]:
                        course_name_list.append(i.name)
                        i.msg_display()
                    flag2 = True
                    while flag2:
                        course_name = input("请输入需要加入的课程名称\n")
                        if course_name in course_name_list:
                            for i in school_dict[school_obj]:
                                if i.name == course_name:
                                    if i.classes:
                                        print("%s课程已有的班级如下:" % (i.name))
                                        for j in i.classes:
                                            print(j)
                                    else:
                                        print("该课程班级都没有,报啥子名,去哪上课??")
                                        flag2 = False
                                        flag = False
                                        break
                                    while 1:
                                        class_name = input("请输入加入的班级名称:\n").strip()
                                        if class_name in i.classes:
                                            print("这门课要%s元,请确认你有钱吗?" % (i.price))
                                            stu_choice = input("这么贵你还要报名吗?Y/N\n").strip()
                                            if stu_choice.lower() == y:
                                                i.classes[class_name].students[student_name] = student_obj
                                                file_opration(__school_db, wb, school_dict)
                                                print("\033[1;32m报名成功!\033[0m")
                                                flag = False
                                                break
                                            else:
                                                print("\033[1;32m穷鬼!!\033[0m\n")
                                                flag = False
                                                break
                                        else:
                                            print("\033[1;35m输入班级名称!!!\033[0m")

                            break
                        else:
                            print("\033[1;35m输入已有课程的名字!understand?\033[0m")


                else:
                    print("\033[1;35m别瞎几把乱输!输入学校名称,懂??\033[0m")

        elif choice == 2:
            break
        else:
            print("\033[1;35m别瞎几把乱输!输入1、2,懂??\033[0m")


def teacher_center():
    print(********\033[1;35m欢迎来到教师管理系统\033[0m!**********)
    teacher_name = input("请输入您的姓名").strip()
    school_dict = file_opration(__school_db, rb)
    while 1:
        choice = input(输入1:查看学生信息  输入2:选择班级上课  输入3:返回\n).strip()
        teacher_exit = False
        if choice == 1:
            for i in school_dict:
                for course_obj in school_dict[i]:
                    for class_obj in course_obj.classes.values():
                        if teacher_name in class_obj.teachers:
                            teacher_exit = True
                            print("***您的班级:\033[1;35m%s\033[0m,当前学生如下:" % (class_obj.name))
                            for i in class_obj.students:
                                print(i, end=  )
                            print()
            if teacher_exit == False:
                print("没有这个老师,请重新检查后再输入")
                break

        elif choice == 2:
            class_dic = {}
            for i in school_dict:
                for course_obj in school_dict[i]:
                    for class_obj in course_obj.classes.values():
                        if teacher_name in class_obj.teachers:
                            teacher_exit = True
                            class_dic[class_obj.name] = class_obj
            if teacher_exit == False:
                print("没有这个老师,请重新检查后再输入")
                break
            print("您当前授课的班级如下请输出班级名称:")
            for j in class_dic:
                print(j, end=‘‘)
            print()
            cls_choice = input(">>>:").strip()
            if cls_choice in class_dic:
                print("成功在\033[1;32m%s\033[0m上课" % (cls_choice))

        elif choice == 3:
            break

        else:
            print("\033[1;35m别瞎几把乱输!输入1、2、3,懂??\033[0m")


def main():
    print(********\033[1;35m欢迎来到老男孩学生管理系统\033[0m!**********)
    while 1:
        choice = input(输入1:学生登录\n输入2:教师登录\n输入3:管理员登录\n输入q:退出\n).strip()
        if choice == 1:
            studet_center()
        elif choice == 2:
            teacher_center()
        elif choice == 3:
            main_center()
        elif choice.upper() == Q:
            break
        else:
            print("\033[1;35m别瞎几把乱输!输入学校名称,懂??\033[0m")


if __name__ == __main__:
    init_database()
    main()
View Code

 

三、关于程序的相关说明

1、这个系统支持一个课程有多个班级,一个同学选择多门课程,一个老师任教多个班级。
2、系统功能基本实现,但是代码还很low,而且还没拆分程序,做文件目录。

 

 

三、测试

登录界面

技术分享图片

 增加课程

 技术分享图片

增加班级(选择学校会显示已有的课程,班级):

技术分享图片

 增加教师:

技术分享图片

 

学生报名:

技术分享图片

技术分享图片

 

 老师方法:

技术分享图片

技术分享图片

 

 注:这个系统支持一个课程有多个班级,一个同学选择多门课程,一个老师任教多个班级。以上代码仅是部分功能测试。

 

python选课系统完整版

标签:class   显示   说明   NPU   位置   creat   分享图片   学生   ict   

原文地址:https://www.cnblogs.com/linshuhui/p/9565134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!