美文网首页
python10-学生选课系统

python10-学生选课系统

作者: AndroidCat | 来源:发表于2017-05-15 22:35 被阅读0次

    创建文件夹注意事项

    • Python2.7每个文件夹都有__init__.py文件
    • 如果没有这个文件,该文件夹下的文件不能作为模块被其他模块引用
    • python3.x可以不需要这个文件

    学生选课系统

    • 文件夹分类:

      • bin:存放可以执行的文件
      • config:存放配置文件,如:文件的存放路径,配置信息等
      • db:存放持久化数据
      • lib:存放模块文件
        • sys.path中只有bin目录下可执行文件的所在文件夹,需要将lib路径添加到sys.path中
      • log:存放日志,如果大项目的话不建议放在项目中,放在系统的某个文件夹下
    • python中的开放封闭原则,代码封闭,配置文件对用户开放

    • 把类写在一个module文件下,引入到执行文件中

    • 代码片段:

    # 程序入口 
    if __name__ == '__main__':
        main()
    
    # 把lib文件夹加入sys.path路径
    import sys
    import os
    
    dirName = os.path.dirname(os.path.dirname(__file__))
    path = os.path.join(dirName,'lib')
    sys.path.append(path)
    from config import conf
    from modules import Manager
    from modules import Teacher
    from modules import Course
    
    # 欢迎界面
    def main():
        print('欢迎登陆学生选课系统'.center(80, '-'))
        while True:
            choose = input('1--管理员登陆     2--学生登陆    0--退出系统')
            if choose == '1':
                # 管理员
                manager()
                pass
            elif choose == "2":
                # 学生
                student()
                pass
            elif choose == '0':
                # 退出
                print('成功退出选课系统'.center(80, '-'))
                break
            else:
                # 输入有误
                print('输入有误,请重新输入'.center(80, '-'))
    
    # 管理员操作
    def manager():
        print('欢迎进入管理员登陆页面'.center(80, '-'))
        while True:
            choose = loginAndRegister()  # 登陆或注册
            if choose == '3':  # 返回
                break
            elif choose == '2':  # 注册
                userInfo = getUserNameAndPwd()
                userName = userInfo.get('userName')
                pwd = userInfo.get('pwd')
                obj = Manager()
                ret = obj.register(userName, pwd)
                if ret == 0:
                    print('注册成功')
                elif ret == 1:
                    print('用户已存在')
                pass
            elif choose == '1':  # 登陆
                userInfo = getUserNameAndPwd()
                userName = userInfo.get('userName')
                pwd = userInfo.get('pwd')
                ret = Manager.login(userName, pwd)
                code = ret.get('code')
                obj = ret.get('obj')
                if code == 1:
                    print('登陆成功')
                    managerToDo(obj)
                elif code == 0:
                    print('用户不存在')
                elif code == -1:
                    print('密码错误')
    
    # manager类
    class Manager():
        def __init__(self):
            self.name = None
            self.pwd = None
    
        def register(self, name, pwd):
            self.name = name
            self.pwd = pwd
            dir = os.path.dirname(os.path.dirname(__file__))
            dirName = os.path.join(dir, conf.MANAGER_DB_DIR)
            dirExist = os.path.exists(dirName)
            if not dirExist:
                os.makedirs(dirName)
                print('创建%s目录' % conf.MANAGER_DB_DIR)
            path = os.path.join(dirName, self.name)
            isExists = os.path.exists(path)
            if isExists:
                return 1
            else:
                with open(path, mode='wb') as file:
                    pickle.dump(self, file)
                return 0
    
        @staticmethod
        def login(name, pwd):
            dir = os.path.dirname(os.path.dirname(__file__))
            dirName = os.path.join(dir, conf.MANAGER_DB_DIR)
            dirExist = os.path.exists(dirName)
            if not dirExist:
                os.makedirs(dirName)
                print('创建%s目录' % dirName)
            path = os.path.join(dirName, name)
            isExists = os.path.exists(path)
            if isExists:
                with open(path, mode='rb') as file:
                    obj = pickle.load(file)
                    if obj.name == name and obj.pwd == pwd:
                        return {'code': 1, 'obj': obj}
                    else:
                        return {'code': -1, 'obj': None}
            else:
                # 用户不存在
                return {'code': 0, 'obj': None}
    
    # 配置文件
    MANAGER_DB_DIR = r'db\manager'
    STUDENT_DB_DIR = r'db\student'
    TEACHER_DB_DIR = r'db\teacher'
    COURSE_DB_DIR = r'db\course'
    
    # 课程类
    class Course:
        def __init__(self):
            self.name = None
            self.cost = None
            self.teacher = None
            self.manager = None
    
        def register(self,name,cost,teacher,manager):
            self.name = name
            self.cost = cost
            self.teacher = teacher
            self.manager = manager
            courseList = []
    
            dir = os.path.dirname(os.path.dirname(__file__))
            dirName = os.path.join(dir,conf.COURSE_DB_DIR)
            if not os.path.exists(dirName):
                os.makedirs(dirName)
            path = os.path.join(dirName,'courseList')
            if os.path.exists(path):
                with open(path, mode='rb') as file:
                    courseList = pickle.load(file)
            flag = False
            ret = "%s&%s"%(self.name,self.cost)
            print(ret)
            for course in courseList:
                if str(course) == ret:
                    flag = True
            if flag:
                return 1
            else:
                courseList.append(self)
                with open(path, mode='wb') as file:
                    pickle.dump(courseList, file)
                return 0
    
        def __str__(self):
            ret = "%s&%s"%(self.name,self.cost)
            return ret
    

    相关文章

      网友评论

          本文标题:python10-学生选课系统

          本文链接:https://www.haomeiwen.com/subject/irrkxxtx.html