首页 > 代码库 > 3.python小项目:学生选课系统
3.python小项目:学生选课系统
学生选课系统
编程核心:在对象中封装对象
目录结构:
1.administrator.py
import random import os import sys sys.path.append(os.path.dirname(os.path.dirname(__file__))) import time import pickle from lib import models from config import settings from lib.models import * # 全部导入 def login(user, pwd): ‘‘‘ 登录,登录成功后:选择是创建老师,创建课程 :param user: :param pwd: :return:返回1:为密码错误,返回2:用户不存在 ‘‘‘ 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: sel = input(‘1.创建老师;2.创建课程‘) if sel == ‘1‘: create_teacher(admin_obj) elif sel == ‘2‘: create_course(admin_obj) else: break else: return 1 else: return 0 def register(user, pwd): ‘‘‘ 注册 :param user: :param pwd: :return: ‘‘‘ admin_obj = models.Admin() admin_obj.register(user, pwd) 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_DE_DIR): exists_list = pickle.load(open(settings.TEACHER_DE_DIR, ‘rb‘)) teacher_list.extend(exists_list) # 将已经存在的列表批量添加到当前列表 pickle.dump(teacher_list, open(settings.TEACHER_DE_DIR, ‘wb‘)) 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(‘请输入课程名称(q退出)‘) if name == ‘q‘: break cost = input(‘输入课时费‘) num = input(‘选择老师(序号)‘) 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 main(): inp = input(‘1.管理员登录 2.管理员注册; \n >>>‘) user = input(‘请输入用户名:‘) pwd = input(‘请输入密码:‘) if inp == ‘1‘: ret = login(user, pwd) if ret == 1: print(‘密码错误‘) elif ret == 0: print(‘用户不存在‘) elif inp == ‘2‘: register(user, pwd) if __name__ == ‘__main__‘: main()
2.student.py
import sys import os import pickle sys.path.append(os.path.dirname(os.path.dirname(__file__))) from lib import models from config import settings from lib.models import Teacher from lib.models import Admin from lib.models import Course def course_info(student_obj): for item in student_obj.course_list: print(item.course_name, item.teacher.name) def select_course(student_obj): 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.name) while True: num = input(‘请选择课程:(q退出)‘) if num == ‘q‘: break select_course_obj = course_list[int(num) - 1] if select_course_obj in student_obj.course_list: ‘‘‘ 相同则跳过 ‘‘‘ pass 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 login(user, pwd): 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‘)) if student_obj.login(user, pwd): while True: inp = input(‘1选课 2上课 3查看课程信息\n>>>‘) if inp == ‘1‘: select_course(student_obj) elif inp == ‘3‘: course_info(student_obj) else: break else: print(‘密码错误‘) else: print(‘用户不存在‘) def register(user, pwd): obj = models.Student() obj.register(user, pwd) def main(): inp = input(‘1.登录;2.注册\n >>>‘) user = input(‘用户名:‘) pwd = input(‘密码:‘) if inp == ‘1‘: login(user, pwd) elif inp == ‘2‘: register(user, pwd) if __name__ == ‘__main__‘: main()
3.settings.py
# 配置文件 开放封闭原则,不改动源码,更改配置文件 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 = ‘/home/alex/‘ #日志路径
4.models.py
import random import os import time import pickle 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: ‘‘‘ pass def decrease(self, cost): ‘‘‘ 花钱 :param cost: :return: ‘‘‘ pass 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‘)) ‘‘‘ 将对象序列化到文件中,方便以后掏出来 xb即写读,文件如果存在,则报错 ‘‘‘ class Course: ‘‘‘ 课程相关信息 ‘‘‘ def __init__(self, course_name, cost, teacher_obj, admin): self.course_name = course_name self.cost = cost self.teacher = teacher_obj self.create_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) self.create_admin = admin class Student: ‘‘‘ 学生相关信息 ‘‘‘ def __init__(self): self.username = None self.password = None self.course_list = [] self.studey_dict = {} def login(self, user, pwd): if self.username == user and self.password == pwd: return True else: return False def register(self, user, pwd): self.username = user self.password = pwd path = os.path.join(settings.BASE_STUDENTS_DIR, self.username) pickle.dump(self, open(path, ‘xb‘))
3.python小项目:学生选课系统
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。