公司员工管理系统python
时间: 2023-12-13 20:33:40 浏览: 43
以下是一个简单的员工管理系统的Python代码示例,包括登录认证、员工信息录入、员工信息删除、信息统计和信息查找等功能模块。代码注释详细,适合学习。
```python
# 导入模块
import os
import json
# 定义员工信息文件路径
data_file = 'data.json'
# 登录认证
def login():
print('欢迎使用员工管理系统,请先登录')
while True:
username = input('请输入用户名:')
password = input('请输入密码:')
if username == 'admin' and password == '123456':
print('登录成功!')
break
else:
print('用户名或密码错误,请重新输入!')
# 员工信息录入
def add_employee():
print('请输入员工信息:')
name = input('姓名:')
age = input('年龄:')
gender = input('性别:')
salary = input('工资:')
employee = {'name': name, 'age': age, 'gender': gender, 'salary': salary}
if os.path.exists(data_file):
with open(data_file, 'r') as f:
employees = json.load(f)
else:
employees = []
employees.append(employee)
with open(data_file, 'w') as f:
json.dump(employees, f)
print('员工信息录入成功!')
# 员工信息删除
def delete_employee():
print('请输入要删除的员工姓名:')
name = input('姓名:')
if os.path.exists(data_file):
with open(data_file, 'r') as f:
employees = json.load(f)
for employee in employees:
if employee['name'] == name:
employees.remove(employee)
with open(data_file, 'w') as f:
json.dump(employees, f)
print('员工信息删除成功!')
break
else:
print('未找到该员工信息!')
else:
print('员工信息文件不存在!')
# 信息统计
def statistics():
if os.path.exists(data_file):
with open(data_file, 'r') as f:
employees = json.load(f)
total_salary = 0
for employee in employees:
total_salary += int(employee['salary'])
print('员工总数:', len(employees))
print('员工平均工资:', total_salary / len(employees))
else:
print('员工信息文件不存在!')
# 信息查找
def search_employee():
print('请输入要查找的员工姓名:')
name = input('姓名:')
if os.path.exists(data_file):
with open(data_file, 'r') as f:
employees = json.load(f)
for employee in employees:
if employee['name'] == name:
print('员工信息如下:')
print('姓名:', employee['name'])
print('年龄:', employee['age'])
print('性别:', employee['gender'])
print('工资:', employee['salary'])
break
else:
print('未找到该员工信息!')
else:
print('员工信息文件不存在!')
# 主函数
def main():
login()
while True:
print('请选择要进行的操作:')
print('1. 员工信息录入')
print('2. 员工信息删除')
print('3. 信息统计')
print('4. 信息查找')
print('5. 退出系统')
choice = input('请选择(1-5):')
if choice == '1':
add_employee()
elif choice == '2':
delete_employee()
elif choice == '3':
statistics()
elif choice == '4':
search_employee()
elif choice == '5':
print('谢谢使用,再见!')
break
else:
print('输入有误,请重新输入!')
# 运行主函数
if __name__ == '__main__':
main()
```