簡易銀行系統,學習Python需要走的必經之路— _ —
阿新 • • 發佈:2018-12-22
簡易的銀行系統
1、需要分好建立的類,簡易的銀行系統分為五大塊:使用者的類、銀行卡的類、管理的類、bank操作的類、atm類。(最好分五個檔案建立 這樣看著清楚好操作。)
2、分好類後確定每個類的屬性和方法(函式)
下面就是各種原始碼:
### 使用者類: import os import pickle class User(): def __init__(self,name,uid,card): self.uid = uid # 身份證 self.name = name #使用者名稱 self.card = card # 銀行卡 def __str__(self): return "姓名{},身份證{},卡號{},密碼{}".format(self.name,self.uid,self.card.cid,self.card.pwd) ### 將使用者物件儲存到檔案中 @staticmethod def save_user(userinfo): pathname = os.path.join(os.getcwd(),'userinfo.db') with open(pathname,'wb') as fp: pickle.dump(userinfo,fp) ### 從檔案中取出使用者物件 @staticmethod def load_user(): pathname = os.path.join(os.getcwd(),'userinfo.db') if os.path.exists(pathname): with open(pathname,'rb') as fp: userinfo = pickle.load(fp) return userinfo else: return {} print(os.getcwd())
#銀行卡的類
class Card():
def __init__(self,cid,pwd):
self.cid= cid # 卡號
self.pwd = pwd # 密碼
self.money = 0 # 餘額
self.is_lock = False # 是否鎖定
# 管理的類 class Admin(): def __init__(self,admin='1',password='1'): self.admin = admin self.password = password ## 顯示歡迎頁面 def welcome(self): print("*"*30) print(" "*6+"歡迎使用銳士銀行") print("*"*30) ## 登入介面 def login(self): admin = input("請輸入管理員賬號:") password = input("請輸入管理員密碼:") if admin == '1' and password == '1': return True return False ## 操作選單 def menu(self): print("*" * 30) print("開戶[1] 銷戶[2] 查詢[3] 存款[4]") print("取款[5] 轉賬[6] 改密[7] 鎖定[8]") print("解鎖[9] 退出[0] 顯示所有使用者[10]") print("*" * 30)
#bank操作的類 from user import User from admin import Admin from atm import Atm if __name__ == "__main__": ## 建立管理員物件 admin = Admin() count = 0 while True: #展示歡迎頁面 admin.welcome() # 展示登入頁面 ret = admin.login() if ret: print('登陸成功') userinfo = User.load_user() # 建立atm物件 atm = Atm(userinfo) #定義變數(為了徹底退出while迴圈) is_quit = False while True: # 顯示操作選單 admin.menu() num = int(input('請輸入你的操作:')) if num == 0: print('徹底退出') is_quit = True break elif num == 1: atm.new_user() elif num ==2: atm.del_user() elif num ==3: atm.query_money() elif num ==4: atm.save_money() elif num ==5: atm.get_money() elif num ==6: atm.pass_money() elif num ==7: atm.change_pwd() elif num ==8: atm.lock_user() elif num ==9: atm.unlock_user() elif num ==10: atm.show_user() if is_quit: break else: print('賬號或者密碼錯誤') count += 1 if count >2: print('密碼錯誤達到上限,禁止登陸') break
ATM操作的類
from user import User
from card import Card
from 隨機生成卡號和md5加密 import Help
class Atm():
def __init__(self,userinfo = {}):
self.userinfo = userinfo
def new_user(self):
name = input("請輸入使用者名稱:")
uid = input("請輸入身份證:")
pwd = input("請輸入密碼:")
# 可以對密碼加密(讓密碼更安全 需要用到加密模組 我會放在最後給出原始碼 可以選擇是否加密)
# pwd = Help.generate_password(pwd)
# 生成卡號
cid = Help.generate_cid()
card = Card(cid,pwd)
user = User(name,uid,card)
#將使用者資訊新增到字典裡
self.userinfo[cid] = user
#儲存所有使用者資訊
User.save_user(self.userinfo)
print("開戶成功")
def del_user(self):
cid = input("請輸入你的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print('該卡被鎖定,請先解鎖')
return
count = 0
while count < 2:
pwd = input("請輸入你的密碼:")
if pwd == user.card.pwd: #if Help.check_password(pwd,user.card.pwd):
del self.userinfo[cid]
User.save_user(self.userinfo)
print('銷戶成功')
break
else:
count+=1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤超過上限,改卡被凍結")
else:
print("該卡號不存在")
def query_money(self):
cid = input("請輸入你的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("改卡已鎖定,請先解鎖")
print("您的餘額:¥{}".format(user.card.money))
else:
print("該卡號不存在")
def save_money(self):
cid = input("請輸入你的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("請解鎖")
return
count = 0
while count < 3:
pwd = input("請輸入密碼:")
if pwd == user.card.pwd :
money = int(input("請輸入金額:"))
user.card.money += money
User.save_user(self.userinfo)
print("存款成功")
break
else:
count += 1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤超過上限,已凍結")
else:
print("該卡號不存在")
def get_money(self):
cid = input("請輸入您的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("改卡被凍結,請解鎖")
return
count = 0
while count < 3:
pwd =input("請輸入你的密碼")
if pwd == user.card.pwd:
money = int(input("請輸入你要取得金額"))
if money < user.card.money :
user.card.money -= money
User.save_user(self.userinfo)
print("取款成功,餘額:{}".format(user.card.money))
break
else:
print("餘額不足,請充值")
else:
count+=1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤炒作上限,改卡被凍結")
else:
print("該卡號不存在或者輸入錯誤")
def pass_money(self):
cid = input("請輸入您的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("改卡被凍結,請解鎖")
return
count = 0
while count < 3:
pwd =input("請輸入你的密碼")
if pwd == user.card.pwd:
dt_cid = input("請輸入轉賬賬號:")
dt_user = self.userinfo.get(dt_cid)
if dt_cid:
money = int(input("請輸入轉賬金額:"))
if money < user.card.money:
user.card.money -= money
dt_user.card.money += money
User.save_user(self.userinfo)
print("轉賬成功")
break
else:
print("餘額不足")
else:
print("收款人卡號錯誤")
else:
count+=1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤炒作上限,改卡被凍結")
else:
print("該卡號不存在或者輸入錯誤")
def change_pwd(self):
cid = input("請輸入您的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("改卡被凍結,請解鎖")
return
count = 0
while count < 3:
pwd =input("請輸入你的密碼")
if pwd == user.card.pwd:
new_pwd = input("請輸入新密碼:")
user.card.pwd = new_pwd
User.save_user(self.userinfo)
print("修改成功")
break
else:
count+=1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤炒作上限,改卡被凍結")
else:
print("該卡號不存在或者輸入錯誤")
def lock_user(self):
cid = input("請輸入您的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("改卡被凍結,請解鎖")
return
count = 0
while count < 3:
pwd = input("請輸入你的密碼")
if pwd == user.card.pwd:
user.card.is_lock = True
User.save_user(self.userinfo)
print("凍結成功")
break
else:
count += 1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤炒作上限,改卡被凍結")
else:
print("該卡號不存在或者輸入錯誤")
def unlock_user(self):
cid = input("請輸入您的卡號:")
user = self.userinfo.get(cid)
if user:
if user.card.is_lock:
print("改卡被凍結,請解鎖")
return
count = 0
while count < 3:
pwd = input("請輸入你的密碼")
if pwd == user.card.pwd:
user.card.is_lock = False
User.save_user(self.userinfo)
print("解凍成功")
break
else:
count += 1
print("密碼錯誤")
else:
user.card.is_lock = True
User.save_user(self.userinfo)
print("密碼錯誤炒作上限,改卡被凍結")
else:
print("該卡號不存在或者輸入錯誤")
def show_user(self):
for user in self.userinfo:
print(self.userinfo[user])
上面我提到的密碼加密情況可以根據自得需要來決定:如果不需要加密直接在後面的判斷密碼程式碼裡直接判斷 : if pwd == user.card.pwd:。如果需要加密,在後面判斷密碼的程式碼需要改變判斷:if Help.check_password(pwd,user.card.pwd):
#隨機的銀行卡號 和 md5加密 和 解密
from random import randint
import hashlib
class Help():
@staticmethod
#生成隨機銀行卡號
def generate_cid(length=8):
cid =''
for i in range(length):
cid += str(randint(0,9))
return cid
# 生成md5加密後的內容
@staticmethod
def generate_password(password):
# 建立一個md5物件
m = hashlib.md5()
#設定加密原始內容
m.update(password.encode('utf-8'))
# 返回加密後的字串
return m.hexdigest()
# 校驗使用md5加密的密碼
@staticmethod
def check_password(pwd,pwd_hash):
m = hashlib.md5()
m.update(pwd.encode('utf-8'))
return m.hexdigest == pwd_hash
if __name__ == "__main__":
print(Help.generate_password('123456'))