本帖最后由 suchocolate 于 2020-11-25 20:45 编辑 # _*_ coding: utf-8 _*_
# Developer: suchocolate
# Date: 11/25/2020 19:52
# File name: classtest.py
# Development tool: PyCharm
class Employee:
def __init__(self, name, ability, age):
self.name = name
self.ability = ability
self.age = age
def doWork(self):
if self.ability >= 5:
self.ability -= 5
return True
else:
print('能力不足')
return False
def str(self):
print(f'员工姓名:{self.name}, 员工能力:{self.ability}, 员工年龄:{self.age}')
class Boss:
def __init__(self, money, work):
self.money = money
self.employeeList = []
self.work = work
def addEmployee(self, *employee):
for e in employee:
if self.money >= 5000:
self.employeeList.append(e)
self.money -= 5000
else:
print('资金不足,无法雇佣员工工作。')
exit(0)
def startWork(self):
i = 1
lg = len(self.employeeList)
while self.work > 0:
r = i % lg
e = self.employeeList[r]
if not e.doWork():
print(f'需要雇佣新员工,剩余工作量:{self.work}')
break
self.work -= 1
i += 1
self.endWork()
def endWork(self):
if self.work == 0:
print('工作完成!')
for x in self.employeeList:
x.str()
if __name__ == '__main__':
ep1 = Employee('yaa', 20, 30)
ep2 = Employee('ybb', 20, 40)
ep3 = Employee('ycc', 20, 50)
bs = Boss(30000, 30)
bs.addEmployee(ep1, ep2, ep3)
bs.startWork()
|