Python 控制台员工管理系统:使用数组和面向对象实现
以下是一个简单的控制台版本的员工管理系统的代码示例:
class Employee:
def __init__(self, id, name, age, gender):
self.id = id
self.name = name
self.age = age
self.gender = gender
class EmployeeManagementSystem:
def __init__(self):
self.employees = []
def add_employee(self, employee):
self.employees.append(employee)
def remove_employee(self, employee):
self.employees.remove(employee)
def print_employees(self):
for employee in self.employees:
print('ID: {}, Name: {}, Age: {}, Gender: {}'.format(employee.id, employee.name, employee.age, employee.gender))
def search_employee(self, employee_id):
for employee in self.employees:
if employee.id == employee_id:
return employee
return None
def main():
system = EmployeeManagementSystem()
while True:
print('Employee Management System')
print('1. Add Employee')
print('2. Remove Employee')
print('3. Print Employees')
print('4. Search Employee')
print('5. Exit')
choice = input('Enter your choice: ')
if choice == '1':
id = int(input('Enter employee ID: '))
name = input('Enter employee name: ')
age = int(input('Enter employee age: '))
gender = input('Enter employee gender: ')
employee = Employee(id, name, age, gender)
system.add_employee(employee)
print('Employee added successfully!')
elif choice == '2':
id = int(input('Enter employee ID: '))
employee = system.search_employee(id)
if employee:
system.remove_employee(employee)
print('Employee removed successfully!')
else:
print('Employee not found!')
elif choice == '3':
system.print_employees()
elif choice == '4':
id = int(input('Enter employee ID: '))
employee = system.search_employee(id)
if employee:
print('ID: {}, Name: {}, Age: {}, Gender: {}'.format(employee.id, employee.name, employee.age, employee.gender))
else:
print('Employee not found!')
elif choice == '5':
break
else:
print('Invalid choice!')
if __name__ == '__main__':
main()
此代码示例中,Employee类表示员工,具有ID、姓名、年龄和性别属性。EmployeeManagementSystem类表示员工管理系统,具有添加员工、删除员工、打印员工列表和搜索员工等方法。
在main函数中,通过循环显示菜单选项,并根据用户选择执行相应的操作。用户可以选择添加员工、删除员工、打印员工列表、搜索员工或退出系统。
原文地址: https://www.cveoy.top/t/topic/pV3V 著作权归作者所有。请勿转载和采集!