Python3 implements simple credit card management procedures and python3 credit card

Source: Internet
Author: User

Python3 implements simple credit card management procedures and python3 credit card

1. Program Execution Code:

# Author by Andy # _ * _ coding: UTF-8 _ * _ import OS, sys, timeBase_dir = OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file _) sys. path. append (Base_dir) str = "welcome to the bank credit card self-service system! \ N "for I in str: sys. stdout. write (I) sys. stdout. flush () time. sleep (0.3) while True: print ("1. Administrator portal. ") Time. sleep (0.3) print (" 2. User Logon portal. ") Print (" 3. to exit, press 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 ("This function Construction in progress. More highlights coming soon! ") 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 ("welcome to use it again. Goodbye! ") Exit_flag = False

2. program functions:

# Author by Andy # _ * _ coding: UTF-8 _ * import json, sys, OS, time, shutilBase_dir = OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file _) sys. path. append (Base_dir) # define the authenticator def auth (func): def wrapper (* args, ** kwargs): # print ("Enter the card number and password for verification! ") 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 = Indium Ut ('enter your card number: ') user_pwd = input (' enter your password: ') if user_id in user_data: if user_pwd = user_data [user_id] ['Password']: Log_file.write (start_time + 'Card Number % s authentication successful! \ 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 = 'enableshment ': bill_log_file.write (start_time + 'Card number '+ user_id +' '+ func_name +' business, amount: % s \ n' % keywords) Bill_log_file.flush () time. sleep (1) Bill_log_file.close return keywords else: print ('Card number or Password error! Enter again! ') Log_file.write (start_time +' Card Number % s authentication failed! \ N' % user_id) Log_file.flush () time. sleep (1) Log_file.close count + = 1 else: print ("the card number does not exist. Please confirm! ") If count = 3: print (" Sorry, you have entered the error three times and the card number is locked! ") Log_file.write (start_time + 'Card Number % s is locked because the verification fails three times in a row! \ N' % user_id) time. sleep (1) Log_file.close return wrapper # define the menu function. Different users cannot access the menu. Def menu (choice): if choice = '2': print ("Select Service category: \ n" "1. query the credit limit. \ N "2. Credit card repayment. \ N "" 3. Credit card withdrawal. \ N "" 4. Change the password. \ N "" 5. Credit card transfers. \ N "" 6. query credit card bills. \ N "" 7. easy shopping. \ N "" 8. to exit, press q! \ N ") service_items = input ('-->') elif choice = '1': print (" Select Service category: \ n "" a. Modify user credit limit. \ N "" B. new credit card users. \ N "" c. delete a credit card user. \ N "" d. Modify the user password. \ N "" e, exit, please press q! \ N ") service_items = input ('-->') else: print (" thanks for your use. Have a good life! ") Exit () return service_items # define the backup user data file function 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") # define the function for Obtaining user data information 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 # define user data changes User_data = get_user_data () # define the query Credit Limit Function @ authdef get_user_credit (): user_credit = user_data [user_id] ['credentials'] print ("your current Credit limit is: % s RMB \ n "% (user_credit) time. sleep (2) return user_credit # define the Credit card repayment function @ authdef repayment (): user_data = get_user_data () user_credit = int (user_data [user_id] ['credentials']) user_balance = int (user_data [user_id] ['balance ']) user_bill = user_credit-user_balance print ("the amount you need to pay now is: % s. \ n "% User_bill) Exit_flag = True while Exit_flag: repayment_value = input ("Enter the repayment amount:") 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 ("congratulations, payment successful! ") Print (" the current repayment amount is: % s. \ n "% (user_data [user_id] ['credentials']-user_data [user_id] ['balance ']) time. sleep (1) Exit_flag = False return repayment_value else: print ("enter the correct amount! ") # Define the Credit card withdrawal function @ authdef enchashment (): user_credit = user_data [user_id] ['credentials'] print (" Your available Credit limit is % s "% user_credit) exit_flag = True while Exit_flag: enchashment_value = input ("Enter the amount you want to withdraw:") if then (): 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 (Ba Se_dir + "\ data \ user_db.txt", 'r + ', encoding = 'utf-8') json. dump (user_data, f) f. close () print ("obtained successfully. Your current available quota is: % s" % user_data [user_id] ['balance ']) time. sleep (1) Exit_flag = False return enchashment_value else: print ("your credit limit must be less than or equal to your credit limit! ") Else: print (" the withdrawal amount must be an integer multiple of 100! ") Else: print (" incorrect input. The withdrawal amount must be a number and an integer multiple of 100 ") @ auth # define the credit card transfer function def transfer (): user_balance = user_data [user_id] ['balance '] print ("your current available quota is: % s" % user_balance) Exit_flag = True while Exit_flag: transfer_user_id = input ("Enter the recipient's account:") transfer_value = input ("Enter the transfer amount:") if transfer_user_id in user_data.keys (): while Exit_flag: if transfer_value.isdigit (): while Exit_flag: transfer_value = int (transfer_value) user_p Asswd = input ("enter the Password for authentication:") 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 ("transfer successful, your current available quota is: % s" % user_balance) time. sleep (1) Exit_flag = False retur N transfer_value else: print ("the password is incorrect. Please enter it again! ") Else: print (" transfer amount, which must be a number. Please confirm! ") Else: print (" the account does not exist. Please confirm! ") # @ Auth # define the credit card bill query function @ authdef billing_query (): print (" currently, only all bills can be queried! ") Print (" your bill is \ 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) # define the function of modifying the credit card quota def change_user_credit (): print ("You are modifying your credit limit! ") Exit_flag = True while Exit_flag: target_user_id = input (" Enter the user ID you want to modify: \ n ") if target_user_id in user_data.keys (): while Exit_flag: new_credit = input ("enter a new credit limit: \ n") if new_credit.isdigit (): new_credit = int (new_credit) user_data [target_user_id] ['credentials'] = new_credit print ("the new Credit line for the card number % s is % s" % (target_user_id, new_credit )) choice = input ("Confirm enter 1 or press any key to cancel: \ n") if choice = '1': f = open (Base_dir + "\ data \ \ User_db.txt ", 'r + ', encoding = 'utf-8') json. dump (user_data, f) f. close () print ("the credit line has been modified successfully, and the new credit line has taken effect! ") Print (" the new Credit line for the card number % s is: % s "% (target_user_id, user_data [target_user_id] ['credentials']) time. sleep (1) Exit_flag = False else: print ("your credit limit has not changed! ") Else: print (" the credit limit must be a number! Please confirm! ") Else: print (" the card number does not exist. Please confirm! ") # Define the password modification function @ authdef change_pwd (): print (" NOTE: Changing the user password! ") Exit_flag = True while Exit_flag: old_pwd = input (" Enter the current Password: ") if old_pwd = get_user_data () [user_id] [" Password "]: new_pwd = input ("Enter the new password:") new_ack = input ("Enter the new password again:") 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 ("congratulations, password changed! ") Time. sleep (1) Exit_flag = False else: print (" the two passwords are inconsistent. Please confirm! ") Else: print (" the password you entered is incorrect. Please confirm it! ") # Define the new credit card function 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 ("the Balance and Credit limit must be numbers! ") Continue Name = input (" Name: ") Password = input (" Password: ") print (" added credit card user information: \ 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 (" submit press 1, cancel press 2, exit press 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 = op En (Base_dir + "\ data \ user_db.txt", 'W + ', encoding = 'utf-8') json. dump (user_data, f) f. close () print ("added user successfully! ") Time. sleep (1) Exit_flag = False elif choice = '2': continue elif choice = 'q' or choice = 'q': time. sleep (1) Exit_flag = False else: print ('valiable Options! ') # Define the function of deleting a credit card def del_user (): Exit_flag = True while Exit_flag: user_id = input ("Enter the ID of the credit card to be deleted :") if user_id = 'q' or user_id = 'q': print ('Welcome to use it again, goodbye! ') Time. sleep (1) Exit_flag = False else: user_data = get_user_data () print ("added credit card user information: \ 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] ['credentials'], user_data [user_id] ['name']) choice = input ("Press 1 for submission, press 2 for cancellation, and press q:" For logout :") 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 ("User deleted! ") Time. sleep (1) Exit_flag = False elif choice = '2': continue elif choice = 'q' or choice = 'q': print ('Welcome to use it again, goodbye! ') Time. sleep (1) Exit_flag = False else: print ('invaliable Options! ')

3. User data files:

{"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. Related log Content:

Logon log:

22:12:18, card No. 005 certification successful!
22:14:20, card No. 005 certification successful!
22:17:26 card No. 006 certification successful!
22:18:06, card No. 005 certification successful!
22:18:06 card No. 006 certification successful!
22:21:10 certification failed on card No. 005!
22:21:10 card No. 006 certification successful!
22:23:17 card No. 006 certification successful!
22:25:33 card No. 006 certification successful!
22:26:14 card No. 006 certification successful!
22:32:15 card No. 006 certification successful!
22:44:57, card No. 005 certification successful!
22:45:50 card No. 006 certification successful!
22:47:10 card No. 006 certification successful!
22:48:27 card No. 006 certification successful!
22:49:30 card No. 006 certification successful!
22:52:13 card No. 006 certification successful!
22:53:44 card No. 006 certification successful!

Transaction log:

21:25:35 on the card No. 006 initiated the repayment business, amounting to: 100
21:27:01 on the card No. 005 initiated the repayment business, amounting to: 100
22:14:20 on the card No. 005 initiated the repayment business, amounting to: 100
22:17:26 on the card number 006 initiated the transfer business, amounting to: 300

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.