python實現(xiàn)銀行實戰(zhàn)系統(tǒng)
本文實例為大家分享了python實現(xiàn)銀行實戰(zhàn)系統(tǒng)的具體代碼,供大家參考,具體內容如下
先附上源代碼:
│ admin.py 管理員界面
│ alluser.txt 保存用戶信息
│ atm.py 銀行的各部分操作方法(存錢取錢等等)
│ card.py 定義卡的類
│ main.py 主程序 while True
│ user.py 用戶的類
main.py的源代碼
""" 人 類名:User 屬性:姓名 身份證號 電話號 卡 行為: 卡 類名:Card 屬性:卡號 密碼 余額 行為: 提款機 類名:ATM 屬性:用戶字典 行為:開戶 查詢 取款 存款 轉賬 改密 鎖定 解密 補卡 銷戶 退出 管理員 類名:Admin 屬性: 行為:管理員界面 管理員驗證 系統(tǒng)功能界面 """ import os import pickle import admin from atm import ATM def main(): # 管理員對象 admin1 = admin.Admin() # 管理員開機 admin1.printAdminView() if admin1.adminOption(): return -1 # 提款機對象 filepath = os.path.join(os.getcwd(), "alluser.txt") f = open(filepath, "rb") allUsers = pickle.load(f) f.close() atm = ATM(allUsers) while True: admin1.printFunctionView() # 等待用戶的操作 option = input("請輸入您的操作:") if option == "1" or option == "開戶": atm.creatUser() elif option == "2" or option == "查詢": atm.searchUserInfo() elif option == "3" or option == "取款": atm.withdrawals() elif option == "4" or option == "存款": atm.saveMoney() elif option == "5" or option == "轉賬": atm.transferMoney() elif option == "6" or option == "改密": atm.changePasswd() elif option == "7" or option == "鎖定": atm.lockUser() elif option == "8" or option == "解鎖": atm.unlockUser() elif option == "9" or option == "補卡": atm.newCard() elif option == "0" or option == "銷戶": atm.killUser() elif option == "t" or option == "退出": if not admin1.adminOption(): # 將當前系統(tǒng)中的用戶信息保存到文件中 f = open(filepath, "wb") pickle.dump(atm.allUsers, f, 2) f.close() return -1 else: print("指令錯誤,請重新輸入?。?) admin.timeFlush() if __name__ == '__main__': main()
admin.py的源代碼
import time def timeFlush(): sum = 2 # 設置倒計時時間 timeflush = 0.25 # 設置屏幕刷新的間隔時間 for i in range(0, int(sum / timeflush)): list = ["\\", "|", "/", "—"] index = i % 4 print("\r操作成功!請稍等 {} ".format(list[index]), end="") time.sleep(timeflush) class Admin(object): admin = "1" passwd = "1" def printAdminView(self): print("*****************************************************************") print("* *") print("* *") print("* 歡迎登錄csdn銀行 *") print("* *") print("* *") print("*****************************************************************") def printFunctionView(self): print("\r*****************************************************************") print("* 開 戶(1) 查 詢(2) *") print("* 取 款(3) 存 款(4) *") print("* 轉 賬(5) 改 密(6) *") print("* 鎖 定(7) 解 鎖(8) *") print("* 補 卡(9) 銷 戶(0) *") print("* 退 出(t) *") print("*******************************************************************") def adminOption(self): inputAdmin = input("請輸入管理員賬號:") if self.admin != inputAdmin: print("賬號輸入有誤!!") return -1 inputPasswd = input("請輸入管理員密碼:") if self.passwd != inputPasswd: print("密碼輸入有誤!!") return -1 # 能執(zhí)行到這里說明賬戶密碼正確??! timeFlush() return 0
user.py的源代碼
class User(object): def __init__(self, name, idCard, phone, card): self.name = name self.idCard = idCard self.phone = phone self.card = card
card.py的源代碼
class Card(object): def __init__(self, cardId, cardPasswd, cardMoney): self.cardId = cardId self.cardPasswd = cardPasswd self.cardMoney = cardMoney self.cardLock = False
atm.py的源代碼
import random from card import Card from user import User class ATM(object): def __init__(self, allUsers): self.allUsers = allUsers # 開戶 def creatUser(self): # 向用戶字典中添加一對鍵值對(卡號 -- 用戶) name = input("請輸入您的姓名:") idCard = input("請輸入您的身份證號碼:") phone = input("請輸入您的電話號碼:") prestoreMoney = int(input("請輸入預存儲金額:")) if prestoreMoney < 0: print("預存儲金額有誤??!開戶失敗") return -1 onePasswd = input("請設置密碼:") # 驗證密碼 if not self.checkPasswd(onePasswd): print("密碼輸入錯誤??!開戶失敗") return -1 # 生成隨機的卡號 cardId = self.randomCardId() # 生成卡的信息 card = Card(cardId, onePasswd, prestoreMoney) # 生成用戶信息 user = User(name, idCard, phone, card) # 存到字典里面 self.allUsers[cardId] = user print("您的卡號是%s, 請牢記卡號!!" % cardId) # 查詢 def searchUserInfo(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在?。〔樵兪?) return -1 if user.card.cardLock: print("該卡已被鎖定??!請解鎖后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤?。≡摽ㄒ驯绘i定?。≌埥怄i后在進行其他操作!") user.card.cardLock = True return -1 print("賬號:%s 余額:%d" % (user.card.cardId, user.card.cardMoney)) # 取款 def withdrawals(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在!!查詢失敗") return -1 if user.card.cardLock: print("該卡已被鎖定?。≌埥怄i后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤!!該卡已被鎖定!!請解鎖后在進行其他操作!") user.card.cardLock = True return -1 # 進行到這一步說明卡號信息都正確,進行取款操作 theMoney = int(input("請輸入您需要取款的金額:")) if theMoney > user.card.cardMoney: print("余額不足?。?) return -1 elif theMoney < 0: print("您輸入金額有誤!!") else: user.card.cardMoney -= theMoney print("取款成功??! 余額為:%d" % user.card.cardMoney) # 存款 def saveMoney(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在?。〔樵兪?) return -1 if user.card.cardLock: print("該卡已被鎖定?。≌埥怄i后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤?。≡摽ㄒ驯绘i定??!請解鎖后在進行其他操作!") user.card.cardLock = True return -1 # 進行到這一步說明卡號信息都正確,進行存款操作 theMoney = int(input("請輸入您需要存款的金額:")) if theMoney < 0: print("您輸入金額有誤!!") else: user.card.cardMoney += theMoney print("存款成功?。?余額為:%d" % user.card.cardMoney) # 轉賬 def transferMoney(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在??!查詢失敗") return -1 if user.card.cardLock: print("該卡已被鎖定??!請解鎖后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤?。≡摽ㄒ驯绘i定??!請解鎖后在進行其他操作!") user.card.cardLock = True return -1 # 到這里說明卡號信息正確,進行轉賬操作 theOtherCardId = input("請輸入您需要轉賬的卡號:") # 驗證是否存在該卡號 otheruser = self.allUsers.get(theOtherCardId) if not otheruser: print("該卡號不存在??!轉賬失敗") return -1 if otheruser.card.cardLock: print("該卡已被鎖定?。?) return -1 theOtherCardName = input("請輸入您需要轉賬人的姓名:") # 驗證轉賬人的姓名是否正確 if otheruser.name != theOtherCardName: print("轉賬人姓名輸入錯誤") return -1 print("您的賬戶為%s 您的余額為%d" % (user.card.cardId, user.card.cardMoney)) # 開始轉賬 theMoney = int(input("請輸入您需要轉賬的金額:")) if theMoney < 0: print("您輸入金額有誤!!") else: user.card.cardMoney -= theMoney otheruser.card.cardMoney += theMoney print("轉賬成功?。∧挠囝~為%d" % user.card.cardMoney) return -1 # 改密 def changePasswd(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在??!查詢失敗") return -1 if user.card.cardLock: print("該卡已被鎖定??!請解鎖后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤?。≡摽ㄒ驯绘i定?。≌埥怄i后在進行其他操作!") user.card.cardLock = True return -1 # 下面進行改密操作 newPasswd = input("請輸入新密碼:") if newPasswd == user.card.cardPasswd: print("新舊密碼不能一致!!操作失敗") return -1 # 驗證密碼 if not self.checkPasswd(newPasswd): print("密碼輸入錯誤??!") return -1 user.card.cardPasswd = newPasswd print("密碼修改成功?。≌埨斡浤拿艽a") # 鎖定 def lockUser(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在??!輸入錯誤") return -1 if user.card.cardLock: print("該卡已被鎖定??!請解鎖后再使用其他功能") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤??!鎖定失敗") return -1 tempIdCard = input("請輸入您的身份證號碼") if tempIdCard != user.idCard: print("身份證輸入錯誤!!鎖定失敗") return -1 # 進行到這一步說明信息輸入成功,鎖定開始 user.card.cardLock = True print("鎖定成功") # 解鎖 def unlockUser(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在??!輸入錯誤") return -1 if not user.card.cardLock: print("該卡沒有被鎖定??!無需解鎖") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤??!鎖定失敗") return -1 tempIdCard = input("請輸入您的身份證號碼") if tempIdCard != user.idCard: print("身份證輸入錯誤?。℃i定失敗") return -1 # 進行到這一步說明信息輸入成功,解鎖開始 user.card.cardLock = False print("解鎖成功") # 補卡 def newCard(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在??!查詢失敗") return -1 if user.card.cardLock: print("該卡已被鎖定??!請解鎖后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤??!該卡已被鎖定??!請解鎖后在進行其他操作!") user.card.cardLock = True return -1 CardName = input("請輸入您的姓名:") # 驗證姓名是否正確 if user.name != CardName: print("姓名輸入錯誤!!") return -1 useridCard = input("請輸入您的身份證號碼:") # 驗證身份證是否正確 if user.idCard != useridCard: print("身份證號碼輸入錯誤??!") return -1 # 進行到這一步說明信息都正確,下面進行補卡操作,只換卡號,其他信息都不換 newIdCard= self.randomCardId() self.allUsers[newIdCard] = self.allUsers.pop(user.card.cardId) user.card.cardId = newIdCard print("您的新卡號為:%s 請牢記??!" % user.card.cardId) # 銷戶 def killUser(self): cardNum = input("請輸入您的卡號:") # 驗證是否存在該卡號 user = self.allUsers.get(cardNum) if not user: print("該卡號不存在??!查詢失敗") return -1 if user.card.cardLock: print("該卡已被鎖定??!請解鎖后在進行其他操作!") return -1 # 驗證密碼 if not self.checkPasswd(user.card.cardPasswd): print("密碼輸入錯誤??!該卡已被鎖定??!請解鎖后在進行其他操作!") user.card.cardLock = True return -1 CardName = input("請輸入您的姓名:") # 驗證姓名是否正確 if user.name != CardName: print("姓名輸入錯誤??!") return -1 useridCard = input("請輸入您的身份證號碼:") # 驗證身份證是否正確 if user.idCard != useridCard: print("身份證號碼輸入錯誤??!") return -1 answer = input("請問您確定要銷戶嗎?確定(1) 取消(2)") if answer == "1" or answer == "確定": del self.allUsers[cardNum] print("已銷戶") return -1 elif answer == "2" or answer == "取消": print("取消成功!!") return -1 else: print("輸入錯誤!!") return -1 # 驗證密碼 def checkPasswd(self, realPasswd): for i in range(3): tempPasswd = input("請再次輸入密碼:") if tempPasswd == realPasswd: return True return False def randomCardId(self): while True: str = "" for i in range(6): # 隨機生成一個數(shù)字 ch = chr(random.randrange(ord("0"), ord("9") + 1)) str += ch # 判斷是否重復 if not self.allUsers.get(str): return str
alluser.txt源代碼
�}q X 123456qcuser User q)�q}q(X nameqX 1qX idCardqhX phoneqhX cardq ccard Card q )�q }q (X cardIdq hX cardPasswdqhX cardMoneyqKX cardLockq�ububs.
因為運用pickle庫,要持久化存儲用戶信息(字典),故打開讀取寫入會亂碼,第一排中的123456是卡號,其他信息全部是1,不要試圖pycharm中修改這個allUsers。txt文件,否則會產(chǎn)生無法啟動程序的bug,這個筆者也不知道該如何改進亂碼現(xiàn)象,希望讀者能優(yōu)化
如果遇到因為alluser.txt而無法運行的情況:請往下看:
因為pickle庫的原因,開始我們是要讀取這個文件的,讀的到程序順利,讀不到程序涼涼,故我們必須要在alluser.txt里面有pickle庫可以識別的源代碼。如果您是在pycharm里面復制粘貼的因為alluser.txt代碼,故pycharm會自動轉化為utf-8或者其他
我們要先把alluser.txt刪除,讓pickle庫先不讀取,創(chuàng)建一個空字典,我們先開戶,然后退出程序是會自動創(chuàng)建一個新的alluser.txt文件,會把我們剛剛創(chuàng)建好的用戶信息全部保存在txt文件中,這樣我們再恢復,達到持久化保存的目的
filepath = os.path.join(os.getcwd(), "alluser.txt") # 把之前的給注釋掉,不讓程序讀取 # f = open(filepath, "rb") # allUsers = pickle.load(f) # f.close() # 創(chuàng)建一個新的空字典 allUsers = {} atm = ATM(allUsers)
然后我們進行開戶操作,最后退出,會自動創(chuàng)建一個全新的alluser.txt文件
filepath = os.path.join(os.getcwd(), "alluser.txt") f = open(filepath, "rb") allUsers = pickle.load(f) f.close() # 然后我們恢復它 # allUsers = {} atm = ATM(allUsers)
ok,完成了
運行結果:
更多學習資料請關注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用 Python 獲取 Linux 系統(tǒng)信息的代碼
在本文中,我們將會探索使用Python編程語言工具來檢索Linux系統(tǒng)各種信息,需要的朋友可以參考下2014-07-07python Stanza處理NLP任務使用詳解(多語言處理工具)
這篇文章主要為大家介紹了python Stanza處理NLP任務使用詳解,多語言處理工具使用實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01Python爬蟲實戰(zhàn)之爬取京東商品數(shù)據(jù)并實實現(xiàn)數(shù)據(jù)可視化
今天再帶大家簡單爬一波京東的商品數(shù)據(jù)唄,廢話不多說,文中有非常詳細的代碼示例,需要的朋友可以參考下2021-06-06解決python nohup linux 后臺運行輸出的問題
今天小編就為大家分享一篇解決python nohup linux 后臺運行輸出的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python在Windows環(huán)境下的文件路徑問題及解決辦法
在Python中處理Windows路徑時,經(jīng)常會遇到一些特殊的問題,在Windows中,路徑使用反斜杠(\)作為分隔符,而在其他操作系統(tǒng)中,路徑使用正斜杠(/)作為分隔符,本文給大家介紹了Python在Windows環(huán)境下的文件路徑問題及解決辦法,需要的朋友可以參考下2024-06-06