Day26.还款功能

1.还款功能_用户视图层(src.py)和银行相关业务接口(bank_interface.py)

  还款功能:src.py中的代码

复制代码
# 5. 还款功能
@common.login_auth
def repay():
    '''
    银行卡还款,无论是信用卡或储蓄卡,是否能充任意大小的金额
    '''
    while True:
        # 1) 让用户输入还款金额
        input_money = input('请输入需要还款的金额:').strip()
        print('input_money.isdigit():{}'.format(input_money.isdigit()))
        # 2)判断用户输入的是否是数字
        if not input_money.isdigit():
            print('请输入正确的金额')
            continue
        input_money = int(input_money)
        # 3)判断用户输入的金额大于0
        if input_money > 0:
            # 4)调用还款接口
            flag, msg = bank_interface.repay_interface(login_user, input_money)
            if flag:
                print(msg)
                break
        else:
            print('输入的金额不能小于0')
复制代码

  还款功能:bank_interface.py中的代码

复制代码
# 还款接口
def repay_interface(username, input_money):
    '''
    1.获取用户的金额
    2.给用户的金额做加钱的操作
    '''
    # 1.获取用户的字典
    user_dic = db_handler.select(username)

    # 2.直接做加钱的操作
    user_dic['balance'] = user_dic['balance'] + input_money
    
    # 3.调用数据处理层,将修改后的数据更新
    db_handler.save(user_dic)

    return True, '用户:{} 还款:{} 成功,当前可以用额度:{}'.format(username, input_money, user_dic['balance'])
复制代码

   查询余额:db_handler.py 的代码

复制代码
# 查看数据
def select(username):
    # 1) 接收接口层传过来的username用户名,拼接用户json文件路径
    user_path = os.path.join(
        settings.USER_DATA_PATH, '{}.json'.format(username)
    )
    # 2) 校验用户json文件是否存在
    if os.path.exists(user_path):
        # 3) 打开数据,并返回分接口层
        with open(user_path, 'r', encoding='utf-8') as f:
            user_dic = json.load(f)
            return user_dic
    # 3) 不return, 默认return None
复制代码

2.还款功能_程序运行结果

 

posted on   与太阳肩并肩  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示