Write simple ATM program

To achieve the following functions:

 1, recharge: recharge amount of money the user to enter the account number to complete the modifications in money db.txt

 2, the transfer function: the user A to the user B transfers membered 1000, db.txt completed Save money account of the user A, the user B to add money account

 3, cash function: the user enters the amount of withdrawals, the account to reduce the amount of money in db.txt

 4, check balances function: enter the account balance inquiry

 5, data from the file db.txt (CRUD are all in the same file among)

 Chosen as the elected to do: Log function

  After the user logs in successfully, the memory record this state, the above functions to whichever state currently logged in, you must be logged in to operation

code show as below:

login_name = None  #选做中的选做重点

# 操作系统相关操作功能

def move_fuc():
    import os
    os.remove(r'{}'.format('db.txt'))
    os.rename(r'{}.swap'.format('db.txt'), r'{}'.format('db.txt'))

# 登录功能

def login_func():
    name_user = input('请输入您的账号:').strip()
    name_pwd = input('请输入您的密码:').strip()
    global login_name
    if not login_name:
        with open('db.txt','r',encoding='utf-8') as f:
            for line in f:
                x, y, *_ = line.strip().split(':')
                if x == name_user and y == name_pwd:
                    print('登陆成功')
                    login_name = name_user
                    return login_name
            else:
                print('账号密码错误')

    else:
        print('您已经登录了,请勿重复登录')

# 充值功能

def recharge_fuc():
    global login_name
    while True:
        if login_name:
            money = input('输入您想充值的金额:')
            with open(r'db.txt', 'rb') as rf, \
                    open(r'db.txt.swap', 'wb') as wf:
                while True:
                    user_msg = rf.readline().decode('utf-8')
                    *_, z = user_msg.strip().split(':')
                    if login_name in user_msg:
                        money = int(z) + int(money)
                        data = user_msg.replace('{}'.format(z), '{}'.format(money))
                        wf.write(data.encode('utf-8'))
                    else:
                        wf.write('{}'.format(user_msg).encode('utf-8'))
                    if len(user_msg) == 0:
                        break
            move_fuc()
            print('充值成功,现有金额为{}元'.format(money))
            return '充值成功'
        else:
            print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
            inp_N = input('请输入符号:>>')
            if inp_N == 'P':
                login_func()
            else:
                print('退出')
                break


# 转账功能

def take_fuc():
    global login_name
    tag = 0
    while True:
        if login_name:
            name = input('输入您想转账的人:')
            with open(r'db.txt', 'rb') as f, \
                    open(r'db.txt.swap', 'wb') as wf:
                while True:
                    take_msg = f.readline().decode('utf-8')
                    x, *_= take_msg.strip().split(':')
                    if name == x:
                        tag = 1
                        break
                    elif not len(take_msg):
                        print('本银行没有该账号,玩呢?')
                        return
                f.seek(0, 0)
                money = input('输入您想转账的金额:')
                while tag:
                    take_msg = f.readline().decode('utf-8')
                    *_, z = take_msg.strip().split(':')
                    if name in take_msg:
                        money1 = int(z) + int(money)
                        data = take_msg.replace('{}'.format(z), '{}'.format(money1))
                        wf.write(data.encode('utf-8'))
                    elif login_name in take_msg:
                        if int(z) > int(money):
                            money2 = int(z) - int(money)
                            data = take_msg.replace('{}'.format(z), '{}'.format(money2))
                            wf.write(data.encode('utf-8'))
                            continue
                        else:
                            print('转账现金已经超过余额')
                            return '转账现金已经超过余额'
                    else:
                        wf.write('{}'.format(take_msg).encode('utf-8'))
                    if len(take_msg) == 0:
                        print('转账成功')
                        query_fuc()
                        break
            move_fuc()
            break
        else:
            print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
            inp_N = input('请输入符号:>>')
            if inp_N == 'P':
                login_func()
            else:
                print('退出')
                break


# 查询余额功能

def query_fuc():
    global login_name
    while True:
        if login_name:
            with open(r'db.txt','rb') as f:
                while True:
                    query_msg = f.readline().decode('utf-8')
                    if login_name in query_msg:
                        *_ , z = query_msg.strip().split(':')
                        print('{}的账号余额为{}元'.format(login_name,z))
                        return z
        else:
            print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
            inp_N = input('请输入符号:>>')
            if inp_N == 'P':
                login_func()
            else:
                print('退出')
                break


# 提现功能

def cash_func():
    global login_name
    while True:
        if login_name:
            money_cash = input('请输入您的提现金额:>>').strip()
            with open(r'db.txt', 'rb') as f, \
                    open(r'db.txt.swap', 'wb') as wf:
                while True:
                    cash_msg = f.readline().decode('utf-8')
                    if login_name in cash_msg:
                        *_ , z = cash_msg.strip().split(':')
                        if int(z) > int(money_cash):
                            money1 = int(z) - int(money_cash)
                            data = cash_msg.replace('{}'.format(z), '{}'.format(money1))
                            wf.write(data.encode('utf-8'))
                        else:
                            print('余额不足,提现失败')
                            return '余额不足,提现失败'
                    else:
                        wf.write('{}'.format(cash_msg).encode('utf-8'))
                    if len(cash_msg) == 0:
                        break
            move_fuc()
            print('提现成功')
            query_fuc()
            return '提现成功'
        else:
            print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
        inp_N = input('请输入符号:>>')
        if inp_N == 'P':
            login_func()
        else:
            print('退出')
            break


# 主函数体

def main_func():
    contests_atm = {
        '1':login_func,
        '2':recharge_fuc,
        '3':take_fuc,
        '4':cash_func,
        '5':query_fuc,
        '6':'6'
    }
    while True:
        print("""
        ==============ATM====================
            '1':登录
            '2':充值
            '3':转账
            '4':提现
            '5':余额
            '6':退出
        ==============END====================     
        """)
        cmd = input('请输入命令编号>>: ').strip()
        if not cmd.isdigit():
            print('必须输入命令编号的数字,傻叉')
            continue
        if cmd not in contests_atm:
            print('输入的命令不存在')
            continue
        if cmd == '6':
            print('已成功退出!')
            break
        contests_atm[cmd]()


main_func()

problem:

1, while loop when extracting the assignment can only take one to two or more will be given, for circulation but no problem.

2, when the dictionary definition of the function later, back value can not add (), otherwise it will run directly, I do not know why.

3, reflection points, when the file read and write operations in mode b, the pointer position should always pay attention!

Guess you like

Origin www.cnblogs.com/Lance-WJ/p/12528924.html