前言:都已经到了python系列第十九期了,也该整合整合前面的知识点来做一个小系统了,当然这个系统完全是用python的终端来显示,也没有数据库的加入,但是牵涉到了我们前面大部分知识点,当然后续我们会持续更新到一个真正能用的系统。
要求:使用MVC架构来完成代码
将显示与输出的部分放入EmployeeView类中
将对数据的封装放入EmployeeModel类中
将业务逻辑放入EmployeeController类中
系统功能:
显示菜单、选择菜单、录入员工信息(部门编号,员工编号、员工姓名、工资),显示所有员工信息,删除员工信息(通过名字删除,如果有同名删除第一个),修改员工信息(名字不能修改,只能修改工资,部门编号,员工编号)
功能展示:
添加与查看
修改
当然当删除或修改的名字不存在时,也应该返回删除失败
删除成功
实现代码:
# 员工信息管理系统
class EmployeeModel:
def __init__(self, name="", index="", department="", wages=0.0):
self.name = name
self.index = index
self.wages = wages
self.department = department
class EmployeeView:
def __init__(self):
self.employeeController = EmployeeController()
@staticmethod
def display_menu():
print("按1添加员工")
print("按2查看员工")
print("按3删除员工")
print("按4修改员工")
def select_menu(self):
number = input("请操作:")
if number == "1":
self.input_employee()
if number == "2":
self.show_all_employee()
if number == "3":
self.delete_employee()
if number == "4":
self.update_employee()
def input_employee(self):
employee = EmployeeModel(
name=input("请输入员工名字:"),
index=input("请输入员工编号:"),
department=input("请输入部门编号:"),
wages=float(input("请输入员工工资:"))
)
print("添加成功") if self.employeeController.add_employee(employee) else print("添加失败")
def show_all_employee(self):
for item in self.employeeController.list_employee:
print("| %s | %s | %s | %s |" % (item.name, item.index, item.department, item.wages))
def delete_employee(self):
employee_name = input("请输入删除员工名:")
print("删除成功") if self.employeeController.delete_employee(employee_name) else print("删除失败")
def update_employee(self):
employee = EmployeeModel(
name=input("请输入修改员工名字:"),
index=input("请输入修改员工编号:"),
department=input("请输入修改部门编号:"),
wages=float(input("请输入修改员工工资:"))
)
print("修改成功") if self.employeeController.update_employee(employee) else print("修改失败")
class EmployeeController:
def __init__(self):
self.list_employee = []
def add_employee(self, employee:EmployeeModel):
self.list_employee.append(employee)
return True
def delete_employee(self, employee_name):
for i in range(len(self.list_employee)):
if self.list_employee[i].name == employee_name:
del self.list_employee[i]
return True
return False
def update_employee(self, employee:EmployeeModel):
for item in self.list_employee:
if employee.name == item.name:
item.__dict__ = employee.__dict__
return True
return False
if __name__ == '__main__':
view = EmployeeView()
while True:
view.display_menu()
view.select_menu()