-
#_*_ coding:utf-8 _*_
-
import os, sys, time
-
#-----------------------------------登录验证-------------------------------------------
-
if os.path.isfile('user.txt'):
-
pass
-
else:
-
sys.exit("\033[31m没有定义用户文件\033[0m")
-
user_li = []
-
with open('user.txt') as f1: #提取用户文件,列表化
-
for i in f1:
-
user = i.strip('\n').split(':')
-
user_li.append(user)
-
-
def user_name(): #用户名列表
-
user_n = []
-
for name in user_li:
-
user_n.append(name[0])
-
return user_n
-
-
def user_passwd(): #密码列表
-
user_pass = []
-
for passwd in user_li:
-
user_pass.append(passwd[1])
-
return user_pass
-
-
def login_code(): #定义登录验证函数
-
while True:
-
user_na = input('请输入用户名:')
-
username = user_na.strip()
-
password = input('请输入密码:')
-
if username in user_name():
-
location = user_name().index(username)
-
while int(user_li[location][2]) < 3:
-
if password == user_li[location][1]:
-
return username
-
else:
-
location = user_name().index(username)
-
user_li.insert(location,[username,user_li[location][1],\
-
str(int(user_li[location][2])+1),user_li[location][3]])
-
user_li.pop(location+1)
-
with open('user.txt','w+') as f1:
-
for i in user_li:
-
s1 = ':'.join(i)
-
f1.write(s1+'\n')
-
print ("\033[31m账户名或密码错误\033[0m")
-
break
-
else:
-
print ("\033[31m账户已经锁定,请联系管理员!\033[0m")
-
sys.exit(0)
-
else:
-
print ("\033[31m账户名或密码错误\033[0m")
-
#--------------------------------------------------------------------------------------------------------
-
def shop_list(): #打印出商品的编码,商品名称,价格
-
for m, n in enumerate(trade_dict, 1):
-
print('\t%s\t%s\t%s'%(m, n, trade_dict[n]))
-
-
def trade_price(trade_code): #通过输入的编码来找到商品的价格
-
for x,y in enumerate(trade_dict, 1):
-
if x == trade_code:
-
return trade_dict[y]
-
-
def trade_name(trade_code): #通过输入编码来找到商品的名称
-
for x, y in enumerate(trade_dict, 1):
-
if x == trade_code:
-
return y
-
-
def trade_list_num(): #统计字典中的编码,列表化,为后面判断商品编码准备
-
trade_n = []
-
for m, n in enumerate(trade_dict, 1):
-
trade_n.append(m)
-
return trade_n
-
-
def result_print(): #格式化打印购买商品的信息
-
li1 = []
-
for i in trade_bill:
-
li2 = []
-
for m in i:
-
li2.append(str(m))
-
li1.append(li2)
-
for x in li1:
-
s1 = ' '.join(x)
-
print(s1+'\n')
-
#----------------------------------------------------------------------------------------------------------
-
user1_name = login_code() #获取当前登录状态下的用户名
-
-
def continue_yn(): #定义是否返回上一级菜单函数
-
while True:
-
res_code1 = input("\033[32m'b'(返回上一级),'q'(退出):\033[0m")
-
if res_code1 == 'b':
-
break
-
elif res_code1 == 'q':
-
sys.exit('ByeBye')
-
else:
-
print('\033[31m错误的输入,请重新输入:\033[0m')
-
-
choice_li = ['查看账户余额','充值','查看历史购物信息','开始购物']
-
while True:
-
for x, y in enumerate(choice_li, 1):
-
print(x, y)
-
choice = input('\033[32m------>请输入你的选择(编码):\033[0m')
-
if choice == '1':
-
for x in user_li:
-
if x[0] == user1_name:
-
print(x[3])
-
continue_yn()
-
-
elif choice == '2':
-
charge_num = input('请输入充值金额:')
-
for x in user_li:
-
if x[0] == user1_name:
-
print('余额:',x[3])
-
location1 = user_li.index(x)
-
user_li.insert(location1, [x[0], x[1], x[2], str(int(x[3]) + int(charge_num))])
-
user_li.pop(location1 + 1)
-
print('充值后金额:',user_li[location1][3])
-
with open('user.txt', 'w+') as f1:
-
for i in user_li:
-
s1 = ':'.join(i)
-
f1.write(s1 + '\n')
-
continue_yn()
-
-
elif choice == '3':
-
if os.path.isfile(user1_name+'_shopping_history'):
-
with open(user1_name+'_shopping_history') as f1:
-
for i in f1:
-
print(i)
-
else:
-
print('没有购物的历史记录')
-
continue_yn()
-
-
elif choice == '4':
-
print('欢迎来到super购物中心'.center(50, '*'))
-
break
-
else:
-
print('\033[31m错误的输入\033[0m')
-
-
if os.path.isfile('menu_info.txt'):
-
pass
-
else:
-
sys.exit("\033[31m没有定义menu_info.txt\033[0m")
-
-
usr_dic = {}
-
with open('menu_info.txt') as f1: #从商品文件menu_info账户中取出商品信息,字典化
-
for i in f1:
-
area_dic = {}
-
l1 = i.split(':')
-
l2 = l1[1].strip('\n').split(' ')
-
d1 = {}
-
for i in l2:
-
l3 = i.split('|')
-
l4 = l3[1].split(';')
-
d2 = {}
-
for m in l4:
-
d3 = {}
-
l5 = m.split('*')
-
l6 = l5[1].split(',')
-
for x in l6:
-
l7 = x.split('.')
-
d3[l7[0]] = l7[1]
-
d2[l5[0]] = d3
-
d1[l3[0]] = d2
-
usr_dic[l1[0]] = d1
-
-
def lay_mem(dic_name):#打印分级菜单,生成编码列表,为下面判断编码准备
-
li_name = []
-
for x, y in enumerate(dic_name, 1):
-
print(x,y)
-
li_name.append(x)
-
a = 3
-
return li_name
-
-
def input_code():
-
code_name = input("\033[32m请输入->'商品编码','b','q'(b:返回上一级 q:退出):\033[0m")
-
return code_name
-
-
for x in user_li: #取出salary值,即用户账户余额
-
if x[0] == user1_name:
-
salary = int(x[3])
-
sum_comsume = 0 #消费总额初始化
-
trade_bill = []
-
while True:
-
result1 = lay_mem(usr_dic) #打印商品一级菜单
-
city_code = input("\033[32m请输入->'商品编码','q'(q:退出):\033[0m")
-
if city_code in str(result1):
-
for m, n in enumerate(usr_dic, 1):
-
if m == int(city_code):
-
while True:
-
result2 = lay_mem(usr_dic[n])
-
res_code = input_code()
-
if res_code in str(result2):#打印商品二级菜单
-
for x,y in enumerate(usr_dic[n],1):
-
if res_code == str(x):
-
while True:
-
result3 = lay_mem(usr_dic[n][y])
-
town_code = input_code()
-
if town_code in str(result3): #打印商品三级菜单
-
for u, v in enumerate(usr_dic[n][y],1):
-
if u == int(town_code):
-
trade_dict = (usr_dic[n][y][v])
-
while True:
-
shop_list()
-
trade_code = input("\033[32m请输入->'购买商品编码','b'(b:返回上一级):\033[0m")
-
if trade_code in str(trade_list_num()): # 判断输入的商品编码是否有误
-
price = trade_price(int(trade_code))
-
name = trade_name(int(trade_code))
-
while True: #判断输入商品数量是否合法,是否为数值型
-
trade_nu = input('\033[32m请输入购买商品数量:\033[0m')
-
if trade_nu.isdigit():
-
trade_num = int(trade_nu)
-
break
-
else:
-
print('\033[31m输入错误请重新输入:\033[0m')
-
-
balance = salary - int(price)*trade_num
-
if balance >= 0: # 当余额大于零时才可以购买商品
-
s1 = '''\t%s\t%s\t%s \n''' % (
-
name, trade_num, int(price)*trade_num)
-
sum_comsume = sum_comsume + int(price)*trade_num # (消费的总额)
-
salary = salary - int(price)*trade_num # 余额
-
# ---------------更新账单,反复购买一件商品会进行自动累加------------------------------------------------
-
trade_li = [name, trade_num, int(price)*trade_num]
-
code_li = []
-
for i in trade_bill:
-
code_li.append(i[0])
-
if trade_li[0] in code_li: # 判断新输入的商品信息是否已经存在,存在则进行累加
-
location = code_li.index(trade_li[0])
-
trade_new_li = ([trade_li[0],
-
trade_bill[location][1] +
-
trade_li[1],
-
trade_bill[location][2] +
-
trade_li[2]])
-
trade_bill.append(trade_new_li)
-
trade_bill.pop(location)
-
else: # 如果,不存在,直接追加到购买的商品列表的后面
-
trade_bill.append(trade_li)
-
while True:
-
final_state = input(
-
'\033[32m请输入下一步操作--> y/n(Y/N)(是否结算);c/C (查看购物信息):\033[0m')
-
if final_state == 'Y' or final_state == 'y':
-
s3 = '%s购物清单'%(user1_name)
-
print(s3.center(50, '*'))
-
s2 = '''消费总额:%s\t账户余额:%s\t消费时间:%s\t''' % \
-
(sum_comsume, salary,
-
time.strftime('%Y-%m-%d %H:%M:%S'))
-
result_print()
-
print('-'.center(50, '-'))
-
print(s2)
-
#---------把购物历史记录写入以用户名命名的文件----
-
with open(user1_name + '_shopping_history',
-
'a+') as f3:
-
li1 = []
-
for i in trade_bill:
-
li2 = []
-
for m in i:
-
li2.append(str(m))
-
li1.append(li2)
-
for x in li1:
-
s1 = ' '.join(x)
-
# print(s1 + '\n')
-
f3.write(s1+'\n')
-
with open(user1_name + '_shopping_history',
-
'a+') as f3:
-
f3.write(s2+'\n')
-
f3.write('-'.center(50,'-')+'\n')
-
#--------------在文件中更新用户的余额--------
-
for x in user_li:
-
if x[0] == user1_name:
-
location2 = user_li.index(x)
-
user_li.insert(location2,
-
[x[0], x[1], x[2], str(salary)])
-
user_li.pop(location2 + 1)
-
with open('user.txt', 'w+') as f1:
-
for i in user_li:
-
s1 = ':'.join(i)
-
f1.write(s1 + '\n')
-
sys.exit(0)
-
elif final_state == 'n' or final_state == 'N':
-
break
-
elif final_state == 'c' or final_state == 'C':
-
result_print()
-
print('\033[32m消费:%s\t余额:%s\033[0m'%(sum_comsume,salary))
-
else:
-
print('\033[31m错误的输入请重新输入!\033[0m')
-
continue
-
else:
-
print("\033[31m余额不足,请重新选择商品\033[0m")
-
elif trade_code == 'b':
-
break
-
else:
-
print('\033[31m商品编码输入有误,请重新输入\033[0m')
-
continue
-
elif town_code == 'b':
-
break
-
elif town_code == 'q':
-
sys.exit('\033[32mByeBye\033[0m')
-
else:
-
print ('\033[31m非法输入,请重新输入!\033[0m')
-
elif res_code == 'b':
-
break
-
elif res_code == 'q':
-
sys.exit('\033[32mByeBye\033[0m')
-
else:
-
print ('\033[31m非法输入,请重新输入!\033[0m')
-
elif city_code == 'q':
-
sys.exit('\033[32mByeBye\033[0m')
-
else:
-
print ("\033[31m非法输入,请重新输入!\033[0m")
[yn@localhost@../menu]$cat menu_info.txt
手机数码:热牌手机|iphone*iphone7.5899,iphone6s.3799;OPPO*OPPO-R9s.2799,OPPO-R9.2499 摄影摄像|单反相机*佳能.7299,尼康.2799;摄像机*索尼.2980,乐道.388
潮流女装:热卖品牌|新品连衣裙*伊莲娜冬款.338,范依琪冬款.138;针织开衫*姐弟女装.400,韩都衣舍.194 特色类目|旗袍*华族经典.299,亲晨秋款.216;礼服*创意狐.1288,希洛莎.298
[yn@localhost@../menu]$cat user.txt
yn:yn123:0:50000
alex:123456:0:50000
tom:tom123:0:50000
sky:123:0:50000
[yn@localhost@../menu]$
阅读(1497) | 评论(0) | 转发(0) |