1. 程式人生 > >python簡單的銀行系統

python簡單的銀行系統

凍結 += int open orm {} 身份證 sta 進行

先定義類:

銀行卡類

class Card:

def __init__(self,cid,pwd):

        self.cid = cid
self.pwd = pwd
self.money = 0
self.islock = False
管理員類
class Admin:
def __init__(self,name = ‘admin‘,password = ‘123456‘):
self.name = name
self.password = password

def welcome(self):
print(‘歡迎使用xx銀行系統‘)

#設置登錄
def login(self):
name = input(‘請輸入您的用戶名:‘)
password = input(‘請輸入您的密碼:‘)
if name == self.name and password == self.password:
return True
else:
return False

#設置菜單界面
def menu(self):
print(‘建戶【0】 銷戶【1】 查看余額【2】 存款【3】 取款【4】‘)
print(‘轉賬【5】 鎖卡【6】 解鎖【7】 用戶信息【8】 退出【9】‘)
用戶類
import os
import pickle

class User:
def __init__(self,name,uid,card):
self.name = name
self.uid = uid
self.card = card

def __str__(self):
return ‘姓名:{} 身份證號:{} 銀行卡:{}‘.format(self.name,self.uid,self.card.cid)

#保存用戶信息到文件中
@staticmethod
def save_info(userinfo):
#拼接要存放信息的目錄
pathname = os.path.join(os.getcwd(),‘user_info.db‘)
with open(pathname,‘wb‘) as fp:
pickle.dump(userinfo,fp)

@staticmethod
def load_info():
pathname = os.path.join(os.getcwd(),‘user_info.db‘)
if os.path.exists(pathname):
with open(pathname,‘rb‘) as fp:
ret = pickle.load(fp)
return ret
else:
return {}
銀行系統功能類
from help import Helper
from user import User
from card import Card

class Operate:
def __init__(self,userinfo={}):
self.userinfo = userinfo

#設置銀行系統的各種功能
def new_user(self):
name = input(‘請輸入您的名字:‘)
uid = input(‘請輸入您的身份證號:‘)
pwd = input(‘請輸入您的銀行卡密碼:‘)
#生成銀行卡號
cid = Helper.generate_card_cid()
#加密銀行卡密碼
pwd = Helper.encry_pwd(pwd)
#創建銀行卡和用戶對象
card = Card(cid,pwd)
user = User(name,uid,card)
self.userinfo[cid] = user
#保存用戶信息
User.save_info(self.userinfo)
print(‘開戶成功!‘)

def del_uesr(self):
while True:
cid = input(‘請輸入您的銀行卡號:‘)
if cid:
user = self.userinfo[cid]
count = 0
while True:
pwd = input(‘請輸入您的銀行卡密碼:‘)
if Helper.check_pwd(pwd,user.card.pwd):
del self.userinfo[cid]
User.save_info(self.userinfo)
break
else:
print(‘密碼錯誤,請重新輸入‘)
count += 1
if count >= 3:
print(‘密碼錯誤次數上限‘)
else:
print(‘銀行卡號不存在,請重新輸入‘)

def query_money(self):
cid = input(‘請輸入您的銀行卡號:‘)
user = self.userinfo[cid]
print(‘金額:{}‘.format(user.card.money))

def save_money(self):
cid = input(‘請輸入您的銀行卡號:‘)
user = self.userinfo[cid]
count = 0
if user.card.islock:
print(‘您的銀行卡已凍結‘)
return
while True:
pwd = input(‘請輸入您的銀行卡密碼:‘)
if Helper.check_pwd(pwd,user.card.pwd):
money = int(input(‘請輸入您要存入的金額:‘))
user.card.money += money
User.save_info(self.userinfo)
print(‘存款成功‘)
break
else:
print(‘密碼錯誤,請重新輸入‘)
count += 1
if count >= 3:
print(‘密碼錯誤次數已達上限‘)

def get_money(self):
cid = input(‘請輸入您的銀行卡號:‘)
user = self.userinfo[cid]
count = 0
if user.card.islock:
print(‘你的銀行卡已凍結‘)
return
while True:
pwd = input(‘請輸入您的銀行卡密碼:‘)
if Helper.check_pwd(pwd,user.card.pwd):
money = int(input(‘請輸入您要獲取的金額:‘))
if user.card.money >= money:
user.card.money -= money
User.save_info(self.userinfo)
print(‘取款成功‘)
break
else:
print(‘余額不足‘)
else:
print(‘密碼錯誤,請重新輸入‘)
count += 1
if count >= 3:
print(‘密碼錯誤已達上限,銀行卡已鎖定‘)
user.card.islock = True
#轉賬
def give_money(self):
cid = input(‘請輸入您的銀行卡號:‘)
user = self.userinfo[cid]
count = 0
if user.card.islock:
print(‘您的銀行卡已凍結‘)
return
count = 0
while True:
pwd = input(‘請輸入您的銀行卡密碼:‘)
if Helper.check_pwd(pwd,user.card.pwd):
cid1 = input(‘請輸入您要轉賬的銀行卡號:‘)
user1 = self.userinfo[cid1]
money = int(input(‘請輸入您要轉賬的金額:‘))
user.card.money -= money
user1.card.money += money
User.save_info(self.userinfo)
print(‘轉賬成功‘)
break
else:
print(‘密碼錯誤,請重新輸入‘)
count += 1
if count >= 3:
print(‘密碼錯誤次數已達上限,銀行卡已鎖定‘)
user.card.islock = True
def lockcard(self):
pass

def nolock(self):
cid = input(‘請輸入您要解鎖的銀行卡號:‘)
uid = input(‘請出示您的身份證:‘)
user = self.userinfo[cid]
if user.uid == uid:
user.card.islock = False
else:
print(‘身份證錯誤‘)

def show(self):
for i in self.userinfo:
print(self.userinfo[i])
再建立一個help模塊用於對卡號密碼的創建
import hashlib
from random import randint

class Helper:

@staticmethod
#生成銀行卡號
def generate_card_cid(length=8):
cid = ‘‘
for i in range(length):
cid += str(randint(0,9))
return cid

#加密用戶密碼信息
@staticmethod
def encry_pwd(pwd):
m = hashlib.md5()
m.update(pwd.encode(‘utf-8‘))
return m.hexdigest()

#核對用戶信息
@staticmethod
def check_pwd(pwd,pwd_hash):
m = hashlib.md5()
m.update(pwd.encode(‘utf-8‘))
return m.hexdigest() == pwd_hash
銀行系統運行代碼
from admin import Admin
from operate import Operate
from user import User
from card import Card

#創建管理員對象
admin = Admin()
admin.welcome()
count = 0

while True:
ret = admin.login()
#加載用戶信息
userinfo = User.load_info()
operate = Operate(userinfo)
if ret:
print(‘登錄成功‘)
while True:
admin.menu()
num = int(input(‘請輸入您要進行的操作:‘))
isbreak = False
if num == 0:
operate.new_user()

if num == 1:
operate.del_uesr()

if num == 2:
operate.query_money()

if num == 3:
operate.save_money()

if num == 4:
operate.get_money()

if num == 5:
operate.give_money()

if num == 6:
operate.lockcard()

if num == 7:
operate.nolock()

if num == 8:
operate.show()

if num == 9:
isbreak = True
break
if isbreak == True:
break

else:
print(‘密碼錯誤,請重新輸入‘)
count += 1
if count >= 3:
print(‘密碼錯誤上限‘)
由於第一次寫,可能寫的不好,希望大家在評論區多多提供寶貴的意見。

python簡單的銀行系統