Python ATM小程式 v1
- 2020 年 1 月 9 日
- 筆記
注意:下面的程式和上一遍中的流程有點區別!
資料庫設置:
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"