管理部分:
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 '''管理员功能区域''' 5 6 import os 7 def acc_info(): 8 """ 9 :return: 返回account_info.txt中以字典形式{ 10 'msj': '123', 'scg': '123', 'lhy': '123', 'egon': '1234', 'flower': '123', 'fgh': '123', 'hua': '123'} 11 """ 12 account_info = {} 13 with open(r'account_info.txt',mode='r',encoding='utf-8') as f: 14 for line in f: 15 line = line.strip(' ') 16 if line != '': 17 u,v = line.split('|') 18 account_info[u] = v 19 return account_info 20 21 def bk_info(): 22 ''' 23 钱和信用额度都是浮点数类型 24 :return: {'msj': {'pwd': '123', 'money': 4600.0, 'credit': 15000.0}} 25 ''' 26 bank_user ={} 27 with open(r'bank_info.txt',mode='r',encoding='utf-8') as f: 28 for line in f: 29 line = line.strip(' ') 30 if line != '': 31 u,p,m,c = line.split('|') 32 bank_user[u]={'pwd':p,'money':float(m),'credit':float(c)} 33 return bank_user 34 35 def select_type(): 36 while True: 37 choice = input('请输入你想要输入的类型 1.shop 2.bank>>:').strip() 38 if choice == '1': 39 return 'shop' 40 elif choice == '2': 41 return 'bank' 42 else: 43 print('非法操作') 44 continue 45 46 def is_name(): 47 type = select_type() 48 if type == 'shop': 49 user_info = acc_info() 50 else: 51 user_info = bk_info() 52 name = input('please input your username(q to exit)>>:').strip() 53 if name in user_info: 54 return name,type 55 else: 56 return False,name,type 57 58 def input_pwd(): 59 pwd = input('请输入密码>>:').strip() 60 return pwd 61 62 63 def check_userinfo(): 64 type = select_type() 65 if type == 'shop': 66 user_info =acc_info() 67 elif type =='bank': 68 user_info = bk_info() 69 for k,v in user_info.items(): 70 print('用户名%s'%k,'信息:%s'%v) 71 72 73 74 def add_user(): 75 """ 76 添加账户 77 :param name: 78 :param pwd: 79 :param type: 账户种类 两种 account 和 bank 80 :return: 81 """ 82 info = is_name() 83 if False in info: 84 name = info[1] 85 type = info[2] 86 else: 87 print('用户名已存在') 88 return 89 pwd = input_pwd() 90 if type =='shop': 91 file = 'account_info.txt' 92 user_info ='%s|%s '%(name,pwd) 93 elif type =='bank': 94 file = 'bank_info.txt' 95 user_info ='%s|%s|0|15000 '%(name,pwd) 96 else: 97 print('非法操作2') 98 return 99 with open(r'%s'%file,mode='a',encoding='utf-8') as f: 100 f.write(user_info) 101 102 def alter_credit(): 103 ''' 104 改变账号中的信用额 105 :param name: 用户名 106 :param money: int 钱 107 :return: 108 ''' 109 name =input('请输入用户名>>:').strip() 110 user_info = bk_info() 111 if name not in user_info: 112 print('用户名不存在') 113 return 114 money = input('请输入金额>>:').strip() 115 if not money.isdigit(): 116 print('输入的金额不对') 117 return 118 money = float(money) 119 user_info[name]['credit']=user_info[name]['credit']+money 120 # print(user_info[name]['money']) 121 with open(r'bank_info.txt',mode='w',encoding='utf-8') as f: 122 for k,v in user_info.items(): 123 msg = '{}|{}|{}|{} '.format(k,v['pwd'],v['money'],v['credit']) 124 f.write(msg) 125 126 def r_hmd(type): 127 hmd =[] 128 if type =='shop': 129 file = 'shop_hmd.txt' 130 elif type == 'bank': 131 file ='bank_hmd.txt' 132 with open(r'%s'%file,mode='r',encoding='utf-8') as f: 133 for i in f: 134 i=i.strip(' ') 135 if i != "": 136 hmd.append(i) 137 return hmd 138 139 140 def add_hmd(): 141 type =select_type() 142 name = input('输入加入黑名单的用户名>>:').strip() 143 if type =='shop': 144 file = 'shop_hmd.txt' 145 user_inf = acc_info() 146 elif type == 'bank': 147 file ='bank_hmd.txt' 148 user_inf = bk_info() 149 if name not in user_inf: 150 print('用户不存在') 151 return 152 hmd = r_hmd(type) 153 if name in hmd: 154 print('用户名已存在黑名单') 155 return 156 with open(r'%s'%file,mode='a',encoding='utf-8') as f: 157 f.write('%s '%name) 158 print('%s 已加入黑名单') 159 160 161 162 def read_hmd(): 163 type = select_type() 164 res =r_hmd(type) 165 print(res) 166 167 def del_hmd(): 168 type = select_type() 169 name = input('想要删除的黑名单>>:').strip() 170 hmd = r_hmd(type) 171 if name not in hmd: 172 print('用户不在黑名单') 173 return 174 if type =='shop': 175 file = 'shop_hmd.txt' 176 elif type == 'bank': 177 file ='bank_hmd.txt' 178 with open(r'%s'%file, mode='r', encoding='utf-8') as rf, 179 open(r'%s.swap'%file, mode='w', encoding='utf-8') as wf: 180 for line in rf: 181 if name == line.strip(' '): 182 line = '' 183 wf.write(line) 184 os.remove('%s'%file) 185 os.rename('%s.swap'%file,'%s'%file) 186 187 188 189 msg = """ 190 1、增加用户 191 2、增加额度 192 3、查看用户信息 193 4、增加黑名单 194 5、查看黑名单 195 6、删除黑名单 196 0、退出 197 """ 198 dic ={ 199 '1':add_user, 200 '2':alter_credit, 201 '3':check_userinfo, 202 '4':add_hmd, 203 '5':read_hmd, 204 '6':del_hmd, 205 } 206 207 def admin(): 208 while True: 209 print(msg) 210 choice = input('请输入你的操作>>:') 211 if choice == '0': 212 print('exit') 213 return 214 elif choice in dic: 215 dic[choice]() 216 else: 217 print('非法操作') 218 continue 219 220 admin()