需求

設計一個培訓機構管理系統,有總部、分校,有學員、老師、員工,實現具體如下需求:

  • 有多個課程,課程要有定價
  • 有多個班級,班級跟課程有關聯
  • 有多個學生,學生報名班級,交這個班級對應的課程的費用
  • 有多個老師,可以分佈在不同校區,上不同班級的課
  • 有多個員工,可以分佈在不同校區,在總部可以統計各校區的賬戶餘額、員工人數、學員人數
  • 學生可以退學

類圖

實現程式碼

這肯定不是最優程式碼, 如果有大佬給出更優解一定要給我評論哦

  1. #!usr/bin/env python
  2. # -*- coding:utf-8 _*-
  3. """
  4. # author: 小菠蘿測試筆記
  5. # blog: https://www.cnblogs.com/poloyy/
  6. # time: 2021/9/7 11:18 下午
  7. # file: 18_實戰6.py
  8. """
  9.  
  10. # 課程類
  11. class Course(object):
  12. def __init__(self, name, price):
  13. # 課程名、課程價格:私有屬性
  14. self.__name = name
  15. self.__price = price
  16.  
  17. @property
  18. def name(self):
  19. return self.__name
  20.  
  21. @name.setter
  22. def name(self, name):
  23. self.__name = name
  24.  
  25. @property
  26. def price(self):
  27. return self.__price
  28.  
  29. @price.setter
  30. def price(self, price):
  31. self.__price = price
  32.  
  33. # 人類
  34. class Person(object):
  35. def __init__(self, name, sex, phone):
  36. self.name = name
  37. self.sex = sex
  38. self.phone = phone
  39.  
  40. def __str__(self):
  41. return f"姓名:{self.name}, 性別{self.sex}, 電話:{self.phone}"
  42.  
  43. # 學生類
  44. class Student(Person):
  45. def __init__(self, name, sex, phone, balance):
  46. super(Student, self).__init__(name, sex, phone)
  47. # 學生餘額、報名的班級:私有屬性
  48. self.__balance = balance
  49. self.__class_list = []
  50.  
  51. @property
  52. def classList(self):
  53. return [class_.name for class_ in self.__class_list]
  54.  
  55. # 報名班級
  56. def addClass(self, class_):
  57. price = class_.price
  58. # 1、如果學生餘額大於班級費用
  59. if self.__balance > price:
  60. # 2、報名成功
  61. self.__class_list.append(class_)
  62. # 3、減去報名費
  63. self.__balance -= price
  64. # 4、班級的學生列表也需要添加當前學生
  65. class_.addStudent(self)
  66. # 5、班級總額增加
  67. class_.totalBalance()
  68. return
  69. print("餘額不足,無法報名班級")
  70.  
  71. # 退學
  72. def removeClass(self, class_):
  73. if class_ in self.__class_list:
  74. # 1、報名班級列表移除
  75. self.__class_list.remove(class_)
  76. # 2、班級學生列表移除當前學生
  77. class_.removeStudent(self)
  78. print("班級不存在,無法退學")
  79.  
  80. # 員工類
  81. class Employ(Person):
  82. def __init__(self, name, sex, phone):
  83. super(Employ, self).__init__(name, sex, phone)
  84. # 工資:私有屬性
  85. self.money = 0
  86.  
  87. # 老師類
  88. class Teacher(Employ):
  89. def __init__(self, name, sex, phone):
  90. super(Teacher, self).__init__(name, sex, phone)
  91. # 授課班級:私有屬性
  92. self.__class_list = []
  93.  
  94. @property
  95. def classList(self):
  96. return [class_.name for class_ in self.__class_list]
  97.  
  98. # 授課
  99. def teachClass(self, class_):
  100. # 1、授課列表新增班級
  101. self.__class_list.append(class_)
  102. # 2、班級新增授課老師
  103. class_.teacher = self
  104.  
  105. # 班級類
  106. class Class(object):
  107. def __init__(self, name):
  108. # 班級名、班級費用、課程列表、學生類表、班級老師、所屬學校:私有屬性
  109. self.__name = name
  110. self.__price = 0
  111. self.__course_list = []
  112. self.__student_list = []
  113. self.__teacher = None
  114. self.__balance = 0
  115. self.__school = None
  116.  
  117. @property
  118. def name(self):
  119. return self.__name
  120.  
  121. @name.setter
  122. def name(self, name):
  123. self.__name = name
  124.  
  125. @property
  126. def school(self):
  127. return self.__school.name
  128.  
  129. @school.setter
  130. def school(self, school):
  131. self.__school = school
  132.  
  133. @property
  134. def price(self):
  135. return self.__price
  136.  
  137. @property
  138. def courseList(self):
  139. return self.__course_list
  140.  
  141. def addCourse(self, course):
  142. # 1、班級費用累加課程費用
  143. self.__price += course.price
  144. # 2、新增到課程列表
  145. self.__course_list.append(course)
  146.  
  147. @property
  148. def studentList(self):
  149. return [stu.name for stu in self.__student_list]
  150.  
  151. def addStudent(self, student):
  152. self.__student_list.append(student)
  153.  
  154. def removeStudent(self, student):
  155. self.__student_list.remove(student)
  156.  
  157. @property
  158. def teacher(self):
  159. return self.__teacher
  160.  
  161. @teacher.setter
  162. def teacher(self, teacher):
  163. self.__teacher = teacher
  164.  
  165. @property
  166. def balance(self):
  167. return self.__balance
  168.  
  169. # 統計班級一個班級收入
  170. def totalBalance(self):
  171. self.__balance = len(self.__student_list) * self.__price
  172.  
  173. # 學校類
  174. class School(object):
  175. def __init__(self, name, balance):
  176. # 學校名、學校餘額、學校員工列表:公共屬性
  177. self.name = name
  178. self.balance = balance
  179. self.employ_list = []
  180. # 分校列表:私有屬性
  181. self.__school_list = []
  182.  
  183. def __str__(self):
  184. return f"學校:{self.name} 餘額:{self.balance}"
  185.  
  186. # 獲取學校分校列表
  187. @property
  188. def schoolList(self):
  189. return [school.name for school in self.__school_list]
  190.  
  191. # 新增分校
  192. def addBranchSchool(self, school):
  193. self.__school_list.append(school)
  194.  
  195. # 新增員工
  196. def addEmploy(self, employ):
  197. self.employ_list.append(employ)
  198.  
  199. # 檢視員工列表
  200. def getEmploy(self):
  201. return [emp.name for emp in self.employ_list]
  202.  
  203. # 統計各分校的賬戶餘額
  204. def getTotalBalance(self):
  205. res = {}
  206. sum = 0
  207. for school in self.__school_list:
  208. # 1、結算一次分校餘額
  209. school.getTotalBalance()
  210. res[school.name] = school.balance
  211. # 2、累加分校餘額
  212. sum += school.balance
  213. res[self.name] = sum
  214. return res
  215.  
  216. # 統計員工人數
  217. def getTotalEmploy(self):
  218. sum_emp = 0
  219. for school in self.__school_list:
  220. sum_emp += len(school.employ_list)
  221. sum_emp += len(self.employ_list)
  222. return sum_emp
  223.  
  224. # 統計學生總人數
  225. def getTotalStudent(self):
  226. sum_stu = 0
  227. for school in self.__school_list:
  228. sum_stu += school.getTotalStudent()
  229. return sum_stu
  230.  
  231. # 分校類
  232. class BranchSchool(School):
  233. def __init__(self, name, balance):
  234. super(BranchSchool, self).__init__(name, balance)
  235. # 分校的班級列表:私有屬性
  236. self.__class_list = []
  237.  
  238. # 獲取班級列表
  239. @property
  240. def classList(self):
  241. return [class_.name for class_ in self.__class_list]
  242.  
  243. # 新增班級
  244. def addClass(self, class_):
  245. # 1、新增班級
  246. self.__class_list.append(class_)
  247. # 2、新增老師員工
  248. self.addEmploy(class_.teacher)
  249.  
  250. # 獲取總的餘額
  251. def getTotalBalance(self):
  252. for class_ in self.__class_list:
  253. # 1、結算班級收入
  254. class_.totalBalance()
  255. # 2、累加班級收入
  256. self.balance += class_.balance
  257.  
  258. # 獲取學生總人數
  259. def getTotalStudent(self):
  260. sum_stu = 0
  261. for class_ in self.__class_list:
  262. sum_stu += len(class_.studentList)
  263. return sum_stu
  264.  
  265. # 總校
  266. school = School("小菠蘿總校", 100000)
  267. # 分校
  268. bj1 = BranchSchool("小猿圈北京分校", 2222)
  269. sz1 = BranchSchool("深圳南山大學城分校", 5555)
  270.  
  271. # 新增分校
  272. school.addBranchSchool(bj1)
  273. school.addBranchSchool(sz1)
  274.  
  275. # 初始化班級
  276. class1 = Class("Python 基礎班級")
  277. class2 = Class("Python 進階班級")
  278.  
  279. # 初始化課程
  280. c1 = Course("Python 基礎", 666)
  281. c2 = Course("Python 進階", 1666)
  282. c3 = Course("Python 實戰", 2666)
  283.  
  284. # 新增課程
  285. class1.addCourse(c1)
  286. class1.addCourse(c2)
  287. class2.addCourse(c2)
  288. class2.addCourse(c3)
  289.  
  290. # 初始化老師
  291. tea1 = Teacher("小菠蘿老師", "girl", 1355001232)
  292. tea2 = Teacher("大菠蘿老師", "boy", 1355001232)
  293.  
  294. # 老師授課
  295. tea1.teachClass(class1)
  296. tea2.teachClass(class2)
  297.  
  298. bj1.addClass(class1)
  299. sz1.addClass(class2)
  300.  
  301. # 初始化學生
  302. stu1 = Student("小菠蘿", "girl", 1355001232, 50000)
  303. stu2 = Student("大菠蘿", "boy", 1355001231, 50000)
  304. stu3 = Student("大大菠蘿", "girl", 1355001233, 10000)
  305. # 學生報名
  306. stu1.addClass(class1)
  307. stu1.addClass(class2)
  308. stu2.addClass(class1)
  309. stu3.addClass(class2)
  310.  
  311. # 普通員工
  312. emp1 = Employ("小菠蘿員工", "girl", 1355001232)
  313. emp2 = Employ("大菠蘿員工", "boy", 1355001231)
  314. emp3 = Employ("小小菠蘿員工", "girl", 1355001233)
  315.  
  316. print(bj1.getTotalStudent())
  317. print(school.getTotalBalance())
  318. print(school.getTotalEmploy())