3.20---购物车练习

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

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

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

 

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

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

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

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

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

 

 

# 注册函数
def register():
    tag = True
    while tag:
        username = input("请输入注册账号:")
        with open("db.txt",mode="a+t",encoding="utf-8") as f:
            f.seek(0,0)
            for line in f:
                name,pwd,account = line.strip().split(":")
                if name == username:
                    print("该用户名已存在,请重新输入!")
                    break
            else:
                while True:
                    password1 = input("请设定密码:")
                    password2 = input("请确认密码:")
                    if password1 == password2:
                        print(f"恭喜!用户<{username}>注册成功!")
                        f.write(f"{username}:{password1}:0\n")
                        tag = False
                        break
                    else:
                        print("两次密码不同!请重新输入!")

# 登录函数
def login():
    import time
    import os
    count = {}
    while True:
        username = input("请输入账号(q:退出):")
        if os.path.exists(f"{username}.locked.txt"):
            print("您的账号已被锁定!请5秒后继续输入!")
            time.sleep(5)
            os.remove(f"{username}.locked.txt")
            count[username] = 0

        if username == "q" or username =="Q":
            break

        with open("db.txt",mode="r+t",encoding="utf-8") as f:
            for line in f:
                name,pwd,account = line.strip().split(":")
                if name == username:
                    while True:
                        password = input("请输入密码(r:重输账号):")
                        if pwd == password:
                            print(f"尊敬的用户<{name}>,您已登录成功!")
                            logined.append(name)
                            break
                        elif password == "r" or password == "R":
                            break
                        else:
                            if username in count:
                                count[username] += 1
                                if count[username] == 3:
                                    with open(f"{username}.locked.txt",mode="wt",encoding="utf-8") as f:
                                        f.write(str(time.time()))
                                    print("您的账号已被锁定!请5秒后继续输入!")
                                    time.sleep(5)
                                    os.remove(f"{username}.locked.txt")
                                    count[username] = 0
                                    break
                            else:
                                count[username] = 1
                            print("密码错误,请重新输入!")
                    break
            else:
                print("该账号不存在!请先注册!")

# 查找函数
# 找到该用户在数据库中的行,并返回
# 若找不到,则返回None
def search_name(user):
    with open("db.txt",mode="rt",encoding="utf-8") as f:
        for line in f:
            name,pwd,account = line.strip().split(":")
            if name == user:
                return line

# 新行换旧行,替换函数
def change(old,new):
    import os
    with open("db.txt",mode="rt",encoding="utf-8") as f,\
        open(".db.swap.txt",mode="wt",encoding="utf-8") as f1:
        for line in f:
            if line == old:
                f1.write(new)
            else:
                f1.write(line)
    os.remove("db.txt")
    os.rename(".db.swap.txt","db.txt")

# 充值函数
def charge():
    # 先找此时登录账号,若无登录账号,提示登录。
    if not logined:
        print("请先登录!")
        return
    old = search_name(logined[0])
    name, password, account = old.strip().split(":")
    account = float(account)
    while True:
        money = input("请输入充值金额:")
        if money.isdigit():
            account += float(money)
            print(f"充值{money}元成功!您的账户当前余额为:{account}元")
            new = f"{name}:{password}:{account}\n"
            break
        else:
            print("请输入数字。")
    change(old,new)

# 转账函数:
def transfer():
    if not logined:
        print("请先登录!")
        return
    old_1 = search_name(logined[0])
    name1, password1, account1 = old_1.strip().split(":")
    account1 = float(account1)
    while True:
        apt_account = input("请输入转账账户:")
        if search_name(apt_account):
            old_2 = search_name(apt_account)
            name2,password2,account2 = old_2.strip().split(":")
            account2 = float(account2)
            money = input("请输入转账金额:")
            if money.isdigit():
                account1 -= float(money)
                account2 += float(money)
                print(f"转账{money}元至账户<{name2}>成功!您的账户当前余额为:{account1}元")
                new_1 = f"{name1}:{password1}:{account1}\n"
                new_2 = f"{name2}:{password2}:{account2}\n"
                break
            else:
                print("请输入数字。")
        else:
            print("转账账户不存在,请确认后重输!")
    change(old_1, new_1)
    change(old_2, new_2)

# 提现函数
def withdraw():
    if not logined:
        print("请先登录!")
        return
    old = search_name(logined[0])
    name, password, account = old.strip().split(":")
    account = float(account)
    while True:
        money = input("请输入提现金额:")
        if money.isdigit():
            if float(money) <= account:
                account -= float(money)
                print(f"提现{money}元成功!您的账户当前余额为:{account}元")
                new = f"{name}:{password}:{account}\n"
                break
            else:
                print(f"余额不足,当前余额为:{account}元")
        else:
            print("请输入数字。")
    change(old, new)

# 查询余额函数
def check_balance():
    if not logined:
        print("请先登录!")
        return
    old = search_name(logined[0])
    name, password, account = old.strip().split(":")
    print(f"当前余额为:{account}")

# 退出登录函数
def user_quit():
    if not logined:
        print("请先登录!")
        return
    logined.pop()

# ATM机流程函数
def Atm():
    while True:
        print(" ATM ".center(50,"-"))
        for key in func_dic:
            print(key,func_dic[key][0])
        print(" END ".center(50,"-"))
        cmd = input("请输入指令:")
        if cmd.isdigit():
            if cmd in func_dic:
                func_dic[cmd][1]()
            else:
                print("指令不存在!")
        else:
            print("请输入数字!")
        input("请按Enter键继续。")

# 函数字典
func_dic = {"1":("充值",charge),"2":("转账",transfer),"3":("提现",withdraw),"4":("查询余额",check_balance),
            "5":("登录",login),"6":("注册",register),"0":("退出当前账号",user_quit),"q":("退出程序",exit)}
# 全局变量,维护登录状态
logined = []

Atm()

 

posted @ 2020-03-20 21:00  Jil-Menzerna  阅读(163)  评论(0编辑  收藏  举报