工资管理系统

需求

工资管理系统

Alex 100000

Rain 80000

Egon 50000

Yuan 30000

-----以上是info.txt文件-----

实现效果:

从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件。

效果演示:

1. 查询员工工资

2. 修改员工工资

3. 增加新员工记录

4. 退出

>>:1

请输入要查询的员工姓名(例如:Alex):Alex

Alex的工资是:100000。

1. 查询员工工资

2. 修改员工工资

3. 增加新员工记录

4. 退出

>>:2

请输入要修改的员工姓名和工资,用空格分隔(例如:Alex 10):Alex 10

修改成功!

1. 查询员工工资

2. 修改员工工资

3. 增加新员工记录

4. 退出

>>:3

请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):Eric 100000

增加成功!

1. 查询员工工资

2. 修改员工工资

3. 增加新员工记录

4. 退出

>>:4

再见!

代码20180424

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 import os
 4 
 5 
 6 option = '''
 7 --------- 请选择功能 ---------
 8 1. 查询员工工资
 9 2. 修改员工工资
10 3. 增加新员工记录
11 4. 退出
12 ---------- The End ----------
13 '''
14 
15 
16 def search(argument):
17         flag = False
18         with open('info.txt', 'r', encoding='utf-8') as f:
19             for line in f:
20                 if line.split()[0] == argument:
21                     flag = True
22                     print('33[31;1m%s33[0m 的工资是 33[31;1m%s33[0m' % (argument, line.split()[1]))
23                 else:
24                     continue
25 
26         if not flag:
27             print('未找到员工33[31;1m%s33[0m信息' % argument)
28 
29 
30 def revise(salary):
31     temp = salary.split(' ')
32     flag = False
33     with open('info.txt', 'r', encoding='utf-8') as read, open('info', 'w', encoding='utf-8') as write:
34         for line in read:
35             if line.split()[0] == temp[0]:
36                 write.write(salary + '
')
37                 flag = True
38             else:
39                 write.write(line)
40 
41     os.rename('info.txt', 'info_bak.txt')
42     os.remove('info_bak.txt')
43     os.rename('info', 'info.txt')
44 
45     if flag:
46         print('员工33[31;1m%s33[0m 的工资已修改为 33[31;1m%s33[0m' % (temp[0], temp[1]))
47     else:
48         print('未找到员工33[31;1m%s33[0m信息' % temp[0])
49 
50 
51 def add(employment):
52     temp1 = employment.split(' ')
53     with open('info.txt', 'a+', encoding='utf-8') as f:
54         for line in f:
55             if line.split(' ')[0] == temp1[0]:
56                 print('用户33[31;1m%s33[0m已存在' % temp1[0])
57         else:
58             f.write('
' + employment)
59             print('员工信息33[31;1m%s33[0m已添加.' % employment)
60 
61 
62 def cvt2dict(cvt):
63     temp2 = cvt.split(' ')
64     result = {temp2[0]: temp2[1]}
65     return result
66 
67 
68 stop_flag = False
69 with open('info.txt', 'r+', encoding='utf-8') as info:
70     data = cvt2dict(info.readline())
71     print(option)
72 
73 choice = input('您要选择?')
74 while not stop_flag:
75     if not choice.isdigit():
76         choice = input('请输入数字选项编号: ')
77     elif int(choice) > 4 or int(choice) < 1:
78         choice = input('请输入有效的数字选项编号: ')
79     elif int(choice) == 1:
80         name = input('请输入要查询员工姓名: ')
81         search(name)
82         stop_flag = True
83     elif int(choice) == 2:
84         source = input('请输入要修改员工姓名和工资, 用空格分隔(例如:Alex 10): ')
85         revise(source)
86         stop_flag = True
87         continue
88     elif int(choice) == 3:
89         source = input('请输入要添加的员工姓名和工资, 用空格分隔(例如:Alex 10): ')
90         add(source)
91         stop_flag = True
92     else:
93         stop_flag = True
94         print('再见')
View Code

info.txt:

Alex 20
Rain 80000
Egon 50000
Yuan 30000
Lucy 30
原文地址:https://www.cnblogs.com/cheese320/p/8930229.html