Python ATM function implementation code Examples

This article describes the Python ATM functions to achieve code examples, this paper describes in great detail by the example code, a certain reference value of learning for all of us to learn or work, a friend in need can refer to the
written ATM program to achieve the following functions, data from document db.txt
. 1, top-up: the user input recharge amount of money, the amount of money to the account db.txt completed modification
2, the transfer function: A user 1000 yuan transfer to the user B, db.txt A user account is completed Save money, add money to the account user B
3, cash function: the user enters the amount of withdrawals, the account in reducing the amount of money db.txt
4, balance inquiry function: enter the account balance inquiry

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

import os
 
user_staus = {'username': None}
 
 
def ad_credit(username, amount):
  """
  充值功能
  :param username:
  :param amount:
  :return:
  """
  if user_staus['username'] is None:
    login()
  with open('db.txt', 'rt', encoding='utf-8')as f1, \
      open('db.txt.swap', 'wt', encoding='utf-8')as f2:
    while True:
      cont = f1.readline()
      if len(cont) == 0:
        break
      name, remain = cont.strip().split(':')
      if username in name:
        remain = int(remain) + int(amount)
        f2.write('{}:{}\n'.format(name, remain))
        print('充值成功,{}的余额为{}'.format(username, remain))
      else:
        f2.write(cont)
  os.remove('db.txt')
  os.rename('db.txt.swap', 'db.txt')
 
 
def transfer(user_out, user_in, amount):
  '''3
  转账功能
  :param user_out:
  :param user_in:
  :param amount:
  :return:
  '''
  if user_staus['username'] is None:
    login()
  with open('db.txt', 'rt', encoding='utf-8')as f1, \
      open('db.txt.swap', 'wt', encoding='utf-8')as f2:
 
    userinfo = {}
    for line in f1:
      name, remind = line.strip().split(':')
      userinfo[name] = int(remind)
    if user_out not in userinfo:
      print('用户不存在')
      return
    if user_in not in userinfo:
      print('收款方不存在')
      return
    if user_out in userinfo and user_in in userinfo:
      if userinfo[user_out] >= int(amount):
        userinfo[user_out] -= int(amount)
        userinfo[user_in] += int(amount)
        print('转账成功,已成功从{}向{}汇款{}'.format(user_out, user_in, amount))
      elif userinfo[user_out] < amount:
        print('余额不足')
        return
    for name, remind in userinfo.items():
      f2.write('{}:{}\n'.format(name, remind))
 
  os.remove('db.txt')
  os.rename('db.txt.swap', 'db.txt')
 
 
def cashon(username, amount):
  '''
  提现功能
  :param username:
  :param amount:
  :return:
  '''
  if user_staus['username'] is None:
    login()
  with open('db.txt', 'rt', encoding='utf-8')as f1, \
      open('db.txt.swap', 'wt', encoding='utf-8')as f2:
    userinfo = {}
    for line in f1:
      name, remind = line.strip().split(':')
      userinfo[name] = int(remind)
    if username not in userinfo:
      print('用户不存在')
      return
    if username in userinfo and userinfo[username] >= int(amount):
      userinfo[username] -= int(amount)
      print('已从余额中取出{},现余额为{}'.format(amount, userinfo[username]))
    elif userinfo[username] < amount:
      print('余额不足,提现失败')
      return
    for name, remind in userinfo.items():
      f2.write('{}:{}\n'.format(name, remind))
  os.remove('db.txt')
  os.rename('db.txt.swap', 'db.txt')
 
 
def check(username):
  '''
  余额查询功能
  :param username:
  :return:
  '''
  if user_staus['username'] is None:
    login()
  with open('db.txt', 'rt', encoding='utf-8')as f:
    userinfo = {}
    for line in f:
      name, remind = line.strip().split(':')
      userinfo[name] = remind
    if username not in userinfo:
      print('用户不存在')
      return
    if username in userinfo:
      print('当前余额为:{}'.format(userinfo[username]))
 
 
def login():
  username = input('输入用户名')
  userpassword = input('输入密码')
  with open('login.txt', 'rt', encoding='utf-8')as login_f:
    login = {}
    for line in login_f:
      name, psd = line.strip().split(':')
      login[name] = psd
      if username in login:
        if login[username] == userpassword:
          print('登陆成功')
          user_staus['username'] = username
          break
      elif username not in login:
        print('用户名不存在')
        return
 
 
def logout():
  user_staus['username'] = None
  print('已成功登出')
  return
 
 
login()
tag = True
while tag:
  cmd = input('''
  请输入你想使用的功能序号
  1:充值
  2:转账
  3:提现
  4:查询余额
  0:登出
  ''')
  if cmd == '1':
    username = input('输入用户名:')
    amount = input('输入充值金额:')
    ad_credit(username, amount)
  elif cmd == '2':
    user_out = input('请输入转账方:')
    user_in = input('请输入接收方:')
    amount = input('输入转账金额:')
    transfer(user_out, user_in, amount)
  elif cmd == '3':
    username = input('输入用户名:')
    amount = input('输入提现金额')
    cashon(username, amount)
  elif cmd == '4':
    username = input('输入用户名:')
    check(username)
  elif cmd == '0':
    logout()
    tag = False
  else:
    print('请正确输入序号')

The following are the results of the simulation

'''
/Users/chenfeng/PycharmProjects/ATM/venv/bin/python /Users/chenfeng/PycharmProjects/ATM/main.py
输入用户名xilou
输入密码666
登陆成功

请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
1
输入用户名:xilou
输入充值金额:200
充值成功,xilou的余额为700

请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
2
请输入转账方:xilou
请输入接收方:heiren
输入转账金额:200
转账成功,已成功从xilou向heiren汇款200

请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
3
输入用户名:xilou
输入提现金额100
已从余额中取出100,现余额为400

请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
4
输入用户名:xilou
当前余额为:400

请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
0
已成功登出

Process finished with exit code 0

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data every day to explain the timing of Python programmers technology, and share some learning methods need to pay attention to small detailsHere Insert Picture Description

Published 20 original articles · won praise 0 · Views 3613

Guess you like

Origin blog.csdn.net/chengxun02/article/details/104999331