包含启动程序start.py和atm.py模块 用户数据通过字典序列化存入userdata.pkl 用户操作记录文件userid.record(为每个用户保存一个记录文件) 商品价格文件goods_list 功能包含:取现 存钱 转账 购物 打印清单 其他说明:没有包含管理端程序(用于添加用户账号,商品上下架),密码验证用户可以自己开发。 start.py如下: #!/usr/bin/python # -*- coding: utf-8 -*- import sys import getpass import pickle from 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" continue atm.py如下: #!/usr/bin/python # -*- coding: utf-8 -*- import sys import pickle import re import time import 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.pkl userinfo={‘10001‘:{‘passwd‘:123456,‘name‘:‘li‘,‘youhave‘:10000}, ‘10002‘:{‘passwd‘:234567,‘name‘:‘skipper‘,‘youhave‘:20000}} 用户操作记录文件10001.record 20150401 18:03:46 withdraw -221 therest:6034 20150401 18:07:39 savemoney +1001 therest:7035 20150401 18:18:09 transfer -123 therest:5911 20150401 18:18:27 shopping -199 therest:5712 商品价格文件goods_list Car 250000 Clothes 399 Shoes 199 Iphone5s 4999 Coffee 35 Foods 68 Bicycle 1688 参考信息:http://www.linuxidc.com/Linux/2014-09/107274.htm
时间: 2024-11-28 23:16:17