商城系統:包含使用者註冊/使用者登陸/商品瀏覽/我的購物車功能.

weixin_30588675發表於2020-04-05
"""
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 + '\n')
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('\n')
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('\n')
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()
"""

轉載於:https://www.cnblogs.com/zjx1/p/10745468.html

相關文章