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, administrative Personnel entrance. ") time.sleep (0.3) print (" 2, user Login Entry. ") Print (" 3, exit please 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 ("the feature is under construction, more exciting, please look forward!" ") 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 again, Goodbye!") ") Exit_flag = False
2, program function function:
#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) #定义认证装饰器def auth (func): def wrapper (*args,**kwargs): # Print ("please Enter the card number and password to Verify!") ") f = Open (base_dir+ ' \data\\user_db.txt ', ' r ') log_file = Open (base_dir+ ' \logs\log.txt ', ' A + ', encoding= ' UT F-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 (' Please enter your card number: ') user_pwd = input (' Please 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 certified successfully!\n '% User_id) Log_file.Flush () time.sleep (1) log_file.close keywords = func (*args, **kwa Rgs) if Func_name = = ' repayment ' or func_name = = ' transfer ' or func_name = = ' Enchashment ': Bill_log_file.write (start_time + ' card number ' + user_id + ' initiate ' + Func_name + ' business, amount:%s \ n '% keywords) Bill_log_file.flush () time.sleep (1) bill_log_file.close return keywords Else:return keywords else: Print (' card number or password error! Please re-enter! ') 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:pr int ("the card number does not exist, please Confirm!") if count = = 3:print ("sorry, You have lost 33 times, the card number is Locked!") ") Log_file.Write (start_time + ' card #%s is locked for three consecutive failed validations!) \ n '% user_id) time.sleep (1) log_file.close return wrapper# defines the menu function, which displays the Non-pass menu according to different users. def menu (choice): if choice = = ' 2 ': print ("please Select service category: \ n" "1, Check the credit limit.) \ n "" 2, credit card Repayment. \ n "" 3, credit card Withdrawal. \ n "" 4, change Password. \ n "" 5, credit card Transfer. \ n "" 6, credit card billing Enquiry. \ n "" 7, Easy Shopping. \ n "" 8, exit please press q!\n ") service_items = input ('--') elif Choice = = ' 1 ': print (" Please select service category: \ n ") "a, Modify the user credit limit." \ n "" b, add credit card Users. \ n "" c, Delete the credit card user. \ n "" d, Modify the user Password. \ n "" e, exit please press q!\n ") service_items = input ('---') else:print (" Thanks for using, wish Life happy! ") Exit () return service_items# defines 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--" + S) Ys_time + ". Bak.txt") #定义获取用User data information function 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 variables User_data = get_user_data () #定义查询信用额度函数 @authdef get_user_credit (): user_credit= user_data[user_id][' Credit ' Print ("your current limit is:%s yuan \ n"% (user_credit)) time.sleep (2) return user_credit# definition Credit card repayment function @authdef repayment (): user_data = get_user_data () user_credit=int (user_data[user_id][' credits ') User_balan Ce=int (user_data[user_id][' Balance ')) user_bill = user_credit-user_balance Print ("you currently need the repayment amount:%s yuan. \ N"%user_bill) Exit_flag=true while Exit_flag:repayment_value=input ("please enter repayment Amount:") if Repayment_value.isdigit (): Repayment_value=int (repayment_value) user_data[user_id][' Balance '] = user_data[user_id][' Balance '] + Repaym Ent_value f = Open (base_dir + "\data\\user_db.txt", ' r+ ', encoding= ' utf-8 ') json.dump (user_data, F) F.close () Print ("congratulations, The repayment is Successful!") ") Print (" You currently need to repay the amount:%s yuan. \ n "% (user_data[user_id][' credit ')-user_data[user_id][' Balance '])) Time. Sleep (1) Exit_flag = False return repayment_value else:print ("please Enter the correct Amount!") ") #定义信用卡提现函数 @authdef enchashment (): user_credit=user_data[user_id][' Credit '] print (" your available withdrawal quota is:%s "%user_credit) E Xit_flag=true while Exit_flag:enchashment_value=input ("please Enter the amount you want to cash In:") if Enchashment_value.isdigit (): Enchashment_value=int (enchashment_value) If enchashment_value% = = 0:if Enchashmen T_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 ("successful, your current available quota is:%s"%user_data[user_id][' Balance ']) Tim E.sleep (1) Exit_flag = False return enchashment_value else:p Rint ("your Withdrawal amount must be less than or equal to your credit Limit!") ") Else:print (" The cash amount must be an integral multiple of 100!) ") Else:print (" The input is incorrect, the cash amount must be a number, and an integer multiple of 100 ") @auth # define 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 = Inpu T ("please enter the offset Account:") transfer_value = input ("please Enter the transfer Amount:") if transfer_user_id in User_data.keys (): while E Xit_flag:if Transfer_value.isdigit (): while Exit_flag:transfer_ Value=int (transfer_value) user_passwd=input ("please Enter password to verify Identity:") 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+ ', E Ncoding= ' utf-8 ') json.dump (user_data, f) f.close () Print ("the transfer is successful, your current quota is:%s"% user_balance) time.sleep (1) Ex It_flag = False return transfer_value else:p Rint ("password is wrong, please Re-enter!") ") else:print (" The transfer amount 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 ("we currently only offer to query all billing Features!") Print ("your Bill is: \ N") bill_log_file = open (base_dir + ' \logs\\bill_log.txt ', ' r ', encoding= ' Utf-8 ') for lines in B Ill_log_file:if user_id in Lines:print (lines.strip ()) print () time.sleep (1) #定义修改信用卡额度函数def Chang E_user_credit (): print ("you are modifying the User's credit Limit!") ") Exit_flag=true while Exit_flag:target_user_id=input ("please Enter the user card number you want to modify: \ N") if target_user_id in User_data.keys (): while Exit_flag:new_credit=input ("please Enter a new credit limit: \ N") if New_credit.isdigit (): new_ credit= int (new_credit) user_data[target_user_id][' credit ']=new_credit print ("card number%s The new credit limit is:%s "% (target_user_id,new_credit)) choice = input (" confirm Please enter 1 or press any key to cancel: \ n ") If Ch Oice = = ' 1 ': f = open (base_dir + "\data\\user_db.txt", ' r+ ', encoding= ' Utf-8 ') Json.dump (user_data, f) f.close () print ("the Credit limit has been successfully modified and the new limit has been Applied!") ") print (the New credit limit for card number%s is:%s"% (target_user_id, user_data[target_user_id][' credits ')) Time.sleep (1) exit_flag = False else:print ("user The 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! ") ") #定义修改口令函数 @authdef change_pwd (): print (" note: The user password is being modified! ") Exit_flag = True while exit_flag:old_pwd = input (" Please enter current password: ") if old_pwd = = Get_user_data () [user_id ["Password"]: new_pwd = input ("please Enter a new Password:") New_ack = input ("please Enter new password Again:") if new_pwd = = New_ Ack:user_data=get_user_data () user_data[user_id]["Password"]=new_pwd f = ope N (base_dir + "\data\\user_db.txt", ' r+ ', encoding= ' utf-8 ') json.dump (user_data, f) f.close () Print ("congratulations, Password modification Succeeded!") ") time.sleep (1) exit_flag = False Else:print (" two times the password is inconsistent, please confirm! ") ") else:print (" The password you entered is not correct, please confirm it! ") ") #定义新增信用卡函数def add_user (): exit_flag = True while exit_flag:user_id = input (" user_id: ") Balance = in Put ("Balance:") credits = input ("credit:") IF balance.isdigit () and credit.isdigit (): Balance = int (Balance) credits = int (credit) else: Print ("the balance and credit limit must be Numbers!") ") Continue Name = input (" name: ") Password = input (" Password: ") print (" new credit card user information is: \ n ") "user_id:%s\n" "balance:%s\n" "credit:%s\n" "name:%s\n" "Pass word:%s\n "% (user_id, Balance, credit, Name, PASSWORD)) choice = input (" Submit Press 1, cancel Please press 2, exit please 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 = open (base_dir + "\data\\user_db.txt", ' w+ ', Encoding= ' utf-8 ') json.dump (user_data, f) f.close () print ("new user success! ") 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! ') #定义删除信用卡函数def del_user (): exit_flag = True while exit_flag:user_id=input ("enter the card number of the credit card you want to Delete:") if user_id = = ' Q ' or user_id = = ' Q ': print (' Welcome to use again, bye! ') 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][' Credit '], User_data[user _id][' Name '])) choice = input ("submit Press 1, cancel please press 2, exit please press 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 ("delete user succeeded! ") Time.sleep (1) Exit_flag = False elif Choice = = ' 2 ': continue E Lif choice = = ' Q ' or choice = = ' Q ': print (' Welcome to use again, bye! ') 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", "credits": 14000, "Balance": 10 "009": {"Password": "qwerty", "name": "hanmeimei", "credit": 15000, "Balance": 15000}, "005": {"Name": "fengqi", "Pa ssWOrd ":" 1234qwer "," credit ": 15000," Balance ": 10700}," 010 ": {" Name ":" lilei "," Password ":" qaswed "," credits ": 50000," Balance ": 50000}," 008 ": {" name ":" zhengshi "," Password ":" 123456 "," credit ": 12345," Balance ": 12345}," 006 ": {" name ":" Z Houba "," Password ":" 123456 "," credit ": 20000," Balance ": 8300}," 001 ": {" Name ":" zhangsan "," Password ":" abcd1234 "," Cred It ": 12000," Balance ": 12000}," 007 ": {" Name ":" wujiu "," Password ":" 123456 "," credit ": 20000," Balance ": 11243}}
4. Related Log content:
Log in Log:
2016-12-20 22:12:18 Card number 005 Certification success!
2016-12-20 22:14:20 Card number 005 Certification success!
2016-12-20 22:17:26 Card number 006 Certification success!
2016-12-20 22:18:06 Card number 005 Certification success!
2016-12-20 22:18:06 Card number 006 Certification success!
2016-12-20 22:21:10 Card number 005 Certification failed!
2016-12-20 22:21:10 Card number 006 Certification success!
2016-12-20 22:23:17 Card number 006 Certification success!
2016-12-20 22:25:33 Card number 006 Certification success!
2016-12-20 22:26:14 Card number 006 Certification success!
2016-12-20 22:32:15 Card number 006 Certification success!
2016-12-20 22:44:57 Card number 005 Certification success!
2016-12-20 22:45:50 Card number 006 Certification success!
2016-12-20 22:47:10 Card number 006 Certification success!
2016-12-20 22:48:27 Card number 006 Certification success!
2016-12-20 22:49:30 Card number 006 Certification success!
2016-12-20 22:52:13 Card number 006 Certification success!
2016-12-20 22:53:44 Card number 006 Certification success!
Transaction log:
2016-12-20 21:25:35 card number 006 launched repayment business, the amount is: 100
2016-12-20
2016-12-20
2016-12-20
Python3 Implementing a simple credit card management program