coding:utf-8
from db import db_handler
父类
class Base:
# 保存数据(对象)方法
def save_obj(self):
db_handler.save(self)
# 查看数据(对象)方法
@classmethod
def select_obj(cls, obj_name):
obj = db_handler.select(cls, obj_name)
return obj
人类
class People(Base):
def init(self, name, password):
self.__name = name
self.__pwd = password
@property
def user_name(self):
return self.__name
@user_name.setter
def user_name(self, name, password):
if type(name) is not str:
print('请输入字符串名字')
return
self.__name = name
self.__pwd = password
self.save_obj()
@user_name.deleter
def user_name(self):
print('不能删除')
学校类
class School(Base):
school_name = 'OLDBOY'
def __init__(self, name, addr):
self.name = name
self.addr = addr
self.__class_list = []
self.save_obj()
def related_class(self, class_obj):
self.__class_list.append(class_obj)
self.save_obj()
@property
def tell_school_class(self):
print(f'{self.name}:')
for class_obj in self.__class_list:
class_obj.tell_class_course()
@property
def classes(self):
return self.__class_list
班级类
class Class(Base):
def init(self, name):
self.name = name
self.course = None
self.student_list = []
self.save_obj()
def related_course(self, course_obj):
self.course = course_obj
self.save_obj()
@property
def tell_class_course(self):
self.course.tell_info()
def add_student(self, student_obj):
self.student_list.append(student_obj)
self.save_obj()
@property
def tell_all_student(self):
if self.student_list:
print(f'班级:{self.name}'.center(50, '='))
for student_obj in self.student_list:
print(student_obj.name)
else:
print('该班级下无学生')
@property
def tell_student_score(self):
print(f'课程:{self.course.name}:', end='')
for student_obj in self.student_list:
print(f'{student_obj.user_name}:{student_obj.score[self.name]}')
课程类
class Course(Base):
def init(self, course_name, course_price, course_time):
self.name = course_name
self.price = course_price
self.time = course_time
self.save_obj()
def tell_info(self):
print(f'<课程:{self.name} 价格:{self.price} 周期:{self.time}>')
学生类
class Student(People):
def init(self, student_name, student_pwd):
People.init(self, student_name, student_pwd)
self.school = None
self.course_list = []
self.score = {}
self.save_obj()
# 选择学校
def choice_school(self, school_obj):
self.school = school_obj.name
self.save_obj()
# 选择课程
def choice_course(self, course_obj):
self.course_list.append(course_obj)
self.score[course_obj.name] = 0
# 课程下也需要添加学生
course_obj.add_student(self)
self.save_obj()
def tell_courses_score(self):
print(f'学生:{self.__name}'.center(50, '='))
for course_obj in self.course_list:
print(f'课程:{course_obj.name} 分数:[{self.score[course_obj.name]}]')
def tell_all_courses(self):
print(f'学生{self.__name}的课程'.center(50, '='))
for course_obj in self.course_list:
print(f'{course_obj.name} ')
class Admin(People):
def init(self, name, pwd):
People.init(self, name, pwd)
self.save_obj()
def create_school(self, school_name, school_addr):
School(school_name, school_addr)
self.save_obj()
def create_class(self, class_name):
Class(class_name)
def create_course(self, course_name, price, date_time):
Course(course_name, price, date_time)
class Teacher(People):
def init(self, teacher_name, teacher_pwd):
People.init(self, teacher_name, teacher_pwd)
self.__class_list = []
self.save_obj()
@property
def classes(self):
if not self.__class_list:
return '未选择班级'
return self.__class_list
@classes.setter
def classes(self, class_obj):
self.__class_list.append(class_obj)
self.save_obj()
#查看班级下的学生
def check_student_of_class(self, class_obj):
return class_obj.tell_all_student
#查看班级所有学生的课程分数
def check_course_of_student(self, class_obj):
return class_obj.tell_class_course