day2 作业
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:kiko0o0 4 5 import os 6 import sys 7 #import getpass 8 9 LOCAL_DIR = os.path.dirname(os.path.abspath(__file__)) 10 USER_DB = "%s/%s" % (LOCAL_DIR,"Userinfo.db") 11 PRODUCT_DB = "%s/%s" % (LOCAL_DIR,"Product.db") 12 13 # 初始化用户[当用户文件不存在时生效] 14 if os.path.exists(USER_DB): 15 pass 16 else: 17 Userinfo = "admin 123456 0 null 0\nqianwei qian123 0 null 0\nwanli wan123 0 null 0\ntotal 0 0 0 0" 18 with open(USER_DB,"w") as w_file: 19 w_file.write(Userinfo) 20 21 # 初始化产品信息[当产品文件不存在时生效] 22 if os.path.exists(PRODUCT_DB): 23 pass 24 else: 25 Product = "Iphone 5800\nMacbook 12000\nBike 800\nWatch 3500\nCoffee 32\nBook 80\n" 26 with open(PRODUCT_DB,"w") as w_file1: 27 w_file1.write(Product) 28 29 # 将用户信息生成一个字典 30 user_info = dict() 31 with open(USER_DB,"r") as read_file: 32 for line in read_file: 33 34 if len(line) !=0: 35 username, password, times, product, balance = line.split() 36 if username == "total": 37 user_info[username] = {"times":int(times)} 38 else: 39 user_info[username] = {"password":password,"times":int(times),"product":product,"balance":int(balance)} 40 41 # 将产品信息生成一个字典 42 product_info = dict() 43 with open(PRODUCT_DB,"r") as read_file1: 44 for line1 in read_file1: 45 if len(line1) !=0: 46 product_name,price = line1.split() 47 product_info[product_name] = {"product_name":product_name,"price":int(price)} 48 49 def write_Productinfo(): 50 global Productinfo 51 Productinfo = "" 52 for product_name in product_info.keys(): 53 productinfo = "%s %s\n" % (product_name,product_info[product_name]["price"]) 54 Productinfo = "%s%s" % (Productinfo,productinfo) 55 with open(PRODUCT_DB,'w') as w_file1: 56 w_file1.write(Productinfo) 57 58 def write_userinfo(): 59 global Userinfo 60 Userinfo = "" 61 #字符串拼接用户信息 62 for info in user_info.keys(): 63 if info == "total": 64 userinfo = "%s %s %s %s %s\n" % (info,0,user_info["total"]["times"],0,0) 65 else: 66 userinfo = "%s %s %s %s %s\n" % (info,user_info[info]["password"],user_info[info]["times"],user_info[info]["product"],user_info[info]["balance"]) 67 Userinfo = "%s%s" % (Userinfo,userinfo) 68 with open(USER_DB,"w") as w_file: 69 w_file.write(Userinfo) 70 71 def temp_menu_list(): 72 global temp_menu 73 temp_menu = "" 74 temp_menu_list_id = [] 75 temp_menu_list_name = [] 76 temp_menu_list_price = [] 77 for item in enumerate(sorted(product_info)): 78 l_id = item[0] 79 l_name = item[1] 80 l_price = product_info[item[1]]["price"] 81 temp_menu_list_id.append(l_id) 82 temp_menu_list_name.append(l_name) 83 temp_menu_list_price.append(l_price) 84 print(l_id, l_name, l_price) 85 temp_menu = list(zip(temp_menu_list_id, temp_menu_list_name, temp_menu_list_price)) 86 87 def rechange(): 88 print("您的账户余额为:\033[1;32m %s \033[0m" % user_info[username]["balance"]) 89 yn = input("请确认本次是否充值,y/n\n>>>:").strip() 90 flag_salay = False 91 while not flag_salay: 92 if yn == "y" or yn == "Y": 93 while not flag_salay: 94 salay = input("请输入本次充值金额:\n>>>:").strip() 95 if salay.isdigit(): 96 salay = int(salay) 97 user_info[username]["balance"] += salay 98 write_userinfo() 99 print("充值成功!您现在的账户余额为:\033[1;32m %s \033[0m" % user_info[username]["balance"]) 100 flag_salay = True 101 break 102 else: 103 print("请输入整数!!!") 104 elif yn == "n" or yn == "N": 105 break 106 else: 107 print("invalid option") 108 109 print('-'*50) 110 111 menu = ['购物中心','管理后台'] 112 menu_up_list = ['修改商品','添加商品','删除商品'] 113 flag_menu = False 114 flag_menu_list = False 115 flag_salay = False 116 flag_added = False 117 flag_add_p =False 118 flag_up = False 119 flag_del = False 120 global b 121 # shopping_list = [] 122 temp_menu_list_id = [] 123 temp_menu_list_name = [] 124 temp_menu_list_price =[] 125 # TEMP_MENU =[] 126 127 128 129 while not flag_menu: 130 for item,m in enumerate(menu): 131 item +=1 132 print("\t",item,m) 133 user_choice = input("请选择菜单\n>>>:").strip() 134 if user_choice == "1": 135 print("欢迎进入购物中心".center(50,'-')) 136 if user_info["total"]["times"] < 3: 137 username = input("input your username\n:>>>").strip() 138 password = input("input your password\n:>>>").strip() 139 if username in user_info: 140 if user_info[username]["times"] == 3: 141 exit("account is lock, Contact the administrator") 142 elif password == user_info[username]["password"]: 143 print("Good morning %s" % username) 144 user_info[username]["times"] = 0 145 user_info["total"]["times"] = 0 146 write_userinfo() 147 rechange() 148 while not flag_added: 149 print("新上架商品".center(50, '-')) 150 temp_menu_list() 151 if user_info[username]["product"] == 'null': 152 print("目前没有购买物品") 153 else: 154 print("已经购买了 \033[1;32m %s \033[0m" % (user_info[username]["product"])) 155 user_choice_num = input('选择要买的物品ID:\n>>>:').strip() 156 if user_choice_num.isdigit(): 157 user_choice_num = int(user_choice_num) 158 if user_choice_num < len(product_info) and user_choice_num >= 0: 159 p_item = temp_menu[user_choice_num] 160 if int(p_item[2]) < user_info[username]["balance"]: 161 shopping_list = user_info[username]["product"] 162 shopping_list = shopping_list.strip(',').split(',') 163 shopping_list.append(p_item[1]) 164 if shopping_list[0] == 'null': 165 shopping_list.remove('null') 166 shopping_list = ','.join(shopping_list) 167 user_info[username]["product"] = shopping_list 168 user_info[username]["balance"] -=p_item[2] 169 write_userinfo() 170 print("已经购买了\033[1;32m %s \033[0m" % (user_info[username]["product"])) 171 print("Added \033[1;32m %s \033[0m into shopping cart, your current blance is \033[1;32m %s \033[0m" % (p_item[1],user_info[username]["balance"])) 172 173 else: 174 print("余额不足!") 175 rechange() 176 177 else: 178 print("没有该商品,请重新选择!") 179 180 elif user_choice_num == 'q': 181 if user_info[username]["product"] == 'null': 182 print("目前没有购买物品") 183 else: 184 print("已经购买了 \033[1;32m %s \033[0m ,金额剩余 \033[1;32m %s \033[0m " %(user_info[username]["product"],user_info[username]["balance"])) 185 break 186 else: 187 print("invalid option") 188 else: 189 input("Enter to wrong account password. (Any key to continue)") 190 user_info[username]["times"] += 1 191 user_info["total"]["times"] += 1 192 print(user_info['total']['times']) 193 write_userinfo() 194 195 else: 196 input("Enter to wrong account password. (Any key to continue)") 197 user_info["total"]["times"] += 1 198 write_userinfo() 199 200 201 else: 202 user_info["total"]["times"] = 0 203 write_userinfo() 204 input("Too many retries, please try again later. (Any key to continue)") 205 exit("88") 206 elif user_choice == "2": 207 print("欢迎进入管理后台!") 208 if user_info["total"]["times"] < 3: 209 username = input("input your username\n:>>>").strip() 210 password = input("input your password\n:>>>").strip() 211 if username in user_info: 212 if user_info[username]["times"] == 3: 213 exit("account is lock, Contact the administrator") 214 elif password == user_info[username]["password"]: 215 print("Good morning %s" % username) 216 user_info[username]["times"] = 0 217 user_info["total"]["times"] = 0 218 write_userinfo() 219 flag_menu_list = False 220 while not flag_menu_list: 221 for index,item in enumerate(menu_up_list): 222 print('\t',index,item) 223 menu_up = input('请选择操作项:\n>>>:').strip() 224 if menu_up == '0': 225 flag_added = False 226 while not flag_added: 227 print("当前库存商品如下:") 228 temp_menu_list() 229 choice_up = input("要修改的商品序号:\n>>>:").strip() 230 if choice_up.isdigit(): 231 choice_up = int(choice_up) 232 if choice_up < len(product_info) and choice_up >= 0: 233 up_item = temp_menu[choice_up] 234 print(up_item) 235 flag_up = False 236 while not flag_up: 237 choice_up1 = input("请输入修改为多少钱:\n>>>:") 238 if choice_up1.isdigit(): 239 choice_up1 = int(choice_up1) 240 choice_up1_key = up_item[1] 241 product_info[choice_up1_key]["price"] = choice_up1 242 write_Productinfo() 243 flag_up = True 244 flag_added = True 245 temp_menu_list() 246 else: 247 print("请重新输入") 248 else: 249 print("没有该商品,请重新选择!") 250 else: 251 print("请输入编号!") 252 253 elif menu_up == '1': 254 print("当前库存商品如下:") 255 temp_menu_list() 256 flag_add_p = False 257 while not flag_add_p: 258 choice_add = input("请输入要添加的商品名称\n>>>:").strip() 259 choice_add1 = input("请输入要添加商品的价格\n>>>:").strip() 260 if choice_add1.isdigit(): 261 choice_add1 = int(choice_add1) 262 product_info[choice_add] = {"product_name": choice_add, "price": int(choice_add1)} 263 write_Productinfo() 264 flag_add_p = True 265 temp_menu_list() 266 else: 267 print("请重新输入价格") 268 elif menu_up == '2': 269 flag_del = False 270 while not flag_del: 271 print("当前库存商品如下:") 272 temp_menu_list() 273 choice_del = input("请选择要删除商品的序列号\n>>>:").strip() 274 if choice_del.isdigit(): 275 choice_del = int(choice_del) 276 if choice_del < len(product_info) and choice_del >=0: 277 del_item = temp_menu[choice_del] 278 del product_info[del_item[1]] 279 write_Productinfo() 280 flag_del = True 281 temp_menu_list() 282 else: 283 print("没有该商品,请重新选择!") 284 elif menu_up == 'q': 285 break 286 287 else: 288 print('请重新输入') 289 else: 290 input("Enter to wrong account password. (Any key to continue)") 291 user_info[username]["times"] += 1 292 user_info["total"]["times"] += 1 293 print(user_info['total']['times']) 294 write_userinfo() 295 296 else: 297 input("Enter to wrong account password. (Any key to continue)") 298 user_info["total"]["times"] += 1 299 write_userinfo() 300 elif user_choice == 'q': 301 break 302 303 else: 304 print("参数不存在!!!")