題目為信用卡消費管理系統:
主程序:main.py
#!usr/bin/env python # encoding: utf-8 import conf,sys,time,re,os import json import login tmp_dic = {} def shopping_center(arg): print('\033[32mWelcome into Credit card store!\033[0m') while True: for i,(k,v) in enumerate(conf.shop_list.items(),1): print('%s.%s:%s'%(i,k,v)) N = int(input('Please select you want to buy goods(input r:return p:pay):').strip()) if N == 7: print('\033[31myou will return main list!\033[0m') time.sleep(1) tmp_dic.clear()#返回后如果沒有支付則清空購物車 break elif N in range(1,len(conf.shop_list)-1): key = list(conf.shop_list.items())[N - 1][0] value = list(conf.shop_list.items())[N - 1][1] if key in tmp_dic.keys(): tmp_dic[key] = tmp_dic[key] + value else: tmp_dic[key] = value print('\033[35mYour shopping cart is as follows:\033[0m') for x,y in tmp_dic.items(): print('\033[35m%-5s:%s\033[0m'%(x,y)) elif N == 8: print('\033[33mThe total order for you:%s\033[0m'%sum(tmp_dic.values())) sum_values = sum(tmp_dic.values()) with open('account.json','r') as f: acc = json.load(f) if sum_values > acc[arg][2]: print('對不起你的信用卡額度不足,請重新購買!!') login.log_record(arg, '消費', sum_values, 'fail') tmp_dic.clear() break else: acc[arg][2] = acc[arg][2] - sum_values acc[arg][3] = acc[arg][3] + sum_values with open('account.json','w') as z: json.dump(acc,z) login.log_record(arg,'消費',sum_values,'success') print('恭喜你購物成功即將返回主菜單!!') time.sleep(1) tmp_dic.clear() break #信用卡管理 def card_center(name): if name == 'admin': print('歡迎%s進入信用卡管理系統'%name) while True: for index,i in enumerate(conf.ad_list,1): print('%s.%s' %(index,i)) p = int(input('請選擇你要的操作的編號:').strip()) with open('account.json', 'r+') as f: acc = json.load(f) if p == 1:#添加用戶 key = input('請輸入你的用戶名:').strip() pawd = input('請輸入密碼:').strip() paws = input('請輸入支付密碼:').strip() acc[key] = [pawd, paws, 15000,0, "unlock"] with open('account.json', 'w') as x: json.dump(acc,x) print('用戶%s已經添加成功額度為15000'%key) elif p == 2:#用戶額度調整 n = input('你的賬號:') limit = int(input('請輸入你要的額度:').strip()) if n in acc.keys(): acc[n][2] = limit with open('account.json','w') as y: json.dump(acc,y) continue elif p == 3:#凍結賬戶 freeze = input('請輸入你要凍結的賬戶:').strip() acc[freeze][3] = "lock" with open('account.json', 'w') as z: json.dump(acc, z) print('賬號%s已凍結'%freeze) continue elif p == 4:#解凍賬戶 unfreeze = input('請輸入你要解凍的賬戶:') acc[unfreeze][3] = "unlock" with open('account.json','w') as g: json.dump(acc,g) print('賬戶%s已解凍'%unfreeze) continue elif p == 5:#用戶操作查詢 pass continue elif p == 6:#返回主菜單 break else:#消費者信用卡管理系統 print('\033[34m歡迎進入消費者信用卡管理系統!\033[0m') with open('account.json', 'r+') as e: acc1 = json.load(e) while True: for l,i in enumerate(conf.customer_list,1): print('%s.%s'%(l,i)) custom_in = int(input('請選擇你需要的操作:').strip()) if custom_in == 1: print('#' * 20) print('賬戶:%s\n額度:%s\n待還款:%s' %(name,acc1[name][2],acc1[name][3])) print('#' * 20) continue elif custom_in == 2: print('你的還款額:%s'%acc1[name][3]) huank = int(input('你要還多少:').strip()) acc1[name][2] = acc1[name][2] + huank acc1[name][3] = acc1[name][3] - huank with open('account.json', 'w') as g: json.dump(acc1, g) print('恭喜你還款成功!') continue elif custom_in == 3: qukuan = int(input('請輸入你要取款的金額:').strip()) acc1[name][2] = acc1[name][2] - qukuan acc1[name][3] = acc1[name][3] + qukuan with open('account.json', 'w') as h: json.dump(acc1, h) continue elif custom_in == 4: print('你的消費記錄如下') ff = open('record.log','r') for line in ff: if name in line: print(line) ff.close() print('-'*20) continue elif custom_in == 5: while True: pa = input('請輸入你要轉賬的賬戶:').strip() pc = int(input('請輸入你要轉賬的金額:').strip()) if pa not in acc1.keys(): print('你輸入的賬戶不存在,請重新輸入!') continue elif pc > acc1[name][2]: print('你的轉賬額度不夠,請重新輸入!') continue else: break acc1[pa][3] = acc1[pa][3] - pc acc1[pa][2] = acc1[pa][2] + pc acc1[name][2] = acc1[name][2] - pc acc1[name][3] = acc1[name][3] + pc with open('account.json','w') as dd: json.dump(acc1,dd) print('恭喜你轉賬成功!') continue elif custom_in == 6: break else: print('你選擇的編號不對,請重新輸入!!') continue if __name__ == "__main__": while True: print('\033[32mWelcome into credit card store!!!\033[0m') for index,i in enumerate(conf.main_list,1): print('%s.%s'%(index,i)) Num = input('Please choose you to enter the system(input b exit):').strip() if Num == 'b': print(1) sys.exit() elif int(Num) == 1: name1 = login.login() card_center(name1) continue elif int(Num) == 2: name = login.login() shopping_center(name) continue
配置文件:conf.py
#!usr/bin/env python # encoding: utf-8 main_list = ['Credit card supervise','Credit card store'] shop_list = {'Car':100000, 'Iphone':5000, 'Mac Book':8000, 'pan':10, 'T-shirt':100, 'coffee':30, 'return':'返回主菜單', 'pay':'買單', } ad_list = ['添加用戶','用戶額度調整','凍結賬戶','解凍賬戶','用戶操作查詢','返回上一級'] customer_list = ['賬戶查詢','還款','提現','消費查詢','轉賬','返回']
登錄系統和日志系統:login.py
#!usr/bin/env python # encoding: utf-8 import json,sys,time # account = {'wyh':['a123',123,15000,'unlock'], # 'bat':['b321',456,15000,'unlock'], # 'admin':['admin',0,0,'unlock']} # with open('account.json','w') as d: # json.dump(account,d) logfile = 'record.log' def login(): with open('account.json', 'r') as f: acc = json.load(f) i = 0 while True: name = input('請輸入賬號:') password = input('請輸入密碼:') if acc[name][3] == 'lock': print('你的賬戶被鎖定請聯系你的客戶經理!!!') sys.exit() elif name not in acc.keys() or password != acc[name][0] : print('你的賬戶信息請重新輸入!!!') i += 1 if i > 2: print('你的賬戶被鎖定請聯系你的客戶經理確認你的賬戶安全!!') acc[name][3] = 'lock' with open('account.json', 'w') as f1: json.dump(acc, f1) sys.exit() continue else: print('歡迎%s'%name) return name def log_record(name,Status,money,description='Null'): date = time.strftime("%Y%m%d %H:%M:%S", time.localtime()) record_line = "%s %s %s %s %s\n" % (date, name, Status,money, description) f = open(logfile, 'a') f.write(record_line) f.flush() f.close()
賬號文件:account.json
{"wyh": ["a123", 123, 14870, 100, "unlock"], "bat": ["b321", 456, 10070, 4930, "unlock"], "admin": ["admin", 0, 0, 0, "unlock"]}
浙公網安備 33010602011771號