python基础-面向对象(七)作业-(选课系统)使用类的组合,继承等

角色:学校,学生,课程,老师

要求:

1.创建北京,上海两所学校

2.创建linux,python,go3个课程,linux、py在北京开,go在上海开

3.课程包含周期,价格

4.班级关联课程,老师

5.创建学员时,选择学校,关联班级

6.创建讲师角色时

low low 版本。没有使用规范化的程序结构。

 

完善版本在这里https://www.cnblogs.com/liaoyifu/p/14179515.html

import pickle
import hashlib


class Hash:  # 用hashlib生成文件名
    def hexdigst(self):
        a = "%s" % self.name
        b = hashlib.md5('sb'.encode('utf8'))
        b.update(a.encode('utf8'))
        return b.hexdigest()


class Base(Hash):  # 文件的生成和读取
    def save(self):
        name = super().hexdigst()  # 调用hash,返回md5,用返回值作为文件名
        with open('%s.db' % name, 'wb') as f1:
            pickle.dump(self, f1)

    def load(self):
        name = super().hexdigst()
        with open('%s.db' % name, 'rb') as f2:
            a = pickle.load(f2)
        return a  # 返回文件读取的内容


class School(Base):
    def __init__(self, name):
        self.name = name


class Student(Base):
    def __init__(self, name, school):
        self.name = name
        self.school = school


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


class Teacher(Base):
    def __init__(self, name, school):
        self.name = name
        self.shool = school


# 创建学校
beijing = School('北京')
beijing.save()
# print(beijing.load().name)
shanghai = School('上海')
shanghai.save()

# 创建课程linex,pythoon,go
linux = Course('linux', '北京', 10, '$100')
linux.save()
python1 = Course('python', '北京', 20, '$200')
python1.save()
go = Course('go', '上海', 30, '$200')
go.save()

# 用户交互
while True:
    student_name = input('请输入你的名字:')
    msg1 = '''1 北京
    2 上海'''
    print(msg1)
    student_school = input('请选择你的学校:')
    choice_school = {'1': beijing, '2': shanghai}
    choiced_school = choice_school[student_school]
    msg2 = '''1 linux Teacher li
    2 python Teacher alex
    3 go Teacher 旺财'''
    print(msg2)
    student_banji = input('请选择你的班级:')
    choice_course = {'1': linux, '2': python1, "3": go}
    choiced_course = choice_course[student_banji]
    choice_teacher = {'1': 'Teacher li', '2': 'alex', '3': '旺财'}
    choiced_teacher = Teacher(choice_teacher[student_banji], choiced_school)
    choiced_teacher.save()
    print('你选择的是%s学校的%s老师的%s课程,课程周期%d,价格%s' % (
    choiced_school.name, choiced_teacher.name, choiced_course.name, choiced_course.period, choiced_course.price))

 

 

posted @ 2020-12-11 15:34  枫叶学python  阅读(185)  评论(0)    收藏  举报