用户操作的用户视图层
core/src.py
‘‘‘
视图层
‘‘‘
from interface import user_interface,bank_interface
from lib import common
#记录用户登录状态
Login_User = None
# 1、注册功能
def register():
while True:
print("============注册功能============")
username = input("请输入用户名:").strip()
password = input("请输入密码:").strip()
re_pwd = input("请再次输入密码:").strip()
if re_pwd == password:
flag,msg = user_interface.register_interface(username,password)
#根据flag判断用户是否注册成功
if flag:
print(msg)
break
else:
print(msg)
else:
print("两次密码输入不一致请重新输入")
# 2、登录功能
def login():
while True:
print("============登录功能============")
username = input("请输入用户名:").strip()
password = input("请输入密码:").strip()
flag,msg = user_interface.user_interface(username,password)
if flag:
print(msg)
global Login_User
Login_User = username
break
else:
print(msg)
@common.login_auth
# 3、查看余额
def check_balance():
print("============查看余额============")
#调用查询余额接口,获取用户余额
balance = user_interface.check_interface(Login_User)
print(f‘用户:{Login_User} 所剩余额:{balance}‘)
# 4、提现功能
@common.login_auth
def withdraw():
while True:
print("============提现功能============")
money = input("请输入提现金额").strip()
if not money.isdigit():
print("输入非法,请重新输入")
continue
#将金额交给接口层处理
flag,msg = bank_interface.withdraw_interface(
Login_User,money
)
if flag:
print(msg)
break
else:
print(msg)
# 5、还款功能
@common.login_auth
def repy():
while True:
print("============还款功能============")
money = input("请输入还款的金额").strip()
if not money.isdigit():
print("输入非法,请重新输入")
continue
# 将金额交给接口层处理
flag, msg = bank_interface.repy_interface(
Login_User, money
)
if flag:
print(msg)
break
else:
print(msg)
# 6、转账功能
@common.login_auth
def transfer():
while True:
print("============转账功能============")
name = input("请输入转款的账户").strip()
money = input("请输入还款的金额").strip()
if not money.isdigit():
print("输入非法,请重新输入")
continue
# 将金额交给接口层处理
flag, msg = bank_interface.transfer_interface(
Login_User,name,money
)
if flag:
print(msg)
break
else:
print(msg)
# 7、查看流水
@common.login_auth
def check_flow():
print("============查看流水============")
pass
# 8、购物功能
@common.login_auth
def shopping():
print("============购物功能============")
pass
# 9、查看购物车
@common.login_auth
def check_shop_car():
print("============查看购物车============")
pass
# 10、管理员功能
def admin():
print("============管理员功能============")
pass
func_dic = {
‘1‘: register,
‘2‘: login,
‘3‘: check_balance,
‘4‘: withdraw,
‘5‘: repy,
‘6‘: transfer,
‘7‘: check_flow,
‘8‘: shopping,
‘9‘: check_shop_car,
‘10‘: admin,
‘11‘: exit
}
#项目主程序
def run():
while True:
print(
‘‘‘
=========ATM+购物车==========
1、注册功能
2、登录功能
3、查看余额
4、提现功能
5、还款功能
6、转账功能
7、查看流水
8、购物功能
9、查看购物车
10、管理员功能
11、退出程序
========== end =============
‘‘‘
)
choice = input("请输入功能编号").strip()
if choice in func_dic:
func_dic.get(choice)()
else:
print("输入有误")
逻辑接口层
interface/bank_interface.py,
interface/shop_interface.py,
interface/user_interface.py
‘‘‘
用户接口
‘‘‘
#注册接口
from db import db_handler
from lib import common
def register_interface(username,password,balance = 15000):
‘‘‘
注册逻辑的核心代码
:return:
‘‘‘
#调用数据处理成中的select,返回用户字典或None
user_dic = db_handler.select(username)
password = common.get_pwd_md5(password)
if user_dic:
return False,‘用户名已存在‘
else:
user_dic = {
‘username‘: username,
‘password‘: password,
‘balance‘: balance,
# 用于记录用户流水的列表
‘flow‘: [],
# 用户车
‘shop_car‘: [],
# 用户冻结状态
‘locked‘: False,
}
#保存数据
db_handler.save(user_dic)
return True,f‘{username}注册成功‘
def user_interface(username,password):
user_dic = db_handler.select(username)
if not user_dic:
return False,‘用户名不存在‘
else:
password = common.get_pwd_md5(password)
if password == user_dic.get(‘password‘):
return True , f‘用户:{username}登录成功‘
else:
return False,"密码错误"
def check_interface(username):
user_dic = db_handler.select(username)
return user_dic.get(‘balance‘)
‘‘‘
银行相关逻辑代码
‘‘‘
from db import db_handler
#提现接口(手续费)
def withdraw_interface(username,money):
user_dic = db_handler.select(username)
#校验用户的钱是否足够
money2 = int(money)*1.05
balance = int(user_dic.get(‘balance‘))
if balance >= money2:
user_dic[‘balance‘] = balance - money2
db_handler.save(user_dic)
return True , f‘用户{username}提现金额:{money},手续费:{money2-float(money)}元‘
else:
return False , ‘提现失败,账户余额不足‘
def repy_interface(username,money):
user_dic = db_handler.select(username)
money = int(money)
balance = int(user_dic.get(‘balance‘))
user_dic[‘balance‘] = balance + money
db_handler.save(user_dic)
return f‘用户{username}还款金额:{money}元,可以使用的金额为{user_dic["balance"]}元‘
def transfer_interface(username,name,money):
transfer_dic = db_handler.select(name)
user_dic = db_handler.select(username)
if not transfer_dic:
return False, "转账用户不存在,请重新输入"
balance = int(user_dic.get(‘balance‘))
t_balance = int(transfer_dic.get(‘balance‘))
if balance >= money:
user_dic[‘balance‘] = balance - money
transfer_dic[‘balance‘] = t_balance+money
db_handler.save(user_dic)
db_handler.save(transfer_dic)
return True, f‘转账成功,为账户:{name}转入{money}元‘
else:
return False, ‘转账失败,账户余额不足‘
程序中的公共部分和装饰器,可以提炼到lib下的common中作为公共方法
lib/common
‘‘‘
公共方法
‘‘‘
import hashlib
from core import src
#密码加盐
def get_pwd_md5(password):
md5_obj = hashlib.md5()
md5_obj.update(password.encode(‘utf-8‘))
slat = ‘一二三四五‘
md5_obj.update(slat.encode(‘utf-8‘))
return md5_obj.hexdigest()
#登录认证装饰器
def login_auth(func):
def inner(*args,**kwargs):
if src.Login_User:
res = func(*args,**kwargs)
return res
else:
print("用户未登录")
src.login()
return inner
对底层数据进行操作的数据处理层
db/db_handler
‘‘‘
数据处理层
‘‘‘
import json
import os
from conf import setting
def select(username):
#接收接口层传过来的username用户名,拼接路径
user_path = os.path.join(
setting.USER_DATA_PATH, f‘{username}.json‘
)
# 判断用户是否存在
if os.path.exists(user_path):
#打开数据并返回接口层
with open(user_path, ‘r‘, encoding=‘utf-8‘) as f:
user_dic = json.load(f)
return user_dic
# 默认返回NONE
def save(user_dic):
#拼接用户的数据字典
user_path = os.path.join(
setting.USER_DATA_PATH, f‘{user_dic.get("username")}.json‘
)
with open(user_path, ‘w‘, encoding=‘utf-8‘)as f:
json.dump(user_dic, f, ensure_ascii=False)
项目的配置信息
conf/setting
‘‘‘
配置信息
‘‘‘
import os
#获取项目根目录路径
BASE_PATH = os.path.dirname(
os.path.dirname(__file__)
)
#获取项目user_data 文件路径
USER_DATA_PATH = os.path.join(
BASE_PATH,‘db‘,‘user_data‘
)
原文:https://www.cnblogs.com/Henry121/p/12622813.html