高度整合选课系统项目1-练习

"""
1、基于上次面向对象代码,扩写Student类
2、加入序列化与反序列化操作
3、对象之间的关联采用id号
4、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象
"""
import pickle
import uuid
import os



class Base:
    base_path = os.path.normpath(os.path.join(__file__, '..'))

    def save(self):
        dir_path = os.path.join(self.base_path, self.__class__.__name__.lower())
        if not os.path.isdir(dir_path):
            os.mkdir(dir_path)

        file_path = os.path.join(dir_path, str(self.uuid))
        with open(file_path, 'wb') as f:
            pickle.dump(self, f)

    @classmethod
    def select(cls, uuid):
        dir_path = os.path.join(cls.base_path, cls.__name__.lower())
        if not os.path.isdir(dir_path):
            print("对不起, 该目录不存在请先创建:", dir_path)
            return

        file_path = os.path.join(dir_path, uuid)
        if not os.path.isdir(dir_path):
            print("对不起, 该文件不存在请先创建:", file_path)
            return

        with open(file_path, 'rb') as f:
            return pickle.load(f)

    @classmethod
    def get_obj(cls):
        dir_path = os.path.join(Base.base_path, cls.__name__.lower())
        for uuid_name in os.listdir(dir_path):
            obj = cls.select(uuid_name)
            yield obj


class School(Base):
    school_name = 'OldBoy'

    def __init__(self, addr):
        self.uuid = uuid.uuid1()
        self.addr = addr
        self.class_list = []

    def related_class(self, class_obj):
        self.class_list.append(class_obj)

    def tell_class(self):
        print(f'{self.school_name}:{self.addr}'.center(70, '='))
        for class_obj in self.class_list:
            class_obj.tell_course()


class Class(Base):
    def __init__(self, name):
        self.uuid = uuid.uuid1()
        self.name = name
        self.course_obj = None
        self.student_list = []

    def related_course(self, course_obj):
        self.course_obj = course_obj

    def tell_course(self):
        print(f'班级:{self.name}', end=' ')
        self.course_obj.tell_info()

    def tell_student(self):
        print(f'班级:{self.name}'.center(50, '-'))
        for student_obj in self.student_list:
            student_obj.tell_info()


class Course(Base):
    def __init__(self, name, period, price):
        self.uuid = uuid.uuid1()
        self.name = name
        self.period = period
        self.price = price

    def tell_info(self):
        print(f"<课程:{self.name} 周期:{self.period}月 价格:{self.price}元>")


class Student(Base):
    def __init__(self, name, age, sex='male'):
        self.uuid = uuid.uuid1()
        self.id_card = None
        self.name = name
        self.age = age
        self.sex = sex
        self.score = None

    def choose_class(self, class_obj):
        current_id_card = max(range(len(class_obj.student_list) + 1)) + 1
        self.id_card = current_id_card
        class_obj.student_list.append(self)

    def tell_info(self):
        if self.score:
            print(f'ID:{self.id_card} 姓名:{self.name} 年龄:{self.age}岁 性别:{self.sex} 成绩:{self.score}分')
        else:
            print(f'ID:{self.id_card} 姓名:{self.name} 年龄:{self.age}岁 性别:{self.sex}')


class Teacher(Base):
    def __init__(self, name, age, sex='male'):
        self.uuid = uuid.uuid1()
        self.name = name
        self.age = age
        self.sex = sex

    @staticmethod
    def modify_score(student_obj, score):  # 修改 modify
        student_obj.score = score
"""
# 实例化学校对象
school_obj1 = School('上海校区')
school_obj2 = School('深圳校区')
school_obj3 = School('北京校区')

# 实例化班级对象
class_obj1 = Class('脱产14期')
class_obj2 = Class('脱产15期')
class_obj3 = Class('脱产16期')

# 实例化课程对象
course_obj1 = Course('python入门到放弃', 6, 21888)
course_obj2 = Course('爬虫入门到入狱', 5, 22777)
course_obj3 = Course('linux风癫之路', 5, 23999)

# 实例化学生对象
student_obj1 = Student('张晨', 23)
student_obj2 = Student('刘旭', 28)
student_obj3 = Student('朱斌成', 24)
student_obj4 = Student('王律盛', 25)
student_obj5 = Student('陈陈', 23, 'female')
student_obj6 = Student('刘洋', 18)

# 实例化老师对象
teacher_obj1 = Teacher('egon', 18)
teacher_obj2 = Teacher('tank', 17)

# 为学校对象关联班级
school_obj1.related_class(class_obj1)
school_obj2.related_class(class_obj2)
school_obj3.related_class(class_obj3)

# 为班级对象关联课程
class_obj1.related_course(course_obj1)
class_obj2.related_course(course_obj2)
class_obj3.related_course(course_obj3)

# 为学生对象选择班级
student_obj1.choose_class(class_obj1)
student_obj2.choose_class(class_obj2)
student_obj3.choose_class(class_obj3)
student_obj4.choose_class(class_obj1)
student_obj5.choose_class(class_obj2)
student_obj6.choose_class(class_obj3)

# 通过老师对象修改学生成绩
teacher_obj1.modify_score(student_obj1, 98)
teacher_obj1.modify_score(student_obj2, 97)
teacher_obj1.modify_score(student_obj3, 96)
teacher_obj2.modify_score(student_obj4, 95)
teacher_obj2.modify_score(student_obj5, 94)
teacher_obj2.modify_score(student_obj6, 93)

# 保存学校对象
school_obj1.save()
school_obj2.save()
school_obj3.save()

# 保存课程对象
course_obj1.save()
course_obj2.save()
course_obj3.save()

# 保存班级对象
class_obj1.save()
class_obj2.save()
class_obj3.save()

# 保存老师对象
teacher_obj1.save()
teacher_obj2.save()

# 保存学生对象
student_obj1.save()
student_obj2.save()
student_obj3.save()
student_obj4.save()
student_obj5.save()
student_obj6.save()
"""
"""
# 通过学校对象查看班级
# school_obj1.tell_class()
# school_obj2.tell_class()
# school_obj3.tell_class()


# 通过班级对象查看课程
# class_obj1.tell_course()
# class_obj2.tell_course()
# class_obj3.tell_course()

# 通过班级对象查看学生
# class_obj1.tell_student()
# class_obj2.tell_student()
# class_obj3.tell_student()
"""

# 通过学校类拿到文件中的学校对象
print('通过学校类拿到文件中的学校对象'.center(100, '#'))
for school_obj in School.get_obj():
    school_obj.tell_class()

# 通过班级类拿到文件中的班级对象
print('通过班级类拿到文件中的班级对象'.center(100, '#'))
for class_obj in Class.get_obj():
    class_obj.tell_student()

# 通过课程类拿到文件中的课程对象
print('通过课程类拿到文件中的课程对象'.center(100, '#'))
for course_obj in Course.get_obj():
    course_obj.tell_info()

# 通过学生类拿到文件中的学生对象
print('通过学生类拿到文件中的学生对象'.center(100, '#'))
for student_obj in Student.get_obj():
    student_obj.tell_info()

# 通过老师类拿到文件中老师对象
print('通过老师类拿到文件中老师对象'.center(100, '#'))
for teacher_obj in Teacher.get_obj():
    print(teacher_obj)
posted @ 2020-04-08 22:47  给你加马桶唱疏通  阅读(111)  评论(0编辑  收藏  举报