Python全栈开发:选课系统实例
程序目录:
- bin文件夹下为可执行文件:administrator,students
- config文件夹下为设置文件,涉及系统参数配置:setting
- db文件夹为数据类文件,涉及系统的输入输出数据:admin,students
- lib文件夹下为公用模块文件
- log为日志文件夹
- bin文件夹
- administrator
#!/usr/bin/env python # -*- coding;utf-8 -*- import random import time import pickle import os import sys from config import settings from lib import models from lib.models import * sys.path.append(os.path.dirname(os.path.dirname(__file__))) def create_course(admin_obj): teacher_list = pickle.load(open(settings.TEACHER_DB_DIR, "rb")) for num, item in enumerate(teacher_list, 1): print(num, item.name, item.age, item.create_time, item.create_admin.username) Course_list = [] while True: name = input("请选择课程名称...:") if name == "q": break cost = input("请选择课时费...:") if cost == "q": break num = input("选择老师(序号):") if num == "q": break obj = models.Course(name, cost, teacher_list[int(num) - 1], admin_obj) Course_list.append(obj) if os.path.exists(settings.COURSE_DB_DIR): exists_list = pickle.load(open(settings.COURSE_DB_DIR, "rb")) Course_list.extend(exists_list) pickle.dump(Course_list, open(settings.COURSE_DB_DIR, "wb")) def create_teacher(admin_obj): teacher_list = [] while True: teacher_name = input("请输入老师姓名(q表示退出):") if teacher_name == "q": break teacher_age = input("请输入老师年龄:") obj = models.Teacher(teacher_name, teacher_age, admin_obj) teacher_list.append(obj) if os.path.exists(settings.TEACHER_DB_DIR): exists_list = pickle.load(open(settings.TEACHER_DB_DIR, "rb")) teacher_list.extend(exists_list) pickle.dump(teacher_list, open(settings.TEACHER_DB_DIR, "wb")) def login(user, pwd): if os.path.exists(os.path.join(settings.BASE_ADMIN_DIR, user)): # 从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法) admin_obj = pickle.load(open(os.path.join(settings.BASE_ADMIN_DIR, user), "rb")) if admin_obj.login(user, pwd): print("登陆成功!") while True: se1 = input("1、创建老师;2、创建课程;(q表示退出)\n>>>") if se1 == '1': create_teacher(admin_obj) elif se1 == '2': create_course(admin_obj) elif se1 == "q": break return 2 else: return 1 else: return 0 def register(user, pwd): admin_obj = models.Admin() admin_obj.register(user, pwd) def main(): inp = input("1、管理员登录;2、管理员注册;(q表示退出) \n>>>") while True: if inp == "1": user = input("请输入用户名:") pwd = input("请输入密码:") ret = login(user, pwd) if ret == 1: print("用户名或密码错误!") elif ret == 0: print("用户不存在!") elif ret == 2: print("退出成功!") break elif inp == "2": user = input("请输入用户名:") pwd = input("请输入密码:") register(user, pwd) print("注册成功!") elif inp == "q": print("退出成功!") break if __name__ == "__main__": main()
- students
#!/usr/bin/env python # -*- coding;utf-8 -*- import random import time from lib import models import os import sys import pickle from config import settings from lib.models import Course, Admin, Teacher sys.path.append(os.path.dirname(os.path.dirname(__file__))) def course_info(student_obj): course_list = pickle.load(open(settings.COURSE_DB_DIR, "rb")) for num, item in enumerate(student_obj.course_list, 1): print(num, item.course_name, item.cost, item.teacher_obj.name) def select_course(student_obj): """ 学生选课 :param student_obj: :return: """ course_list = pickle.load(open(settings.COURSE_DB_DIR, "rb")) for num, item in enumerate(course_list, 1): print(num, item.course_name, item.cost, item.teacher_obj.name) while True: num = input("请选择课程(q退出)\n>>>") if num == "q": break select_course_obj = course_list[int(num) - 1] if select_course_obj not in student_obj.course_list: student_obj.course_list.append(select_course_obj) pickle.dump(student_obj, open(os.path.join(settings.BASE_STUDENTS_DIR, student_obj.username), "wb")) def register(user, pwd): """ 学生注册 :param user: :param pwd: :return: """ obj = models.Students() obj.register(user, pwd) def login(user, pwd): """ 学生登录 :param user: :param pwd: :return: """ if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR, user)): student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR, user), "rb")) while True: if student_obj.login(user, pwd): inp = input("1、选课;2、上课;3、查看课程信息(q表示退出!)\n>>>") if inp == "1": select_course(student_obj) elif inp == "3": course_info(student_obj) elif inp == '2': pass elif inp == 'q': break else: print("用户名或密码错误!") return 1 else: print("用户不存在") return 0 def main(): inp = input("1、登录;2、注册;\n>>>") user = input("请输入用户名:\n>>>") pwd = input("请输入密码:\n>>>") if inp == "1": ret = login(user, pwd) elif inp == '2': ret = register(user, pwd) if ret: print("注册成功!") if __name__ == "__main__": main()
- config配置文件
#!/usr/bin/env python # -*- coding;utf-8 -*- import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_ADMIN_DIR = os.path.join(BASE_DIR, "db", "admin") BASE_STUDENTS_DIR = os.path.join(BASE_DIR, "db", "students") TEACHER_DB_DIR = os.path.join(BASE_DIR, "db", "teacher_list") COURSE_DB_DIR = os.path.join(BASE_DIR, "db", "course_list") LOG_PATH = os.path.join(BASE_DIR, "log")
- db数据文件:用来存放管理员和学生的注册用户名密码,以及选课开课信息。注意:开课信息和学生选课信息不能混淆,注意存放路径
- lib公用模块文件
#!/usr/bin/env python # -*- coding;utf-8 -*- import random import time import pickle import os from config import settings class Teacher: """ 封装老师的相关信息 """ def __init__(self, name, age, admin): self.name = name self.age = age self.__assets = 0 self.create_time = time.strftime("%Y-%m-%d %H:%M:%S") self.create_admin = admin def gain(self, cost): """ 增加资产 :param cost: 增加的数量 :return: 收入 """ self.__assets += cost def decrease(self, cost): """ 减少资产 :param cost: 减少的数量 :return: 收入 """ self.__assets -= cost class Admin: """ 封装管理员的信息 """ def __init__(self): self.username = None self.password = None def login(self, user, pwd): """ 管理员登录 :param user: 用户名 :param pwd: 密码 :return: """ if self.username == user and self.password == pwd: return True else: return False def register(self, user, pwd): """ 管理员注册 :param user: :param pwd: :return: """ self.username = user self.password = pwd path = os.path.join(settings.BASE_ADMIN_DIR, self.username) pickle.dump(self, open(path, "xb")) return True class Course: """ 课程相关信息 """ def __init__(self, course_name, cost, teacher_obj, admin ): self.course_name = course_name self.cost = cost self.teacher_obj = teacher_obj self.create_time = time.strftime("%Y-%m-%d %H:%M:%S") self.create_admin = admin def have_lesson(self): """ 课程上课,自动给相关联的任课老师增加课时费 :return: 课程内容返回给上课者 """ self.teacher.gain(self.cost) content = random.randrange(10, 100) class Students: """ 学生相关信息 """ def __init__(self): self.username = None self.password = None self.course_list = [] self.study_dict = {} def select_course(self,course_obj): """ 学生选课 :param course_obj: :return: """ self.course_list.append(course_obj) def study(self,course_obj): """ 学生上课 :param course_obj: :return: """ class_result = course_obj.have_lesson() if course_obj in self.study_dict.keys(): self.study_dict[course_obj].append(class_result) else: self.study_dict[course_obj] = [class_result,] def login(self, user, pwd): """ 学生登录 :param user: :param pwd: :return: """ if self.username == user and self.password == pwd: return True else: return False def register(self, user, pwd): """ 学生注册 :param user: :param pwd: :return: """ self.username = user self.password = pwd path = os.path.join(settings.BASE_STUDENTS_DIR, self.username) try: pickle.dump(self, open(path, "xb")) except Exception as f: return False else: return True
- 日志文件:保存系统相关操作记录