#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-04-29 12:16:23
# @Author : hjc ([email protected])
# @Link : http://www.cnblogs.com/tootooman/
# @Version : 2
import os
import re
import time
import json
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
userdb = 'userdb'
if not os.path.isfile(userdb):
db = open(userdb, 'w', encoding='utf8')
db.write('{}')
db.close()
shopping_car = {}
flag = True
while flag:
username = input('用户名:').strip()
password = input('密码:').strip()
with open(userdb, 'r+') as users:
users_data = json.load(users)
if username in users_data:
if password == users_data[username]:
# os.path.isfile(username)
check = input('是否要查看消费历史[y:查看/任意键:进入商城]:').strip()
f = open(username, 'r', encoding='utf8')
if check == 'y':
print('start'.center(60, '-'))
for line in f:
print(line)
print('end'.center(60, '-'))
ret = ''
f.seek(0)
for line in f:
ret = re.findall('余额.*', line)
salary = int(ret[0].split(':')[-1])
f.close()
break
else:
print('用户名或密码错误。')
else:
print('你不是会员!系统已自动帮你注册会员!')
users.seek(0)
users_data[username] = password
json.dump(users_data, users)
while 1:
salary = input('兜里有多少钱::').strip()
if salary.isdigit():
salary = int(salary)
f = open(username, 'w', encoding='utf8')
f.close()
flag = False
break
else:
print('你兜里的不是钱!')
while 1:
title = '''
%s
%s
%s
''' % (''.center(60, '*'), '商品列表'.center(60, ' '), ''.center(60, '*'))
print(title)
for i in range(len(goods)):
info = '''
商品编号:%s 商品名称:%s 商品价格:%s
''' % (i, goods[i]['name'], goods[i]['price'])
print(info)
choice = input('输入商品编号[q:结算]:').strip()
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(goods):
if salary >= goods[choice]['price']:
goods_name = goods[choice]['name']
goods_price = goods[choice]['price']
salary -= goods_price
if shopping_car.get(goods_name):
shopping_car[goods_name]['total'] += 1
print('\033[1;32m%s\033[0m已放到购物车,购买后兜里还剩 \033[1;31m%s\033[0m' % (
goods_name, salary))
else:
shopping_car[goods_name] = {}
shopping_car[goods_name]['price'] = goods_price
shopping_car[goods_name]['total'] = 1
print('\033[1;32m%s\033[0m已放到购物车,购买后兜里还剩 \033[1;31m%s\033[0m' % (
goods_name, salary))
else:
print('兜里钱不够!兜里还剩 \033[1;31m%s\033[0m !' % (salary,))
while 1:
get_money = input('要不要借一点?[n:不要]:').strip()
if get_money.isdigit():
get_money = int(get_money)
salary += get_money
print('借 钱 成 功!当前余额为:%s' % (salary,))
break
elif get_money == 'n':
break
else:
print('信不信我揍你。。。')
else:
print('没有相关商品。')
elif choice == 'q':
if len(shopping_car) != 0:
pay_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print('购物车信息:买完兜里还剩 \033[1;31m%s\033[0m' % (salary))
with open(username, 'a+', encoding='utf8') as f:
f.write('%s\n' % (pay_time.center(50, '=')))
for item in shopping_car:
shopping_list = '''
商品名称:%s 商品价格:%s 已买数量:%s 合计:%s
''' % (item,
shopping_car[item]['price'],
shopping_car[item]['total'],
shopping_car[item]['total'] *
shopping_car[item]['price'],
)
print(shopping_list.strip())
f.write('%s\n' % (shopping_list.strip()))
f.write('余额:%s\n' % (salary))
break
else:
print('没有买任何东西。')
break