用字典设计一个数据类型,包括三个数据信息,分别是员工工号,员工姓名,工资,员工工号范围是1001-1101,姓名,工资不限如(1001,小明,800) 2设计一个类,实现员工查询,增加,修改,删除等功能 3员工数据信息最少5条
时间: 2024-10-23 21:06:38 浏览: 9
员工管理系统数据库设计与报告.doc
我们可以使用Python的内置`dict`类型来设计这样一个简单的员工数据结构,同时创建一个Employee类来实现增删改查功能。以下是实现步骤:
首先,定义一个字典数据类型表示每个员工的信息,这里我们限定工号范围为1001到1101:
```python
class EmployeeInfo:
def __init__(self):
self.employees = {i: {'id': i, 'name': '', 'salary': 0} for i in range(1001, 1102)}
# 示例员工信息
example_employee = (1001, '小明', 800)
self.employees[example_employee[0]]['name'] = example_employee[1]
self.employees[example_employee[0]]['salary'] = example_employee[2]
```
接下来,创建Employee类:
```python
class EmployeeManager:
def __init__(self, employee_info):
self.employee_info = employee_info
# 查询员工
def query_employee(self, id):
if id in self.employee_info:
return self.employee_info[id]
else:
return "Employee not found."
# 添加员工
def add_employee(self, id, name, salary):
if 1001 <= id <= 1101:
self.employee_info[id] = {'id': id, 'name': name, 'salary': salary}
else:
print("Invalid ID.")
# 修改员工信息
def update_employee(self, id, name=None, salary=None):
if id in self.employee_info:
if name is not None:
self.employee_info[id]['name'] = name
if salary is not None:
self.employee_info[id]['salary'] = salary
else:
print("Employee not found.")
# 删除员工
def delete_employee(self, id):
if id in self.employee_info:
del self.employee_info[id]
print("Employee deleted.")
else:
print("Employee not found.")
# 创建EmployeeManager实例并操作数据
manager = EmployeeManager(EmployeeInfo())
manager.add_employee(*example_employee) # 添加示例员工
print(manager.query_employee(1001)) # 查询员工
manager.update_employee(1001, name='小红') # 修改员工信息
manager.delete_employee(1002) # 删除员工
```
阅读全文