• 选课系统练习程序


    需求如下:

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

    如果用类的继承,代码应该会缩减不少,偷懒不做了。

    管理视图:

    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:3
    请输入用户名:admin
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:3
    请输入学校名称:oldboy沙河校区
    请输入学校所在城市:北京
    请输入学校地址:北京沙河
    学校创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:3
    请输入学校名称:oldboy上海校区
    请输入学校所在城市:上海
    请输入学校地址:上海黄浦
    学校创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    添加学校
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:3
    请输入学校名称:oldboy上海校区
    请输入学校所在城市:上海
    请输入学校地址:上海黄浦
    学校创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:4
    请输入学校:oldboy沙河校区
    请输入课程名称:python
    请输入课程周期:360
    请输入课程价格:5888
    课程创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:4
    请输入学校:oldboy上海校区
    请输入课程名称:go
    请输入课程周期:180
    请输入课程价格:4888
    课程创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:4
    请输入学校:oldboy上海校区
    请输入课程名称:go
    请输入课程周期:180
    请输入课程价格:4888
    课程创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:4
    请输入学校:oldboy沙河校区
    请输入课程名称:linux
    请输入课程周期:400
    请输入课程价格:4000
    课程创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:
    添加课程
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:1
    请输入老师姓名:egon
    请输入初始密码:123456
    请输入工资:50000
    请输入学校:oldboy沙河校区
    老师创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:1
    请输入老师姓名:alex
    请输入初始密码:123456
    请输入工资:60000
    请输入学校:oldboy沙河校区
    老师创建成功!
    
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:1
    请输入老师姓名:aobama
    请输入初始密码:123456
    请输入工资:10
    请输入学校:oldboy沙河校区
    老师创建成功!
    添加老师
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:3
    请输入用户名:admin
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:5
    请输入班级名称python第1期
    请输入班级开课日期2017-11-28
    请输入所属学校名称oldboy沙河校区
    请输入班级所学的课程python
    请输入班级负责的老师egon
    课程创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:5
    请输入班级名称go第1期
    请输入班级开课日期2015-11-22
    请输入所属学校名称oldboy上海校区
    请输入班级所学的课程go
    请输入班级负责的老师aaa
    课程创建成功!
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:5
    请输入班级名称linux第1期
    请输入班级开课日期2017-11-11
    请输入所属学校名称oldboy沙河校区
    请输入班级所学的课程linux
    请输入班级负责的老师alex
    课程创建成功!
    创建班级
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:3
    请输入用户名:admin
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:2
    请输入学生姓名:lit1
    请输入初始密码:123456
    请输入性别(F or M):M
    请输入学习的课程:python
    请输入学校:oldboy沙河校区
    请输入班级:python第1期
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:2oldboy沙河校区
    请输入学生姓名:lit2
    请输入初始密码:123456
    请输入性别(F or M):M
    请输入学习的课程:python
    请输入学校:oldboy沙河校区
    请输入班级:python第1期
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:2
    请输入学生姓名:lit3
    请输入初始密码:123456
    请输入性别(F or M):M
    请输入学习的课程:python
    请输入学校:oldboy沙河校区
    请输入班级:python第1期
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:2
    请输入学生姓名:lit4
    请输入初始密码:123456
    请输入性别(F or M):M
    请输入学习的课程:go
    请输入学校:oldboy上海校区
    请输入班级:go第1期
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:2
    请输入学生姓名:lit5
    请输入初始密码:123456
    请输入性别(F or M):F
    请输入学习的课程:linux
    请输入学校:oldboy沙河校区
    请输入班级:linux第1期
    
    -------------选课系统欢迎您!-------------1】添加老师
    【2】添加学生
    【3】添加校区
    【4】新建课程
    【5】新建班级
    【6】返回
    
    请选择:6
    
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:4
    选课系统和您再见!
    创建学生

    老师视图:

    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:2
    请输入用户名:egon
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】创建上课记录
    【2】课程作业打分
    【3】查看签到
    【4】返回
    
    请选择:1
    请输入班级名称python第1期
    请输入学习日期,如2017-11-28:2017-11-28
    上课记录创建成功!
    创建上课记录
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:2
    请输入用户名:egon
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】创建上课记录
    【2】课程作业打分
    【3】查看签到
    【4】返回
    
    请选择:3
    请输入班级名称:python第1期
    班级上课记录如下:
    python第1期2017-11-28
    请输入上课记录,以便查询对应课程的签到状态:python第1期2017-11-28
    lit1     未签到
    lit2     未签到
    lit3     未签到
    查看签到
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:2
    请输入用户名:egon
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】创建上课记录
    【2】课程作业打分
    【3】查看签到
    【4】返回
    
    请选择:2
    请输入班级名称python第1期
    班级学生姓名如下:
    lit1
    lit2
    lit3
    请输入班级内学生姓名:lit2
    班级课程如下:
    python第1期2017-11-28
    请输入课程记录python第1期2017-11-28
    请输入学生成绩(0-100):88
    打分

    学生视图:

    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:1
    
    -------------选课系统欢迎您!-------------1】注册
    【2】登录
    【3】返回
    
    请选择:2
    请输入用户名:lit4
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】交学费
    【2】上课签到
    【3】查看作业成绩
    【4】返回
    
    请选择:1
    python
    go
    linux
    请输入想学习的课程:go
    你已经学习了这个课程。
    
    -------------选课系统欢迎您!-------------1】交学费
    【2】上课签到
    【3】查看作业成绩
    【4】返回
    
    请选择:1
    python
    go
    linux
    请输入想学习的课程:python
    这个课程花费360元
    是否缴费Y or N:y
    请输入你要进入的班级
    python第1期
    请选择班级:python第1期
    你已经缴费成功。
    交学费
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:1
    
    -------------选课系统欢迎您!-------------1】注册
    【2】登录
    【3】返回
    
    请选择:2
    请输入用户名:lit2
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】交学费
    【2】上课签到
    【3】查看作业成绩
    【4】返回
    
    请选择:2
    python第1期2017-11-28
    请输入上面中的一个上课记录python第1期2017-11-28
    
    -------------选课系统欢迎您!-------------1】交学费
    【2】上课签到
    【3】查看作业成绩
    【4】返回
    
    请选择:4
    
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:2
    请输入用户名:egon
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】创建上课记录
    【2】课程作业打分
    【3】查看签到
    【4】返回
    
    请选择:3
    请输入班级名称:python第1期
    班级上课记录如下:
    python第1期2017-11-28
    请输入上课记录,以便查询对应课程的签到状态:python第1期2017-11-28
    lit1     未签到
    lit2     已签到
    lit3     未签到
    
    -------------选课系统欢迎您!-------------1】创建上课记录
    【2】课程作业打分
    【3】查看签到
    【4】返回
    
    请选择:
    学生签到
    -------------选课系统欢迎您!-------------1】学员入口
    【2】老师入口
    【3】管理员入口
    【4】退出
    
    请选择:1
    
    -------------选课系统欢迎您!-------------1】注册
    【2】登录
    【3】返回
    
    请选择:2
    请输入用户名:lit
    请输入密码:123456
    用户名密码错误
    请输入用户名:lit1
    请输入密码:123456
    
    -------------选课系统欢迎您!-------------1】交学费
    【2】上课签到
    【3】查看作业成绩
    【4】返回
    
    请选择:3
    lit1
    python第1期2017-11-28
    请输入对应的上课记录:python第1期2017-11-28
    你的成绩是 99-------------选课系统欢迎您!-------------1】交学费
    【2】上课签到
    【3】查看作业成绩
    【4】返回
    查看成绩

    测试添加的数据:

    所有用户的默认密码都设置的123456
    
    学校:
    oldboy沙河校区
    oldboy上海校区
    
    课程:
    python---北京
    go---上海
    linux---北京
    
    老师:
    egon/123456--oldboy沙河校区
    alex/123456--oldboy沙河校区
    aobama/123456--oldboy沙河校区
    bbb/123456--oldboy沙河校区
    aaa/123456--oldboy上海校区
    
    班级:
    python第1期--egon
    go第1期--aaa
    linux第1期--alex
    
    学生:
    lit1--python--python第1期
    lit2--python--python第1期
    lit3--python--python第1期
    lit4--go--go第1期
    lit5--linux--linux第1期
    测试数据

    总的流程图:

    主程序:

      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 # @Time    : 2017/11/19 16:10
      4 # @Author  : lichuan
      5 # @File    : select_course.py
      6 
      7 import uuid
      8 from config import template
      9 from module import common
     10 from module.users import Users
     11 import pickle
     12 import os
     13 from module.users import Users
     14 
     15 LOG_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+os.sep+'db'
     16 # DB_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+os.sep+'log'
     17 
     18 #学生类
     19 class Student:
     20     def __init__(self,name,passwd,xingbie,salary=0):
     21         self.name = name
     22         self.passwd = passwd
     23         self.xingbie = xingbie
     24         self.salary=salary
     25         self.course = []
     26         self.banji = []
     27         self.study_record = []
     28         self.ids = self.createid()
     29 
     30     @staticmethod
     31     def createid():
     32         return str(uuid.uuid1())
     33 
     34     # 创建的对象写入到文件
     35     def create_uuid_file(self):
     36         path = LOG_DIR + os.sep + self.ids
     37         with open(path, 'wb') as write_f:
     38             pickle.dump(self, write_f)
     39 
     40         # 更新student的列表(name,ids)以元组的方式存进列表
     41     def update_students_list(self):
     42         students_list_path = os.path.dirname(
     43             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'student_lists'
     44         student_list = self.get_student_list()
     45         for (student_name, student_id) in student_list:
     46             if student_name == self.name:
     47                 print('学生姓名已存在,请用别的名字!')
     48                 return True
     49 
     50         student_list.append((self.name, self.ids))
     51         with open(students_list_path, 'wb') as write_f:
     52             pickle.dump(student_list, write_f)
     53         return False
     54 
     55     @staticmethod
     56     def get_student_list():
     57         teachers_list_path = os.path.dirname(
     58             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'student_lists'
     59         if os.path.exists(teachers_list_path):
     60             with open(teachers_list_path, 'rb') as read_f:
     61                 teachers_list = pickle.load(read_f)
     62                 return teachers_list
     63         else:
     64             return []
     65 
     66     @staticmethod
     67     def get_obj_byid(ids):
     68         path = LOG_DIR + os.sep + ids
     69         with open(path, 'rb') as write_f:
     70             obj = pickle.load(write_f)
     71             return obj
     72 
     73     @staticmethod
     74     def get_obj_byname(student_name):
     75         school_list = Student.get_student_list()
     76         for (name, ids) in school_list:
     77             if student_name == name:
     78                 obj = Student.get_obj_byid(ids)
     79                 return obj
     80 
     81     # 判断学校名字是否在列表中,是返回Ture,否返回False
     82     @staticmethod
     83     def name_check(name):
     84         student_list = Student.get_student_list()
     85         for (student_name, ids) in student_list:
     86             if name == student_name:
     87                 return True
     88         return False
     89 
     90 #老师类
     91 class Teacher:
     92     teacher_list=[]
     93     def __init__(self,name,passwd,salary,school):
     94         self.name=name
     95         self.passwd=passwd
     96         self.salary=salary
     97         self.school=school
     98         self.ids=self.createid()
     99 
    100     @staticmethod
    101     def createid():
    102         return str(uuid.uuid1())
    103 
    104     @staticmethod
    105     def get_obj_byid(ids):
    106         path = LOG_DIR + os.sep + ids
    107         with open(path, 'rb') as write_f:
    108             obj = pickle.load(write_f)
    109             return obj
    110 
    111     @staticmethod
    112     def get_teacher_by_name(teacher_name):
    113         teacher_list = Teacher.get_teacher_list()
    114         for (name, ids) in teacher_list:
    115             if teacher_name == name:
    116                 obj = Teacher.get_obj_byid(ids)
    117                 return obj
    118 
    119     # 创建的对象写入到文件
    120     def create_uuid_file(self):
    121         path = LOG_DIR + os.sep + self.ids
    122         with open(path, 'wb') as write_f:
    123             pickle.dump(self, write_f)
    124 
    125     # 更新teacher的列表(name,ids)以元组的方式存进列表
    126     def update_teachers_list(self):
    127         teachers_list_path = os.path.dirname(
    128             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'teacher_lists'
    129         teacher_list = self.get_teacher_list()
    130         for (teacher_name, teacher_id) in teacher_list:
    131             if teacher_name == self.name:
    132                 print('老师已存在!')
    133                 return True
    134         teacher_list.append((self.name, self.ids))
    135         with open(teachers_list_path, 'wb') as write_f:
    136             pickle.dump(teacher_list, write_f)
    137         return False
    138 
    139     @staticmethod
    140     def get_teacher_list():
    141         teachers_list_path = os.path.dirname(
    142             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'teacher_lists'
    143         if os.path.exists(teachers_list_path):
    144             with open(teachers_list_path, 'rb') as read_f:
    145                 teachers_list = pickle.load(read_f)
    146                 return teachers_list
    147         else:
    148             return []
    149 
    150 #课程类
    151 class Course:
    152     course_list=[]
    153     def __init__(self,name,price,zhouqi):
    154         self.name=name
    155         self.price=price
    156         self.zhouqi=zhouqi
    157         self.banji=[]
    158         self.ids=self.createid()
    159 
    160     @staticmethod
    161     def createid():
    162         return str(uuid.uuid1())
    163 
    164     #通过名字获取到课程对象
    165     @staticmethod
    166     def get_course_by_name(course_name):
    167         course_list = Course.get_course_list()
    168         for (name,ids) in course_list:
    169             if name == course_name:
    170                 course_list_path = os.path.dirname(
    171                     os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + ids
    172                 if os.path.exists(course_list_path):
    173                     with open(course_list_path, 'rb') as read_f:
    174                         course = pickle.load(read_f)
    175                         return course
    176         print('课程不存在!')
    177         return None
    178 
    179     # 创建的对象写入到文件
    180     def create_uuid_file(self):
    181         path = LOG_DIR + os.sep + self.ids
    182         with open(path, 'wb') as write_f:
    183             pickle.dump(self, write_f)
    184 
    185     # 更新course的列表(name,ids)以元组的方式存进列表
    186     def update_course_list(self):
    187         course_list_path = os.path.dirname(
    188             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'course_lists'
    189         course_list = self.get_course_list()
    190         for (course_name, course_id) in course_list:
    191             if course_name == self.name:
    192                 print('课程已存在!')
    193                 return True
    194         course_list.append((self.name, self.ids))
    195         with open(course_list_path, 'wb') as write_f:
    196             pickle.dump(course_list, write_f)
    197             print('课程创建成功!')
    198         return False
    199 
    200     @staticmethod
    201     def get_course_list():
    202         course_list_path = os.path.dirname(
    203             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'course_lists'
    204         if os.path.exists(course_list_path):
    205             with open(course_list_path, 'rb') as read_f:
    206                 course_list = pickle.load(read_f)
    207                 return course_list
    208         else:
    209             return []
    210 
    211     def create_uuid_file(self):
    212         path = LOG_DIR + os.sep + self.ids
    213         with open(path, 'wb') as write_f:
    214             pickle.dump(self, write_f)
    215 
    216     @staticmethod
    217     def name_check(name):
    218         course_list = Course.get_course_list()
    219         for (course_name, ids) in course_list:
    220             if name == course_name:
    221                 return True
    222         return False
    223 
    224 #学校类
    225 class School:
    226     school_list=[]
    227     def __init__(self,name,address,city):
    228         self.name=name
    229         self.address=address
    230         self.city=city
    231         self.ids=self.createid()
    232         self.course=[]
    233         self.teacher=[]
    234 
    235     #创建的对象写入到文件
    236     def create_uuid_file(self):
    237         path = LOG_DIR + os.sep+self.ids
    238         with open(path,'wb') as write_f:
    239             pickle.dump(self,write_f)
    240 
    241     @staticmethod
    242     def createid():
    243         return str(uuid.uuid1())
    244 
    245     #根据uuid读取文件内容,变成具体的对象
    246     @staticmethod
    247     def get_obj_byid(ids):
    248         path = LOG_DIR + os.sep + ids
    249         with open(path,'rb') as write_f:
    250             obj=pickle.load(write_f)
    251             return obj
    252 
    253     @staticmethod
    254     def get_obj_byname(school_name):
    255         school_list=School.get_school_list()
    256         for (name,ids) in school_list:
    257             if school_name == name:
    258                 obj=School.get_obj_byid(ids)
    259                 return obj
    260 
    261     #更新school的列表(name,ids)以元组的方式存进列表
    262     def update_schools_list(self):
    263         schools_list_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+os.sep+'db'+os.sep+'school_lists'
    264         school_list=self.get_school_list()
    265         for (school_name,school_id) in school_list:
    266             if school_name == self.name:
    267                 print('学校已存在!')
    268                 return True
    269         school_list.append((self.name,self.ids))
    270         with open(schools_list_path,'wb') as write_f:
    271             pickle.dump(school_list,write_f)
    272             print('学校创建成功!')
    273         return False
    274 
    275     @staticmethod
    276     def get_school_list():
    277         schools_list_path = os.path.dirname(
    278             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'school_lists'
    279         if os.path.exists(schools_list_path):
    280             with open(schools_list_path,'rb') as read_f:
    281                 schools_list=pickle.load(read_f)
    282                 return schools_list
    283         else:
    284             return []
    285 
    286     #判断学校名字是否在列表中,是返回Ture,否返回False
    287     @staticmethod
    288     def name_check(name):
    289         school_list=School.get_school_list()
    290         for (school_name,ids) in school_list:
    291             if name == school_name:
    292                 return True
    293         return False
    294 
    295 #班级类
    296 class Classes:
    297 
    298     def __init__(self,name,open_date):
    299         self.name=name
    300         self.course_id=[]
    301         self.open_date=open_date
    302         self.teacher_id=[]
    303         self.student_id=[]
    304         self.class_record=[]
    305         self.ids=self.createid()
    306 
    307     # 更新class的列表(name,ids)以元组的方式存进列表
    308     def update_course_list(self):
    309         class_list_path = os.path.dirname(
    310             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'class_lists'
    311         class_list = self.get_class_list()
    312         for (course_name, course_id) in class_list:
    313             if course_name == self.name:
    314                 print('班级已存在!')
    315                 return True
    316         class_list.append((self.name, self.ids))
    317         with open(class_list_path, 'wb') as write_f:
    318             pickle.dump(class_list, write_f)
    319             print('课程创建成功!')
    320         return False
    321 
    322     @staticmethod
    323     def get_obj_byname(classes_name):
    324         class_list = Classes.get_class_list()
    325         for (name, ids) in class_list:
    326             if classes_name == name:
    327                 obj = School.get_obj_byid(ids)
    328                 return obj
    329 
    330 
    331     @staticmethod
    332     def get_obj_byid(ids):
    333         path = LOG_DIR + os.sep + ids
    334         if os.path.exists(path):
    335             with open(path, 'rb') as write_f:
    336                 obj = pickle.load(write_f)
    337                 return obj
    338 
    339     @staticmethod
    340     def get_class_list():
    341         class_list_path = os.path.dirname(
    342             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'class_lists'
    343         if os.path.exists(class_list_path):
    344             with open(class_list_path, 'rb') as read_f:
    345                 course_list = pickle.load(read_f)
    346                 return course_list
    347         else:
    348             return []
    349 
    350     @staticmethod
    351     def createid():
    352         return str(uuid.uuid1())
    353 
    354     # 创建的对象写入到文件
    355     def create_uuid_file(self):
    356         path = LOG_DIR + os.sep + self.ids
    357         with open(path, 'wb') as write_f:
    358             pickle.dump(self, write_f)
    359             # 判断学校名字是否在列表中,是返回Ture,否返回False
    360 
    361     @staticmethod
    362     def name_check(name):
    363         school_list = Classes.get_class_list()
    364         for (school_name, ids) in school_list:
    365             if name == school_name:
    366                 return True
    367         return False
    368 
    369 #学生签到学习记录
    370 # class StudyRecord:
    371 #     def __init__(self,address,record_date):
    372 #         # self.class_record=class_record
    373 #         self.address=address
    374 #         self.record_date=record_date
    375 #         self.id=self.createid()
    376 #
    377 #     @staticmethod
    378 #     def createid():
    379 #         return str(uuid.uuid1())
    380 #
    381 #     def createid(self,class_record):
    382 #         self.class_record=class_record
    383 
    384 #老师上课的记录
    385 class ClassRecord:
    386     def __init__(self,classes_name,study_date):
    387         self.name=classes_name+study_date
    388         self.classes=classes_name
    389         self.study_date=study_date
    390         self.student_name=[]
    391         self.score={}
    392         self.ids=self.createid()
    393 
    394     @staticmethod
    395     def createid():
    396         return str(uuid.uuid1())
    397 
    398     # 更新ClassRecord的列表(name,ids)以元组的方式存进列表
    399     def update_course_list(self):
    400         class_list_path = os.path.dirname(
    401             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'ClassRecord_lists'
    402         class_list = self.get_class_list()
    403         for (course_name, course_id) in class_list:
    404             if course_name == self.name:
    405                 print('上课记录已存在!')
    406                 return True
    407         class_list.append((self.name, self.ids))
    408         with open(class_list_path, 'wb') as write_f:
    409             pickle.dump(class_list, write_f)
    410             print('上课记录创建成功!')
    411         return False
    412 
    413     @staticmethod
    414     def get_obj_byname(classes_name):
    415         class_list = ClassRecord.get_class_list()
    416         for (name, ids) in class_list:
    417             if classes_name == name:
    418                 obj = ClassRecord.get_obj_byid(ids)
    419                 return obj
    420 
    421     @staticmethod
    422     def get_obj_byid(ids):
    423         path = LOG_DIR + os.sep + ids
    424         if os.path.exists(path):
    425             with open(path, 'rb') as write_f:
    426                 obj = pickle.load(write_f)
    427                 return obj
    428 
    429     @staticmethod
    430     def get_class_list():
    431         class_list_path = os.path.dirname(
    432             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + 'ClassRecord_lists'
    433         if os.path.exists(class_list_path):
    434             with open(class_list_path, 'rb') as read_f:
    435                 course_list = pickle.load(read_f)
    436                 return course_list
    437         else:
    438             return []
    439 
    440     @staticmethod
    441     def createid():
    442         return str(uuid.uuid1())
    443 
    444     # 创建的对象写入到文件
    445     def create_uuid_file(self):
    446         path = LOG_DIR + os.sep + self.ids
    447         with open(path, 'wb') as write_f:
    448             pickle.dump(self, write_f)
    449 
    450 #学生视图函数
    451 def func_student():
    452     #交学费
    453     def give_money(name):
    454         while True:
    455             course_list=Course.get_course_list()
    456             c=[]
    457             student = Student.get_obj_byname(name)
    458             for (i,j) in course_list:
    459                 print(i)
    460                 c.append(i)
    461             course=input('请输入想学习的课程:').strip()
    462             if course not in c:
    463                 print('输入错误!')
    464                 continue
    465             if course in student.course :
    466                 print('你已经学习了这个课程。')
    467                 break
    468             c=Course.get_course_by_name(course)
    469             print('这个课程花费%s元' %c.price)
    470             choice=input('是否缴费Y or N:').strip()
    471             if choice.upper() == 'Y':
    472                 print('请输入你要进入的班级')
    473                 for i in c.banji:print(i)
    474                 b=input('请选择班级:').strip()
    475                 if b not in c.banji:
    476                     print('input wrong!')
    477                     break
    478                 print('你已经缴费成功。')
    479                 student.course.append(course)
    480                 student.banji.append(b)
    481                 student.create_uuid_file()
    482                 classes=Classes.get_obj_byname(b)
    483                 classes.student_id.append(name)
    484                 classes.create_uuid_file()
    485                 break
    486             elif choice.upper() == 'N':
    487                 break
    488             else:
    489                 print('输入错误!')
    490                 continue
    491 
    492     #查看上课成绩
    493     def view_score(name):
    494         while True:
    495             student=Student.get_obj_byname(name)
    496             if len(student.banji) == 0:
    497                 print('你还没有班级!')
    498                 break
    499             elif len(student.banji) == 1:
    500                 banji=student.banji[0]
    501             else:
    502                 for b in student.banji:
    503                     print(b)
    504                 banji=input('请输入要查询的班级').strip()
    505                 if banji not in student.banji:
    506                     print('输入错误!')
    507                     break
    508             classes=Classes.get_obj_byname(banji)
    509             if len(classes.class_record) == 0:
    510                 print('该班级还没有上课记录,没有成绩!')
    511                 break
    512             for c in classes.class_record:
    513                 print(c)
    514             record=input('请输入对应的上课记录:').strip()
    515             if record not in classes.class_record:
    516                 print('输入错误!')
    517                 break
    518             class_record=ClassRecord.get_obj_byname(record)
    519             if name not in class_record.score:
    520                 print('尚未评定成绩!')
    521             else:
    522                 print('你的成绩是 %s 分' %class_record.score[name])
    523             break
    524 
    525     #上课签到
    526     def study_record(name):
    527         student=Student.get_obj_byname(name)
    528         if len(student.banji) == 0:
    529             print('你还没有交学费报班级呢')
    530             return
    531         if len(student.banji) == 1:
    532             banji=student.banji[0]
    533         else:
    534             for b in student.banji:
    535                 print(b)
    536             banji=input('请输入你要签到的班级:').strip()
    537         banji=Classes.get_obj_byname(banji)
    538         if len(banji.class_record) == 0:
    539             print('班级还没有创建上课记录')
    540             return
    541         for i in banji.class_record:
    542             print(i)
    543         record = input('请输入上面中的一个上课记录').strip()
    544         class_record = ClassRecord.get_obj_byname(record)
    545         class_record.student_name.append(name)
    546         class_record.create_uuid_file()
    547 
    548     func_view = {
    549         '1':give_money,
    550         '2':study_record,
    551         '3':view_score,
    552     }
    553     def student_view(name):
    554         while True:
    555             print(template.index_student_menu)
    556             choice = input('请选择:').strip()
    557             if choice.isdigit() and int(choice) in range(1, 4):
    558                 func_view[choice](name)
    559             elif choice.isdigit() and int(choice) == 4:
    560                 break
    561             else:
    562                 print('输入错误')
    563                 continue
    564 
    565     def login():
    566         count = 0
    567         while True:
    568             count += 1
    569             if count >= 3:
    570                 print('错误次数太多')
    571                 break
    572             name=common.login('student')
    573             if name:
    574                 student_view(name)
    575                 break
    576 
    577     def segister():
    578         name = input('请输入学生姓名:').strip()
    579         passwd = input('请输入初始密码:').strip()
    580         xingbie = input('请输入性别(F or M):').strip()
    581         if Student.name_check(name):
    582             print('学生姓名已存在!')
    583             return
    584         if xingbie.upper() != 'F' and xingbie.upper() != 'M':
    585             print('性别输入错误!')
    586             return
    587 
    588         password=common.encrypt(passwd)
    589         student = Student(name, password, xingbie.upper())
    590         student.update_students_list()
    591         student.create_uuid_file()
    592         Users['student'][name]= {'name': name, 'password': password}
    593         common.update_users(Users)
    594 
    595     func={
    596         '1':segister,
    597         '2':login
    598     }
    599 
    600     while True:
    601         print(template.index_student_default_menu)
    602         choice=input('请选择:').strip()
    603         if choice.isdigit() and int(choice) in range(1,3):
    604             func[choice]()
    605             break
    606         elif choice.isdigit() and int(choice) == 3:
    607             break
    608         else:
    609             print('输入错误!')
    610             continue
    611 
    612 #管理视图的函数
    613 def admin():
    614 
    615     def new_teacher():
    616         while True:
    617             name=input('请输入老师姓名:').strip()
    618             passwd=input('请输入初始密码:').strip()
    619             salary=input('请输入工资:').strip()
    620             school_name=input('请输入学校:').strip()
    621             if not School.name_check(school_name):
    622                 print('学校不存在!')
    623                 continue
    624             if not salary.isdigit():
    625                 print('salary is wrong!')
    626                 continue
    627             password = common.encrypt(passwd)
    628             teacher=Teacher(name,password,salary,school_name)
    629             if not teacher.update_teachers_list():
    630                 teacher.create_uuid_file()
    631                 Users['teacher'][name]={'name': name, 'password': password,'salary':int(salary)}
    632                 common.update_users(Users)
    633                 school=School.get_obj_byname(school_name)
    634                 school.teacher.append(name)
    635                 school.create_uuid_file()
    636                 # print(school.__dict__)
    637                 print('老师创建成功!')
    638                 break
    639 
    640     def new_student():
    641         while True:
    642             name=input('请输入学生姓名:').strip()
    643             passwd=input('请输入初始密码:').strip()
    644             xingbie=input('请输入性别(F or M):').strip()
    645             course_name=input('请输入学习的课程:').strip()
    646             school_name=input('请输入学校:').strip()
    647             class_name=input('请输入班级:').strip()
    648 
    649             if Student.name_check(name):
    650                 print('学生姓名已存在!')
    651                 continue
    652             if xingbie.upper() != 'F' and xingbie.upper() != 'M':
    653                 print('性别输入错误!')
    654                 continue
    655             if not Course.name_check(course_name):
    656                 print('课程信息不存在!,请重新输入!')
    657                 continue
    658             if not School.name_check(school_name):
    659                 print('学校不存在!')
    660                 continue
    661             if not Classes.name_check(class_name):
    662                 print('班级不存在!')
    663                 continue
    664             school = School.get_obj_byname(school_name)
    665             if course_name not in school.course:
    666                 print('该学校没有这个课程!')
    667                 continue
    668             password = common.encrypt(passwd)
    669             student=Student(name,password,xingbie.upper())
    670             classes=Classes.get_obj_byname(class_name)
    671             if not student.update_students_list():
    672                 student.course.append(course_name)
    673                 student.banji.append(class_name)
    674                 student.create_uuid_file()
    675                 Users['student'][name]={'name': name, 'password': password}
    676                 common.update_users(Users)
    677                 classes.student_id.append(name)
    678                 classes.create_uuid_file()
    679                 break
    680 
    681     def new_school():
    682         name = input('请输入学校名称:').strip()
    683         city = input('请输入学校所在城市:').strip()
    684         address = input('请输入学校地址:').strip()
    685 
    686         #判断名字是否在学校列表中
    687         # schools_list=School.get_school_list()
    688         # for (school_name,school_ids) in schools_list:
    689         #     if name == school_name:
    690         #         print('学校名字已存在!')
    691         #         return
    692         school=School(name,address,city)
    693         if not school.update_schools_list():
    694             school.create_uuid_file()
    695 
    696     def new_course():
    697         while True:
    698             school_name=input('请输入学校:').strip()
    699             course_name=input('请输入课程名称:').strip()
    700             course_zhouqi=input('请输入课程周期:').strip()
    701             course_price=input('请输入课程价格:').strip()
    702             if School.name_check(school_name):
    703                 course=Course(course_name,course_zhouqi,course_price)
    704                 if not course.update_course_list():
    705                     course.create_uuid_file()
    706                     school=School.get_obj_byname(school_name)
    707                     school.course.append(course_name)
    708                     school.create_uuid_file()
    709                     break
    710             else:
    711                 print('学校不存在!')
    712                 break
    713 
    714     def new_class():
    715         while True:
    716             name=input('请输入班级名称').strip()
    717             open_date=input('请输入班级开课日期').strip()
    718             school_name = input('请输入所属学校名称').strip()
    719             couser_name=input('请输入班级所学的课程').strip()
    720             teacher_name=input('请输入班级负责的老师').strip()
    721 
    722             if not School.name_check(school_name):
    723                 print('学校不存在!')
    724                 continue
    725             course=Course.get_course_by_name(couser_name)
    726             if course == None:
    727                 print('课程不存在')
    728                 continue
    729             school=School.get_obj_byname(school_name)
    730             if teacher_name not in school.teacher:
    731                 print('该老师没有在该学校内!')
    732                 continue
    733             if couser_name not in school.course:
    734                 print('该学校没有这个课程!')
    735                 continue
    736 
    737             c_class = Classes(name,open_date)
    738             if not c_class.update_course_list():
    739                 c_class.course_id.append(couser_name)
    740                 c_class.teacher_id.append(teacher_name)
    741                 c_class.create_uuid_file()
    742                 course.banji.append(name)
    743                 course.create_uuid_file()
    744                 school.course.append(name)
    745                 school.create_uuid_file()
    746                 break
    747 
    748     func={
    749         '1':new_teacher,
    750         '2':new_student,
    751         '3':new_school,
    752         '4':new_course,
    753         '5':new_class,
    754     }
    755 
    756     Flag=True
    757     count=0
    758     while Flag:
    759         if count >= 3:
    760             Flag
    761             Flag=False
    762             print('错误次数过多')
    763             break
    764         sucess=common.login('admin')
    765         if not sucess:
    766             count+=1
    767             continue
    768         while Flag:
    769             print(template.index_admin_menu)
    770             choice=input('请选择:').strip()
    771             if choice.isdigit() and int(choice) in range(1,7):
    772                 if int(choice) == 6:
    773                     Flag=False
    774                     break
    775                 else:
    776                     func[choice]()
    777             else:continue
    778 
    779 #老师视图函数
    780 def func_teacher():
    781     #创建老师的上课记录
    782     def create_record(name):
    783         while True:
    784             class_name = input('请输入班级名称').strip()
    785             # jieci = input('请输入节次').strip()
    786             study_date = input('请输入学习日期,如2017-11-28:').strip()
    787             if not Classes.name_check(class_name):
    788                 print('班级不存在!')
    789                 break
    790             classrecored=ClassRecord(class_name,study_date)
    791             classes=Classes.get_obj_byname(class_name)
    792             if classrecored.name not in classes.class_record:
    793                 classes.class_record.append(classrecored.name)
    794             else:
    795                 print('上课记录已存在!')
    796                 continue
    797             classrecored.update_course_list()
    798             classrecored.create_uuid_file()
    799             classes.create_uuid_file()
    800             break
    801 
    802     #上课成绩打分
    803     def create_score(name):
    804         while True:
    805             class_name = input('请输入班级名称').strip()
    806             if not Classes.name_check(class_name):
    807                 print('班级不存在!')
    808                 continue
    809             classes=Classes.get_obj_byname(class_name)
    810             print('班级学生姓名如下:')
    811             for i in classes.student_id:
    812                 print(i)
    813             student_name = input('请输入班级内学生姓名:').strip()
    814             if student_name not in classes.student_id:
    815                 print('学生姓名有误')
    816                 continue
    817             print('班级课程如下:')
    818             for i in classes.class_record:
    819                 print(i)
    820             classrecored_name = input('请输入课程记录').strip()
    821             if classrecored_name not in classes.class_record:
    822                 print('课程记录输入有误!')
    823                 continue
    824             student_score=input('请输入学生成绩(0-100):').strip()
    825             if not student_score.isdigit() or int(student_score) not in range(1,101):
    826                 print('成绩有误!')
    827                 continue
    828             classrecord=ClassRecord.get_obj_byname(classrecored_name)
    829             classrecord.score[student_name]=student_score
    830             classrecord.create_uuid_file()
    831             break
    832 
    833     #查看学生上课签到情况
    834     def scan_class_record(name):
    835         while True:
    836             class_name = input('请输入班级名称:').strip()
    837             if not Classes.name_check(class_name):
    838                 print('班级不存在')
    839                 break
    840             classes=Classes.get_obj_byname(class_name)
    841             print('班级上课记录如下:')
    842             for i in classes.class_record:
    843                 print(i)
    844             record=input('请输入上课记录,以便查询对应课程的签到状态:').strip()
    845             if record not in classes.class_record:
    846                 print('输入错误!')
    847                 break
    848             class_record=ClassRecord.get_obj_byname(record)
    849             for s in classes.student_id:
    850                 if s in class_record.student_name:
    851                     print('{:<8} 已签到'.format(s))
    852                 elif s not in class_record.student_name:
    853                     print('{:<8} 未签到'.format(s))
    854             break
    855 
    856 
    857     func={
    858         '1':create_record,
    859         '2':create_score,
    860         '3':scan_class_record
    861     }
    862     count = 0
    863     Flag = True
    864     while Flag:
    865         if count >= 3:
    866             print('错误次数过多')
    867             break
    868         name = common.login('teacher')
    869         if not name:
    870             count += 1
    871             continue
    872         while Flag:
    873             print(template.index_teacher_menu)
    874             choice=input('请选择:')
    875             if choice.isdigit() and int(choice) in range(1,4):
    876                 func[choice](name)
    877             elif  choice.isdigit() and int(choice) == 4:
    878                 Flag=False
    879                 break
    880             else:
    881                 print('输入错误!')
    882                 continue
    883 
    884 if __name__ == '__main__':
    885     flag=True
    886     while flag:
    887         print(template.index_default_menu)
    888         choice=input('请选择:')
    889         if choice.isdigit() and int(choice) in range(1,5):
    890             choice=int(choice)
    891             if choice == 1:
    892                 func_student()
    893             elif choice == 2:
    894                 func_teacher()
    895             elif choice == 3:
    896                 admin()
    897             else:
    898                 print('选课系统和您再见!')
    899                 flag=False
    900                 break
    901         else:
    902             print('输入错误,请重试!')
    903             continue
    select_course.py

    优化了一版的主程序:

      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 # @Time    : 2017/11/30 22:13
      4 # @Author  : lichuan
      5 # @File    : new_select_course.py
      6 
      7 import uuid
      8 from config import template
      9 from module import common
     10 from module.users import Users
     11 import pickle
     12 import os
     13 from module.users import Users
     14 
     15 LOG_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+os.sep+'db'
     16 
     17 class Interface:
     18     def __init__(self):
     19         self.ids = credits()
     20 
     21     @staticmethod
     22     def createid():
     23         return str(uuid.uuid1())
     24 
     25     # 创建的对象写入到文件
     26     def create_uuid_file(self):
     27         path = LOG_DIR + os.sep + self.ids
     28         with open(path, 'wb') as write_f:
     29             pickle.dump(self, write_f)
     30 
     31     # 更新student的列表(name,ids)以元组的方式存进列表
     32     def update_students_list(self,strings):
     33         students_list_path = os.path.dirname(
     34             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + strings
     35         student_list = self.get_student_list(strings)
     36         for (student_name, student_id) in student_list:
     37             if student_name == self.name:
     38                 print('名称已存在,请用别的名字!')
     39                 return True
     40 
     41         student_list.append((self.name, self.ids))
     42         with open(students_list_path, 'wb') as write_f:
     43             pickle.dump(student_list, write_f)
     44         return False
     45 
     46     @staticmethod
     47     def get_student_list(strings):
     48         teachers_list_path = os.path.dirname(
     49             os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + strings
     50         if os.path.exists(teachers_list_path):
     51             with open(teachers_list_path, 'rb') as read_f:
     52                 teachers_list = pickle.load(read_f)
     53                 return teachers_list
     54         else:
     55             return []
     56 
     57     @staticmethod
     58     def get_obj_byid(ids):
     59         path = LOG_DIR + os.sep + ids
     60         with open(path, 'rb') as write_f:
     61             obj = pickle.load(write_f)
     62             return obj
     63 
     64     @staticmethod
     65     def get_obj_byname(student_name,strings):
     66         school_list = Student.get_student_list(strings)
     67         for (name, ids) in school_list:
     68             if student_name == name:
     69                 obj = Student.get_obj_byid(ids)
     70                 return obj
     71 
     72     # 判断学校名字是否在列表中,是返回Ture,否返回False
     73     @staticmethod
     74     def name_check(name,strings):
     75         student_list = Student.get_student_list(strings)
     76         for (student_name, ids) in student_list:
     77             if name == student_name:
     78                 return True
     79         return False
     80 #学生类
     81 class Student(Interface):
     82     def __init__(self,name,passwd,xingbie,salary=0):
     83         self.name = name
     84         self.passwd = passwd
     85         self.xingbie = xingbie
     86         self.salary=salary
     87         self.course = []
     88         self.banji = []
     89         self.study_record = []
     90         self.ids = self.createid()
     91 
     92 #老师类
     93 class Teacher(Interface):
     94     def __init__(self,name,passwd,salary,school):
     95         self.name=name
     96         self.passwd=passwd
     97         self.salary=salary
     98         self.school=school
     99         self.banji=[]
    100         self.ids=self.createid()
    101 
    102 #课程类
    103 class Course(Interface):
    104     def __init__(self,name,price,zhouqi):
    105         self.name=name
    106         self.price=price
    107         self.zhouqi=zhouqi
    108         self.banji=[]
    109         self.ids=self.createid()
    110 
    111 #学校类
    112 class School(Interface):
    113     school_list=[]
    114     def __init__(self,name,address,city):
    115         self.name=name
    116         self.address=address
    117         self.city=city
    118         self.ids=self.createid()
    119         self.course=[]
    120         self.teacher=[]
    121 
    122 #班级类
    123 class Classes(Interface):
    124 
    125     def __init__(self,name,open_date):
    126         self.name=name
    127         self.course_id=[]
    128         self.open_date=open_date
    129         self.teacher_id=[]
    130         self.student_id=[]
    131         self.class_record=[]
    132         self.ids=self.createid()
    133 
    134 #老师上课的记录
    135 class ClassRecord(Interface):
    136     def __init__(self,classes_name,study_date):
    137         self.name=classes_name+study_date
    138         self.classes=classes_name
    139         self.study_date=study_date
    140         self.student_name=[]
    141         self.score={}
    142         self.ids=self.createid()
    143 
    144 #学生视图函数
    145 def func_student():
    146     #交学费
    147     def give_money(name):
    148         while True:
    149             course_list=Course.get_student_list('course_lists')
    150             c=[]
    151             student = Student.get_obj_byname(name,'student_lists')
    152             for (i,j) in course_list:
    153                 print(i)
    154                 c.append(i)
    155             course=input('请输入想学习的课程:').strip()
    156             if course not in c:
    157                 print('输入错误!')
    158                 continue
    159             if course in student.course :
    160                 print('你已经学习了这个课程。')
    161                 break
    162             c=Course.get_obj_byname(course,'course_lists')
    163             print('这个课程花费%s元' %c.price)
    164             choice=input('是否缴费Y or N:').strip()
    165             if choice.upper() == 'Y':
    166                 print('请输入你要进入的班级')
    167                 for i in c.banji:print(i)
    168                 b=input('请选择班级:').strip()
    169                 if b not in c.banji:
    170                     print('input wrong!')
    171                     break
    172                 print('你已经缴费成功。')
    173                 student.course.append(course)
    174                 student.banji.append(b)
    175                 student.create_uuid_file()
    176                 classes=Classes.get_obj_byname(b,'class_lists')
    177                 classes.student_id.append(name)
    178                 classes.create_uuid_file()
    179                 break
    180             elif choice.upper() == 'N':
    181                 break
    182             else:
    183                 print('输入错误!')
    184                 continue
    185 
    186     #查看上课成绩
    187     def view_score(name):
    188         while True:
    189             student=Student.get_obj_byname(name,'student_lists')
    190             if len(student.banji) == 0:
    191                 print('你还没有班级!')
    192                 break
    193             elif len(student.banji) == 1:
    194                 banji=student.banji[0]
    195             else:
    196                 for b in student.banji:
    197                     print(b)
    198                 banji=input('请输入要查询的班级').strip()
    199                 if banji not in student.banji:
    200                     print('输入错误!')
    201                     break
    202             classes=Classes.get_obj_byname(banji,'class_lists')
    203             if len(classes.class_record) == 0:
    204                 print('该班级还没有上课记录,没有成绩!')
    205                 break
    206             for c in classes.class_record:
    207                 print(c)
    208             record=input('请输入对应的上课记录:').strip()
    209             if record not in classes.class_record:
    210                 print('输入错误!')
    211                 break
    212             class_record=ClassRecord.get_obj_byname(record,'classrecord_lists')
    213             if name not in class_record.score:
    214                 print('尚未评定成绩!')
    215             else:
    216                 print('你的成绩是 %s 分' %class_record.score[name])
    217             break
    218 
    219     #上课签到
    220     def study_record(name):
    221         student=Student.get_obj_byname(name,'student_lists')
    222         if len(student.banji) == 0:
    223             print('你还没有交学费报班级呢')
    224             return
    225         if len(student.banji) == 1:
    226             banji=student.banji[0]
    227         else:
    228             for b in student.banji:
    229                 print(b)
    230             banji=input('请输入你要签到的班级:').strip()
    231             if banji not in student.banji:
    232                 print('班级错误')
    233                 return
    234         banji=Classes.get_obj_byname(banji,'class_lists')
    235         if len(banji.class_record) == 0:
    236             print('班级还没有创建上课记录')
    237             return
    238         for i in banji.class_record:
    239             print(i)
    240         record = input('请输入上面中的一个上课记录').strip()
    241         if record not in banji.class_record:
    242             print('输入错误')
    243             return
    244         class_record = ClassRecord.get_obj_byname(record,'classrecord_lists')
    245         class_record.student_name.append(name)
    246         class_record.create_uuid_file()
    247 
    248     func_view = {
    249         '1':give_money,
    250         '2':study_record,
    251         '3':view_score,
    252     }
    253     def student_view(name):
    254         while True:
    255             print(template.index_student_menu)
    256             choice = input('请选择:').strip()
    257             if choice.isdigit() and int(choice) in range(1, 4):
    258                 func_view[choice](name)
    259             elif choice.isdigit() and int(choice) == 4:
    260                 break
    261             else:
    262                 print('输入错误')
    263                 continue
    264 
    265     def login():
    266         count = 0
    267         while True:
    268             count += 1
    269             if count >= 3:
    270                 print('错误次数太多')
    271                 break
    272             name=common.login('student')
    273             if name:
    274                 student_view(name)
    275                 break
    276 
    277     def segister():
    278         name = input('请输入学生姓名:').strip()
    279         passwd = input('请输入初始密码:').strip()
    280         xingbie = input('请输入性别(F or M):').strip()
    281         if Student.name_check(name,'student_lists'):
    282             print('学生姓名已存在!')
    283             return
    284         if xingbie.upper() != 'F' and xingbie.upper() != 'M':
    285             print('性别输入错误!')
    286             return
    287 
    288         password=common.encrypt(passwd)
    289         student = Student(name, password, xingbie.upper())
    290         student.update_students_list('student_lists')
    291         student.create_uuid_file()
    292         Users['student'][name]= {'name': name, 'password': password}
    293         common.update_users(Users)
    294         student_view(name)
    295 
    296     func={
    297         '1':segister,
    298         '2':login
    299     }
    300 
    301     while True:
    302         print(template.index_student_default_menu)
    303         choice=input('请选择:').strip()
    304         if choice.isdigit() and int(choice) in range(1,3):
    305             func[choice]()
    306             break
    307         elif choice.isdigit() and int(choice) == 3:
    308             break
    309         else:
    310             print('输入错误!')
    311             continue
    312 
    313 #管理视图的函数
    314 def admin():
    315 
    316     def new_teacher():
    317         while True:
    318             name=input('请输入老师姓名:').strip()
    319             passwd=input('请输入初始密码:').strip()
    320             salary=input('请输入工资:').strip()
    321             if not salary.isdigit():
    322                 print('salary is wrong!')
    323                 continue
    324             school_lists = School.get_student_list('school_lists')
    325             for  (s_name, ids) in school_lists:
    326                 print(s_name)
    327             school_name=input('请输入上面一个学校:').strip()
    328             if not School.name_check(school_name,'school_lists'):
    329                 print('学校不存在!')
    330                 continue
    331             password = common.encrypt(passwd)
    332             teacher=Teacher(name,password,salary,school_name)
    333             if not teacher.update_students_list('teacher_lists'):
    334                 teacher.create_uuid_file()
    335                 Users['teacher'][name]={'name': name, 'password': password,'salary':int(salary)}
    336                 common.update_users(Users)
    337                 school=School.get_obj_byname(school_name,'school_lists')
    338                 school.teacher.append(name)
    339                 school.create_uuid_file()
    340                 print('老师创建成功!')
    341                 break
    342             else:
    343                 print('老师已经存在!')
    344                 break
    345 
    346     def new_student():
    347         while True:
    348             name=input('请输入学生姓名:').strip()
    349             if Student.name_check(name,'student_lists'):
    350                 print('学生姓名已存在!')
    351                 continue
    352             passwd=input('请输入初始密码:').strip()
    353             xingbie=input('请输入性别(F or M):').strip()
    354             if xingbie.upper() != 'F' and xingbie.upper() != 'M':
    355                 print('性别输入错误!')
    356                 continue
    357             for (s_name,s_ids) in School.get_student_list('school_lists'):
    358                 print(s_name)
    359             school_name = input('请从上面选择一个学校:').strip()
    360             if not School.name_check(school_name,'school_lists'):
    361                 print('学校不存在!')
    362                 continue
    363             school = School.get_obj_byname(school_name,'school_lists')
    364             for i in school.course:
    365                 print(i)
    366             course_name=input('请从上面输入一个学习的课程:').strip()
    367             if course_name not in school.course:
    368                 print('该学校没有这个课程!')
    369                 continue
    370             course=Course.get_obj_byname(course_name,'course_lists')
    371             for i in course.banji:
    372                 print(i)
    373             class_name=input('请选择上面的一个班级:').strip()
    374             if class_name not in course.banji:
    375                 print('班级不对')
    376                 continue
    377             password = common.encrypt(passwd)
    378             student=Student(name,password,xingbie.upper())
    379             classes=Classes.get_obj_byname(class_name,'class_lists')
    380             if not student.update_students_list('student_lists'):
    381                 student.course.append(course_name)
    382                 student.banji.append(class_name)
    383                 student.create_uuid_file()
    384                 Users['student'][name]={'name': name, 'password': password}
    385                 common.update_users(Users)
    386                 classes.student_id.append(name)
    387                 classes.create_uuid_file()
    388                 break
    389 
    390     def new_school():
    391         name = input('请输入学校名称:').strip()
    392         s_lists=School.get_student_list('school_lists')
    393         for (school_name,ids) in s_lists:
    394             if school_name == name:
    395                 print('学校名称已存在')
    396                 return
    397         city = input('请输入学校所在城市:').strip()
    398         address = input('请输入学校地址:').strip()
    399         school=School(name,address,city)
    400         school.update_students_list('school_lists')
    401         school.create_uuid_file()
    402         print('学校创建成功!')
    403 
    404     def new_course():
    405         while True:
    406             school_lists=School.get_student_list('school_lists')
    407             for (s_name,ids) in school_lists:
    408                 print(s_name)
    409             school_name=input('请输入上面的一个学校:').strip()
    410             course_name=input('请输入课程名称:').strip()
    411             course_zhouqi=input('请输入课程周期:').strip()
    412             course_price=input('请输入课程价格:').strip()
    413             if School.name_check(school_name,'school_lists'):
    414                 school = School.get_obj_byname(school_name,'school_lists')
    415                 course=Course(course_name,course_zhouqi,course_price)
    416                 if not course.update_students_list('course_lists'):
    417                     course.create_uuid_file()
    418                     school.course.append(course_name)
    419                     school.create_uuid_file()
    420                     break
    421                 else:
    422                     print('课程名称已存在!')
    423                     break
    424             else:
    425                 print('学校不存在!')
    426                 break
    427 
    428     def new_class():
    429         while True:
    430             name=input('请输入班级名称').strip()
    431             open_date=input('请输入班级开课日期').strip()
    432             school_list=School.get_student_list('school_lists')
    433             for (sname,ids) in school_list:
    434                 print(sname)
    435             school_name = input('请输入所属学校名称').strip()
    436             if not School.name_check(school_name,'school_lists'):
    437                 print('学校错误!')
    438                 continue
    439             school = School.get_obj_byname(school_name,'school_lists')
    440             for i in school.course:
    441                 print(i)
    442             couser_name=input('请输入班级所学的课程').strip()
    443             if couser_name not in school.course:
    444                 print('课程输入错误!')
    445                 continue
    446             for i in school.teacher:
    447                 print(i)
    448             teacher_name=input('请输入班级负责的老师').strip()
    449             if teacher_name not in school.teacher:
    450                 print('该学校没有这个老师!')
    451                 continue
    452             teacher=Teacher.get_obj_byname(teacher_name,'teacher_lists')
    453             course=Course.get_obj_byname(couser_name,'course_lists')
    454             c_class = Classes(name,open_date)
    455             if not c_class.update_students_list('class_lists'):
    456                 c_class.course_id.append(couser_name)
    457                 c_class.teacher_id.append(teacher_name)
    458                 c_class.create_uuid_file()
    459                 course.banji.append(name)
    460                 course.create_uuid_file()
    461                 teacher.banji.append(name)
    462                 teacher.create_uuid_file()
    463                 break
    464 
    465     func={
    466         '1':new_teacher,
    467         '2':new_student,
    468         '3':new_school,
    469         '4':new_course,
    470         '5':new_class,
    471     }
    472 
    473     Flag=True
    474     count=0
    475     while Flag:
    476         if count >= 3:
    477             Flag
    478             Flag=False
    479             print('错误次数过多')
    480             break
    481         sucess=common.login('admin')
    482         if not sucess:
    483             count+=1
    484             continue
    485         while Flag:
    486             print(template.index_admin_menu)
    487             choice=input('请选择:').strip()
    488             if choice.isdigit() and int(choice) in range(1,7):
    489                 if int(choice) == 6:
    490                     Flag=False
    491                     break
    492                 else:
    493                     func[choice]()
    494             else:continue
    495 
    496 #老师视图函数
    497 def func_teacher():
    498     #创建老师的上课记录
    499     def create_record(name):
    500         while True:
    501             teacher=Teacher.get_obj_byname(name,'teacher_lists')
    502             for i in teacher.banji:
    503                 print(i)
    504             class_name = input('请输入班级名称').strip()
    505             if class_name not in teacher.banji:
    506                 print('输入错误')
    507                 break
    508             study_date = input('请输入学习日期,如2017-11-28:').strip()
    509             if not Classes.name_check(class_name,'class_lists'):
    510                 print('班级不存在!')
    511                 break
    512             classrecored=ClassRecord(class_name,study_date)
    513             classes=Classes.get_obj_byname(class_name,'class_lists')
    514             if classrecored.name not in classes.class_record:
    515                 classes.class_record.append(classrecored.name)
    516             else:
    517                 print('上课记录已存在!')
    518                 continue
    519             classrecored.update_students_list('classrecord_lists')
    520             classrecored.create_uuid_file()
    521             classes.create_uuid_file()
    522             break
    523 
    524     #上课成绩打分
    525     def create_score(name):
    526         while True:
    527             teacher = Teacher.get_obj_byname(name, 'teacher_lists')
    528             for i in teacher.banji:
    529                 print(i)
    530             class_name = input('请输入上面的班级名称').strip()
    531             if not Classes.name_check(class_name,'class_lists'):
    532                 print('班级不存在!')
    533                 continue
    534             classes=Classes.get_obj_byname(class_name,'class_lists')
    535             print('班级学生姓名如下:')
    536             for i in classes.student_id:
    537                 print(i)
    538             student_name = input('请输入班级内学生姓名:').strip()
    539             if student_name not in classes.student_id:
    540                 print('学生姓名有误')
    541                 continue
    542             print('班级课程如下:')
    543             for i in classes.class_record:
    544                 print(i)
    545             classrecored_name = input('请输入课程记录').strip()
    546             if classrecored_name not in classes.class_record:
    547                 print('课程记录输入有误!')
    548                 continue
    549             student_score=input('请输入学生成绩(0-100):').strip()
    550             if not student_score.isdigit() or int(student_score) not in range(1,101):
    551                 print('成绩有误!')
    552                 continue
    553             classrecord=ClassRecord.get_obj_byname(classrecored_name,'classrecord_lists')
    554             classrecord.score[student_name]=student_score
    555             classrecord.create_uuid_file()
    556             break
    557 
    558     #查看学生上课签到情况
    559     def scan_class_record(name):
    560         while True:
    561             teacher = Teacher.get_obj_byname(name, 'teacher_lists')
    562             for i in teacher.banji:
    563                 print(i)
    564             class_name = input('请输入班级名称:').strip()
    565             if not Classes.name_check(class_name,'class_lists'):
    566                 print('班级不存在')
    567                 break
    568             classes=Classes.get_obj_byname(class_name,'class_lists')
    569             print('班级上课记录如下:')
    570             for i in classes.class_record:
    571                 print(i)
    572             record=input('请输入上课记录,以便查询对应课程的签到状态:').strip()
    573             if record not in classes.class_record:
    574                 print('输入错误!')
    575                 break
    576             class_record=ClassRecord.get_obj_byname(record,'classrecord_lists')
    577             for s in classes.student_id:
    578                 if s in class_record.student_name:
    579                     print('{:<8} 已签到'.format(s))
    580                 elif s not in class_record.student_name:
    581                     print('{:<8} 未签到'.format(s))
    582             break
    583 
    584 
    585     func={
    586         '1':create_record,
    587         '2':create_score,
    588         '3':scan_class_record
    589     }
    590     count = 0
    591     Flag = True
    592     while Flag:
    593         if count >= 3:
    594             print('错误次数过多')
    595             break
    596         name = common.login('teacher')
    597         if not name:
    598             count += 1
    599             continue
    600         while Flag:
    601             print(template.index_teacher_menu)
    602             choice=input('请选择:')
    603             if choice.isdigit() and int(choice) in range(1,4):
    604                 func[choice](name)
    605             elif  choice.isdigit() and int(choice) == 4:
    606                 Flag=False
    607                 break
    608             else:
    609                 print('输入错误!')
    610                 continue
    611 
    612 if __name__ == '__main__':
    613     func = {
    614         '1' : func_student,
    615         '2' : func_teacher,
    616         '3' : admin,
    617     }
    618     flag=True
    619     while flag:
    620         print(template.index_default_menu)
    621         choice=input('请选择:')
    622         if choice.isdigit() and int(choice) in range(1,5):
    623             if int(choice) == 4:
    624                 print('选课系统和您再见!')
    625                 flag = False
    626                 break
    627             else:
    628                 func[choice]()
    629         else:
    630             print('输入错误,请重试!')
    631             continue
    new_select_course.py

    显示的模板文件:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 # @Time    : 2017/10/20 15:28
     4 # @Author  : lichuan
     5 # @File    : template.py
     6 
     7 
     8 """
     9 该模块用来定义系统的菜单模板,用的网上别人的模板。
    10 """
    11 # 主程序中的主菜单
    12 
    13 index_default_menu = '''
    14 -------------选课系统欢迎您!-------------
    15 【1】学员入口
    16 【2】老师入口
    17 【3】管理员入口
    18 【4】退出
    19 '''
    20 index_student_menu='''
    21 -------------选课系统欢迎您!-------------
    22 【1】交学费
    23 【2】上课签到
    24 【3】查看作业成绩
    25 【4】返回
    26 '''
    27 index_admin_menu='''
    28 -------------选课系统欢迎您!-------------
    29 【1】添加老师
    30 【2】添加学生
    31 【3】添加校区
    32 【4】新建课程
    33 【5】新建班级
    34 【6】返回
    35 '''
    36 
    37 index_student_default_menu = '''
    38 -------------选课系统欢迎您!-------------
    39 【1】注册
    40 【2】登录
    41 【3】返回
    42 '''
    43 
    44 index_teacher_menu = '''
    45 -------------选课系统欢迎您!-------------
    46 【1】创建上课记录
    47 【2】课程作业打分
    48 【3】查看签到
    49 【4】返回
    50 '''
    template.py

    公共函数文件:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 # @Time    : 2017/10/20 15:46
     4 # @Author  : lichuan
     5 # @File    : common.py
     6 
     7 import hashlib
     8 from module.users import Users
     9 import os
    10 import re
    11 from log import my_log_settings
    12 import logging
    13 import pickle
    14 
    15 def login(student):
    16     '''
    17     用户登录函数
    18     :param student:
    19     :param name:
    20     :param passwd:
    21     :return:
    22     '''
    23     name=input('请输入用户名:').strip()
    24     passwd=input('请输入密码:').strip()
    25     password=encrypt(passwd)
    26     if name in Users[student] and password == Users[student][name]['password']:
    27         return name
    28     else:
    29         print('用户名密码错误')
    30         return None
    31 
    32 def encrypt(str):
    33     '''
    34     对传入字符串进行加盐加密
    35     :param str: 需要进行加密的字符串
    36     :return: 返回加密过的字符串
    37     '''
    38     encrpt=hashlib.md5()
    39     encrpt.update(bytes('admin1234nnnnnn',encoding='utf-8'))
    40     encrpt.update(bytes(str,encoding='utf-8'))
    41     return encrpt.hexdigest()
    42 
    43 #从文件中读出报错的列表信息,包含学校名、课程名等等
    44 def get_name_from_list(strings):
    45     course_list_path = os.path.dirname(
    46         os.path.dirname(os.path.abspath(__file__))) + os.sep + 'db' + os.sep + strings
    47     if os.path.exists(course_list_path):
    48         with open(course_list_path, 'rb') as read_f:
    49             course_list = pickle.load(read_f)
    50             return course_list
    51     else:
    52         return []
    53 
    54 def update_users(Users):
    55     '''
    56     更新Users信息的函数
    57     :param Users: 用户信息,是个字典
    58     :return:
    59     '''
    60     import os
    61     user_path=os.path.dirname(os.path.abspath(__file__))+'\\users.py'
    62     with open(user_path,'w',encoding='utf-8') as write_f:
    63         Users_new='Users='+str(Users)
    64         write_f.write(Users_new)
    common.py

    存放用户信息的文件(其实可以存到类里面,当时没想到,继续这么用吧):

    Users={'student': {'lit1': {'name': 'lit1', 'password': 'bc5b9cb3e4ab483335edab3347f3c102'}, 'lit2': {'name': 'lit2', 'password': 'bc5b9cb3e4ab483335edab3347f3c102'}, 'lit3': {'name': 'lit3', 'password': 'bc5b9cb3e4ab483335edab3347f3c102'}, 'lit4': {'name': 'lit4', 'password': 'bc5b9cb3e4ab483335edab3347f3c102'}, 'lit5': {'name': 'lit5', 'password': 'bc5b9cb3e4ab483335edab3347f3c102'}, 'lit7': {'name': 'lit7', 'password': 'bc5b9cb3e4ab483335edab3347f3c102'}}, 'admin': {'admin': {'islocked': 0, 'password': 'bc5b9cb3e4ab483335edab3347f3c102', 'name': 'admin'}}, 'teacher': {'egon': {'name': 'egon', 'password': 'bc5b9cb3e4ab483335edab3347f3c102', 'salary': 50000}, 'alex': {'name': 'alex', 'password': 'bc5b9cb3e4ab483335edab3347f3c102', 'salary': 60000}, 'aobama': {'name': 'aobama', 'password': 'bc5b9cb3e4ab483335edab3347f3c102', 'salary': 10}, 'aaa': {'name': 'aaa', 'password': 'bc5b9cb3e4ab483335edab3347f3c102', 'salary': 111}, 'bbb': {'name': 'bbb', 'password': 'bc5b9cb3e4ab483335edab3347f3c102', 'salary': 111}}}
    users.py

      

  • 相关阅读:
    NYOJ 158 省赛来了(变相组合数)
    NYOJ 111 分数加减法
    NYOJ 14 会场安排问题 (贪心)
    POJ 3903 Stock Exchange(LIS)
    NYOJ 456 邮票分你一半(01背包)
    HDU 4521 小明系列问题——小明序列 (LIS加强版)
    CSU 1120 病毒(经典模板例题:最长公共递增子序列)
    挑战程序设计竞赛里面的几道深度优先搜索
    2009 Multi-University Training Contest 4
    USACO sec1.1
  • 原文地址:https://www.cnblogs.com/litzhiai/p/7929406.html
Copyright © 2020-2023  润新知