1设计一个数据类型(列表,元组,字典等),包括三个数据信息,分别是员工工号,员工姓名,工资,员工工号范围是1001-1101,姓名,工资不限如(1001,小明,800) 2设计一个类,实现员工查询,增加,修改,删除等功能 3员工数据信息最少5条
时间: 2024-10-23 20:06:55 浏览: 14
为了实现这个需求,我们可以设计一个名为`Employee`的类,它包含员工的基本信息,并提供相应的操作功能。我们将创建一个`EmployeeRecord`数据结构,用于存储每个员工的信息。
```python
class EmployeeRecord:
def __init__(self, employee_id, name, salary):
self.employee_id = int(employee_id) if 1001 <= int(employee_id) <= 1101 else None
self.name = str(name)
self.salary = float(salary)
# 创建一个员工记录列表来保存至少5条数据
employee_records = [
EmployeeRecord(1001, "小明", 800),
EmployeeRecord(1002, "小红", 900),
EmployeeRecord(1003, "张三", 700),
EmployeeRecord(1004, "李四", 1000),
EmployeeRecord(1005, "王五", 650),
]
class EmployeeDatabase:
def __init__(self):
self.records = employee_records
# 员工查询
def search_employee(self, id):
for record in self.records:
if record.employee_id == id:
return record
return None
# 增加员工
def add_employee(self, employee_id, name, salary):
if 1001 <= employee_id <= 1101:
new_record = EmployeeRecord(employee_id, name, salary)
self.records.append(new_record)
else:
print("员工工号超出范围")
# 修改员工信息
def update_employee(self, id, new_name=None, new_salary=None):
for record in self.records:
if record.employee_id == id:
if new_name is not None:
record.name = new_name
if new_salary is not None:
record.salary = new_salary
break
# 删除员工
def delete_employee(self, id):
for index, record in enumerate(self.records):
if record.employee_id == id:
del self.records[index]
break
# 使用示例
db = EmployeeDatabase()
print(db.search_employee(1001))
db.add_employee(1006, "赵六", 1200)
db.update_employee(1002, new_name="小红新名")
db.delete_employee(1004)
```
阅读全文