python實現(xiàn)銀行實戰(zhàn)系統(tǒng)
本文實例為大家分享了python實現(xiàn)銀行實戰(zhàn)系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
先附上源代碼:
│ admin.py 管理員界面
│ alluser.txt 保存用戶信息
│ atm.py 銀行的各部分操作方法(存錢取錢等等)
│ card.py 定義卡的類
│ main.py 主程序 while True
│ user.py 用戶的類
main.py的源代碼
"""
人
類名:User
屬性:姓名 身份證號 電話號 卡
行為:
卡
類名:Card
屬性:卡號 密碼 余額
行為:
提款機(jī)
類名:ATM
屬性:用戶字典
行為:開戶 查詢 取款 存款 轉(zhuǎn)賬 改密 鎖定 解密 補(bǔ)卡 銷戶 退出
管理員
類名:Admin
屬性:
行為:管理員界面 管理員驗證 系統(tǒng)功能界面
"""
import os
import pickle
import admin
from atm import ATM
def main():
# 管理員對象
admin1 = admin.Admin()
# 管理員開機(jī)
admin1.printAdminView()
if admin1.adminOption():
return -1
# 提款機(jī)對象
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 == "轉(zhuǎn)賬":
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 == "補(bǔ)卡":
atm.newCard()
elif option == "0" or option == "銷戶":
atm.killUser()
elif option == "t" or option == "退出":
if not admin1.adminOption():
# 將當(dāng)前系統(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 # 設(shè)置倒計時時間
timeflush = 0.25 # 設(shè)置屏幕刷新的間隔時間
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("* 轉(zhuǎn) 賬(5) 改 密(6) *")
print("* 鎖 定(7) 解 鎖(8) *")
print("* 補(bǔ) 卡(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("請輸入預(yù)存儲金額:"))
if prestoreMoney < 0:
print("預(yù)存儲金額有誤!!開戶失敗")
return -1
onePasswd = input("請設(shè)置密碼:")
# 驗證密碼
if not self.checkPasswd(onePasswd):
print("密碼輸入錯誤!!開戶失敗")
return -1
# 生成隨機(jī)的卡號
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("該卡已被鎖定?。≌埥怄i后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤!!該卡已被鎖定?。≌埥怄i后在進(jìn)行其他操作!")
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("該卡已被鎖定!!請解鎖后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤?。≡摽ㄒ驯绘i定?。≌埥怄i后在進(jìn)行其他操作!")
user.card.cardLock = True
return -1
# 進(jìn)行到這一步說明卡號信息都正確,進(jìn)行取款操作
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("該卡已被鎖定??!請解鎖后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤!!該卡已被鎖定!!請解鎖后在進(jìn)行其他操作!")
user.card.cardLock = True
return -1
# 進(jìn)行到這一步說明卡號信息都正確,進(jìn)行存款操作
theMoney = int(input("請輸入您需要存款的金額:"))
if theMoney < 0:
print("您輸入金額有誤!!")
else:
user.card.cardMoney += theMoney
print("存款成功!! 余額為:%d" % user.card.cardMoney)
# 轉(zhuǎn)賬
def transferMoney(self):
cardNum = input("請輸入您的卡號:")
# 驗證是否存在該卡號
user = self.allUsers.get(cardNum)
if not user:
print("該卡號不存在!!查詢失敗")
return -1
if user.card.cardLock:
print("該卡已被鎖定!!請解鎖后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤?。≡摽ㄒ驯绘i定??!請解鎖后在進(jìn)行其他操作!")
user.card.cardLock = True
return -1
# 到這里說明卡號信息正確,進(jìn)行轉(zhuǎn)賬操作
theOtherCardId = input("請輸入您需要轉(zhuǎn)賬的卡號:")
# 驗證是否存在該卡號
otheruser = self.allUsers.get(theOtherCardId)
if not otheruser:
print("該卡號不存在??!轉(zhuǎn)賬失敗")
return -1
if otheruser.card.cardLock:
print("該卡已被鎖定!!")
return -1
theOtherCardName = input("請輸入您需要轉(zhuǎn)賬人的姓名:")
# 驗證轉(zhuǎn)賬人的姓名是否正確
if otheruser.name != theOtherCardName:
print("轉(zhuǎn)賬人姓名輸入錯誤")
return -1
print("您的賬戶為%s 您的余額為%d" % (user.card.cardId, user.card.cardMoney))
# 開始轉(zhuǎn)賬
theMoney = int(input("請輸入您需要轉(zhuǎn)賬的金額:"))
if theMoney < 0:
print("您輸入金額有誤!!")
else:
user.card.cardMoney -= theMoney
otheruser.card.cardMoney += theMoney
print("轉(zhuǎn)賬成功??!您的余額為%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("該卡已被鎖定??!請解鎖后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤??!該卡已被鎖定??!請解鎖后在進(jìn)行其他操作!")
user.card.cardLock = True
return -1
# 下面進(jìn)行改密操作
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("該卡已被鎖定?。≌埥怄i后再使用其他功能")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤??!鎖定失敗")
return -1
tempIdCard = input("請輸入您的身份證號碼")
if tempIdCard != user.idCard:
print("身份證輸入錯誤??!鎖定失敗")
return -1
# 進(jìn)行到這一步說明信息輸入成功,鎖定開始
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("身份證輸入錯誤!!鎖定失敗")
return -1
# 進(jìn)行到這一步說明信息輸入成功,解鎖開始
user.card.cardLock = False
print("解鎖成功")
# 補(bǔ)卡
def newCard(self):
cardNum = input("請輸入您的卡號:")
# 驗證是否存在該卡號
user = self.allUsers.get(cardNum)
if not user:
print("該卡號不存在?。〔樵兪?)
return -1
if user.card.cardLock:
print("該卡已被鎖定??!請解鎖后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤?。≡摽ㄒ驯绘i定?。≌埥怄i后在進(jìn)行其他操作!")
user.card.cardLock = True
return -1
CardName = input("請輸入您的姓名:")
# 驗證姓名是否正確
if user.name != CardName:
print("姓名輸入錯誤??!")
return -1
useridCard = input("請輸入您的身份證號碼:")
# 驗證身份證是否正確
if user.idCard != useridCard:
print("身份證號碼輸入錯誤?。?)
return -1
# 進(jìn)行到這一步說明信息都正確,下面進(jìn)行補(bǔ)卡操作,只換卡號,其他信息都不換
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("該卡已被鎖定??!請解鎖后在進(jìn)行其他操作!")
return -1
# 驗證密碼
if not self.checkPasswd(user.card.cardPasswd):
print("密碼輸入錯誤??!該卡已被鎖定!!請解鎖后在進(jìn)行其他操作!")
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):
# 隨機(jī)生成一個數(shù)字
ch = chr(random.randrange(ord("0"), ord("9") + 1))
str += ch
# 判斷是否重復(fù)
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.
因為運(yùn)用pickle庫,要持久化存儲用戶信息(字典),故打開讀取寫入會亂碼,第一排中的123456是卡號,其他信息全部是1,不要試圖pycharm中修改這個allUsers。txt文件,否則會產(chǎn)生無法啟動程序的bug,這個筆者也不知道該如何改進(jìn)亂碼現(xiàn)象,希望讀者能優(yōu)化
如果遇到因為alluser.txt而無法運(yùn)行的情況:請往下看:
因為pickle庫的原因,開始我們是要讀取這個文件的,讀的到程序順利,讀不到程序涼涼,故我們必須要在alluser.txt里面有pickle庫可以識別的源代碼。如果您是在pycharm里面復(fù)制粘貼的因為alluser.txt代碼,故pycharm會自動轉(zhuǎn)化為utf-8或者其他
我們要先把alluser.txt刪除,讓pickle庫先不讀取,創(chuàng)建一個空字典,我們先開戶,然后退出程序是會自動創(chuàng)建一個新的alluser.txt文件,會把我們剛剛創(chuàng)建好的用戶信息全部保存在txt文件中,這樣我們再恢復(fù),達(dá)到持久化保存的目的
filepath = os.path.join(os.getcwd(), "alluser.txt")
# 把之前的給注釋掉,不讓程序讀取
# f = open(filepath, "rb")
# allUsers = pickle.load(f)
# f.close()
# 創(chuàng)建一個新的空字典
allUsers = {}
atm = ATM(allUsers)
然后我們進(jìn)行開戶操作,最后退出,會自動創(chuàng)建一個全新的alluser.txt文件
filepath = os.path.join(os.getcwd(), "alluser.txt")
f = open(filepath, "rb")
allUsers = pickle.load(f)
f.close()
# 然后我們恢復(fù)它
# allUsers = {}
atm = ATM(allUsers)
ok,完成了
運(yùn)行結(jié)果:

更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用 Python 獲取 Linux 系統(tǒng)信息的代碼
在本文中,我們將會探索使用Python編程語言工具來檢索Linux系統(tǒng)各種信息,需要的朋友可以參考下2014-07-07
python Stanza處理NLP任務(wù)使用詳解(多語言處理工具)
這篇文章主要為大家介紹了python Stanza處理NLP任務(wù)使用詳解,多語言處理工具使用實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python爬蟲實戰(zhàn)之爬取京東商品數(shù)據(jù)并實實現(xiàn)數(shù)據(jù)可視化
今天再帶大家簡單爬一波京東的商品數(shù)據(jù)唄,廢話不多說,文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下2021-06-06
解決python nohup linux 后臺運(yùn)行輸出的問題
今天小編就為大家分享一篇解決python nohup linux 后臺運(yùn)行輸出的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python在Windows環(huán)境下的文件路徑問題及解決辦法
在Python中處理Windows路徑時,經(jīng)常會遇到一些特殊的問題,在Windows中,路徑使用反斜杠(\)作為分隔符,而在其他操作系統(tǒng)中,路徑使用正斜杠(/)作為分隔符,本文給大家介紹了Python在Windows環(huán)境下的文件路徑問題及解決辦法,需要的朋友可以參考下2024-06-06
Python對象的底層實現(xiàn)源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python對象的底層實現(xiàn)源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

