亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

使用Python3 編寫(xiě)簡(jiǎn)單信用卡管理程序

original 2017-01-09 09:07:20 391
abstrait:這篇文章主要介紹了使用Python3 編寫(xiě)簡(jiǎn)單信用卡管理程序的代碼.1、程序執(zhí)行代碼:#Author by Andy #_*_ coding:utf-8 _*_ import os,sys,time Base_dir=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.pa

這篇文章主要介紹了使用Python3 編寫(xiě)簡(jiǎn)單信用卡管理程序的代碼.

1、程序執(zhí)行代碼:

#Author by Andy
#_*_ coding:utf-8 _*_
import os,sys,time
Base_dir=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(Base_dir)
str="歡迎使用銀行信用卡自助服務(wù)系統(tǒng)!\n"
for i in str:
  sys.stdout.write(i)
  sys.stdout.flush()
  time.sleep(0.3)
while True:
  print("1、管理人員入口。")
  time.sleep(0.3)
  print("2、用戶登錄入口。")
  print("3、退出請(qǐng)按q!")
  choice=input(":")
  from core import main
  Exit_flag=True
  while Exit_flag:
    user_choice=main.menu(choice)
    if user_choice == '1':
      main.get_user_credit()
    elif user_choice == '2':
      main.repayment()
    elif user_choice == '3':
      main.enchashment()
    elif user_choice == '4':
      main.change_pwd()
    elif user_choice == '5':
      main.transfer()
    elif user_choice == '6':
      main.billing_query()
    elif user_choice == '7':
      print("該功能正在建設(shè)中,更多精彩,敬請(qǐng)期待!")
    elif user_choice == 'a':
      main.change_user_credit()
    elif user_choice == 'b':
      main.add_user()
    elif user_choice == 'c':
      main.del_user()
    elif user_choice == 'd':
      main.change_pwd()
    elif user_choice == 'q' or user_choice == 'Q':
      print("歡迎再次使用,再見(jiàn)!")
      Exit_flag = False

2、程序功能函數(shù):

#Author by Andy#_*_ coding:utf-8 _*
import json,sys,os,time,shutil
Base_dir=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(Base_dir)
#定義認(rèn)證裝飾器
def auth(func):
  def wrapper(*args,**kwargs):
    # print("請(qǐng)輸入卡號(hào)和密碼進(jìn)行驗(yàn)證!")
    f = open(Base_dir+'\data\\user_db.txt', 'r')
    Log_file = open(Base_dir+'\logs\log.txt', 'a+', encoding='utf-8')
    Bill_log_file = open(Base_dir + '\logs\\bill_log.txt', 'a+', encoding='utf-8')
    func_name = func.__name__
    Time_formate = '%Y-%m-%d %X'
    start_time = time.strftime(Time_formate, time.localtime())
    user_data = json.load(f)
    count=0
    while count < 3:
      global user_id
      global user_pwd
      user_id = input('請(qǐng)輸入您的卡號(hào):')
      user_pwd = input('請(qǐng)輸入您的密碼:')
      if user_id in user_data:
        if user_pwd == user_data[user_id]['Password']:
          Log_file.write(start_time + ' 卡號(hào) %s 認(rèn)證成功!\n' % user_id)
          Log_file.flush()
          time.sleep(1)
          Log_file.close
          keywords = func(*args, **kwargs)
          if func_name == 'repayment' or func_name == 'transfer' or func_name == 'enchashment':
            Bill_log_file.write(start_time + ' 卡號(hào) '+ user_id + ' 發(fā)起 ' + func_name + ' 業(yè)務(wù),金額為: %s \n' % keywords)
            Bill_log_file.flush()
            time.sleep(1)
            Bill_log_file.close
            return keywords
          else:
            return keywords
        else:
          print('卡號(hào)或密碼錯(cuò)誤!請(qǐng)重新輸入!')
          Log_file.write(start_time + ' 卡號(hào) %s 認(rèn)證失敗!\n' % user_id)
          Log_file.flush()
          time.sleep(1)
          Log_file.close
          count +=1
      else:
        print("卡號(hào)不存在,請(qǐng)確認(rèn)!")
      if count == 3:
        print("對(duì)不起,您已輸錯(cuò)3三次,卡號(hào)已鎖定!")
        Log_file.write(start_time + ' 卡號(hào) %s 因連續(xù)三次驗(yàn)證失敗而被鎖定!\n' % user_id)
        time.sleep(1)
        Log_file.close
  return wrapper
#定義菜單函數(shù),根據(jù)不同用戶顯示不通菜單。
def menu(choice):
  if choice == '2':
    print( "請(qǐng)選擇服務(wù)類別:\n"
       "1、查詢信用額度。\n"
       "2、信用卡還款。\n"
       "3、信用卡提現(xiàn)。\n"
       "4、修改口令。\n"
       "5、信用卡轉(zhuǎn)賬。\n"
       "6、信用卡賬單查詢。\n"
       "7、輕松購(gòu)物。\n"
       "8、退出請(qǐng)按q!\n")
    service_items = input('-->')
  elif choice == '1':
    print("請(qǐng)選擇服務(wù)類別:\n"
       "a、修改用戶信用額度。\n"
       "b、新增信用卡用戶。\n"
       "c、刪除信用卡用戶。\n"
       "d、修改用戶口令。\n"
       "e、退出請(qǐng)按q!\n")
    service_items = input('-->')
  else:
    print("感謝使用,祝生活愉快!")
    exit()
  return service_items
# 定義備份用戶數(shù)據(jù)文件函數(shù)
def back_up_file():
  Time_formate = '%Y-%m-%d'
  Sys_time = time.strftime(Time_formate, time.localtime())
  shutil.copy(Base_dir + "\data\\user_db.txt", Base_dir + "\data\\user_db--" + Sys_time + ".bak.txt")
#定義獲取用戶數(shù)據(jù)信息函數(shù)
def get_user_data():
  with open(Base_dir + "\data\\user_db.txt", 'r+',encoding='utf-8') as f:
    user_data = json.load(f)
  return user_data
#定義用戶數(shù)據(jù)變量
user_data = get_user_data()
#定義查詢信用額度函數(shù)
@auth
def get_user_credit():
  user_credit=user_data[user_id]['Credit']
  print("您目前的信用額度為:%s元\n"
     %(user_credit))
  time.sleep(2)
  return user_credit
#定義信用卡還款函數(shù)
@auth
def repayment():
  user_data = get_user_data()
  user_credit=int(user_data[user_id]['Credit'])
  user_balance=int(user_data[user_id]['Balance'])
  user_bill = user_credit - user_balance
  print("您目前需要還款金額為:%s元.\n" %user_bill)
  Exit_flag=True
  while Exit_flag:
    repayment_value=input("請(qǐng)輸入還款金額:")
    if repayment_value.isdigit():
      repayment_value=int(repayment_value)
      user_data[user_id]['Balance'] = user_data[user_id]['Balance'] + repayment_value
      f = open(Base_dir + "\data\\user_db.txt", 'r+', encoding='utf-8')
      json.dump(user_data, f)
      f.close()
      print("恭喜,還款成功!")
      print("您目前需要還款金額為:%s元.\n" % (user_data[user_id]['Credit'] - user_data[user_id]['Balance']))
      time.sleep(1)
      Exit_flag = False
      return repayment_value
    else:
      print("請(qǐng)輸入正確的金額!")
#定義信用卡提現(xiàn)函數(shù)
@auth
def enchashment():
  user_credit=user_data[user_id]['Credit']
  print("你可用的取現(xiàn)額度為:%s" %user_credit)
  Exit_flag=True
  while Exit_flag:
    enchashment_value=input("請(qǐng)輸入您要取現(xiàn)的金額:")
    if enchashment_value.isdigit():
      enchashment_value=int(enchashment_value)
      if enchashment_value % 100 == 0:
        if enchashment_value <= user_credit:
          user_data[user_id]['Balance'] = user_credit - enchashment_value
          f = open(Base_dir + "\data\\user_db.txt", 'r+', encoding='utf-8')
          json.dump(user_data, f)
          f.close()
          print("取現(xiàn)成功,您目前的可用額度為:%s" %user_data[user_id]['Balance'])
          time.sleep(1)
          Exit_flag = False
          return enchashment_value
        else:
          print("您的取現(xiàn)額度必須小于或等于您的信用額度!")
      else:
        print("取現(xiàn)金額必須為100的整數(shù)倍!")
    else:
      print("輸入有誤,取現(xiàn)金額必須為數(shù)字,且為100的整數(shù)倍")
@auth
#定義信用卡轉(zhuǎn)賬函數(shù)
def transfer():
  user_balance=user_data[user_id]['Balance']
  print("您目前的可用額度為:%s" %user_balance)
  Exit_flag=True
  while Exit_flag:
    transfer_user_id = input("請(qǐng)輸入對(duì)方帳號(hào):")
    transfer_value = input("請(qǐng)輸入轉(zhuǎn)賬金額:")
    if transfer_user_id in user_data.keys():
      while Exit_flag:
        if transfer_value.isdigit():
          while Exit_flag:
            transfer_value=int(transfer_value)
            user_passwd=input("請(qǐng)輸入口令以驗(yàn)證身份:")
            if user_passwd == user_data[user_id]['Password']:
              user_balance = user_balance- transfer_value
              user_data[transfer_user_id]['Balance']=int(user_data[transfer_user_id]['Balance']) + transfer_value
              f = open(Base_dir + "\data\\user_db.txt", 'r+', encoding='utf-8')
              json.dump(user_data, f)
              f.close()
              print("轉(zhuǎn)賬成功,您目前的可用額度為:%s" % user_balance)
              time.sleep(1)
              Exit_flag = False
              return transfer_value
            else:
              print("密碼錯(cuò)誤,請(qǐng)重新輸入!")
        else:
          print("轉(zhuǎn)賬金額,必須為數(shù)字,請(qǐng)確認(rèn)!")
    else:
      print("帳號(hào)不存在,請(qǐng)確認(rèn)!")
# @auth
#定義信用卡賬單查詢函數(shù)
@auth
def billing_query():
  print("我們目前僅提供查詢所有賬單功能!")
  print("您的賬單為:\n")
  Bill_log_file = open(Base_dir + '\logs\\bill_log.txt', 'r', encoding='utf-8')
  for lines in Bill_log_file:
    if user_id in lines:
      print(lines.strip())
  print()
  time.sleep(1)
  
#定義修改信用卡額度函數(shù)
def change_user_credit():
  print("您正在修改用戶的信用額度!")
  Exit_flag=True
  while Exit_flag:
    target_user_id=input("請(qǐng)輸入您要修改的用戶卡號(hào):\n")
    if target_user_id in user_data.keys():
      while Exit_flag:
        new_credit=input("請(qǐng)輸入新的信用額度:\n")
        if new_credit.isdigit():
          new_credit= int(new_credit)
          user_data[target_user_id]['Credit']=new_credit
          print("卡號(hào) %s 的新信用額度為:%s " %(target_user_id,new_credit))
          choice = input("確認(rèn)請(qǐng)輸入1或者按任意鍵取消:\n")
          if choice == '1':
            f = open(Base_dir + "\data\\user_db.txt", 'r+', encoding='utf-8')
            json.dump(user_data, f)
            f.close()
            print("信用額度修改成功,新額度已生效!")
            print("卡號(hào) %s 的新信用額度為:%s " % (target_user_id, user_data[target_user_id]['Credit']))
            time.sleep(1)
            Exit_flag = False
          else:
            print("用戶的信用額度未發(fā)生改變!")
        else:
          print("信用額度必須為數(shù)字!請(qǐng)確認(rèn)!")
    else:
      print("卡號(hào)不存在,請(qǐng)確認(rèn)!")
#定義修改口令函數(shù)
@auth
def change_pwd():
  print("注意:正在修改用戶密碼!")
  Exit_flag = True
  while Exit_flag:
    old_pwd = input("請(qǐng)輸入當(dāng)前密碼:")
    if old_pwd == get_user_data()[user_id]["Password"]:
      new_pwd = input("請(qǐng)輸入新密碼:")
      new_ack = input("請(qǐng)?jiān)俅屋斎胄旅艽a:")
      if new_pwd == new_ack:
        user_data=get_user_data()
        user_data[user_id]["Password"]=new_pwd
        f = open(Base_dir + "\data\\user_db.txt", 'r+', encoding='utf-8')
        json.dump(user_data, f)
        f.close()
        print("恭喜,密碼修改成功!")
        time.sleep(1)
        Exit_flag = False
      else:
        print("兩次密碼不一致,請(qǐng)確認(rèn)!")
    else:
      print("您輸入的密碼不正確,請(qǐng)?jiān)诖_認(rèn)!")
#定義新增信用卡函數(shù)
def add_user():
  Exit_flag = True
  while Exit_flag:
    user_id = input("user_id:")
    Balance = input("Balance:")
    Credit = input("Credit:")
    if Balance.isdigit() and Credit.isdigit():
      Balance = int(Balance)
      Credit = int(Credit)
    else:
      print("余額和信用額度必須是數(shù)字!")
      continue
    Name = input("Name:")
    Password = input("Password:")
    print("新增信用卡用戶信息為:\n"
       "User_id:%s\n"
       "Balance:%s\n"
       "Credit:%s\n"
       "Name:%s\n"
       "Password:%s\n"
       %(user_id, Balance, Credit, Name, Password))
    choice = input("提交請(qǐng)按1,取消請(qǐng)按2,退出請(qǐng)按q:")
    if choice == '1':
      back_up_file()
      user_data=get_user_data()
      user_data[user_id] = {"Balance": Balance, "Credit": Credit, "Name": Name, "Password": Password}
      f = open(Base_dir + "\data\\user_db.txt", 'w+', encoding='utf-8')
      json.dump(user_data, f)
      f.close()
      print("新增用戶成功!")
      time.sleep(1)
      Exit_flag=False
    elif choice == '2':
      continue
    elif choice == 'q' or choice == 'Q':
      time.sleep(1)
      Exit_flag = False
    else:
      print('Invaliable Options!')
#定義刪除信用卡函數(shù)
def del_user():
  Exit_flag = True
  while Exit_flag:
    user_id=input("請(qǐng)輸入要?jiǎng)h除的信用卡的卡號(hào):")
    if user_id == 'q' or user_id == 'Q':
      print('歡迎再次使用,再見(jiàn)!')
      time.sleep(1)
      Exit_flag=False
    else:
      user_data=get_user_data()
      print("新增信用卡用戶信息為:\n"
         "User_id:%s\n"
         "Balance:%s\n"
         "Credit:%s\n"
         "Name:%s\n"
         % (user_id, user_data[user_id]['Balance'], user_data[user_id]['Credit'], user_data[user_id]['Name']))
      choice = input("提交請(qǐng)按1,取消請(qǐng)按2,退出請(qǐng)按q:")
      if choice == '1':
        back_up_file()
        user_data.pop(user_id)
        f = open(Base_dir + "\data\\user_db.txt", 'w+',encoding='utf-8')
        json.dump(user_data, f)
        f.close()
        print("刪除用戶成功!")
        time.sleep(1)
        Exit_flag = False
      elif choice == '2':
        continue
      elif choice == 'q' or choice == 'Q':
        print('歡迎再次使用,再見(jiàn)!')
        time.sleep(1)
        Exit_flag = False
      else:
        print('Invaliable Options!')

3、用戶數(shù)據(jù)文件:

{"003": {"Name": "wangwu", "Password": "qazwsx", "Credit": 16000, "Balance": 8000}, "004": {"Name": "zhaoliu", "Password": "edcrfv", "Credit": 18000, "Balance": 6000}, "002": {"Name": "lisi", "Password": "123456", "Credit": 14000, "Balance": 10000}, "009": {"Password": "qwerty", "Name": "hanmeimei", "Credit": 15000, "Balance": 15000}, "005": {"Name": "fengqi", "Password": "1234qwer", "Credit": 15000, "Balance": 10700}, "010": {"Name": "lilei", "Password": "qaswed", "Credit": 50000, "Balance": 50000}, "008": {"Name": "zhengshi", "Password": "123456", "Credit": 12345, "Balance": 12345}, "006": {"Name": "zhouba", "Password": "123456", "Credit": 20000, "Balance": 8300}, "001": {"Name": "zhangsan", "Password": "abcd1234", "Credit": 12000, "Balance": 12000}, "007": {"Name": "wujiu", "Password": "123456", "Credit": 20000, "Balance": 11243}}

4、相關(guān)日志內(nèi)容:

登錄日志:

2016-12-20  22:12:18 卡號(hào) 005 認(rèn)證成功!
2016-12-20  22:14:20 卡號(hào) 005 認(rèn)證成功!
2016-12-20  22:17:26 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:18:06 卡號(hào) 005 認(rèn)證成功!
2016-12-20  22:18:06 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:21:10 卡號(hào) 005 認(rèn)證失敗!
2016-12-20  22:21:10 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:23:17 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:25:33 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:26:14 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:32:15 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:44:57 卡號(hào) 005 認(rèn)證成功!
2016-12-20  22:45:50 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:47:10 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:48:27 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:49:30 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:52:13 卡號(hào) 006 認(rèn)證成功!
2016-12-20  22:53:44 卡號(hào) 006 認(rèn)證成功!

交易日志:

2016-12-20  21:25:35 卡號(hào) 006 發(fā)起 repayment 業(yè)務(wù),金額為: 100
2016-12-20  21:27:01 卡號(hào) 005 發(fā)起 repayment 業(yè)務(wù),金額為: 100 
2016-12-20  22:14:20 卡號(hào) 005 發(fā)起 repayment 業(yè)務(wù),金額為: 100 
2016-12-20  22:17:26 卡號(hào) 006 發(fā)起 transfer 業(yè)務(wù),金額為: 300

更多關(guān)于使用Python3 編寫(xiě)簡(jiǎn)單信用卡管理程序法的對(duì)比請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!

Notes de version

Entrées populaires