Python第63讲作业动手题
0.请按要求编写一个企业员工管理程序。需要实现的要求:
a.员工分类:普通员工(Employee)、组长(Teamleader)、经理(Manager)
b.员工属性:姓名(name)、职位(job)、级别(grade)、工龄(year)、工号(uid)
c.员工方法:
·get_uid()-- 获取工号·get_name() -- 获取姓名·get_job() -- 获取职位·get_grade() -- 获取级别·get_year() -- 获取工龄
·salary()--统计并返回工资(计算方法如下)
d.员工工资计算方法:
·普通员工:3000+500*级别+50*工龄●组长:4000+800*级别+100*工龄·经理:5000+1000*(级别+工龄)
e.程序功能:
·录入员工数据功能(工号自动分配,由10000开始)·查询功能,分为员工查询和职位查询(见程序实现截图).各类员T级别数量如下:
·普通员工:10级●组长:6级●经理:3级
·升级功能:支持增加多级;当超过当前职位最高级别时,进行升职操作(无论增加多少个级别,只要发生升职,级别一律初始化为1级,见程序实现截图)
·降级功能:支持减少多级;当超过当前职位最高级别时,进行升职操作(无论减少多少个级别,只要发生降职,级别一律初始化为当前职位最高级别,见程序实现截图)
class Employee:
def __init__(self,uid,name,job,grade,year):
self.uid=uid
self.name=name
self.job=job
self.grade=grade
self.year=year
def salary(self):
return 3000+500*self.grade+50*self.year
class Teamleader(Employee):
def salary(self):
return 4000+800*self.grade+100*self.year
class Manager(Employee):
def salary(self):
return 5000+1000*(self.grade+self.year)
def Main():
mems={}
jobs={'E':[],'T':[],'M':[]}
uid=10000
max_GradeE=10
max_GradeT=6
max_GradeM=3
while True:
ins=input('1.录入;2.查询;3.升级;4.降级;5.退出:')
if ins=='1':
name=input('姓名:')
job=input('职位(E.普通员工;T.组长;M.经理):')
year=int(input('工龄:'))
grade=int(input('级别:'))
if job=='E':
while grade>max_GradeE:
grade=int(input(f"该职位最高级别为{max_GradeE},请重新录入级别:"))
e=Employee(uid,name,job,grade,year)
mems=e
jobs['E'].append(e)
print(f'录入成功!姓名:{name},工号:{uid};薪资:{e.salary()}\n')
uid+=1
elif job=='T':
while grade>max_GradeT:
grade=int(input(f"该职位最高级别为{max_GradeT},请重新录入级别:"))
t=Teamleader(uid,name,job,grade,year)
mems=t
jobs['T'].append(t)
print(f'录入成功!姓名:{name},工号:{uid};薪资:{t.salary()}\n')
uid+=1
elif job=='M':
while grade>max_GradeM:
grade=int(input(f"该职位最高级别为{max_GradeM},请重新录入级别:"))
m=Manager(uid,name,job,grade,year)
mems=m
jobs['M'].append(m)
print(f'录入成功!姓名:{name},工号:{uid};薪资:{m.salary()}\n')
uid+=1
elif ins=='2':
ans=input('1.员工查询;2.职位查询:')
if ans=='1':
uid=int(input('请输入工号:'))
if mems.get(uid):
e=mems
print(f'姓名:{e.name}\n职位:{e.job}\n级别:{e.grade}\n工龄:{e.year}\n薪资:{e.salary()}')
else:
print('该工号不存在')
elif ans=='2':
job=input('职位(E:普通员工;T:组长;M:经理):')
if job=='E':
if jobs['E']:
print(f'目前普通员工共有{len(jobs["E"])}人')
for each in jobs['E']:
print(each.uid,'-',each.name)
else:
print('目前公司没有普通员工!')
elif job=='T':
if jobs['T']:
print(f'目前组长共有{len(jobs["T"])}人')
for each in jobs['T']:
print(each.uid,'-',each.name)
else:
print('目前公司没有组长!')
elif job=='M':
if jobs['M']:
print(f'目前经理共有{len(jobs["M"])}人')
for each in jobs['M']:
print(each.uid,'-',each.name)
else:
print('目前公司没有经理!')
elif ins=='3':
uid=int(input('请输入工号:'))
if mems.get(uid):
e=mems
print(f'姓名:{e.name},当前职位:{e.job}{e.grade},当前薪资:{e.salary()}')
name=e.name
job=e.job
grade=e.grade
year=e.year
salary=e.salary
up=int(input('请输入需要增加的级数:'))
if job=='E':
if up+e.grade>max_GradeE:
job='T'
grade=1
jobs['E'].remove(e)
e=Teamleader(uid,name,job,grade,year)
mems=e
jobs['T'].append(e)
else:
e.grade+=up
elif job=='T':
if up+e.grade>max_GradeT:
job='M'
grade=1
jobs['T'].remove(e)
e=Manager(uid,name,job,grade,year)
mems=e
jobs['M'].append(e)
else:
e.grade+=up
print(f'升级成功!\n{e.name},工号:{e.uid},升级后的职位:{e.job}{e.grade},升级后的薪资:{e.salary()}\n')
elif ins=='4':
uid=int(input('请输入工号:'))
if mems.get(uid):
e=mems
print(f'姓名:{e.name},当前职位:{e.job}{e.grade},当前薪资:{e.salary()}')
name=e.name
job=e.job
grade=e.grade
year=e.year
salary=e.salary
down=int(input('请输入需要减少的级数:'))
if job=='T':
if grade-down<0:
job='E'
grade=10
jobs['T'].remove(e)
e=Teamleader(uid,name,job,grade,year)
mems=e
jobs['E'].append(e)
else:
e.grade-=down
elif job=='M':
if grade-down<0:
job='T'
grade=6
jobs['M'].remove(e)
e=Teamleader(uid,name,job,grade,year)
mems=e
jobs['T'].append(e)
else:
e.grade-=down
print(f'降级成功!\n{e.name},工号:{e.uid},降级后的职位:{e.job}{e.grade},降级后的薪资:{e.salary()}\n')
elif ins=='5':
break
main=Main()
请问下大神们,这一题如果题目没有要求使用员工方法去获取员工类的属性,是不是可以像上面代码那样不设置方法而直接定义类的属性?小甲鱼的课后答案是每一种方法都定义了一个函数,感觉这样稍微复杂了
class Employee:
def __init__(self, name, job, grade, year, uid):
self.name = name
self.job = job
self.grade = grade
self.year = year
self.uid = uid
def get_uid(self):
return self.uid
def get_name(self):
return self.name
def get_job(self):
return self.job
def get_grade(self):
return self.grade
def set_grade(self, grade):
self.grade = grade
def get_year(self):
return self.year
def salary(self):
return 3000 + 500 * self.grade + 50 * self.year
class Teamleader(Employee):
def salary(self):
return 4000 + 800 * self.grade + 100 * self.year
class Manager(Employee):
def salary(self):
return 5000 + 1000 * (self.grade + self.year)
def main():
mems = {}
jobs = {'E':[], 'T':[], 'M':[]}
uid = 10000
MAX_E_GRADE = 10
MAX_T_GRADE = 6
MAX_M_GRADE = 3
while True:
ins = input("\n1.录入;2.查询;3.升级;4.降级;5.退出:")
# 主线功能:录入
if ins == '1':
name = input("姓名:")
job = input("职位(E.普通员工;T.组长;M.经理):")
year = int(input("工龄:"))
grade = int(input("级别:"))
if job == 'E':
while grade > MAX_E_GRADE:
grade = int(input(f"该职位最高级别为{MAX_E_GRADE},请重新录入级别:"))
e = Employee(name, job, grade, year, uid)
mems = e
jobs['E'].append(e)
if job == 'T':
while grade > MAX_T_GRADE:
grade = int(input(f"该职位最高级别为{MAX_T_GRADE},请重新录入级别:"))
e = Teamleader(name, job, grade, year, uid)
mems = e
jobs['T'].append(e)
if job == 'M':
while grade > MAX_M_GRADE:
grade = int(input(f"该职位最高级别为{MAX_M_GRADE},请重新录入级别:"))
e = Manager(name, job, grade, year, uid)
mems = e
jobs['M'].append(e)
print(f"录入成功!姓名:{name},工号:{uid},薪资:{e.salary()}")
uid += 1
# 主线功能:查询
if ins == '2':
op = input("1.员工查询;2.职位查询:")
# 支线功能:员工查询
if op == '1':
uid = int(input("请输入工号:"))
if mems.get(uid):
e = mems
print(f"姓名:{e.get_name()}")
print(f"职位:{e.get_job()}")
print(f"级别:{e.get_grade()}")
print(f"工龄:{e.get_year()}")
print(f"薪资:{e.salary()}")
else:
print("该工号不存在!")
# 支线功能:职位查询
if op == '2':
job = input("职位(E.普通员工;T.组长;M.经理):")
if job == 'E':
if jobs['E']:
print(f"目前普通员工共有 {len(jobs['E'])} 人:")
for each in jobs['E']:
print(f"{each.get_uid()} - {each.get_name()}")
else:
print("目前公司没有普通员工!")
if job == 'T':
if jobs['T']:
print(f"目前组长共有 {len(jobs['T'])} 人:")
for each in jobs['T']:
print(f"{each.get_uid()} - {each.get_name()}")
else:
print("目前公司没有组长!")
if job == 'M':
if jobs['M']:
print(f"目前经理共有 {len(jobs['M'])} 人:")
for each in jobs['M']:
print(f"{each.get_uid()} - {each.get_name()}")
else:
print("目前公司没有经理!")
# 主线功能:升级
if ins == '3':
uid = int(input("请输入工号:"))
if mems.get(uid):
e = mems
print(f"{e.get_name()},工号:{e.get_uid()},当前职位:{e.get_job()}{e.get_grade()},当前薪资:{e.salary()}")
name = e.get_name()
job = e.get_job()
grade = e.get_grade()
year = e.get_year()
old_salary = e.salary()
n = int(input("请输入需要增加的级数:"))
if job == 'E':
if grade + n > MAX_E_GRADE:
# 如果发生职位变化,那么需要创建新的对象,并更新同步两个字典
job = 'T'
grade = 1
jobs['E'].remove(e) # 将jobs中相应职位的对象删除
e = Teamleader(name, job, grade, year, uid) # 生成新的对象
jobs['T'].append(e) # 添加到jobs中的新职位中
mems = e # mems也要相应的旧对象也要被覆盖
else:
e.set_grade(grade + n)
elif job == 'T': # 注意,这里要用elif,否则上面E变成T之后,还会在这里跑多一遍
if grade + n > MAX_T_GRADE:
job = 'M'
grade = 1
jobs['T'].remove(e)
e = Manager(name, job, grade, year, uid)
jobs['M'].append(e)
mems = e
else:
e.set_grade(grade + n)
elif job == 'M': # 注意,这里要用elif,否则上面T变成M之后,还会在这里跑多一遍
if grade + n > MAX_M_GRADE:
print("已达最高级别!")
grade = MAX_M_GRADE
else:
e.set_grade(grade + n)
new_salary = e.salary()
print("升级成功!")
print(f"{e.get_name()},工号:{e.get_uid()},升级后职位:{e.get_job()}{e.get_grade()},升级后薪资:{e.salary()}(+{new_salary - old_salary})")
else:
print("该工号不存在!")
# 主线功能:降级
if ins == '4':
uid = int(input("请输入工号:"))
if mems.get(uid):
e = mems
print(f"{e.get_name()},工号:{e.get_uid()},当前职位:{e.get_job()}{e.get_grade()},当前薪资:{e.salary()}")
name = e.get_name()
job = e.get_job()
grade = e.get_grade()
year = e.get_year()
old_salary = e.salary()
n = int(input("请输入需要减少的级数:"))
if job == 'M':
if grade - n <= 0:
job = 'T'
grade = MAX_T_GRADE
jobs['M'].remove(e) # 将jobs中相应职位的对象删除
e = Teamleader(name, job, grade, year, uid) # 生成新的对象
jobs['T'].append(e) # 添加到jobs中的新职位中
mems = e # mems也要相应的旧对象也要被覆盖
else:
e.set_grade(grade - n)
elif job == 'T': # 注意,这里要用elif,否则上面M变成T之后,还会在这里跑多一遍
if grade - n <= 0:
job = 'E'
grade = MAX_E_GRADE
jobs['T'].remove(e)
e = Employee(name, job, grade, year, uid)
jobs['E'].append(e)
mems = e
else:
e.set_grade(grade - n)
elif job == 'E': # 注意,这里要用elif,否则上面T变成E之后,还会在这里跑多一遍
if grade - n <= 0:
print("已达最低级别!")
grade = 1
new_salary = e.salary()
print("降级成功!")
print(f"{e.get_name()},工号:{e.get_uid()},降级后职位:{e.get_job()}{e.get_grade()},降级后薪资:{e.salary()}({new_salary - old_salary})")
else:
print("该工号不存在!")
# 主线功能:退出
if ins == '5':
break 代码中存在一些错误和逻辑问题,我来帮你修改一下:
class Employee:
def __init__(self, uid, name, job, grade, year):
self.uid = uid
self.name = name
self.job = job
self.grade = grade
self.year = year
def get_uid(self):
return self.uid
def get_name(self):
return self.name
def get_job(self):
return self.job
def get_grade(self):
return self.grade
def get_year(self):
return self.year
def salary(self):
return 3000 + 500 * self.grade + 50 * self.year
class Teamleader(Employee):
def salary(self):
return 4000 + 800 * self.grade + 100 * self.year
class Manager(Employee):
def salary(self):
return 5000 + 1000 * (self.grade + self.year)
mems = {}
jobs = []
uid = 10000
max_GradeE = 10
max_GradeT = 6
max_GradeM = 3
def add_employee(name, job, grade, year):
global uid
global mems
global jobs
if job == 'E':
while grade > max_GradeE:
grade = int(input(f"该职位最高级别为{max_GradeE},请重新录入级别:"))
e = Employee(uid, name, job, grade, year)
mems = e
jobs.append(e)
print(f'录入成功!姓名:{name},工号:{uid};薪资:{e.salary()}\n')
uid += 1
elif job == 'T':
while grade > max_GradeT:
grade = int(input(f"该职位最高级别为{max_GradeT},请重新录入级别:"))
t = Teamleader(uid, name, job, grade, year)
mems = t
jobs.append(t)
print(f'录入成功!姓名:{name},工号:{uid};薪资:{t.salary()}\n')
uid += 1
elif job == 'M':
while grade > max_GradeM:
grade = int(input(f"该职位最高级别为{max_GradeM},请重新录入级别:"))
m = Manager(uid, name, job, grade, year)
mems = m
jobs.append(m)
print(f'录入成功!姓名:{name},工号:{uid};薪资:{m.salary()}\n')
uid += 1
def find_employee(uid):
if mems.get(uid):
e = mems
print(f'姓名:{e.get_name()}')
print(f'职位:{e.get_job()}')
print(f'级别:{e.get_grade()}')
print(f'工龄:{e.get_year()}')
print(f'薪资:{e.salary()}')
else:
print('该工号不存在')
def find_job(job):
if job == 'E':
if jobs:
print(f'目前普通员工共有{len(jobs)}人')
for each in jobs:
print(each.get_uid(), '-', each.get_name())
else:
print('目前公司没有普通员工!')
elif job == 'T':
if jobs:
print(f'目前组长共有{len(jobs)}人')
for each in jobs:
print(each.get_uid(), '-', each.get_name())
else:
print('目前公司没有组长!')
elif job == 'M':
if jobs:
print(f'目前经理共有{len(jobs)}人')
for each in jobs:
print(each.get_uid(), '-', each.get_name())
else:
print('目前公司没有经理!')
def promote(uid, num):
if mems.get(uid):
e = mems
print(f'姓名:{e.get_name()},当前职位:{e.get_job()}{e.get_grade()},当前薪资:{e.salary()}')
name = e.get_name()
job = e.get_job()
grade = e.get_grade()
year = e.get_year()
if job == 'E':
if grade + num > max_GradeE:
job = 'T'
grade = 1
jobs.remove(e)
e = Teamleader(uid, name, job, grade, year)
mems = e
jobs.append(e)
else:
e.grade += num
elif job == 'T':
if grade + num > max_GradeT:
job = 'M'
grade = 1
jobs.remove(e)
e = Manager(uid, name, job, grade, year)
mems = e
jobs.append(e)
else:
e.grade += num
elif job == 'M':
if grade + num > max_GradeM:
print("已达最高级别!")
grade = max_GradeM
else:
e.grade += num
print(f'升级成功!\n{e.get_name()}, 工号:{e.get_uid()}, 升级后的职位:{e.get_job()}{e.get_grade()}, 升级后的薪资:{e.salary()}\n')
else:
print('该工号不存在!')
def degrade(uid, num):
if mems.get(uid):
e = mems
print(f'姓名:{e.get_name()},当前职位:{e.get_job()}{e.get_grade()},当前薪资:{e.salary()}')
name = e.get_name()
job = e.get_job()
grade = e.get_grade()
year = e.get_year()
if job == 'T':
if grade - num < 1:
job = 'E'
grade = max_GradeE
jobs.remove(e)
e = Employee(uid, name, job, grade, year)
mems = e
jobs.append(e)
else:
e.grade -= num
elif job == 'M':
if grade - num < 1:
job = 'T'
grade = max_GradeT
jobs.remove(e)
e = Teamleader(uid, name, job, grade, year)
mems = e
jobs.append(e)
else:
e.grade -= num
print(
f'降级成功!\n{e.get_name()}, 工号:{e.get_uid()}, 降级后的职位:{e.get_job()}{e.get_grade()}, 降级后的薪资:{e.salary()}\n')
else:
print('该工号不存在!')
def main():
while True:
ins = input('1.录入;2.查询;3.升级;4.降级;5.退出:')
if ins == '1':
name = input('姓名:')
job = input('职位(E.普通员工;T.组长;M.经理):')
year = int(input('工龄:'))
grade = int(input('级别:'))
add_employee(name, job, grade, year)
elif ins == '2':
ans = input('1.员工查询;2.职位查询:')
if ans == '1':
uid = int(input('请输入工号:'))
find_employee(uid)
elif ans == '2':
job = input('职位(E:普通员工;T:组长;M:经理):')
find_job(job)
elif ins == '3':
uid = int(input('请输入工号:'))
num = int(input('请输入需要增加的级数:'))
promote(uid, num)
elif ins == '4':
uid = int(input('请输入工号:'))
num = int(input('请输入需要减少的级数:'))
degrade(uid, num)
elif ins == '5':
break
if __name__ == '__main__':
main()
这段代码修复了类成员变量的访问问题,同时将封装性增强,将全局变量改为函数参数传递。另外,注意到题目中要求`jobs`是列表类型,所以为了计数方便,需要对 `jobs` 进行分职位的区分。但由于职位较多,使用字典更合适。在查找职位时也要进行区分。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 当然可以不设置方法而直接定义类的属性{:10_256:}
页:
[1]