管理员:
创建老师:姓名、性别、年龄、资产
创建课程:课程名称、上课时间、课时费、关联老师
使用pickle保存在文件
学生:
学生:用户名、密码、性别、年龄、选课列表[]、上课记录{课程1:【di,a,】}
1、列举所有课程
2、选择课程
3、学生上课,
4、ret = 课程.work() 获取课程的返回; 资产+=课时费
a、Teacher类:关联管理员,由哪个管理员创建
b、 Course类:关联老师对象、管理员对象(注意:关联的是对象)
a、管理员文件:写入的是管理员对象(封装管理员的用户名和密码)
b、学生文件:写入的是学生对象(封装了学生已选课程列表,已经上过的课程字典:key:课程对象 value:上课信息列表,是列表格式)
c、课程列表course_list、老师列表teacher_lis,都是列表格式。
bin目录:存放administrator.py文件和students.py文件
config目录:存放settings.py文件,即配置文件
db目录:存放与数据相关的文件,包括课程列表course_list、老师列表teacher_list、学生文件夹、管理员文件夹
lib目录:存放models.py文件,即存放公共的模块
log目录:存放日志文件
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") #课程列表目录
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
import time
import pickle
from config import settings
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
def have_lesson(self):
"""
课程上课,自动给相关联的任课老师增加课时费
:return: 课程内容返回给上课者
"""
self.teacher.gain(self.cost)
content = random.randrange(10, 100)
r = time.strftime('%Y-%m-%d %H:%M:%S')
temp = "课程:%s;老师:%s;内容:%d;时间:%f" % (self.course_name, self.teacher, content, r)
return temp
def absence(self):
"""
教学事故
:return:
"""
self.teacher.decrease(self.cost * 2)
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')) #将管理员对象写入文件
class Student:
"""
学生相关信息
"""
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(): #key:课程对象 value:上课信息列表,是列表格式
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) #学生目录
pickle.dump(self, open(path, 'xb')) #将学生对象写入学生目录
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import random
import time
import pickle
import os
from lib import models
from config import settings
from lib.models import *
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 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:
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 regiter(user,pwd):
admin_obj = models.Admin()
admin_obj.register(user, pwd)
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':
regiter(user, pwd)
if __name__ == "__main__":
main()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import pickle
from lib import models
from config import settings
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher
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;
selected_course_obj = course_list[int(num)-1] #根据序号选择课程
if selected_course_obj not in student_obj.course_list:
student_obj.course_list.append(selected_course_obj) #添加进该学生的课程列表
pickle.dump(student_obj, open(os.path.join(settings.BASE_STUDENTS_DIR, student_obj.username), 'wb'))#将学生对象dump进文件,封装学生选课列表,上课字典信息
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、查看课程信息')
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()