python--面向对象编程之学生选课系统练习
1、admin.py
主要是系统管理初始化,用于管理员注册、登录系统、创建授课老师、创建课程等操作。
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 模拟学校的选课系统 """ import random import time import pickle import os #定义老师类 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 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 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 = self.username pickle.dump(self, open(path, ‘xb‘)) #定义创建老师的方法 def create_teacher(admin_obj): teacher_list = [] while True: teacher_name = input("请输入老师的姓名:(q退出)") if teacher_name == "q": break teacher_age = input("请输入老师的年龄:") obj = Teacher(teacher_name, teacher_age, admin_obj) teacher_list.append(obj) if os.path.exists("teacher_list"): #判断老师列表是否存在 exists_list = pickle.load(open(‘teacher_list‘, ‘rb‘)) teacher_list.extend(exists_list) pickle.dump(teacher_list, open(‘teacher_list‘, ‘wb‘)) #定义创建课程的方法 def create_course(admin_obj): teacher_list = pickle.load(open(‘teacher_list‘, ‘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 = Course(name, cost, teacher_list[int(num) - 1], admin_obj) course_list.append(obj) if os.path.exists("course_list"): #判断课程列表是否存在 exists_list = pickle.load(open(‘course_list‘, ‘rb‘)) course_list.extend(exists_list) pickle.dump(course_list, open(‘course_list‘, ‘wb‘)) #定义查看课程表的方法 def look_course(): course_list = pickle.load(open(‘course_list‘, ‘rb‘)) for num, item in enumerate(course_list, 1): print(num, item.course_name, item.cost, item.teacher.name, item.create_time, item.create_admin.username) #定义管理员登录方法 def to_login(user, pwd): if os.path.exists(user): # 从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录访求) admin_obj = pickle.load(open(user, ‘rb‘)) if admin_obj.login(user, pwd): print(‘登录成功!‘) while True: sel = input("1、创建老师;2、创建课程; 3、查看课程表(q退出程序) \n >>>>") if sel == ‘1‘: #创建老师 create_teacher(admin_obj) elif sel == ‘2‘: #创建课程 create_course(admin_obj) elif sel == ‘3‘: look_course() else: break else: return 1 else: return 0 #定义管理员注册方法 def to_register(user, pwd): admin_obj = Admin() admin_obj.register(user, pwd) def main(): inp = input("1、管理员登录; 2、管理员注册;\n >>>>>") user = input(‘请输入用户名:‘) pwd = input(‘请输入密码:‘) if inp == ‘1‘: ret = to_login(user, pwd) if ret == 1: print("密码错误!") elif ret == 0: print("用户不存在!") elif inp == ‘2‘: to_register(user, pwd) if __name__ == "__main__": main()
相关推荐
YClimb 2020-09-15
amazingbo 2020-08-16
CyborgLin 2020-08-15
一叶不知秋 2020-07-09
啸林 2020-07-04
magicandy 2020-06-16
鲁氏汤包王 2020-06-08
YEEHOLIC 2020-06-03
晨曦之星 2020-05-29
paopao00 2020-05-20
czsay 2020-05-20
cwgxiaoguizi 2020-05-19
Yasin 2020-05-06
loviezhang 2020-05-03
FellowYourHeart 2020-04-23
东方咖啡屋 2020-04-19