创建有员工类,包括姓名、工号、性别、出生年月、家庭地址等。本系统能够按姓名或工号查找员工,并显示相关员工情况。包括:主控程序、员工档案管理子系统、员工类等。 其他要求: (1)实用,各模块自成系统 (2)完成最低要求:建立一个文件,包括同一个专业10个员工的必要信息,能对文件进行补充、修订、删除,并能进行查找。
时间: 2024-03-16 19:46:37 浏览: 100
好的,这是一个员工档案管理系统的需求,我可以提供一些思路和代码实现的建议。首先,我们需要定义一个员工类,包括姓名、工号、性别、出生年月、家庭地址等属性,以及相关的方法,如打印员工信息等。代码如下:
```python
class Employee:
def __init__(self, name, id, gender, birthday, address):
self.name = name
self.id = id
self.gender = gender
self.birthday = birthday
self.address = address
def print_info(self):
print(f"Name: {self.name}")
print(f"ID: {self.id}")
print(f"Gender: {self.gender}")
print(f"Birthday: {self.birthday}")
print(f"Address: {self.address}")
```
接下来,我们可以定义一个员工档案管理子系统,包括添加员工、修改员工、删除员工、查找员工等方法。这里我们使用一个文件来存储员工信息,文件格式可以是CSV或JSON等,这里我们使用CSV格式。代码如下:
```python
import csv
class EmployeeManagementSystem:
def __init__(self, file_path):
self.file_path = file_path
self.employees = []
def load_data(self):
with open(self.file_path, 'r') as f:
reader = csv.reader(f)
for row in reader:
name, id, gender, birthday, address = row
employee = Employee(name, id, gender, birthday, address)
self.employees.append(employee)
def save_data(self):
with open(self.file_path, 'w') as f:
writer = csv.writer(f)
for employee in self.employees:
writer.writerow([employee.name, employee.id, employee.gender, employee.birthday, employee.address])
def add_employee(self, employee):
self.employees.append(employee)
self.save_data()
def delete_employee_by_name(self, name):
for employee in self.employees:
if employee.name == name:
self.employees.remove(employee)
self.save_data()
return True
return False
def delete_employee_by_id(self, id):
for employee in self.employees:
if employee.id == id:
self.employees.remove(employee)
self.save_data()
return True
return False
def update_employee(self, employee):
for i, e in enumerate(self.employees):
if e.id == employee.id:
self.employees[i] = employee
self.save_data()
return True
return False
def find_employee_by_name(self, name):
for employee in self.employees:
if employee.name == name:
return employee
return None
def find_employee_by_id(self, id):
for employee in self.employees:
if employee.id == id:
return employee
return None
```
最后,我们可以编写一个主控程序来测试我们的员工档案管理子系统。代码如下:
```python
if __name__ == '__main__':
system = EmployeeManagementSystem('employees.csv')
system.load_data()
while True:
print('1. Add employee')
print('2. Delete employee by name')
print('3. Delete employee by id')
print('4. Update employee')
print('5. Find employee by name')
print('6. Find employee by id')
print('7. Exit')
choice = input('Enter your choice: ')
if choice == '1':
name = input('Enter name: ')
id = input('Enter id: ')
gender = input('Enter gender: ')
birthday = input('Enter birthday: ')
address = input('Enter address: ')
employee = Employee(name, id, gender, birthday, address)
system.add_employee(employee)
print('Employee added successfully.')
elif choice == '2':
name = input('Enter name: ')
if system.delete_employee_by_name(name):
print('Employee deleted successfully.')
else:
print('Employee not found.')
elif choice == '3':
id = input('Enter id: ')
if system.delete_employee_by_id(id):
print('Employee deleted successfully.')
else:
print('Employee not found.')
elif choice == '4':
id = input('Enter id: ')
employee = system.find_employee_by_id(id)
if employee is not None:
name = input('Enter name: ')
gender = input('Enter gender: ')
birthday = input('Enter birthday: ')
address = input('Enter address: ')
employee.name = name
employee.gender = gender
employee.birthday = birthday
employee.address = address
system.update_employee(employee)
print('Employee updated successfully.')
else:
print('Employee not found.')
elif choice == '5':
name = input('Enter name: ')
employee = system.find_employee_by_name(name)
if employee is not None:
employee.print_info()
else:
print('Employee not found.')
elif choice == '6':
id = input('Enter id: ')
employee = system.find_employee_by_id(id)
if employee is not None:
employee.print_info()
else:
print('Employee not found.')
elif choice == '7':
break
else:
print('Invalid choice.')
```
这样,我们就实现了一个简单的员工档案管理系统。当然,这只是一个初级的版本,还有很多可以改进和优化的地方,比如增加异常处理、加密存储等。
阅读全文