1. 程式人生 > >python面向物件銀行後臺管理系統

python面向物件銀行後臺管理系統

用簡單的面向物件方法模擬一個銀行終端處理器
首先,先處理管理員登入,在這裡預設管理員登入賬號為admin 密碼為123456

# 管理員系統
class Admin:
    # 輸入引數   管理員卡號   管理員密碼
    def __init__(self, account='admin', password='123456'):
        self.account = account
        self.password = password
    # 歡迎介面

    def welcome(self):
        print('+' + '-' * 30 + '+')
        print('|' + ' ' * 30 + '|')
        print('|' + ' ' * 3 + '歡迎使用xx銀行作業系統' + ' ' * 5 + '|')
        print('|' + ' ' * 30 + '|')
        print('+' + '-' * 30 + '+')
    # 操作介面

    def menu(self):
        print('+' + '-' * 30 + '+')
        print('|' + '開    戶[1]         銷    戶[2]' + '|')
        print('|' + '查詢餘額[3]         取    款[4]' + '|')
        print('|' + '存    款[5]         轉    賬[6]' + '|')
        print('|' + '修改密碼[7]         鎖定賬戶[8]' + '|')
        print('|' + '解鎖賬戶[9]         退    出[0]' + '|')
        print('|' + '     查詢所有賬戶資訊[10]      ' + '|')
        print('+' + '-' * 30 + '+')
    # 管理員登入

    def login(self):
        account = input('請輸入管理員賬號:')
        password = input('請輸入管理員密碼:')

        if account == 'admin' and password == '123456':
            return True
        return False
# 這裡模擬了管理員的登入以及操作頁面,接下來我們建立一個簡單的使用者登入的類
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_user(userinfo):
        pathname = os.path.join(os.getcwd(), 'userinfo.bd')
        with open(pathname, 'wb') as fp:
            pickle.dump(userinfo, fp)
    # 將資料讀取

    @staticmethod
    def load_user():
        pathname = os.path.join(os.getcwd(), 'userinfo.bd')
        if os.path.exists(pathname):
            with open(pathname, 'rb') as fp:
                userinfo = pickle.load(fp)
                return userinfo
        else:
            return {}
# 使用者類包含了使用者的資訊以及使用者資訊的列印,與上個程式碼裡的10:檢視使用者資訊相對應,為了更加簡單的測試,後兩個類函式使用靜態方法,可以使銀行開戶時把使用者資訊進行簡單持久化儲存(在這裡用的是序列化的方式)和使用者登入使用系統時提取使用者資料

大家眾所周知當我們去銀行的時候,我們需要插入銀行卡,那麼我們就需要一個銀行卡類來儲存我們的使用者密碼,銀行卡號,卡上金額。當然,當你信譽不好時,銀行同樣也會鎖定你的銀行卡

class Card:
    def __init__(self, cid, pwd):
        self.cid = cid
        self.pwd = pwd
        self.money = 0              # 餘額
        self.is_lock = False        # 是否鎖定

當我們把所有的類都建立好後,我們需要把我們寫的類的內容進行一個操作整理

from user import User
from admin import Admin
from operate import Operate


if __name__ == '__main__':
    # 建立管理員物件
    admin = Admin()
    # 統計管理員登入次數
    count = 0
    while True:
        # 匯入歡迎介面
        admin.welcome()
        # 管理員登入
        ret = admin.login()
        if ret:
            print('登入成功')
            # 載入所有使用者資料
            userinfo = User.load_user()
            # 建立操作物件
            op = Operate(userinfo)
            while True:
                # 匯入操作頁面
                admin.menu()
                num = int(input('請輸入您的操作:'))
                if num == 0:
                    print('徹底退出')
                    is_quit = True
                    break
                elif num == 1:
                    op.new_user()
                elif num == 2:
                    op.del_user()
                elif num == 3:
                    op.query_money()
                elif num == 4:
                    op.get_money()
                elif num == 5:
                    op.save_money()
                elif num == 6:
                    op.pass_money()
                elif num == 7:
                    op.change_pwd()
                elif num == 8:
                    op.lock_user()
                elif num == 9:
                    op.unlock_user()
                elif num == 10:
                    op.show_users()
                else:
                    print('操作有誤,請重新輸入')
            if is_quit:
                break
        else:
            print('登入失敗')
            count += 1
            if count >= 3:
                print('錯誤已達上限,禁止登入')
                break

我在這裡描寫的很清楚,管理員的登入,以及系統的退出,一系列的操作只是為了規劃我們的程式,接下來大家會發現還少了一個類,那就是操作類,我們需要把操作的函式歸結到一個類裡,那就是操作類,在這裡我們可以實行各種操作的新增:

from helper import Helper
from card import Card
from user import User
import time


class Operate:

    def __init__(self, userinfo):
        self.userinfo = userinfo

    # 開戶
    def new_user(self):
        name = input('請輸入您的姓名:')
        uid = input('請輸入您的身份證號碼:')
        while True:
            pwd = input('請輸入您的密碼:')
            pwd2 = input('請再次輸入您的密碼:')
            if pwd != pwd2:
                print('您所輸入的兩次密碼不一致,請重新輸入:')
            else:
                pwd = Helper.generate_password_hash(pwd)
                cid = Helper.generate_cid()
                card = Card(cid, pwd)
                user = User(name, uid, card)
                self.userinfo[cid] = user
                User.save_user(self.userinfo)
                time.sleep(1)
                print('開戶成功')
                break

    # 銷戶
    def del_user(self):
        cid = input('請輸入您的卡號:')
        user = self.userinfo.get(cid)
        if user:
            if user.card.is_lock:
                print('該卡已經鎖定,請先去解鎖')
                return
            else:
                count = 0
                while count < 3:
                    pwd = input('請輸入您的密碼:')
                    if Helper.check_password_hash(pwd, user.card.pwd):
                        del self.userinfo[cid]
                        time.sleep(1)
                        print('銷戶成功')
                        User.save_user(self.userinfo)
                        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('該卡已經鎖定,請先去解鎖')
                return
            else:
                count = 0
                while count < 3:
                    pwd = input('請輸入您的密碼:')
                    if Helper.check_password_hash(pwd, user.card.pwd):
                        time.sleep(1)
                        print('您的餘額為¥{}元'.format(user.card.money))
                        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[cid]
        if user:
            if user.card.is_lock:
                print('該卡已經鎖定,請先去解鎖')
                return
            else:
                money = int(input('請輸入金額'))
                count = 0
                while count < 3:
                    pwd = input('請輸入您的密碼:')
                    if Helper.check_password_hash(pwd, user.card.pwd):
                        if user.card.money > money:
                            user.card.money -= money
                            User.save_user(self.userinfo)
                            time.sleep(1)
                            print('取款成功')
                            break
                        else:
                            print('餘額不足,請及時充值')
                    else:
                        count += 1
                        print('密碼錯誤,請重新輸入密碼')
                else:
                    user.card.is_lock = True
                    User.save_user(self.userinfo)
                    print('密碼錯誤已達上限,該卡已鎖定')
        else:
            print('卡號不存在')

    # 存錢
    def save_money(self):
        cid = input('請輸入您的卡號:')
        user = self.userinfo[cid]
        if user:
            if user.card.is_lock:
                print('該卡已經鎖定,請先去解鎖')
                return
            else:
                money = int(input('請輸入金額'))
                count = 0
                while count < 3:
                    pwd = input('請輸入您的密碼:')
                    if Helper.check_password_hash(pwd, user.card.pwd):
                        user.card.money += money
                        User.save_user(self.userinfo)
                        time.sleep(1)
                        print('存款成功')
                        break
                    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[cid]
        if user:
            if user.card.is_lock:
                print('該卡已經鎖定,請先去解鎖')
                return
            else:
                money = int(input('請輸入金額'))
                count = 0
                while count < 3:
                    pwd = input('請輸入您的密碼:')
                    if Helper.check_password_hash(pwd, user.card.pwd):
                        dist_cid = input('請輸入收款人的卡號:')
                        dist_user = self.userinfo[dist_cid]
                        if dist_user:
                            if user.card.money >= money:
                                user.card.money -= money
                                dist_user.card.money += money
                                User.save_user(self.userinfo)
                                time.sleep(1)
                                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:
            count = 0
            while count < 3:
                pwd = input('請輸入原密碼:')
                if Helper.check_password_hash(pwd, user.card.pwd):
                        new_pwd = input('請輸入新密碼:')
                        new_pwd2 = input('請再次輸入新密碼:')
                        if new_pwd == new_pwd2:
                            user.card.pwd = Helper.generate_password_hash(new_pwd)
                            User.save_user(self.userinfo)
                            time.sleep(1)
                            print('密碼修改成功')
                        else:
                            print('您所輸入的兩次密碼不一致')
                        break
                else:
                    count += 1
                    print('密碼錯誤,請重新輸入密碼')
        else:
            print('卡號不存在')

    # 鎖定賬號
    def lock_user(self):
        cid = input('請輸入您的卡號:')
        user = self.userinfo.get(cid)
        if user:
            uid = input('請出示您的身份證:')
            if uid == user.uid:
                user.card.is_lock = True
                User.save_user(self.userinfo)
                time.sleep(1)
                print('鎖定成功')
            else:
                print('身份證是不是帶錯了@[email protected]')
        else:
            print('卡是不是折了:卡號錯了')

    # 解鎖賬號
    def unlock_user(self):
        cid = input('請輸入您的卡號:')
        user = self.userinfo.get(cid)
        if user:
            uid = input('請出示您的身份證:')
            if uid == user.uid:
                user.card.is_lock = False
                User.save_user(self.userinfo)
                time.sleep(1)
                print('解鎖成功')
            else:
                print('身份證是不是帶錯了@[email protected]')
        else:
            print('卡是不是折了:卡號錯了')

    # 檢視賬戶資訊
    def show_users(self):
        for user in self.userinfo:
            print(self.userinfo[user])
            time.sleep(1)

在這個類裡,大家不免看到了幾個helper類的方法呼叫,因為這些方法(隨機生成卡號,登入密碼加密儲存)是一些輔助手段,所以我們放在了helper類裡面

from random import randint
import hashlib


class Helper:
    # 生成卡號
    @staticmethod
    def generate_cid(lenght=8):
        cid = ''
        for i in range(lenght):
            cid += str(randint(0, 9))
        return cid
    # 生成md5加密後的內容

    @staticmethod
    def generate_password_hash(password):
        m = hashlib.md5()
        m.update(password.encode('utf-8'))
        return m.hexdigest()
    # 校驗md5加密後的密碼

    @staticmethod
    def check_password_hash(pwd, pwd_hash):
        m = hashlib.md5()
        m.update(pwd.encode('utf-8'))
        return m.hexdigest() == pwd_hash

我們的密碼加密使用的是雜湊加密,而我們的生成卡號也不那麼嚴謹,採用的是隨機數方法

接下來我們就可以盡情的測試程式碼了,可以開戶,並根據銀行卡號和密碼進行存取和轉賬,當然如果你忘了密碼可是要被鎖定的哦,千萬別忘了去解鎖賬戶,我們也可以更改密碼呦!

當然我寫的程式碼裡還有很多漏洞,畢竟是一個初學者,我才學了兩星期了,期待各位大佬的指教,謝謝!