ATM+shopping_car ——面条版(待补充)——三层架构思路
# coding:utf-8
import os
import sys
import json
root_dir = os.path.dirname(os.path.dirname(__file__))
user_db = os.path.join(root_dir, 'db')
sys.path.append(root_dir)
if not os.path.exists(user_db):
os.mkdir(user_db)
user_status = {
'user_name': None,
'black_user': None
}
def status(func_name):
def inner(*args, **kwargs):
if not user_status['user_name']:
print("Your don't login!")
login()
return
res = func_name(*args, **kwargs)
return res
return inner
def register():
while True:
user_name = input('Please input your name>>>:')
user_real_db = os.path.join(user_db, f'{user_name}.json')
if not os.path.exists(user_real_db):
pass_word = input('Please input your password>>>:')
pass_word_again = input('Please input your password again>>>:')
if not pass_word == pass_word_again:
print('Your twice password is different!')
with open(user_real_db, 'w', encoding='utf-8') as creat:
user_dict = {
'user_name': user_name,
'pass_word': pass_word,
'all_money': 0,
'user_shopping': {}
}
json.dump(user_dict, creat)
print('user creat success!')
break
else:
print('User name already exists!')
def login():
while True:
user_name = input('Please input your name>>>:')
user_real_db = os.path.join(user_db, f'{user_name}.json')
if not os.path.exists(user_real_db):
print('Your username not in database!')
continue
pass_word = input('Please input your password>>>:')
with open(user_real_db, 'r', encoding='utf-8') as check:
user_dict = json.load(check)
if pass_word in user_dict.get('pass_word'):
print('Login success!')
user_status['user_name'] = user_name
break
else:
print('Your input is wrong!')
@status
def check_money():
user_real_db = os.path.join(user_db, f'{user_status.get("user_name")}.json')
with open(user_real_db, 'r', encoding='utf-8') as check:
user_dict = json.load(check)
user_money = user_dict.get('all_money')
print(f'{user_status.get("user_name")},your all money is {user_money}')
@status
def withdraw_money():
withdraw_money_number = input('Please input your need withdraw number>>>:')
withdraw_money_number = int(withdraw_money_number)
user_real_db = os.path.join(user_db, f'{user_status.get("user_name")}.json')
with open(user_real_db, 'r', encoding='utf-8') as withdraw:
user_dict = json.load(withdraw)
now_money = user_dict.get('all_money')
if now_money < withdraw_money_number:
print('Your balance is not enough!')
else:
now_money -= withdraw_money_number
user_dict['all_money'] = now_money
with open(user_real_db, 'w', encoding='utf-8') as final_money:
json.dump(user_dict, final_money, ensure_ascii=False)
print(f'your withdraw money is {withdraw_money_number},balance is {now_money}')
@status
def return_money():
deposit_money = input('Please deposit more money,Baby>>>:').strip()
deposit_money = int(deposit_money)
user_real_db = os.path.join(user_db, f'{user_status.get("user_name")}.json')
with open(user_real_db, 'r', encoding='utf-8') as deposit:
user_dict = json.load(deposit)
user_money = user_dict.get('all_money')
if user_money:
user_money += deposit_money
else:
user_money = deposit_money
user_dict['all_money'] = user_money
with open(user_real_db, 'w', encoding='utf-8') as keep:
json.dump(user_dict, keep, ensure_ascii=False)
print('Deposit money success!')
return
@status
def transfer_money():
while True:
target_user = input('Please input your transfer username>>>:')
user_real_db = os.path.join(user_db, f'{user_status.get("user_name")}.json')
target_user_db = os.path.join(user_db, f'{target_user}.json')
if not target_user_db:
print('Your need input real username!')
continue
transfer_money_number = input('Please input transfer number>>>:')
transfer_money_number = int(transfer_money_number)
with open(user_real_db, 'r', encoding='utf-8') as transfer:
user_dict = json.load(transfer)
gold_user_money = user_dict.get('all_money')
if transfer_money_number < gold_user_money:
gold_user_money -= transfer_money_number
user_dict['all_money'] = gold_user_money
with open(user_real_db, 'w', encoding='utf-8') as gold:
json.dump(user_dict, gold, ensure_ascii=False)
print('transfer success!')
with open(target_user_db, 'r', encoding='utf-8') as target:
target_user_dict = json.load(target)
target_user_money = target_user_dict.get('all_money')
target_user_money += transfer_money_number
user_dict['all_money'] = target_user_money
with open(target_user_db, 'w', encoding='utf-8') as f:
json.dump(user_dict, f, ensure_ascii=False)
print('accept money')
@status
def money_view():
pass
@status
def add_shop():
temp_shop_car = {}
while True:
goods_list = [
['挂壁面', 3],
['印度飞饼', 22],
['极品木瓜', 66],
['土耳其土豆', 99],
['伊拉克拌面', 100],
['董卓戏张飞公仔', 200],
['仿真玩偶', 1000]
]
for goods_number, goods_info in enumerate(goods_list, start=1):
print(goods_number, goods_info)
choose_shopping = input('Please input you need shopping(q = quit add shopping)>>>:')
if choose_shopping == 'q':
user_real_db = os.path.join(user_db, f'{user_status.get("user_name")}.json')
with open(user_real_db, 'r', encoding='utf-8') as read:
user_dict = json.load(read)
old_car = user_dict.get('user_shopping')
for g_name, g_number in temp_shop_car.items():
if g_name in old_car:
old_car[g_name][0] += temp_shop_car[g_name][0]
else:
old_car = temp_shop_car
user_dict['user_shopping'] = old_car
with open(user_real_db, 'w', encoding='utf-8') as write:
json.dump(user_dict, write, ensure_ascii=False)
return
if not choose_shopping.isdigit():
print('Please input digit!')
continue
choose_shopping = int(choose_shopping)
if choose_shopping not in range(1, len(goods_list)):
print('Please right number!')
continue
target_shopping = goods_list[choose_shopping - 1]
shopping_name = target_shopping[0]
shopping_price = target_shopping[1]
need_number = input(f'Please input your need {shopping_name} number>>>:')
need_number = int(need_number)
if shopping_name in temp_shop_car:
temp_shop_car.get(shopping_name)[0] += need_number
else:
temp_shop_car[shopping_name] = [need_number, shopping_price]
print(temp_shop_car)
@status
def check_shop():
user_real_db = os.path.join(user_db, f'{user_status.get("user_name")}.json')
with open(user_real_db, 'r', encoding='utf-8') as check:
user_dict = json.load(check)
print(f"user_shopping: {user_dict.get('user_shopping')}" )
@status
def pay_shop():
user_final_db = os.path.join(user_db, f'{user_status["user_name"]}.json')
with open(user_final_db, 'r', encoding='utf-8') as pay:
user_dict = json.load(pay)
shopping_car = user_dict.get('user_shopping')
user_money = user_dict.get('all_money')
if not shopping_car:
print('Your shopping is None!')
shopping_money = 0
for shopping_number, shopping_price in shopping_car.values():
shopping_money += shopping_number * shopping_price
if shopping_money < user_money:
user_money -= shopping_money
user_dict['all_money'] = user_money
with open(user_final_db, 'w', encoding='utf-8') as keep:
json.dump(user_dict, keep, ensure_ascii=False)
return
@status
def admin():
pass
user_operator = {
'1': register,
'2': login,
'3': check_money,
'4': withdraw_money,
'5': return_money,
'6': transfer_money,
'7': money_view,
'8': add_shop,
'9': check_shop,
'10': pay_shop,
'11': admin,
}
def run():
while True:
print('''
'1': register,
'2': login,
'3': check_money,
'4': withdraw_money,
'5': return_money,
'6': transfer_money,
'7': money_view,
'8': add_shop,
'9': check_shop,
'10': pay_shop,
'11': admin,,
'12': quit_system
''')
user_choose = input('Please input your choose number>>>:').strip()
if user_choose in user_operator.keys():
user_operator.get(user_choose)()
elif user_choose == '12':
break
else:
print('Please input right number!')
创建文件目录
'''
bin文件夹
start.py
conf文件夹
settings.py
core文件夹
conf.py
db 文件夹
保存用户数据文件
interface文件夹
user_interface 第二层逻辑运算
bank_interface
shop_interface
admin_interface
lib文件
common
readme.txt
requirements 项目版本
'''
start启动文件内容
# coding:utf-8
import os
import sys
# 获取根目录并设置为全局变量
root_dir = os.path.dirname(os.path.dirname(__file__))
sys.path.append(root_dir)
if __name__ == '__main__':
from core import src
src.run()
注册功能
'''
1.获取用户名和密码
2.再次获取密码进行比较
3.两次密码一致调用注册接口
4.校验用户名已存在
5.密码加密处理 加密功能放在公共功能下common
6.构造用户字典
7.save()保存数据 日志记录
8.用户注册成功 保存返回数据
'''
登录功能
'''
1.获取用户相关数据
2.直接调用用户登录接口
3.先查询是否有当前用户名对应的数据值
4.如果用户名正确 比对加密密码
5.正确登录 错误返回 都返回两个值
'''
查看余额
'''
1.全局用户字典是否登录
2.装饰器套用
3.直接调用查看账户余额的接口
4.查询当前登录用户的字典数据
5.从字典中获取余额信息并返回给第一层
6.日志记录
'''
提现功能
'''
1. 获取用户的想要提现的具体现金
2. 判断用户输入的是否是整数或者小数
# 代码异常处理
3.转数字操作 float()
4.调用银行接口完成提现操作
5.1 获取当前登录用户的字典数据
5.2 获取用户余额并判断是否满足登录提现要求 收取手续费
5.3 添加流水信息
5.4 保存提现之后的用户数据
'''
充值功能
'''
1.直接获取用户想要充值的钱数
2.直接调用银行充值接口 并判断金额数字是否符合要求
3.获取用户字典数据
4.直接添加余额
5.添加流水记录
6.保存用户数据
7.日志记录
'''
转账功能
'''
1.获取想要转账的用户名
2.获取想要转账的金额
3.直接调用转账的接口
4.1 先校验目标用户是否存在
4.2 再校验用户数的金额是否是整数或者小数
5 获取当前登录用户的数据
6.判断 转账者的余额是否充足
7. 记录加钱 扣钱的流水
8.记录转账之后的两个账户的数据
9.日志记录
10.返回数据
'''
用户流水
'''
1.直接调用查看的流水接口即可
2.获取用户名对应的用户字典
for 循环打印
3.日志记录
4.返回数据
'''
添加购物车
'''
1.先获取商品数据
2.打印商品数据并让用户选择
3.用户一旦退出 调用接口完成购物车数据更新
直接调用购物车接口
1.获取商品信息(目前是写死的 后期可以动态获取)
2.循环打印商品信息供用户选择
3.获取用户输入的商品编号
4.判断编号是否是纯数字
5.判断数字是否超出范围
6.根据商品编号获取商品信息
7.获取想要购买的商品个数
8.构造临时小字典存储商品信息
9.写入临时小字典中
10.添加结束标志 用于保存购物车数据
11.获取当前登录用户的字典数据
12.保存购物车数据
'''
查看购物车
'''
1.直接调用查看购物车接口
1.1 获取当前登录用户的字典数据
1.2 获取当前用户购物车数据
1.3 如果购物车有数据
1.4 如果没有
2.日志记录
'''
结算购物车
'''
1.直接调用结算购物车接口文件
2.获取当前登录用户的字典数据
3.获取当前用户车数据以及余额
4.统计购物车商品总价
5.比较余额是否充足
6.清空购物车
7.流水记录
8.日志记录
9.保存新数据
10.返回数据值
'''
标签:shopping,name,car,ATM,db,dict,user,money
From: https://www.cnblogs.com/HaiMan/p/16846147.html