第三周作业-工资管理系统(练习文件操作)
需求:
工资管理系统
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
再见!
程序实现
1.逻辑图
2.主程序
博客地址:http://www.cnblogs.com/Mr-hu/ 程序运行步骤: 第1步:根据提示选择你要做的操作:1.查询2.增加3.删除4.退出 1.查询:输入huwei回车,显示工资记录 2.增加:输入 姓名+空格+工资 回车,增加记录 3.修改:输入 姓名+工资 ,如果不存在,则提示用户不存在,重新回到菜单选择 4.退出
#-*- coding:utf-8 -*- import sys,os def search(name_item): with open("info.txt", "r+") as s: name_list = s.readlines() for name in name_list: new_name = name.strip("\n").split(" ") sname,salary = new_name if name_item == sname: print("%s的工资是:" % name_item, salary) break else: print("用户不存在!") def modify(m_info_list): m_item = "%s %s" % (m_info_list[0], m_info_list[1]) flag = True writen = False with open("info.txt","r") as m,open("new_info.txt","w") as w: name = m.read() name_list = name.strip().split() if m_info_list[0] not in name_list: sys.exit("用户不存在!") with open("info.txt", "r") as m, open("new_info.txt", "w") as w: for line in m: if flag and line.startswith(m_info_list[0]): continue w.write(line) if flag: if not writen: w.write(m_item+"\n") writen = True print("修改成功!") os.rename("info.txt", "info.bak") os.rename("new_info.txt", "info.txt") def update(u_info_list): u_item = "%s %s" % (u_info_list[0], u_info_list[1]) flag = True writen = False with open("info.txt", "r") as m, open("new_info.txt", "w") as w: name = m.read() name_list = name.strip().split() if u_info_list[0] in name_list: sys.exit('用户已存在!') with open("info.txt", "r") as m, open("new_info.txt", "w") as w: for line in m: w.write(line) if flag: if not writen: w.write(u_item+"\n") writen = True print("增加成功!") os.rename("info.txt", "info.bak") os.rename("new_info.txt", "info.txt") while True: choice_list = ["查询员工工资", "修改员工工资", "增加员工记录", "退出"] count = 0 for choice in choice_list: count += 1 print(str(count) + '.' + choice) user_choice = input(">>>:") if user_choice.isdecimal(): if int(user_choice) == 1: search_name = input("请输入要查询的员工姓名:") search(search_name) elif int(user_choice) == 2: modify_info = input("请输入要修改的员工姓名和工资,用空格分隔(例如:Alex 10):") y = modify_info.split(" ") modify(y) elif int(user_choice) == 3: update_staff = input("请输入要增加的员工姓名和工资,用空格分隔(例如:Eric 100000):") z = update_staff.split(" ") update(z) elif int(user_choice) == 4: print("再见!") break else: print("无效的字符")
alex 30000
sunny 19000
5.程序运行