首页 > 代码库 > ATM小程序 v1
ATM小程序 v1
注意:下面的程序和上一遍中的流程有点区别!
数据库设置:
mysql> desc back_card; ##信用卡表结构! +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | card_id | int(10) | NO | PRI | NULL | | | card_passwd | varchar(128) | NO | | NULL | | | card_user | varchar(64) | YES | | NULL | | | card_balance | float | YES | | NULL | | | create_time | datetime | YES | | NULL | | | login_time | datetime | YES | | NULL | | | err_time | datetime | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+ 7 rows in set (0.01 sec) mysql> desc item_list; ##商品表结构 +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | item_id | int(11) | NO | PRI | NULL | auto_increment | | item_name | varchar(256) | YES | | NULL | | | item_price | float | YES | | NULL | | | item_total | float | YES | | NULL | | | up_time | datetime | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.02 sec) mysql>
Python atm程序 v1
################main.py ##程序入口文件 #!/usr/bin/python # -*- coding: utf-8 -*- __author__ = ‘gaogd‘ import atm_login import atm_main def SelectFunction(): content=u‘‘‘ 1.登陆信用卡 2.申请信用卡 3.修改信用卡密码 4.删除信用卡 ‘‘‘ print content num = raw_input(u‘请输入您的选择:‘) if num.strip() == ‘1‘: Flag = True Flag,card_id = atm.Login() while Flag: Flag = atm_main.atm_choose(card_id) #atm.elect(card_id) return True if num.strip() == ‘2‘: atm.Register() return True if num.strip() == ‘3‘: atm.UpdatePasswd() return True if num.strip() == ‘4‘: atm.Deleuser() return True print u‘您的输入有误!!请输入您需要的功能对应的序号‘ if __name__ == "__main__": atm = atm_login.ATM() #atm.Register() #atm.Login() #atm.Deleuser() SelectFunction()
#######atm_main.py #!/usr/bin/python # -*- coding: utf-8 -*- __author__ = ‘gaogd‘ import atm_login import shooping def atm_choose(card_id): choose_item = u""" 1. 查询 2. 消费 3. 存取转账 4. 修改账户密码 5. 退出 """ print choose_item choose_item_num = raw_input(u"请输入的想要的操作:") if choose_item_num.strip() == ‘1‘: ‘‘‘ 查询 ‘‘‘ Flag = True while Flag: atm = atm_login.ATM() Flag = atm.Select(card_id) return True if choose_item_num.strip() == ‘2‘: ‘‘‘ 消费 ‘‘‘ Flag = True while Flag: shoop = shooping.Shoop() Flag = shoop.shooping(card_id) print Flag return True if choose_item_num.strip() == ‘3‘: ‘‘‘ 存取转款 ‘‘‘ atm = atm_login.ATM() Flag = True while Flag: Flag = atm.PutGetMoney(card_id) return True if choose_item_num.strip() == ‘4‘: ‘‘‘ 修改密码 ‘‘‘ atm = atm_login.ATM() atm.UpdatePasswd() return True if choose_item_num.strip() == ‘5‘: ‘‘‘ 退出 ‘‘‘ return False print u‘您的输入有误!!请输入您需要的功能对应的序号,请重新输入!!‘ return True
##################atm_login.py #!/usr/bin/python # -*- coding: utf-8 -*- __author__ = ‘gaogd‘ import datetime import MySQLdb as mysql import atm_select import atm_main class ATM(object): def __init__(self): self.db = mysql.connect(user="root", passwd="wdzj@2015", db="ATM", host="192.168.10.12") # 数据库连接信息 self.db.autocommit(True) self.cur = self.db.cursor() self.cur.execute(‘set names utf8‘) def Login(self): # addtime = datetime.datetime.now().__format__("%Y-%m-%d %H:%M:%S") addtime = datetime.datetime.now() self.card_id = raw_input(u‘请输入你的银行卡号:‘) err_count = 0 Flag = True while Flag: self.password = raw_input(u‘请输入密码:‘) if self.card_id.strip() == ‘‘: print u‘银行卡号不能为空!‘ Flag = False return Flag if not self.card_id.isdigit(): print u‘用户卡号必须为数字!‘ Flag = False return Flag if self.password.strip() == ‘‘: print u‘密码不能为空!‘ Flag = False return Flag ## select err_time from loginuser where user=‘xiaoming‘; get_err_time_sql = "select err_time from back_card where card_id=‘%s‘;" % self.card_id self.cur.execute(get_err_time_sql) get_err_time = self.cur.fetchall() if len(get_err_time) == 0: ## 卡号不存在 print u‘输入的用户卡或密码错误‘ Flag = False return Flag print ‘1------‘,get_err_time print ‘2------‘,get_err_time[0][0] if get_err_time[0][0] == None: print ‘ok pass‘ #pass else: print ‘get_err_time::‘, get_err_time[0][0], ‘=======‘, addtime err_time = get_err_time[0][0] diff_time = (addtime - err_time).seconds print ‘diff_time::‘, diff_time if not diff_time >= 100: Flag = False if Flag: # sql = select user,passwd from loginuser where user=‘gao3‘and passwd=‘gao3‘; select_user_passwd_sql = "select card_id,card_user,card_passwd from back_card where card_id=%s and card_passwd=‘%s‘;" % (self.card_id, self.password) #print select_user_passwd_sql self.cur.execute(select_user_passwd_sql) select_user_passwd = self.cur.fetchall() if len(select_user_passwd) == 0: print u‘用户名或密码不正确!!‘ err_count += 1 if err_count == 3: print u‘错误输入密码超过三次,程序自动退出!!‘ ##sql = update loginuser set err_time=‘2016-09-02 15:40:01‘ where user=‘gao1‘ ; update_err_sql = "update back_card set err_time=‘%s‘ where card_id=%s " % (addtime, self.card_id) self.cur.execute(update_err_sql) Flag = False else: err_count = 0 self.card_user = select_user_passwd[0][1] ##sql = update loginuser set login_time=‘2016-09-02 15:40:01‘ where user=‘gao1‘ ; update_login_sql = "update back_card set login_time=‘%s‘ where card_id=‘%s‘ " % (addtime, self.card_id) self.cur.execute(update_login_sql) print u‘欢迎%s您来到中国银行网上银行!!‘ % self.card_user #atm_main.atm_choose() return True,self.card_id else: print u‘%s在5分钟登陆错误超过3次,请三十分钟后登陆!!‘ % self.card_id def Register(self): username = raw_input(u‘请输入你要申请的用户名:‘) password_one = raw_input(u‘请输入密码:‘) password_two = raw_input(u‘请再次输入密码:‘) addtime = datetime.datetime.now() if username.strip() == ‘‘: print u‘用户名不能为空!‘ if not username.isalnum(): print u‘用户名不能包含特殊字符‘ return False if password_one.strip() == ‘‘: print u‘密码不能为空!‘ return False if not password_one.strip() == password_two.strip(): print u‘你两次输入的密码不一致‘ return False ##生成银行卡号 ##select * from back_card ORDER BY card_id desc limit 1; card_last_id_sql = "select * from back_card ORDER BY card_id desc limit 1;" self.cur.execute(card_last_id_sql) card_last_id = self.cur.fetchall()[0][0] card_id = int(card_last_id) + 1 ## insert into back_card(card_id,card_passwd,card_user,card_balance,create_time) values(999990001,‘asdasd‘,‘gao‘,0.0,addtime); create_card_sql = "insert into back_card(card_id,card_passwd,card_user,card_balance,create_time) values(%s,‘%s‘,‘%s‘,0.0,‘%s‘);" % (card_id,password_one,username,addtime) try: print create_card_sql self.cur.execute(create_card_sql) except Exception as e: print e print u‘创建用户失败!!‘ return False print u‘创建用户成功:‘, username def UpdatePasswd(self): ‘‘‘ 修改信用卡密码 ‘‘‘ card_id = raw_input(u‘请输入你要修改密码的卡号: ‘) username = raw_input(u‘请输入用户名: ‘) password = raw_input(u‘请输入密码: ‘) new_passwd = raw_input(u‘请输入你的新密码: ‘) new_passwd_two = raw_input(u‘请输入你的新密码: ‘) if username.strip() == ‘‘: print u‘输入的用户名不能为空!‘ return False if card_id.strip() == ‘‘: print u‘信用卡号不能为空!‘ return False if not card_id.isdigit(): print u‘信用卡号输入有误,卡号只能是数字!‘ return False if not new_passwd.strip() == new_passwd_two.strip(): print u‘您两次输入的新密码不一致,请重新输入!‘ return False # select card_id,card_passwd from back_card where card_id=999990005 and card_user=‘gao3‘and card_passwd=‘gao33‘ ; select_card_passwd_sql = "select card_id,card_passwd from back_card where card_id=%s and card_user=‘%s‘and card_passwd=‘%s‘ ;" % ( card_id, username, password) # print select_card_passwd_sql self.cur.execute(select_card_passwd_sql) select_card_passwd = self.cur.fetchall() # print select_card_passwd,‘lend2‘,len(select_card_passwd) if len(select_card_passwd) == 0: print u‘用户名或密码不正确!!‘ return False else: ##修改密码 ##update back_card set card_passwd=‘okokok‘ where card_id=999990001; select_updatepasswd_sql = " update back_card set card_passwd=‘%s‘ where card_id=%s ;" % (new_passwd,card_id) #print select_updatepasswd_sql self.cur.execute(select_updatepasswd_sql) print u‘用户%s 密码已经修改成功!!!‘ % username def Deleuser(self): card_id = raw_input(u‘请输入你要删除的卡号:‘) username = raw_input(u‘请输入你要删除的用户名:‘) password = raw_input(u‘请输入密码:‘) if username.strip() == ‘‘ : print u‘输入的用户名不能为空!‘ return False if card_id.strip() == ‘‘: print u‘银行卡不能为空!‘ return False if not card_id.isdigit(): print u‘信用卡号输入有误,卡号只能是数字!‘ return False # select card_id,card_passwd from back_card where card_id=999990005 and card_user=‘gao3‘and card_passwd=‘gao33‘ ; select_card_passwd_sql = "select card_id,card_passwd from back_card where card_id=%s and card_user=‘%s‘and card_passwd=‘%s‘ ;" % (card_id,username,password) #print select_card_passwd_sql self.cur.execute(select_card_passwd_sql) select_card_passwd = self.cur.fetchall() #print select_card_passwd,‘lend2‘,len(select_card_passwd) if len(select_card_passwd) == 0: print u‘用户名或密码不正确!!‘ return False else: if password.strip() == select_card_passwd[0][1].strip(): ##删除用户 ##DELETE FROM loginuser WHERE user=‘gao1‘; select_deluser_sql = "DELETE FROM back_card where card_id=%s and card_user=‘%s‘ ;" % (card_id,username) self.cur.execute(select_deluser_sql) print u‘已经成功删除%s用户‘ % username def Select(self,card_id): select_content = u‘‘‘ 1.查询余额 2.查询消费记录 3.退出查询 ‘‘‘ print select_content select_content_num = raw_input(u‘输入你要查询的项目:‘) if select_content_num.strip() == ‘1‘: #print ‘card_id:::‘,card_id #select card_balance from back_card where card_id=999990004 ; card_balance_sql = "select card_balance from back_card where card_id=%s ;" % card_id #print card_balance_sql self.cur.execute(card_balance_sql) self.card_balance = self.cur.fetchall() print u‘您的余额是::‘, self.card_balance[0][0] return True if select_content_num.strip() == ‘2‘: return True if select_content_num.strip() == ‘3‘: return False print u‘您的输入有误,请重新输入!!‘ return True def PutGetMoney(self,card_id): put_get_item = u‘‘‘ 1. 存钱 2. 取钱 3. 转账 4. 退出存取转款 ‘‘‘ print put_get_item put_get_item_num = raw_input(u‘输入你要查询的项目:‘) ## 查现在的存款 card_balance_sql = "select card_balance from back_card where card_id=%s ;" % card_id self.cur.execute(card_balance_sql) self.card_balance = self.cur.fetchall() print u‘您的余额是::‘, self.card_balance[0][0] if put_get_item_num.strip() == ‘1‘: ‘‘‘ 存钱 ‘‘‘ put_money = raw_input(u‘请您输入要存的款额: ‘) ##这里日后还可以添加输入小数的 if not put_money.isdigit(): print u‘您的输入的存款有误!必须输入整数!!‘ return True new_money = float(put_money) + float(self.card_balance[0][0]) # update back_card set card_balance=610 where card_id=999990004 ; put_money_sql = "update back_card set card_balance=%s where card_id=%s ;" % (new_money,card_id) self.cur.execute(put_money_sql) print u‘已成功帮您存入%s元,您现在的余额是%s元!!‘ %(put_money,new_money) return True if put_get_item_num.strip() == ‘2‘: ‘‘‘ 取钱 ‘‘‘ get_money = raw_input(u‘请您输入要存的款额: ‘) ##这里日后还可以添加输入小数的 if not get_money.isdigit(): print u‘您的输入的取款有误!必须输入整数哦!!‘ return True new_money = float(self.card_balance[0][0]) - float(get_money) if new_money < 0: print u‘您的输入的余额不足%s元,无法为您取款!!‘ % get_money return True put_money_sql = "update back_card set card_balance=%s where card_id=%s ;" % (new_money,card_id) self.cur.execute(put_money_sql) print u‘已成功帮您取出%s元,您现在的余额是%s元!!‘ %(get_money,new_money) return True if put_get_item_num.strip() == ‘3‘: ‘‘‘ 转账 ‘‘‘ tranfer_money = raw_input(u‘请您输入要存的款额: ‘) ##这里日后还可以添加输入小数的 tranfer_card_id = raw_input(u‘请您输入转到目的卡号: ‘) tranfer_card_user = raw_input(u‘请您输入转到目的卡的用户名: ‘) if not tranfer_money.isdigit(): print u‘您的输入的转款有误!必须输入整数哦!!‘ return True new_my_money = float(self.card_balance[0][0]) - float(tranfer_money) if new_my_money < 0: print u‘您的输入的余额不足%s元,无法为您转账!!‘ % tranfer_money return True if not tranfer_card_id.strip().isdigit(): print u‘您的输入的目的卡号%s不正确,应为数字!!‘ % tranfer_card_id return True if not tranfer_card_user.strip().isalpha(): print u‘您的输入的目的用户%s不正确,应为字母!!‘ % tranfer_card_id return True ## # select * from back_card where card_id=999990003 and card_user=‘gao1‘ ; select_tranfer_card_user_sql = "select card_balance from back_card where card_id=%s and card_user=‘%s‘ ;" %(tranfer_card_id,tranfer_card_user) print select_tranfer_card_user_sql self.cur.execute(select_tranfer_card_user_sql) peer_money = self.cur.fetchall() if len(peer_money) == 0: print u‘您输入的目的卡号或用户名不一致,无法为您转账‘ return True #本卡操作 put_money_sql = "update back_card set card_balance=%s where card_id=%s ;" % (new_my_money,card_id) self.cur.execute(put_money_sql) #目的卡操作 peer_old_money = peer_money[0][0] new_peer_money = float(peer_old_money) + float(tranfer_money) get_money_sql = "update back_card set card_balance=%s where card_id=%s ;" % (new_peer_money,tranfer_card_id) print get_money_sql self.cur.execute(get_money_sql) print u‘已经成功为您转账%s元,到%s账户上!您现在的余额是%s元‘ %(tranfer_money,tranfer_card_user,new_my_money) return True if put_get_item_num.strip() == ‘4‘: return False print u‘您输入的序号有误!‘ return True
#################shooping.py #!/usr/bin/python # -*- coding: utf-8 -*- __author__ = ‘gaogd‘ import MySQLdb as mysql import datetime class Shoop(object): def __init__(self): self.db = mysql.connect(user="root", passwd="wdzj@2015", db="ATM", host="192.168.10.12") # 数据库连接信息 self.db.autocommit(True) self.cur = self.db.cursor() self.cur.execute(‘set names utf8‘) self.addtime = datetime.datetime.now() def add_goods(self): self.goods_item = raw_input(u‘请输入商店出售的新商品名称: ‘).strip() goods_price = raw_input(u‘请输入商店出售的新商品单价: ‘).strip() self.goods_total = raw_input(u‘请输入商店出售的新商品总数: ‘).strip() if self.goods_item == ‘‘: print u‘商品名称不能为空!‘ Flag = False return Flag if not self.goods_total.isdigit(): print u‘商品名称总量必须为整数!‘ Flag = False return Flag try: self.goods_price = float(goods_price) except ValueError as e: print u‘商品价格必须是数字!‘ Flag = False return Flag try: # insert into item_list(item_name,item_price,item_total) values(‘ip‘,22,33); add_goods_sql = "insert into item_list(item_name,item_price,item_total,up_time) values(‘%s‘,%s,%s,‘%s‘);" %(self.goods_item,self.goods_price,self.goods_total,self.addtime) print add_goods_sql self.cur.execute(add_goods_sql) except Exception as e: print u‘新商品上架失败!使用原因是:%s‘,e print u‘商品:%s ,单价:%s ,总数:%s,上架成功!‘ %(self.goods_item,self.goods_price,self.goods_total) def shooping(self,card_id): #select item_id,item_name,item_price from item_list ; select_item_sql = "select item_id,item_name,item_price,item_total from item_list ;" self.cur.execute(select_item_sql) select_item = self.cur.fetchall() self.item_list = [] ##商店的所有商品 self.goods_list = {} ##都购买了什么,单价,数量,总额 Flag = True print u"\033[31m 商品序号 \t\t\t 商品名称 \t\t\t 商品价格 \t\t\t 商品总量 \33[0m" for m in range(len(select_item)): print "\33[33m %3s \t\t\t %10s \t\t\t %8s \t\t\t %s " %(select_item[m][0] ,select_item[m][1] ,select_item[m][2],select_item[m][3]) self.item_list.append(‘%d‘ %select_item[m][0]) print u"\033[32m %3s \t\t\t 结账!! \33[0m" % int(m+2) print u"\033[36m %3s \t\t\t 退出购买,放弃购物车的内容!! \33[0m" % int(m+3) while Flag: goods = raw_input(u‘请您输入购买的商品序号以及购买数量空格分开: ‘).strip() if len(goods.split()) == 2: goods_num, goods_nums = goods.split() ## 判断输入的商品序号是否有问题 if not goods_num.isdigit(): print u"\033[31m 输入的商品序号有误,应该是数字!! \33[0m" return True ## 判断输入的商品数量是否有问题 if not goods_nums.isdigit(): print u"\033[34m 输入购买商品数量有误,应数字!! \33[0m" return True ##判断商品是否存在!! if not goods_num in self.item_list: print u"\033[33m 您购买的商品不存在!! \33[0m" return True ### 商品总量,是否大于购买商品数量 def compare(goods_total,goods_nums): #print u"商品总量", goods_total, u"购买数量:", goods_nums if float(goods_total) < int(goods_nums): print u"\033[33m 商品数量不足!! \33[0m" return False return True n = int(goods_num) goods_price = float(select_item[n-1][2]) # 单价 if not self.goods_list.has_key(goods_num): ## 新加商品 self.goods_list[goods_num] = [float(goods_nums),goods_price,float(goods_nums) * goods_price] com_res = compare(select_item[n - 1][3],self.goods_list[goods_num][0]) if not com_res: self.goods_list[goods_num] = [0] else: self.goods_list[goods_num][0] = float(float(self.goods_list[goods_num][0]) + float(goods_nums)) com_res = compare(select_item[n - 1][3],self.goods_list[goods_num][0]) if not com_res: self.goods_list[goods_num][0] = self.goods_list[goods_num][0] - float(goods_nums) else: self.goods_list[goods_num][2] = float(self.goods_list[goods_num][0]) * goods_price else: ## 判断是否退出购买 if int(goods) == int(m + 3): print u"\033[36m 正在退出购买,放弃购物车的商品!! \33[0m" self.goods_list = {} return False elif int(goods) == int(m + 2): print u"\033[36m 正在为你结账 !! \33[0m" Flag =False else: print u"\033[31m 您的输入有误,输入应该是以空格分开的两个数字,或单独一个退出购物的序号!! \33[0m" Flag = False return True ## 钱够不够 # 购买商品总价 print len(self.goods_list) goods_total = self.goods_list.values() goods_total_money = 0 for goods in range(len(goods_total)): ##商品总费用 #print goods_total[goods][2] goods_total_money = goods_total_money + goods_total[goods][2] #print u"您的商品总价格是:%s ",goods_total_money ##查看存款 #select card_balance from back_card where card_id=999990004; select_money_sql = "select card_balance from back_card where card_id=%s" % card_id self.cur.execute(select_money_sql) select_money = self.cur.fetchall() if int(select_money[0][0]) < goods_total_money: print u‘你的商品总费用是:%s,您的余额是:%s,不足购买这些商品!!‘ % (goods_total_money, select_money[0][0]) return True try: ##为您结账 new_card_balance = int(select_money[0][0]) - goods_total_money new_card_balance_sql = "update back_card set card_balance=%s where card_id=%s ;" % (new_card_balance, card_id) self.cur.execute(new_card_balance_sql) ##库存更新 ##查现在的库存 goods_items = self.goods_list.keys() ##购买的所有商品id for goods in range(len(goods_items)): ## 从购物车中遍历商品id goods_id = goods_items[goods] ##商品id goods_item_total = float(select_item[int(goods_id) -1 ][3]) ##购买前库存 ,从商品id中找到商品库存 shoop_goods_total = float(goods_total[goods][0]) ##购买的商品数量 new_item_total = float(goods_item_total) - float(shoop_goods_total) ##购买后的库存 update_item_total_sql = "update item_list set item_total=%s where item_id=%s;" % (new_item_total,goods_id ) self.cur.execute(update_item_total_sql) print u‘您的账户有%s元,你的商品总费用是:%s现在余额:%s元,!!‘ % (select_money[0][0],goods_total_money, new_card_balance) return True except ReferenceError as e: print u‘无法为您购买商品,系统发送故障:%s‘ % e return True if __name__ == "__main__": shooping = Shoop() #shooping.add_goods() shooping.shooping(999990004)
本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1846263
ATM小程序 v1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。