標籤:python atm類比 信用卡類比
包含啟動程式start.py和atm.py模組使用者資料通過字典序列化存入userdata.pkl使用者操作記錄檔案userid.record(為每個使用者儲存一個記錄檔案)商品價格檔案goods_list功能包含:取現 存錢 轉賬 購物 列印清單其他說明:沒有包含管理端程式(用於添加使用者帳號,商品上下架),密碼驗證使用者可以自己開發。start.py如下:#!/usr/bin/python# -*- coding: utf-8 -*- import sysimport getpassimport picklefrom atm import *#擷取使用者資料f1=file(‘userdata.pkl‘,‘rb‘)userinfo=pickle.load(f1)f1.close()#主程式while True: userid = str(raw_input("\033[1;32;40myour id:\033[0m")) if userinfo.has_key(userid): while True: password = int(getpass.getpass(‘\033[1;32;40mEnter password:\033[0m‘)) if password != userinfo[userid][‘passwd‘]: print "\033[1;32;40mThe password is incorrect\033[0m" continue else: print "\033[1;31;40mwenlcome %s.\033[0m" % userinfo[userid][‘name‘] show_menu() while True: input=int(raw_input(‘請選擇:‘)) if input == 1: money = int(userinfo[userid][‘youhave‘]) show_balance(money) elif input == 2: withdraw(userid,**userinfo) elif input == 3: save_money(userid,**userinfo) elif input == 4: transfer(userid,**userinfo) elif input == 5: shopping(userid,**userinfo) elif input == 6: history_list(userid) elif input == 7: sys.exit(0) else : print ‘輸入錯誤‘ else: print "\033[1;31;40mThe userid is not vaild, please re-input:\033[0m" continueatm.py如下:#!/usr/bin/python# -*- coding: utf-8 -*- import sysimport pickleimport reimport timeimport datetime#列印菜單def show_menu(): print ‘1:Balance inquiry‘ print ‘2:withdraw money‘ print ‘3:save money‘ print ‘4:transfer‘ print ‘5:shopping‘ print ‘6:history list‘ print ‘7:退出‘#顯示餘額def show_balance(money): print ‘賬戶餘額:%d‘ % money#取現 def withdraw(uid,**uinfo): draw=int(raw_input(‘請輸入取現金額:‘)) draw_str=‘-%d‘ % draw if uinfo[uid][‘youhave‘] < draw: print ‘餘額不足‘ else: uinfo[uid][‘youhave‘] -= draw rest=uinfo[uid][‘youhave‘] with file(‘userdata.pkl‘,‘wb‘) as f1: pickle.dump(uinfo,f1) write_oper(uid,‘withdraw‘,draw_str,rest)#存錢def save_money(uid,**uinfo): save = int(raw_input(‘請輸入存款金額:‘)) save_str=‘+%d‘ % save uinfo[uid][‘youhave‘] += save rest=uinfo[uid][‘youhave‘] write_oper(uid,‘savemoney‘,save_str,rest)#轉賬def transfer(uid,**uinfo): tran_id = raw_input(‘請輸入行內帳號:‘) if uinfo.has_key(tran_id): tran_money = int(raw_input(‘請輸入轉賬金額:‘)) uinfo[uid][‘youhave‘] -= tran_money uinfo[tran_id][‘youhave‘] += tran_money tran_money_str=‘-%d‘ % tran_money rest=uinfo[uid][‘youhave‘] write_oper(uid,‘transfer‘,tran_money_str,rest)#記錄操作def write_oper(uid,o1,m1,r1): t1=time.strftime(‘%Y%m%d %H:%M:%S‘,time.localtime(time.time())) with file(‘%s.record‘ % uid,‘a‘) as f1: f1.write(‘%s %s %s therest:%d\n‘ % (t1,o1,m1,r1))#購物 def shopping(uid,**uinfo):"atm.py" 118L, 3853C 48,7 頂端#購物 def shopping(uid,**uinfo): goods_dict={} goods = file(‘goods_list‘,‘r+‘) print ‘商品名稱 價格‘ #列印可購買商品清單 for line in goods.readlines(): print line, key = line.split()[0] value = int(line.split()[1]) goods_dict[key] = value #購物車購物 ok=‘yes‘ sh_total=0 while ok==‘yes‘: sh_cart=[] sh_choose=raw_input(‘選擇商品:‘) if goods_dict.has_key(sh_choose): sh_cart.append(sh_choose) sh_total+=goods_dict[sh_choose] ok = raw_input(‘是否繼續 yes/no?:‘) else :print "無此商品,請重新輸入" print ‘您選擇商品:%s 總計:%d‘ % (sh_cart,sh_total) if uinfo[uid][‘youhave‘] < sh_total: print ‘餘額不足‘ else: uinfo[uid][‘youhave‘] -= sh_total with file(‘userdata.pkl‘,‘wb‘) as f1: pickle.dump(uinfo,f1) sh_total_str=‘-%d‘ % sh_total rest=uinfo[uid][‘youhave‘] write_oper(uid,‘shopping‘,sh_total_str,rest) goods.close()#查詢曆史清單def seach_list(uid,start_date,end_date): f1=file(‘%s.record‘ % uid) for line in f1.readlines(): cu_date=int(line.split()[0]) if cu_date<=end_date and cu_date>=start_date: print line,#查詢曆史清單主函數def history_list(uid): print ‘1:查詢最近一個月‘ print ‘2:查詢最近3個月‘ print ‘3:選擇時間‘ input=int(raw_input(‘請選取查詢時間:‘)) sys_time=int(time.strftime(‘%Y%m%d‘,time.localtime(time.time()))) now_time=datetime.datetime.now() thr_mon_ago_time=now_time-datetime.timedelta(days=90) one_mon_ago_time=now_time-datetime.timedelta(days=30) one_mon_ago=int(one_mon_ago_time.strftime(‘%Y%m%d‘)) thr_mon_ago=int(thr_mon_ago_time.strftime(‘%Y%m%d‘)) if input == 1: seach_list(uid,one_mon_ago,sys_time) elif input == 2: seach_list(uid,thr_mon_ago,sys_time) 55,1 87% elif input == 2: seach_list(uid,thr_mon_ago,sys_time) elif input == 3: start_d=int(raw_input(‘輸入開始日期(格式:20150301):‘)) end_d=int(raw_input(‘輸入結束日期(格式:20150301):‘)) seach_list(uid,start_d,end_d) else: print ‘input error‘ 使用者資料通過字典序列化存入userdata.pkluserinfo={‘10001‘:{‘passwd‘:123456,‘name‘:‘li‘,‘youhave‘:10000}, ‘10002‘:{‘passwd‘:234567,‘name‘:‘skipper‘,‘youhave‘:20000}} 使用者操作記錄檔案10001.record20150401 18:03:46 withdraw -221 therest:603420150401 18:07:39 savemoney +1001 therest:703520150401 18:18:09 transfer -123 therest:591120150401 18:18:27 shopping -199 therest:5712 商品價格檔案goods_listCar 250000Clothes 399Shoes 199Iphone5s 4999Coffee 35Foods 68Bicycle 1688參考資訊:http://www.linuxidc.com/Linux/2014-09/107274.htm
本文出自 “我博故我在” 部落格,請務必保留此出處http://encoding.blog.51cto.com/1403836/1627430
python小程式 atm類比