"""
import os
import json
from datetime import datetime
USER_STATUS = False
USER_COUNT = {}
SHOPPING_CAR = {}
def decorate(arg):
def inner():
if not USER_STATUS:
print('请先登陆后再查看!')
return
arg()
return
return inner
def register():
while 1:
username = input('注册用户名:')
with open('8login.txt', 'r', encoding='utf-8') as f:
register1 = True
for line in f:
if line.split('----')[0].strip() == username:
register1 = False
print('帐户已存在!')
break
if not register1:
break
pwd = input('密码:')
sj = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
msg = '----'.join([username, pwd, sj, '0'])
with open('8login.txt', 'a', encoding='utf-8') as f:
f.write(msg + '
')
print('注册成功!')
return
def login():
while 1:
username = input('用户名:')
with open('8login.txt', 'r', encoding='utf-8') as f:
status = False
for line in f:
if line.split('----')[0] == username:
if line.split('----')[-1].strip() == '3':
print('帐户已锁定!')
return
status = True
break
if not status:
print('用户不存在!')
return
pwd = input('密码:')
with open('8login.txt', 'r', encoding='utf-8') as f:
for line in f:
if line.split('----')[0] == username and line.split('----')[1] == pwd:
USER_COUNT[username] = 0
print('登陆成功')
global USER_STATUS
USER_STATUS = username
return
if USER_COUNT.get(username) == None:
USER_COUNT[username] = 0
if USER_COUNT[username] < 3:
USER_COUNT[username] += 1
if not USER_COUNT[username] < 3:
with open('8login.txt', 'r', encoding='utf-8') as f1,
open('8login(改).txt', 'w', encoding='utf-8') as f2:
for line in f1:
if line.split('----')[0] == username:
new_line = line.replace('0', '3')
f2.write(new_line)
else:
f2.write(line)
os.remove('8login.txt')
os.rename('8login(改).txt', '8login.txt')
print('输入错误超过3次,锁定账号!')
return
print('登录失败请重试!')
@decorate
def goods():
f = open('8商品列表', 'r', encoding='utf-8')
a = f.read()
lst = a.split('
')
max_page, end = divmod(len(lst), 3) # 最大页,最后一页条数(总条数,每页条数)
if end > 0:
max_page += 1
x, y, z = 0, 3, 1
while 1:
if y > len(lst):
y = len(lst)
for i in range(x, y):
print(lst[i])
print('当前第%s页,共%s页.' % (z, max_page))
user = input('请输入序号加入到购物车,Y/y进入下一页,N/n退出:')
if user.upper() == 'N':
return '已退出'
elif user.upper() == 'Y':
x += 3
y += 3
z += 1
if y == len(lst) + 3:
x, y, z = 0, 3, 1
continue
elif user.isdigit():
pand = False
f = open('8商品列表', 'r', encoding='utf-8')
for line in f:
cut = line.split('|')
if user == cut[0]:
pand = True
if SHOPPING_CAR.get(cut[1]) == None:
SHOPPING_CAR[cut[1]] = 0
break
if not pand:
print('输入错误!')
continue
while 1:
count = input('请选择数量:')
if not count.isdigit():
print('请输入阿拉伯数字!')
continue
SHOPPING_CAR[cut[1]] += int(count)
date = datetime.now().strftime('%Y-%m-%d-%H-%M')
global USER_STATUS
dir_path = os.path.exists('shopping_car\%s' % USER_STATUS)
if not dir_path:
os.mkdir('shopping_car\%s' % USER_STATUS)
with open("shopping_car\%s\%s.txt" % (USER_STATUS, date), 'w', encoding='utf-8') as f:
up = json.dumps(SHOPPING_CAR, ensure_ascii=False)
f.write(up)
break
print('已添加进购物车!')
print(SHOPPING_CAR)
else:
print('输入有误!')
@decorate
def shopping_car():
if not os.path.exists('shopping_car\%s' % USER_STATUS):
print('您的购物车是空的,快去购买吧!')
return
content = os.walk('shopping_car\%s' % USER_STATUS)
for a, b, c in content:
for x in c:
print(x)
f = open('shopping_car\%s\%s' % (USER_STATUS, x), 'r', encoding='utf-8')
for line in f:
a = json.loads(line)
for item in a:
f = open('8商品列表', 'r', encoding='utf-8')
ls = f.read().split('
')
for lm in ls:
if lm.split('|')[1] == item:
price = lm.split('|')[2]
print(' %s|%s|%s个' % (item,price , a[item]))
return
def main():
'''
主页面
:return:
'''
dic = {'1': register, '2': login, '3': goods, '4': shopping_car}
while 1:
print('''*****欢迎来到沙河商城!*****
1.注册帐户
2.登陆帐户
3.浏览商品
4.查看购物车''')
a = input('请选择(N/n退出):')
if a.upper() == 'N':
return
if dic.get(a) == None:
print('输入有误!')
continue
dic.get(a)()
main()
"""