• 作业13


    作业13

    周末作业

    编写ATM程序实现下述功能,数据来源于文件db.txt

    0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt

    1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)

    下述操作,要求登录后才能操作

    1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改

    2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱

    3、提现功能:用户输入提现金额,db.txt中该账号钱数减少

    4、查询余额功能:输入账号查询余额

    # 0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
    import os
    def logon():
        tag = 1
        while tag:
            print("开始注册")
            inp_user = input("请输入要注册的账号:")
            inp_password = input("请输入注册账号的密码:")
            with open("db.txt", mode="r", encoding="utf-8") as f:
                for line in f:
                    user, password, money = line.strip().split(":")
                    if user == inp_user:
                        print("用户名已存在,注册失败")
                        break
                    with open("db.txt", mode="a", encoding="utf-8") as f:
                        f.write("{}:{}:{}
    ".format(inp_user, inp_password, 0))
                    print("注册成功")
                    tag = 0
                    break
    
    
    # logon()
    online_user = None
    
    
    
    # 1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
    def login():
        global online_user
        tag = 1
        while tag:
            inp_user = input("请输入账号:")
            with open("lock_list.txt", mode="r", encoding="utf-8") as f:
                for line in f:
                    user, count = line.strip().split(":")
                    count = int(count)
                    if user == inp_user and count == 3:
                        print("该账号已被锁定")
                        break
                    elif user == inp_user and count < 3:
                        tag = 0
                        break
                else:
                    count = 0
                    break
        while count < 3:
            inp_password = input("请输入密码:")
            with open("db.txt", mode="r", encoding="utf-8") as f:
                for line in f:
                    user, password, money = line.strip().split(":")
                    if user == inp_user and password == inp_password:
                        online_user = inp_user
                        print("登陆成功")
                        return online_user
                    elif user == inp_user and password != inp_password:
                        print("密码错误,请重新输入")
                        count += 1
                        break
                else:
                    print("您输入的用户名不存在,请先注册")
                    logon()
                    break
            with open("lock_list.txt", mode="r", encoding="utf-8") as f1, 
                    open("lock_list.txt.swap", mode="w", encoding="utf-8") as f2:
                for line in f1:
                    if line.startswith(inp_user):
                        f2.write("{}:{}
    ".format(inp_user, str(count)))
                    else:
                        f2.write(line)
                if tag == 1:
                    f2.write("{}:{}
    ".format(inp_user, str(count)))
                    tag = 0
            os.remove("lock_list.txt")
            os.rename("lock_list.txt.swap", "lock_list.txt")
    
    
    # login()
    # print(online_user)
    
    
    # 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
    online_user = "wu"
    
    import os
    def invest():
        if online_user:
            print("开始充值")
            invest_money = input("请输入要充值的钱为多少:")
            if not invest_money.isdigit():
                print("只能输入数字")
                return
            else:
                invest_money = float(invest_money)
                with open("db.txt","r",encoding="utf-8") as f1,
                open("db.txt.swap","w",encoding="utf-8") as f2:
                    for line in f1:
                        user, password, balance = line.strip().split(":")
                        if user == online_user:
                            balance = str(float(balance)+ invest_money)
                            f2.write("{}:{}:{}
    ".format(user,password,balance))
                        else:
                            f2.write(line)
                os.remove("db.txt")
                os.rename("db.txt.swap","db.txt")
                print("充值成功")
        else:
            print("请先登录")
    invest()
    # 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
    online_user = "wu"
    
    import os
    def transfer():
        if online_user:
            print("开始转账")
            transfer_money = input("请输入要转账的钱为多少:")
            if not transfer_money.isdigit():
                print("只能输入数字")
                return
            else:
                if float(check_balance())-float(transfer_money)<0:
                    print("余额不足,无法转账")
                    return
                transfer_user = input("请输入要转账的账号:")
                transfer_money = float(transfer_money)
                with open("db.txt", "r", encoding="utf-8") as f1:
                    for line in f1:
                        user, _,_ = line.strip().split(":")
                        if user == transfer_user:
                            break
                    else:
                        print("账号不存在")
                        return
                with open("db.txt","r",encoding="utf-8") as f1,
                open("db.txt.swap","w",encoding="utf-8") as f2:
                    for line in f1:
                        user, password, balance = line.strip().split(":")
                        if user == online_user :
                            balance = str(float(balance)- transfer_money)
                            f2.write("{}:{}:{}
    ".format(user,password,balance))
                        elif user == transfer_user :
                            balance = str(float(balance) + transfer_money)
                            f2.write("{}:{}:{}
    ".format(user, password, balance))
                            tag = 1
                        else:
                            f2.write(line)
                os.remove("db.txt")
                os.rename("db.txt.swap","db.txt")
                print("转账成功")
        else:
            print("请先登录")
    
    
    # 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
    
    online_user = "wu"
    
    import os
    
    
    def with_draw():
        if online_user:
            print("开始转账")
            with_draw_money = input("请输入要提现的钱为多少:")
            if not with_draw_money.isdigit():
                print("只能输入数字")
                return
            else:
                with_draw_money = int(with_draw_money)
                if float(check_balance()) - float(with_draw_money) < 0:
                    print("余额不足,无法提现")
                    return
                with open("db.txt", "r", encoding="utf-8") as f1, 
                        open("db.txt.swap", "w", encoding="utf-8") as f2:
                    for line in f1:
                        user, password, balance = line.strip().split(":")
                        if user == online_user:
                            balance = str(float(balance) - with_draw_money)
                            f2.write("{}:{}:{}
    ".format(user, password, balance))
                        else:
                            f2.write(line)
                os.remove("db.txt")
                os.rename("db.txt.swap", "db.txt")
                print("提现成功")
        else:
            print("请先登录")
    
    
    # 4、查询余额功能:输入账号查询余额
    def check_balance():
        if online_user:
            with open("db.txt", "r", encoding="utf-8") as f:
                for line in f:
                    user, _, balance = line.strip().split(":")
                    if user == online_user:
                        print("您的余额为{}".format(balance))
                        return balance
        else:
            print("请先登录")
    
    
    # check_balance()
    
    # transfer()
    # with_draw()
    

    1

  • 相关阅读:
    HBase编程 API入门系列之delete.deleteColumn和delete.deleteColumns区别(客户端而言)(4)
    证明,为什么HBase在创建表时,列簇是必须要,列可不要?
    HBase编程 API入门系列之delete(客户端而言)(3)
    HBase编程 API入门系列之get(客户端而言)(2)
    HBase编程 API入门系列之put(客户端而言)(1)
    Hadoop HBase概念学习系列之HBase里的时间戳(二十六)
    Zookeeper(1、3、5节点)集群安装
    hbase无法启动,The node /hbase is not in ZooKeeper
    HBase、Hive、MapReduce、Hadoop、Spark 开发环境搭建后的一些步骤(export导出jar包方式 或 Ant 方式)
    Hadoop HBase概念学习系列之hbase shell中执行java方法(高手必备)(二十五)
  • 原文地址:https://www.cnblogs.com/achai222/p/12548933.html
Copyright © 2020-2023  润新知