Python 实现ATM机相关功能

编写ATM程序实现下述功能,数据来源于文件db.txt
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
4、查询余额功能:输入账号查询余额

具体代码如下:

import os
import time


def recharge():
    """充值功能"""
    recharge_amount = int(input('请输入您要充值的金额: ').strip())
    with open(r'db.txt', mode='rt', encoding='utf-8') as f1, \
            open(r'db.txt.swap', mode='wt', encoding='utf-8') as f2, \
            open(r'log.txt', mode='at', encoding='utf-8') as f3:
        for line in f1:
            if username in line:
                current_amount = int(line.strip().split(':')[1])  # 获得充值前的账户金额
                new_amount = recharge_amount + current_amount
                print('充值后的余额为: {}'.format(new_amount))
            f2.write(line.replace(str(current_amount), str(new_amount)))
        f3.write(
            '{} {} 冲值{}元,充值后的金额为:{}元\n'.format(time.asctime(time.localtime(time.time())), username, recharge_amount,
                                               new_amount))
    os.remove('db.txt')
    os.rename('db.txt.swap', 'db.txt')


def transfer():
    """转账功能"""
    transfer_to_user = input('请输入您要转账到用户的名字:').strip()
    transfer_amount = int(input('请输入您要转账的金额: ').strip())
    with open(r'db.txt', mode='rt', encoding='utf-8') as f1, \
            open(r'db.txt.swap', mode='wt', encoding='utf-8') as f2, \
            open(r'log.txt', mode='at', encoding='utf-8') as f3:
        for line in f1:
            if username in line:
                current_amount = int(line.strip().split(':')[1])  # 获得充值前的账户金额
                if current_amount > transfer_amount:
                    new_amount = current_amount - transfer_amount
                    print('您已成功给用户{}转账了{}元,当前账户还剩余额为{}元'.format(transfer_to_user, transfer_amount, new_amount))
                else:
                    print('您的转账金额已超出您的当前余额,请重新输入......')
                    transfer()

            if transfer_to_user in line:
                current_amount = int(line.strip().split(':')[1])
                new_amount = current_amount + transfer_amount
            f2.write(line.replace(str(current_amount), str(new_amount)))
        f3.write('{} 用户{}成功转账给用户{}{}元\n'.format(time.asctime(time.localtime(time.time())), username, transfer_to_user,
                                                transfer_amount))
    os.remove('db.txt')
    os.rename('db.txt.swap', 'db.txt')


def withdraw():
    """提现功能"""
    withdraw_amount = int(input('请输入您要提现的金额:').strip())
    with open(r'db.txt', mode='rt', encoding='utf-8') as f1, \
            open(r'db.txt.swap', mode='wt', encoding='utf-8') as f2, \
            open(r'log.txt', mode='at', encoding='utf-8') as f3:
        for line in f1:
            if username in line:
                current_amount = int(line.strip().split(':')[1])
                if current_amount > withdraw_amount:
                    new_amount = current_amount - withdraw_amount
                    print('用户{}您已成功提现{}元,当前账户还剩余额为{}元'.format(username,withdraw_amount, new_amount))
                else:
                    print('您的提现的金额已超出您的当前余额,请重新输入......')
                    withdraw()
            f2.write(line.replace(str(current_amount), str(new_amount)))
        f3.write('{} 用户{}成功转提现{}元\n'.format(time.asctime(time.localtime(time.time())), username, withdraw_amount))
    os.remove('db.txt')
    os.rename('db.txt.swap', 'db.txt')




def check_balance():
    """查询余额"""
    with open(r'db.txt',mode='rt',encoding='utf-8') as f:
        for line in f:
            if username in line:
                current_amount = line.strip().split(':')[1]
                print('用户{}的当前余额为:{}元'.format(username,current_amount))




if __name__ == '__main__':
    function_list = ['充值功能', '转账功能', '提现功能', '查询余额']
    tag = True
    print("""
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◆◆◆◆◆◆◆◆◆◇◇◇◇◇◆◆◆◆◇◇◇◇◆◆◆◇◇
◇◇◇◇◇◇◆◆◆◇◇◇◇◇◇◇◇◇◆◆◇◇◆◇◇◆◆◇◇◇◇◇◇◆◆◆◇◇◇◇◆◆◇◇◇
◇◇◇◇◇◇◆◆◆◇◇◇◇◇◇◇◇◇◆◆◇◇◆◇◇◆◆◇◇◇◇◇◇◇◆◆◆◇◇◆◆◆◇◇◇
◇◇◇◇◇◇◆◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◆◆◆◇◇◆◆◆◇◇◇
◇◇◇◇◇◆◆◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◆◆◆◆◇◆◇◆◇◇◇
◇◇◇◇◇◆◆◆◆◆◆◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◆◇◆◆◆◆◇◆◇◇◇
◇◇◇◇◆◆◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◆◇◆◆◆◇◇◆◇◇◇
◇◇◇◇◆◆◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◆◇◇◆◆◇◇◆◇◇◇
◇◇◇◆◆◆◇◇◆◆◆◆◆◇◇◇◇◇◇◇◆◆◆◆◆◇◇◇◇◇◇◇◆◆◆◆◇◆◆◆◆◆◆◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
        """)
    while tag:
        username = input('请输入您账号的用户名: ')
        password = input('请输入您账号的密码:')
        with open(r'user_info.txt', mode='rt', encoding='utf-8') as f:
            for line in f:
                uname, passwd = line.strip().split(':')
                if username == uname and password == passwd:
                    print('{name}认证通过'.format(name=username).center(50, '*'))
                    while True:
                        for id, item in enumerate(function_list):
                            print(id, item)
                        user_selector = input('请输入您要选择的功能[0-3],要退出程序请输入Q: ').strip()
                        if user_selector == '0':
                            recharge()
                        elif user_selector == '1':
                            transfer()
                        elif user_selector == '2':
                            withdraw()
                        elif user_selector == '3':
                            check_balance()
                        elif user_selector == 'Q':
                            exit()
                        else:
                            print('*输入的是无效字符,请在功能【0-3】内进行选择*')
            else:
                print('您输入账号的用户名或密码无效,请重新输入')

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值