需求
設計一個培訓機構管理系統,有總部、分校,有學員、老師、員工,實現具體如下需求:
- 有多個課程,課程要有定價
- 有多個班級,班級跟課程有關聯
- 有多個學生,學生報名班級,交這個班級對應的課程的費用
- 有多個老師,可以分佈在不同校區,上不同班級的課
- 有多個員工,可以分佈在不同校區,在總部可以統計各校區的賬戶餘額、員工人數、學員人數
- 學生可以退學
類圖
實現程式碼
這肯定不是最優程式碼, 如果有大佬給出更優解一定要給我評論哦
- #!usr/bin/env python
- # -*- coding:utf-8 _*-
- """
- # author: 小菠蘿測試筆記
- # blog: https://www.cnblogs.com/poloyy/
- # time: 2021/9/7 11:18 下午
- # file: 18_實戰6.py
- """
- # 課程類
- class Course(object):
- def __init__(self, name, price):
- # 課程名、課程價格:私有屬性
- self.__name = name
- self.__price = price
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, name):
- self.__name = name
- @property
- def price(self):
- return self.__price
- @price.setter
- def price(self, price):
- self.__price = price
- # 人類
- class Person(object):
- def __init__(self, name, sex, phone):
- self.name = name
- self.sex = sex
- self.phone = phone
- def __str__(self):
- return f"姓名:{self.name}, 性別{self.sex}, 電話:{self.phone}"
- # 學生類
- class Student(Person):
- def __init__(self, name, sex, phone, balance):
- super(Student, self).__init__(name, sex, phone)
- # 學生餘額、報名的班級:私有屬性
- self.__balance = balance
- self.__class_list = []
- @property
- def classList(self):
- return [class_.name for class_ in self.__class_list]
- # 報名班級
- def addClass(self, class_):
- price = class_.price
- # 1、如果學生餘額大於班級費用
- if self.__balance > price:
- # 2、報名成功
- self.__class_list.append(class_)
- # 3、減去報名費
- self.__balance -= price
- # 4、班級的學生列表也需要添加當前學生
- class_.addStudent(self)
- # 5、班級總額增加
- class_.totalBalance()
- return
- print("餘額不足,無法報名班級")
- # 退學
- def removeClass(self, class_):
- if class_ in self.__class_list:
- # 1、報名班級列表移除
- self.__class_list.remove(class_)
- # 2、班級學生列表移除當前學生
- class_.removeStudent(self)
- print("班級不存在,無法退學")
- # 員工類
- class Employ(Person):
- def __init__(self, name, sex, phone):
- super(Employ, self).__init__(name, sex, phone)
- # 工資:私有屬性
- self.money = 0
- # 老師類
- class Teacher(Employ):
- def __init__(self, name, sex, phone):
- super(Teacher, self).__init__(name, sex, phone)
- # 授課班級:私有屬性
- self.__class_list = []
- @property
- def classList(self):
- return [class_.name for class_ in self.__class_list]
- # 授課
- def teachClass(self, class_):
- # 1、授課列表新增班級
- self.__class_list.append(class_)
- # 2、班級新增授課老師
- class_.teacher = self
- # 班級類
- class Class(object):
- def __init__(self, name):
- # 班級名、班級費用、課程列表、學生類表、班級老師、所屬學校:私有屬性
- self.__name = name
- self.__price = 0
- self.__course_list = []
- self.__student_list = []
- self.__teacher = None
- self.__balance = 0
- self.__school = None
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, name):
- self.__name = name
- @property
- def school(self):
- return self.__school.name
- @school.setter
- def school(self, school):
- self.__school = school
- @property
- def price(self):
- return self.__price
- @property
- def courseList(self):
- return self.__course_list
- def addCourse(self, course):
- # 1、班級費用累加課程費用
- self.__price += course.price
- # 2、新增到課程列表
- self.__course_list.append(course)
- @property
- def studentList(self):
- return [stu.name for stu in self.__student_list]
- def addStudent(self, student):
- self.__student_list.append(student)
- def removeStudent(self, student):
- self.__student_list.remove(student)
- @property
- def teacher(self):
- return self.__teacher
- @teacher.setter
- def teacher(self, teacher):
- self.__teacher = teacher
- @property
- def balance(self):
- return self.__balance
- # 統計班級一個班級收入
- def totalBalance(self):
- self.__balance = len(self.__student_list) * self.__price
- # 學校類
- class School(object):
- def __init__(self, name, balance):
- # 學校名、學校餘額、學校員工列表:公共屬性
- self.name = name
- self.balance = balance
- self.employ_list = []
- # 分校列表:私有屬性
- self.__school_list = []
- def __str__(self):
- return f"學校:{self.name} 餘額:{self.balance}"
- # 獲取學校分校列表
- @property
- def schoolList(self):
- return [school.name for school in self.__school_list]
- # 新增分校
- def addBranchSchool(self, school):
- self.__school_list.append(school)
- # 新增員工
- def addEmploy(self, employ):
- self.employ_list.append(employ)
- # 檢視員工列表
- def getEmploy(self):
- return [emp.name for emp in self.employ_list]
- # 統計各分校的賬戶餘額
- def getTotalBalance(self):
- res = {}
- sum = 0
- for school in self.__school_list:
- # 1、結算一次分校餘額
- school.getTotalBalance()
- res[school.name] = school.balance
- # 2、累加分校餘額
- sum += school.balance
- res[self.name] = sum
- return res
- # 統計員工人數
- def getTotalEmploy(self):
- sum_emp = 0
- for school in self.__school_list:
- sum_emp += len(school.employ_list)
- sum_emp += len(self.employ_list)
- return sum_emp
- # 統計學生總人數
- def getTotalStudent(self):
- sum_stu = 0
- for school in self.__school_list:
- sum_stu += school.getTotalStudent()
- return sum_stu
- # 分校類
- class BranchSchool(School):
- def __init__(self, name, balance):
- super(BranchSchool, self).__init__(name, balance)
- # 分校的班級列表:私有屬性
- self.__class_list = []
- # 獲取班級列表
- @property
- def classList(self):
- return [class_.name for class_ in self.__class_list]
- # 新增班級
- def addClass(self, class_):
- # 1、新增班級
- self.__class_list.append(class_)
- # 2、新增老師員工
- self.addEmploy(class_.teacher)
- # 獲取總的餘額
- def getTotalBalance(self):
- for class_ in self.__class_list:
- # 1、結算班級收入
- class_.totalBalance()
- # 2、累加班級收入
- self.balance += class_.balance
- # 獲取學生總人數
- def getTotalStudent(self):
- sum_stu = 0
- for class_ in self.__class_list:
- sum_stu += len(class_.studentList)
- return sum_stu
- # 總校
- school = School("小菠蘿總校", 100000)
- # 分校
- bj1 = BranchSchool("小猿圈北京分校", 2222)
- sz1 = BranchSchool("深圳南山大學城分校", 5555)
- # 新增分校
- school.addBranchSchool(bj1)
- school.addBranchSchool(sz1)
- # 初始化班級
- class1 = Class("Python 基礎班級")
- class2 = Class("Python 進階班級")
- # 初始化課程
- c1 = Course("Python 基礎", 666)
- c2 = Course("Python 進階", 1666)
- c3 = Course("Python 實戰", 2666)
- # 新增課程
- class1.addCourse(c1)
- class1.addCourse(c2)
- class2.addCourse(c2)
- class2.addCourse(c3)
- # 初始化老師
- tea1 = Teacher("小菠蘿老師", "girl", 1355001232)
- tea2 = Teacher("大菠蘿老師", "boy", 1355001232)
- # 老師授課
- tea1.teachClass(class1)
- tea2.teachClass(class2)
- bj1.addClass(class1)
- sz1.addClass(class2)
- # 初始化學生
- stu1 = Student("小菠蘿", "girl", 1355001232, 50000)
- stu2 = Student("大菠蘿", "boy", 1355001231, 50000)
- stu3 = Student("大大菠蘿", "girl", 1355001233, 10000)
- # 學生報名
- stu1.addClass(class1)
- stu1.addClass(class2)
- stu2.addClass(class1)
- stu3.addClass(class2)
- # 普通員工
- emp1 = Employ("小菠蘿員工", "girl", 1355001232)
- emp2 = Employ("大菠蘿員工", "boy", 1355001231)
- emp3 = Employ("小小菠蘿員工", "girl", 1355001233)
- print(bj1.getTotalStudent())
- print(school.getTotalBalance())
- print(school.getTotalEmploy())