目录
购物车的升级系统
要求如下:
FUNC_MSG = {
'0': '注销',cancel
'1': '登录',login
'2': '注册',refister
'3': '查看余额',Check the balance
'4': '转账',transfewr accounts
'5': '还款',repayment
'6': '取款',withdrawal
'7': '查看流水',Check the current account
'8': '购物',shopping
'9': '购物车',shopping cart
'q': '退出'quit
}
account information 账户信息
# -*- encoding:utf-8 -*-
import os
product_list = [
['Iphone7', 5800],
['Coffee', 30],
['python Book', 99],
['Bike', 199],
['viv0 X9', 2499]
]
db_file = r'account_information.txt'
account_information = list() # 账户信息
information = list()
uname_pwd = list() # 用户名,与密码
shopping_cart = dict() # 购物车
yao_balance = list() # 别人的
message_info=list() #自己的流水
# 00删除用户信息
def del_cancel():
if len(uname_pwd) == 0:
login()
del_cancel()
elif len(uname_pwd) == 3:
longinhou()
else:
print('已经被注销,请登陆后删除信息')
# 如果删除信息,检测没登陆执行
def longinhou():
uname = uname_pwd[0]
pwd = uname_pwd[1]
print('开始删除')
str_file = db_file
dst_file = '%s.swap' % db_file
with open(str_file, 'r', encoding='utf8') as rf,
open(dst_file, 'w', encoding='utf8') as wf:
for line in rf:
line = line.strip('
').split(',',3)
if line[0] == uname and pwd == line[1]:
continue
else:
line = ','.join(line) + '
'
wf.write(line)
os.remove(str_file)
os.rename(dst_file, str_file)
print('删除成功')
# 0注销
def cancel():
if len(uname_pwd) == 0:
login()
cancel()
elif len(uname_pwd) == 2:
print('已经注销')
else:
print('开始注销')
uname_pwd.pop(0)
print('注销成功')
# 1登陆
def login():
count = 0
tag = True
while tag:
count += 1
uname = input('请输入用户名>:').strip()
pwd = input('请输入密码>:').strip()
with open(db_file, 'r', encoding='utf8') as f:
for line in f:
line = line.strip('
').split(',',3)
if line[0] == uname and line[1] == pwd:
print('登陆成功')
uname_pwd.extend(line[:3])
print(uname_pwd)
tag = False
break
else:
continue
if count == 3:
tag = False
# 2注册
def refister():
if len(uname_pwd) == 3:
print('请退出先注销在注册')
else:
uname = input('请输入用户名>:').strip()
with open(db_file, 'r', encoding='utf8') as fr:
for line in fr:
line = line.strip('
').split(',',3)
if line[0] == uname:
print('已经被注册,请直接登陆')
return 0
while True:
pwd1 = input('请输入密码>:').strip()
pwd2 = input('请确认密码>:').strip()
if pwd2 != pwd1:
print('密码不一致,请从新输入')
continue
else:
print('正在注册,请等待')
break
balance = input('请充钱>:').strip()
messgae = list() #type:list
with open(db_file, 'a', encoding='utf8') as f:
f.write(('%s,%s,%s,%s
') % (uname, pwd1, balance,messgae))
print('注册成功')
# 3查看余额
def Check_the_balance():
if len(uname_pwd) == 0:
login()
print(uname_pwd[2])
elif len(uname_pwd) == 3:
print(uname_pwd[2])
else:
print('已经被注销')
# 4转账
def transfewr_accounts():
"""给某人进行转账"""
if len(uname_pwd) == 0:
login()
transfewr_accounts()
elif len(uname_pwd) == 3:
payee_name = input('请输入收款人的姓名:').strip()
with open(db_file, 'r', encoding='utf8') as fr:
for line in fr:
line = line.strip('
').split(',',3)
if payee_name in line:
yao_balance.append(int(line[2]))
print(12)
money = int(input('请输入转账金额>>>:').strip())
yao_balance[0] += money
balance = int(uname_pwd[2])
balance -= money
uname_pwd[2] = balance
message_info_list =eval(line[3]) #type:list
info_list = f'收到{uname_pwd[0]}转账{money}'
info_list2 = f'向{payee_name}转账{money}'
message_info.append(info_list2)
informat(payee_name,info_list)
yao_balance.clear()
print('转账成功,请查看')
return message_info_list
else:
print('已经注销,请登陆')
# 5还款
def repayment():
"""还款"""
if len(uname_pwd) == 0:
login()
repayment()
elif len(uname_pwd) == 3:
bank = 'bank'
with open(db_file, 'r', encoding='utf8') as fr:
for line in fr:
line = line.strip('
').split(',',3)
if bank in line:
yao_balance.append(int(line[2]))
money = int(input('请输入还款金额>>>:').strip())
yao_balance[0] += money
balance = int(uname_pwd[2])
balance -= money
uname_pwd[2] = balance
message_info_list =eval(line[3]) #type:list
info_list = f'收到{uname_pwd[0]}转账{money}'
info_list2 = f'向{bank}转账{money}'
message_info.append(info_list2)
informat(bank,info_list)
print('还款成功,一共取出', money)
yao_balance.clear()
else:
print('已经注销,请登陆')
# 6取款
def withdrawal():
"""从银行取款"""
if len(uname_pwd) == 0:
login()
withdrawal()
elif len(uname_pwd) == 3:
bank = 'bank'
with open(db_file, 'r', encoding='utf8') as fr:
for line in fr:
line = line.strip('
').split(',',3)
if bank in line:
yao_balance.append(int(line[2]))
money = int(input('请输入取款金额>>>:').strip())
yao_balance[0] -= money
balance = int(uname_pwd[2])
balance -= money
uname_pwd[2] = balance
message_info_list =eval(line[3]) #type:list
info_list = f'收到{uname_pwd[0]}转账{money}'
info_list2 = f'向{bank}转账{money}'
message_info.append(info_list2)
informat(bank,info_list)
print('取款成功,一共取出', money)
yao_balance.clear()
else:
print('已经注销,请登陆')
# 7查看流水
#想不出来,如何调用
def Check_the_current_account():
if len(uname_pwd) == 0:
login()
Check_the_current_account()
elif not uname_pwd[0].isdigit():
with open(db_file,'r',encoding='utf8') as fr:
for line in fr:
line = line.strip('
').split(',',3)
if uname_pwd[0] in line:
print(line[-1])
else:
print('请登陆')
# 8购物
def shopping():
if len(uname_pwd) == 0:
login()
shopping()
elif not uname_pwd[0].isdigit():
uname = uname_pwd[0]
balance = int(uname_pwd[2])
tag = True
while tag:
for i, product in enumerate(product_list):
print(i, product)
choice = input('请输入商品编号,输入q退出>>').strip()
if choice.isdigit():
choice = int(choice)
if choice < 0 or choice >= len(product_list): continue
pname = product_list[choice][0]
pprice = product_list[choice][1]
if balance > pprice:
if pname in shopping_cart:
shopping_cart[pname]['count'] += 1
print(shopping_cart)
else:
shopping_cart[pname] = {
'pprice': pprice,
'count': 1
}
balance -= pprice
uname_pwd[2] = balance
info_list2 = f'购物消费了{balance}'
message_info.append(info_list2)
else:
print("买不起,穷逼! 产品价格是{price},你还差{lack_price}".format(
price=pprice, lack_price=(pprice - balance)))
elif choice == 'q':
break
else:
print('请输入正确的编号')
informat()
else:
print('已经被注销')
print(shopping_cart)
# 9购物车
def Shopping_cart():
if len(uname_pwd) == 0:
login()
Shopping_cart()
elif len(uname_pwd) == 3:
balance = int(uname_pwd[2])
print("""
--------------------已购买商品列表-------------------------
id 商品 数量 单价 总价
""")
total_cost = 0
for i, key in enumerate(shopping_cart):
print('%10s%15s%10s%15s%12s' %
(i, key, shopping_cart[key]['count'],
shopping_cart[key]['pprice'],
shopping_cart[key]['pprice'] *
shopping_cart[key]['count']))
total_cost += shopping_cart[key][
'pprice'] * shopping_cart[key]['count']
print("""
您的花费总额为: %s
您的余额为: %s
--------------------------end---------------------------------
""" % (total_cost, balance))
shopping_cart.clear()
else:
print('已经被注销')
### 更改信息
def informat(to_people=None,message=None):
if to_people :
print(message)
src_file = db_file
dst_file = r'%s.swap' % db_file
with open(src_file, 'r', encoding='utf-8') as read_f,
open(dst_file, 'w', encoding='utf-8') as write_f:
for line in read_f:
if line.startswith(to_people):
l = line.strip('
').split(',',3)
l[-2] = str(yao_balance[0])
l[-1]=eval(l[-1])#type:list
l[-1].append(message)
l[-1] = str(l[-1])
line = ','.join(l) + '
'
write_f.write(line)
os.remove(src_file)
os.rename(dst_file, src_file)
uname = uname_pwd[0]
pad = uname_pwd[1]
balance = uname_pwd[2]
src_file = db_file
dst_file = r'%s.swap' % db_file
with open(src_file, 'r', encoding='utf-8') as read_f,
open(dst_file, 'w', encoding='utf-8') as write_f:
for line in read_f:
if line.startswith(uname):
l = line.strip('
').split(',',3)
l[-2] = str(balance)
l[-1] = eval(l[-1]) # type:list
l[-1].append(message_info)
l[-1] = str(l[-1])
line = ','.join(l) + '
'
write_f.write(line)
os.remove(src_file)
os.rename(dst_file, src_file)
message_info.clear()
# 执行程序
while True:
choice = input("请选择00: 删除用户信息,0:注销,1: 登录,2:注册,3:查看余额,"
"4:转账,5:还款,6:取款,7:查看流水,8:购物,9:购物车,q:退出>>>:")
if choice == '00':
del_cancel()
elif choice == '0':
cancel()
elif choice == '1':
login()
elif choice == '2':
refister()
elif choice == '3':
Check_the_balance()
elif choice == '4':
transfewr_accounts()
elif choice == '5':
repayment()
elif choice == '6':
withdrawal()
elif choice == '7':
Check_the_current_account()
elif choice == '8':
shopping()
elif choice == '9':
Shopping_cart()
elif choice == 'q':
break