• ATM购物车部分功能


    1. 额度 15000或自定义
    2. 实现购物商城,买东西加入 购物车,调用信用卡接口结账
    3. 可以提现,手续费5%
    4. 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
    5. 支持多账户登录
    6. 支持账户间转账
    7. 记录每月日常消费流水
    8. 提供还款接口
    9. ATM记录操作日志 
    10. 提供管理接口,包括添加账户、用户额度,冻结账户等。。。
    11. 用户认证用装饰器

    思维导图

    目录结构

    atm作业
    ├── README
    ├── atm #ATM主程目录
    │   ├── bin #ATM 执行文件 目录
    │   │   ├── __init__.py
    │   │   ├── atm_start.py  #ATM 主程序  执行程序
    │   │   
    │   ├── conf #配置文件
    │   │   ├── __init__.py
    │   │   └── settings.py #未用到
    │   ├── core #主要程序逻辑都 在这个目录 里
    │   │   ├── __init__.py
    │   │   ├── auth.py  #用户,信用卡,管理员认证模块
    │   │   ├── log.py      #日志记录模块
    │   │   ├── creditcard.py   #信用卡模块转账还款取现等
    │   │   ├── shopping.py       #购物模块商城购物车购物结算等
    │   │   ├── main.py         #主逻辑交互程序
    │   │   └── user.py  # 用户模块创建锁定解锁等
    │   ├── db  #数据库
    │   │   ├── __init__.py
    │   │   ├── Blacklist    # 用户黑名单文件
    │   │   └── user_data #用户文件,用户的各种信息
    │   │   └── creditcard  #信用卡文件,信用卡的各种信息
    │   │   └── shopping_car  #购物车文件
    │   │   └── shopping_list  #商品列表文件
    │   └── log #日志目录
    │         ├── __init__.py
    └──     └── water_record    #所有的用户,信用卡交易日志

    代码说明:实现TAM 装饰器 和 信用卡认证相关功能

    代码

    #ATM购物功能,本代码 实现信用卡和装饰器认证 相关功能  不是完整版
    import os
    import json
    
    #遇到问题:无法调用方法
    #问题总结:函数应该先定义在调用
    
    
    
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    creditcard_dic = BASE_DIR + r"/database/creditcard_data"
    user_dic = BASE_DIR + r"/database/user_data"
    user_Blacklist = BASE_DIR + r"/database/Blacklist_user"
    creditcard_record = BASE_DIR + r"/database/creditcard_record"
    
    '''信用卡信息'''
    def creditcard_data():
    
        while True:
            with open(creditcard_dic,"r") as f:
                creditcard_data = json.loads(f.read())
                choice = input("请输入要查看信息的信用卡账号 '6位数字' :").strip()
                if choice in creditcard_data.keys():
                    print("我的信用卡信息".center(50,"-"))
                    print("持卡人:	[ %s ]
    卡号:	[ %s ]
    额度:	[ ¥%s ]
    可用额度:	[ ¥%s ]
    取现额度:	[ ¥%s ]"
                          %(creditcard_data[choice]["personinfo"],
                            choice,
                            creditcard_data[choice]["deflimit"],
                            creditcard_data[choice]["limit"],
                            creditcard_data[choice]["limitcash"]))
    
                else:
                    print("您输入的信用卡,不存在。")
                choice = input("返回输入’q':")
                if choice == "q":
                    break
    
    
    '''信用卡信息'''
    def creditcard_data():
    
        while True:
            with open(creditcard_dic,"r") as f:
                creditcard_data = json.loads(f.read())
                choice = input("请输入要查看信息的信用卡账号 '6位数字' :").strip()
                if choice in creditcard_data.keys():
                    print("我的信用卡信息".center(50,"-"))
                    print("持卡人:	[ %s ]
    卡号:	[ %s ]
    额度:	[ ¥%s ]
    可用额度:	[ ¥%s ]
    取现额度:	[ ¥%s ]"
                          %(creditcard_data[choice]["personinfo"],
                            choice,
                            creditcard_data[choice]["deflimit"],
                            creditcard_data[choice]["limit"],
                            creditcard_data[choice]["limitcash"]))
    
                else:
                    print("您输入的信用卡,不存在。")
                choice = input("返回输入’q':")
                if choice == "q":
                    break
    
    
    '''信用卡信息'''
    def creditcard_data():
    
        while True:
            with open(creditcard_dic,"r") as f:
                creditcard_data = json.loads(f.read())
                choice = input("请输入要查看信息的信用卡账号 '6位数字' :").strip()
                if choice in creditcard_data.keys():
                    print("我的信用卡信息".center(50,"-"))
                    print("持卡人:	[ %s ]
    卡号:	[ %s ]
    额度:	[ ¥%s ]
    可用额度:	[ ¥%s ]
    取现额度:	[ ¥%s ]"
                          %(creditcard_data[choice]["personinfo"],
                            choice,
                            creditcard_data[choice]["deflimit"],
                            creditcard_data[choice]["limit"],
                            creditcard_data[choice]["limitcash"]))
    
                else:
                    print("您输入的信用卡,不存在。")
                choice = input("返回输入’q':")
                if choice == "q":
                    break
    
    '''信用卡取现'''
    def takenow():
        while True:
            print(" 取现 ".center(40, "-"))
            with open(creditcard_dic, "r+") as f:
                creditcard_data = json.loads(f.read())
                choice = input("请输入取现的信用卡账号, 返回'q' :").strip()
                if choice == 'q': break
                if choice in creditcard_data:
                    #print(creditcard_data)
                    limit = creditcard_data[choice]["limit"]
                    limitcash = creditcard_data[choice]["limitcash"]
                    takenow = limit // 2
                    print("信用卡卡号:	[ %s ]
    信用卡额度:	[ ¥ %s ]
    取现额度:	[ ¥ %s ]"
                          % (choice,limit,takenow))
                    if limit >= limitcash:
                        print("可取现金额为:	 [ ¥%s ]
    " % (limitcash))
                        cash = input("请输入要取现的金额,收取%5手续费 :").strip()
                        if cash.isdigit():
                            cash = int(cash)
                            if cash <= limitcash:
                                if cash > 0 :
                                    password = input("请输入信用卡[ %s ] 的密码 :"
                                                % (choice)).strip()
                                    if password and password == creditcard_data[choice]["password"]:
                                        limitcash = int(limitcash - (cash * 0.05 + cash))
                                        limit = int(limit - (cash * 0.05 + cash))
                                        creditcard_data[choice]["limit"] = limit
                                        creditcard_data[choice]["limitcash"] = limitcash
                                        f.seek(0)
                                        f.truncate(0)
                                        dic = json.dumps(creditcard_data)
                                        f.write(dic)
                                        takenow_data = [str(choice),"信用卡取现",str(cash),"手续费",str(int(cash*0.05))]
                                        msg = "---".join(takenow_data)
    
                                        print("取现成功".center(40,"-"))
                                        print("取现金额:	[%s]
    手续费:	[%s]" % (cash, cash * 0.05))
    
                                    else:
                                        print("密码输入错误
    ")
                                else:
                                    print("金额不能为0")
                            else:
                                print("您的取现金额已经超出取现额度了。")
                    else:
                        print("您的信用额度已经小于取现额度,不能取现了")
                else:
                    print("您输入的信用卡账号 [ %s ] 错误"%(choice))
    
    
    '''信用卡还款'''
    def repayment():
        while True:
            print(" 还款 ".center(40, "-"))
            with open(creditcard_dic, "r+") as f:
                creditcard_data = json.loads(f.read())
                choice = input("请输入还款的信用卡账号, 返回'q' :").strip()
                if choice == 'q': break
                if choice in creditcard_data:
                    money = input("请输入还款金额:").strip()
                    pwd = input("请输入还款密码 :").strip()
                    if pwd == creditcard_data[choice]["password"]:
                        if money.isdigit():
                            money = int(money)
                            with open(creditcard_dic, "r+") as f:
                                creditcard_data = json.loads(f.read())
                                limit = creditcard_data[choice]["limit"]
                                limitcash = creditcard_data[choice]["limitcash"]
                                limit += money
                                limitcash += (money//2)
                                creditcard_data[choice]["limit"]=limit
                                creditcard_data[choice]["limitcash"] = limitcash
                                dic = json.dumps(creditcard_data)
                                rapayment_data = [str(choice), "信用卡还款", str(money)]
                                msg = "---".join(rapayment_data)
                                f.seek(0)
                                f.truncate(0)
                                f.write(dic)
                                f.flush()
    
                                print("信用卡 [ %s ] 还款金额 [ ¥%s ] 还款成功" % (choice, money))
                                print("信用卡	[ %s ]
    可用额度:	[ ¥%s ]
    取现额度:	[ ¥%s ] "
                                      %(choice,
                                        creditcard_data[choice]["limit"],
                                        creditcard_data[choice]["limitcash"]))
                        else:
                            print("输入金额格式有误")
                    else:
                        print("密码输入错误")
                else:
                    print("您输入的信用卡不存在")
    
    
    '''信用卡转账'''
    def transfer():
        while True:
            print(" 转账 ".center(40, "-"))
            with open(creditcard_dic, "r+") as f:
                creditcard_data = json.loads(f.read())
                choice = input("请输入信用卡账号, 返回'q' :").strip()
                if choice == 'q': break
                if choice in creditcard_data:
                    current_limit = creditcard_data[choice]["limit"]
                    transfer_account = input("请输入转账账号:").strip()
                    if transfer_account.isdigit():
                        #print("----------")
                        if len(transfer_account) == 6 :
                            if transfer_account in creditcard_data.keys():
                                money = input("请输入转账金额:").strip()
                                if money.isdigit():
                                    money = int(money)
                                    creditcard_pwd = input("请输入信用卡账号密码:")
                                    if creditcard_pwd == creditcard_data[choice]["password"]:
                                        if money <= current_limit:
                                            creditcard_data[choice]["limit"] -= money
                                            creditcard_data[choice]["limitcash"] -= money//2
                                            creditcard_data[transfer_account]["limit"] += money
                                            creditcard_data[transfer_account]["limitcash"] += money//2
                                            print("转账成功".center(40,"-"))
                                            print("转账卡号:	[ %s ]
    转账金额:	[ ¥%s ]"%(transfer_account,money))
                                            print("信用卡:	[ %s ]	可用额度还剩:	[ ¥%s ]
    "%(creditcard_data[choice]["creditcard"],
                                                                           creditcard_data[choice]["limit"]))
    
    
                                            transfer_data = [str(choice), "信用卡转账", str(money)]
                                            msg = "---".join(transfer_data)
    
                                            f.seek(0)
                                            f.truncate(0)
                                            dic = json.dumps(creditcard_data)
                                            f.write(dic)
    
    
                                        else:
                                            print("转账金额不能超过信用额度")
                                    else:
                                        print("密码输入错误")
                                else:
                                    print("请输入数字的金额")
                            else:
                                print("您输入的卡号不存在")
                        else:
                            print("您输入的卡号不存在")
                    else:
                        print("请输入正确的卡号")
                else:
                    print("您输入的信用卡不存在")
    
    '''申请信用卡'''
    def new_creditcard(limit=15000,locked=False):
    
        while True:
            print("申请信用卡".center(50, "-"))
            with open(creditcard_dic, "r+") as f:
                creditcard_data = json.loads(f.read())
                for key in creditcard_data:
                    print("系统已有信用卡 【%s】 	持卡人 【%s】" % (key,creditcard_data[key]["personinfo"]))
                choice = input("
    33[34;0m是否申请新的信用卡 确定'y' 返回'q'33[0m:").strip()
                if choice == "q":break
                if choice == "y":
                    creditcard = input("33[34;0m输入要申请的信用卡卡号(6位数字):33[0m").strip()
                    if creditcard not in creditcard_data.keys():
                        if creditcard.isdigit() and len(creditcard) == 6:
                            password = input("33[34;0m请输入申请的信用卡密码:33[0m").strip()
                            if len(password) > 0:
                                personinfo = input("33[34;0m请输入信用卡申请人:33[0m").strip()
                                if len(personinfo) > 0:
                                    creditcard_data[creditcard] = {"creditcard":creditcard, "password":password, "personinfo":personinfo,
                                                            "limit":limit,"limitcash":limit//2,"locked":locked,"deflimit":limit,}
                                    dict = json.dumps(creditcard_data)
                                    f.seek(0)
                                    f.truncate(0)
                                    f.write(dict)
                                    print("信用卡:	[ %s ] 申请成功
    持卡人:	[ %s ]
    额度:	[ ¥%s ]
    取现额度:	[ ¥%s ]"%(creditcard,
                                                                                              personinfo,
                                                                                              limit,
                                                                                              creditcard_data[creditcard]["limitcash"]))
                                else:
                                    print("信用卡申请人不能为空
    ")
                            else:
                                print("输入的密码不正确
    ")
                        else:
                            print("信用卡 %s 卡号不符合规范
    " % (creditcard))
                    else:
                        print("信用卡 %s 已经存在
    " % (creditcard))
    
    
    '''信用卡绑定'''
    def link_creditcard():
        while True:
            print("33[32;0m修改信用卡绑定33[0m".center(40, "-"))
    
            with open(user_dic, "r+") as f:
                user_data = json.loads(f.read())
                user_name = input("请输入绑定信用卡的用户名  返回 'q' :").strip()
                if user_name == "q":break
                if user_name in user_data.keys():
                    creditcard = user_data[user_name]["creditcard"]
                    if creditcard == 0 :
                        print("当前账号: 	%s"%(user_name))
                        print("信用卡绑定:33[31;0m未绑定33[0m
    ")
                    else:
                        print("当前账号: 	%s" %(user_name))
                        print("绑定的信用卡: %s
    "%(creditcard))
                    choice = input("33[34;0m是否要修改信用卡绑定 确定 'y' 返回'q' 33[0m:")
                    if choice == "q":break
                    if choice == "y":
                        creditcard_new = input("33[34;0m输入新的信用卡卡号(6位数字)33[0m:").strip()
                        if creditcard_new.isdigit() and len(creditcard_new) ==6:
                            with open(creditcard_dic, "r+") as f1:
                                creditcard_data = json.loads(f1.read())
                                if creditcard_new in creditcard_data.keys():
                                    user_data[user_name]["creditcard"]=creditcard_new
                                    dict = json.dumps(user_data)
                                    f.seek(0)
                                    f.truncate(0)
                                    f.write(dict)
                                    print("33[31;1m信用卡绑定成功33[0m
    ")
                                else:
                                    print("33[31;0m输入信用卡卡号不存在(未发行)33[0m
    ")
                        else:
                            print("33[31;0m输入信用卡格式错误33[0m
    ")
                    else:
                        print("请选择 ’y‘ 或 ’q‘ ")
                else:
                    print("您输入的用户 [ %s ] 不存在 ")
    
    '''查看信用卡流水'''
    def cat_cred_record():
        while True:
            choice = input("请输入要查看流水记录的信用卡账号:").strip()
            with open(creditcard_dic) as f1:
                cred_record = json.loads(f1.read())
                if choice:
                    if choice in cred_record.keys():
                        print("信用卡 [ %s ] 流水记录".center(50, "-") % (choice))
                        with open(user_Water) as f:
                            for i in f:
                                if choice in i:
                                    #print("33[31;0m信用卡 [ %s ] 还没有进行过消费,去商城买点东西吧33[0m
    " % (choice))
                                    print(i.strip())
                    else:
                        print("您输入的信用卡 [ %s ] 不存在"%(choice))
                else:
                    print("您输入的信用卡 [ %s ] 不存在"%(choice))
    
            choice = input("返回 'q':")
            if choice == "q":
                break
    
    
    
    
    
    '''认证装饰器'''
    def auth(auth_type):
        def out_wrapper(func):
            if auth_type == "user_auth":  # 用户认证
                def wrapper():
                    res = func
                    user_name = input("请输入登录用户名 :").strip()
                    user_pwd = input("请输入登录密码 :").strip()
                    if len(user_name) > 0:
                        with open(user_dic, "r") as f:
                            user_data = json.loads(f.read())
                            if user_name in user_data.keys() and user_pwd == user_data[user_name]["password"]:
                                if user_data[user_name]["locked"] == False:
                                    print("[ %s ] 用户认证成功" % (user_name))
                                    return res, user_name
                                else:
                                    print("[ %s ] 用户已经被锁定,认证失败" % (user_name))
                            else:
                                print("[ %s ] 用户或密码错误,认证失败" % (user_name))
                    else:
                        print("[ %s ] 用户输入不能为空" % (user_name))
    
                return wrapper
    
            if auth_type == "creditcard_auth":  # 信用卡认证
                def wrapper():
                    res = func()
                    creditcard = input("请输入信用卡卡号(6位数字):").strip()
                    password = input("请输入信用卡密码 : ").strip()
                    if creditcard:
                        with open(creditcard_dic, "r") as f:
                            creditcard_data = json.loads(f.read())
                            if creditcard in creditcard_data.keys() and password == creditcard_data[creditcard]["password"]:
                                if creditcard_data[creditcard]["locked"] == False:
                                    print("信用卡 [ %s ] 验证成功" % (creditcard))
    
                                    return res, creditcard
                                else:
                                    print("信用卡 [ %s ]已经被冻结,请使用其他信用卡" % (creditcard))
    
                            else:
                                print("信用卡卡账号或密码输入错误")
                    else:
                        print("信用卡账号输入不能为空")
    
                return wrapper
    
            if auth_type == "admin_auth":  # 管理员认证
                def wrapper():
                    res = func()
                    admin_dic = {"admin": "admin", "passwrod": "123"}
                    admin_name = input("请输入管理员账号 :").strip()
                    admin_pwd = input("请输入密码 :").strip()
                    if admin_name:
                        if admin_name in admin_dic and admin_pwd == admin_dic["passwrod"]:
                            print("管理员账号[%s] 登陆成功。" % (admin_name))
    
                            return res, admin_name
    
                        else:
                            print("账号或密码错误")
                    else:
                        print("管理员账号输入不能为空")
    
                return wrapper
    
        return out_wrapper
    
    @auth(auth_type="user_auth")
    def user_auth():
        print("用户登录认证".center(40,"-"))
        return "True"
    
    @auth(auth_type="creditcard_auth")
    def creditcard_auth():
        print("信用卡登录认证".center(40,"-"))
        return "True"
    
    @auth(auth_type="admin_auth")
    def admin_auth():
        print("管理员登录认证".center(40,"-"))
        return "True"
    
    
    
    
    
    
    
    
    
    
    
    def main_list():
        msg = ["  ATM  ",
               "购物商城",
               "管理系统",
               "退出程序 输入 q", ]
    
        index = 0
        for i in msg:
            print("				", index + 1, "	 ", i)
            index += 1
    
    def atm_list():
        msg = ["信用卡信息",
               "信用卡取现",
               "信用卡转账",
               "信用卡还款",
               "申请信用卡",
               "信用卡绑定",
               "信用卡流水",
               "返回上一层 输入 q",
               "退出程序 输入 exit"]
    
        index = 0
        for i in msg:
            print("				", index + 1, "	 ", i)
            index += 1
    
    
    
    if __name__ == '__main__':
        def main():
            print("购物商城ATM系统".center(40, "-"))
            # lock()  # 三次锁定模块
            while True:
                print("欢迎来到购物商城ATM系统".center(40, "-"))
                print(" 				 ID		信息")
                main_list()
                choice = input("请选择 ID :").strip()
                if choice == "q":
                    print(" bye bye ".center(50, "-"))
                    exit()
                if choice.isdigit():
                    choice = int(choice)
                    if choice >= 1 and choice <= 4:
                        if choice == "q": break
                        while True:
                            if choice == 1:
                                print("欢迎来到信用卡中心".center(50, "-"))
                                print(" 				 ID		ATM信息")
                                atm_list()  # 信用卡列表
                                atm_choice = input("请选择 ATM ID :").strip()
                                if atm_choice == "q": break
                                if atm_choice == "exit": exit()
                                if atm_choice.isdigit():
                                    atm_choice = int(atm_choice)
                                    if atm_choice >= 1 and atm_choice <= 7:
                                        while True:
                                            if atm_choice == 1:
                                                creditcard_data()  # 信用卡信息模块
                                                break
                                            elif atm_choice == 2:
                                                creditcard_auth()  # 信用卡认证模块
                                                takenow()  # 信用卡取现模块
                                                break
                                            elif atm_choice == 3:
                                                creditcard_auth()  # 信用卡认证模块
                                                transfer()  # 信用卡转账模块
                                                break
                                            elif atm_choice == 4:
                                                creditcard_auth()  # 信用卡认证模块
                                                repayment()  # 信用卡还款模块
                                                break
                                            elif atm_choice == 5:
                                                new_creditcard(limit=15000, locked=False)  # 申请信用卡模块
                                                break
                                            elif atm_choice == 6:
                                                link_creditcard()  # 用户绑定信用卡模块
                                                break
                                            elif atm_choice == 7:
                                                cat_cred_record()  # 查看信用卡流水模块
                                                break
                                    else:
                                        print("请输入正确的 ID ")
                                else:
                                    print("请输入正确的 ID ")
                elif choice == 4:
                    exit()
    
        main()

    代码写完再上传后续代码

  • 相关阅读:
    音视频知识点
    JavaScript 设计模式单例模式
    实时音视频livepusher、liveplayer组件的使用
    音视频相关资料
    001RTSP、RTMP、HLS对比
    004coredns
    017tcpflow的简单用法
    018容器不退出调试
    017docker镜像占用太大问题
    3. nacos服务发现
  • 原文地址:https://www.cnblogs.com/Lollipop1/p/8203460.html
Copyright © 2020-2023  润新知