- 功能要求:
- 登录系统用户认证通过后才能列出下一级菜单员工信息表
- 登录系统要有用户登录、注册账号、删除账号、修改密码、退出
- 登录密码要有加密功能
- 从info.txt文件读取员工及工资信息,最后通过增加,修改员工信息写入原info.txt文件
- 开发环境:
- python 3.6.1
- PyCharm 2016.2.3
- 目录结构:
1 project 2 |--day2 3 |--login.py 4 |--PayrollStstem.py 5 |--log.txt 6 |--info.txt
- 程序设计:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python 2 #coding=utf-8 3 __author__ = 'yinjia' 4 5 import hashlib 6 import sys 7 from project.day2 import PayrollSystem 8 9 #存储文件 10 filename = 'log.txt' 11 #账号信息表 12 result = {} 13 14 def md5(arg): 15 """ 16 密码进行md5加密 17 :param arg: 用户的密码 18 :return: 返回进行加密后的密码 19 """ 20 result = hashlib.md5(bytes("k1:1a2 k2:688",encoding='utf-8')) 21 result.update(bytes(arg,encoding='utf-8')) 22 return result.hexdigest() 23 24 25 def login(username,password): 26 """ 27 用于验证用户密码 28 :param username:用户名 29 :param password:密码 30 :return: True,用户名验证成功;False,用户验证失败 31 """ 32 33 with open(filename,"r") as f: 34 for line in f: 35 u,p = line.strip().split(":") 36 if username == u and md5(password) == p: 37 return True 38 return False 39 40 def register(username,password): 41 """ 42 用户注册 43 :param username: 用户名 44 :param password: 密码 45 :return: True,用户注册成功; 46 """ 47 48 with open(filename,"a") as f: 49 temp = "\n" + username + ":" + md5(password) 50 f.write(temp) 51 return True 52 53 def user_exist(username): 54 """ 55 检查用户名是否存在 56 :param username: 用户名 57 :return: True,用户名存在; False,用户名不存在 58 """ 59 with open(filename,"r") as f: 60 for line in f: 61 line = line.strip() 62 line_list = line.split(":") 63 if username == line_list[0]: 64 return True 65 return False 66 67 def delete(username): 68 """ 69 删除用户名 70 :param username: 用户名 71 :return: True,删除成功; False,删除失败 72 """ 73 info() 74 del result[username] 75 with open(filename,'w') as f: 76 for key,value in result.items(): 77 f.write("%s%s%s\n" % (key,":",value)) 78 return True 79 80 def edit(username,old_password,new_password): 81 """ 82 修改密码 83 :param username:用户名 84 :param old_password:旧密码 85 :param new_password:新密码 86 :return:True,修改成功;False,修改失败 87 """ 88 info() 89 ret = result[username] 90 if ret == md5(old_password): 91 result[username] = md5(new_password) 92 with open(filename, 'w') as f: 93 for key, value in result.items(): 94 f.write("%s%s%s\n" % (key, ":", value)) 95 return True 96 97 def info(): 98 """ 99 提取账号信息 100 :return: 返回账号信息表 101 """ 102 with open(filename,'r') as f: 103 for line in f: 104 line = line.strip() 105 result[line.split(":")[0]] = line.split(":")[1] 106 return result 107 108 def main(): 109 info_msg = """ 110 1、登录系统 111 2、注册账号 112 3、删除账号 113 4、修改密码 114 """ 115 116 print('\033[32;1m') 117 print('*' * 50) 118 print('\t\t\t\t欢迎登录工资管理系统') 119 print('*' * 50) 120 print('\033[0m') 121 print(info_msg) 122 pingtai = str(input("\033[1;34;1m请选择操作(退出请输入q|Q):\033[0m")).strip() 123 if pingtai == "1": 124 user = input("请输入用户名:") 125 pwd = input("请输入密码:") 126 ret = login(user,pwd) 127 if ret: 128 print("\033[1;31;1m<<<<<<<<登录成功>>>>>>>>>\033[0m") 129 info = PayrollSystem.salary() 130 info.comm() 131 else: 132 print("\033[1;31;1m<<<<<<<<登录失败>>>>>>>>>\033[0m") 133 elif pingtai == "2": 134 user = input("请输入用户名:") 135 pwd = input("请输入密码:") 136 ret = user_exist(user) 137 if ret: 138 print("\033[1;31;1m用户名已存在,不能注册!\033[0m") 139 else: 140 add_user = register(user,pwd) 141 if add_user: 142 print("注册成功") 143 else: 144 print("注册失败") 145 elif pingtai == "3": 146 user = input("请输入用户名:") 147 del_user1 = user_exist(user) 148 if del_user1: 149 del_user2 = delete(user) 150 if del_user2: 151 print("删除成功!") 152 else: 153 print("删除失败!") 154 else: 155 print("\033[1;31;1m用户名不存在,无法删除!\033[0m") 156 157 elif pingtai == "4": 158 user = input("请输入用户名:") 159 pas1 = input('请输入旧密码:') 160 pas2 = input('请输入新密码:') 161 del_user = user_exist(user) 162 if del_user: 163 edit_user = edit(user,pas1,pas2) 164 if edit_user: 165 print("修改密码成功!") 166 else: 167 print("\033[1;31;1m旧密码错误,修改密码失败!\033[0m") 168 else: 169 print("\033[1;31;1m用户名不存在,无法修改!\033[0m") 170 elif str(pingtai).lower() == "q": 171 print("GoodBye!") 172 sys.exit() 173 else: 174 print("\033[1;31;1m非法输入!\033[0m") 175 176 if __name__ == "__main__": 177 main()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 __author__ = 'Yin Jia' 4 5 import sys 6 7 # 员工存储文件 8 filename = "info.txt" 9 #提取姓名信息表 10 info_dict = {} 11 12 class salary: 13 14 def Query(self): 15 """ 16 查询员工 17 :return:输出员工的工资 18 """ 19 while True: 20 self.info() 21 user = input("请输入要查询的员工姓名(例如:Jon)").strip() 22 if user in info_dict.keys(): 23 print('%s的工资:%s' % (user,info_dict[user])) 24 break 25 else: 26 print('用户不存在!') 27 28 def Insert(self): 29 while True: 30 self.info() 31 user = input("请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):").strip() 32 # 转换成列表 33 u, p = user.split(" ") 34 if u not in info_dict.keys(): 35 info_dict[u] = p 36 with open(filename, 'w') as f: 37 # 循环读取字典信息并写入文件 38 for key, value in info_dict.items(): 39 f.write("%s%s%s\n\n" % (key, " ", value)) 40 print('%s 新增成功!' % u) 41 break 42 else: 43 print('用户已存在,不能增加!') 44 45 46 def Update(self): 47 while True: 48 self.info() 49 user = input("请输入要修改的员工姓名和工资,用空格分隔(例如:Jon 100):").strip() 50 #转换成列表 51 u,p = user.split(" ") 52 if u in info_dict.keys(): 53 info_dict[u] = p 54 with open(filename,'w') as f: 55 # 循环读取字典信息并写入文件 56 for key,value in info_dict.items(): 57 f.write("%s%s%s\n\n" % (key," ",value)) 58 print('%s 修改成功!' % u) 59 break 60 else: 61 print('用户不存在!') 62 63 def Delete(self): 64 while True: 65 self.info() 66 user = input("请输入要删除的员工姓名(例如:Jon):").strip() 67 if user in info_dict.keys(): 68 del info_dict[user] 69 with open(filename, 'w') as f: 70 # 循环读取字典信息并写入文件 71 for key, value in info_dict.items(): 72 f.write("%s%s%s\n\n" % (key, " ", value)) 73 print('%s 删除成功!' % user) 74 break 75 else: 76 print('用户不存在!') 77 78 def info(self): 79 """ 80 读取员工信息表 81 :return: 返回员工信息表 82 """ 83 with open(filename,'r',encoding='utf-8') as f: 84 for i in f: 85 if not i.startswith("\n"): 86 u,p = i.strip().split(" ") 87 info_dict[u] = p 88 return info_dict 89 90 def Exit(self): 91 print('GoodBye!') 92 sys.exit() 93 def comm(self): 94 info_msg = """ 95 1. 查询员工工资 96 2. 修改员工工资 97 3. 增加新员工记录 98 4、删除员工记录 99 5、退出 100 """ 101 payroll = salary() 102 103 info_payroll = { 104 '1': payroll.Query, 105 '2': payroll.Update, 106 '3': payroll.Insert, 107 '4': payroll.Delete, 108 '5': payroll.Exit 109 } 110 111 while True: 112 print('\033[32;1m') 113 print('*' * 50) 114 print('\t\t\t\t员工工资信息表') 115 print('*' * 50) 116 print('\033[0m') 117 print(info_msg) 118 choose = input('\033[1;31;1m请输入操作选项>>>:\033[0m').strip() 119 if choose not in info_payroll.keys(): 120 print("不合法,请重新录入!") 121 continue 122 info_payroll[choose]()
- 效果演示
部分效果截图展示: