# 1、练习上课作业讲解的面向对象代码,明天默写
# 2、基于上课作业讲解的面向对象代码,扩写Student类
# 3、加入序列化与反序列化操作
# 4、对象之间的关联采用id号
# 5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象
import pickle
import uuid
import os
class Student:
    uuid = uuid.uuid4()
    def __init__(self,name,age,student_id,male):
        self.name =name
        self.age =age
        self.student_id =student_id
        self.male =male
        self.uuid = None

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

    def tell_data(self):
        print(f'{self.name}选择了')
        self.course_obj.tell_info()

    def save_student_info(self):
        user_dic = f'姓名:{self.name} 学号:{self.student_id} 年龄:{self.age} 性别:{self.male}'
        if not os.path.exists(f'{str(uuid)}.pkl'):
            os.mkdir(f'{str(uuid)}.pkl')
        with open(f'{str(uuid)}.pkl', 'wb') as f:
            pickle.dump(user_dic,f)

    def tell_student_info(self):
        # print(f'姓名:{self.name} 学号:{self.id} 年龄:{self.age} 性别:{self.gender}')
        with open(f'{str(uuid)}.pkl', 'rb') as f:
            my_new_list = pickle.load(f)
            print(my_new_list)


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

    def tell_info(self):
        print('<课程名:%s 周期:%s 价钱:%s>' %(self.name,self.period,self.price))

class Class:
    def __init__(self, name):
        self.name = name
        self.course = None

    def related_course(self, course_obj):
        # self.course = course_name
        self.course = course_obj

    def tell_course(self):
        print('%s' % self.name,end=" ")
        self.course.tell_info() # 打印课程的详细信息

class School:
    school_name = 'OLDBOY'

    def __init__(self,nickname,addr):
        self.nickname = nickname
        self.addr = addr
        # self.course = {}  # {课程名:课程实例}
        # self.teacher = {}  # {老师名:老师实例}
        self.classes = []  #{班级名:班级实例}

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

    def tell_class(self):
        print(self.nickname.center(60, '='))
        for class_obj in self.classes:
            class_obj.tell_course()

school_obj1=School('老男孩魔都校区','上海浦东')
school_obj2=School('老男孩帝都校区','北京沙河')


class_obj1 = Class('脱产14期')
class_obj2 = Class('脱产15期')
class_obj3 = Class('脱产29期')

school_obj1.related_class(class_obj1)
school_obj1.related_class(class_obj2)
school_obj2.related_class(class_obj3)


course_obj1=Course('python全栈开发','6mons',20000)
course_obj2=Course('linux运维','5mons',18000)

class_obj1.related_course(course_obj1)
class_obj2.related_course(course_obj2)
class_obj3.related_course(course_obj1)

student_obj1 = Student('张三',18, '001',  'female')
student_obj2 = Student('梦梦', 23,'002',  'male')

student_obj1.choose_course(course_obj1)
student_obj2.choose_course(course_obj2)

student_obj1.tell_data()
student_obj2.tell_data()

student_obj1.save_student_info()
student_obj1.tell_student_info()

school_obj1.tell_class()
school_obj2.tell_class()