Bootstrap

Python写银行系统

简介

两种都是静态银行系统,采用的知识都差不多,接下来大家来看看那种写的好。

静态银行系统(一)

此银行系统有11种功能,分别是开户、查询、取款、存款、转账、改密、冻结、解冻、补卡、销户以及退出,功能挺齐全的。

设置访问页面和菜单页面

import time
import time
import os
import pickle

class Admin(object):
    admin = 'admin'
    passwd = 'passwd'

    def AdminView(self):
        print("**********************************************")
        print("*               欢迎登录银行系统               *")
        print("**********************************************")

    #菜单界面
    def FunctionView(self):
        print("**********************************************")
        print("*           (1)开户     (2)查询              *")
        print("*           (3)取款     (4)存款               *")
        print("*           (5)转账     (6)改密               *")
        print("*           (7)锁定     (8)解锁               *")
        print("*           (9)补卡    (0)销户              *")
        print("*                  退出(q)                   *")
        print("**********************************************")

管理员登录验证

    def Check(self):
        inputAdmin = input("请输入管理员账户: ")
        if self.admin != inputAdmin:
            print("管理员账号输入错误!")
            return -1
        inputPasswd = input("请输入管理员密码: ")
        if self.passwd != inputPasswd:
            print("管理员密码输入错误!")
            return -1
        print("操作成功,请稍后...")
        time.sleep(2)
        return 0

开户功能

import random
class ATM(object):
    def __init__(self, allusers):
        #储存信息
        self.allUsers = allusers
#用户建立
    def CreatUser(self):
        name = input("请输入您的姓名:")
        idCard = input("请输入您的身份证号:")
        phone = input("请输入您的电话号码:")
        preMoney = int(input("请输入您的预存款金额:"))
        if preMoney < 0:
                print("预存款输入有误,开户失败......")
                return -1
        onePasswd = input("请输入密码:")
        if not self.checkPasswd(onePasswd):
            print("密码输入有误,开户失败......")
            return -1
            # 生成卡号
        cardStr = self.creatcardStr()

        card = Card(cardStr, onePasswd, preMoney)
        user = Person(name, idCard, phone, card)
        self.allUsers[cardStr] = user
        print("开户成功,请牢记卡号(%s)......" % cardStr)

查询功能

        # 查询余额
    def searchUserInfo(self):
        cardnum = input("请输入您的卡号:")
            # 验证是否存在该卡号
        user = self.allUsers.get(cardnum)
        if not user:
            print("该卡号不存在,查询失败......")
            return -1
            # 判断是否锁定
        if user.card.cardlock == True:
            print("该卡已经被锁定,请解锁后再使用其他操作......")
            return -1
            # 验证密码
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误,该卡已经被锁定......")
            user.card.cardlock = True
            return -1
            # 查询成功,输出结果
        print("账户:%s     余额:%d" % (user.card.cardStr, int(user.card.cardmoney)))

取款功能

        # 取款
    def getMoney(self):
        cardnum = input("请输入您的卡号:")
            # 验证是否存在该卡号
        user = self.allUsers.get(cardnum)
        if not user:
            print("该卡号不存在,查询失败......")
            return -1
            # 判断是否锁定
        if user.card.cardlock == True:
            print("该卡已经被锁定,请解锁后再使用其他操作......")
            return -1
            # 验证密码
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误,该卡已经被锁定......")
            user.card.cardlock = True
            return -1
        print('您目前余额为%d'% user.card.cardmoney)
        getmoney = int(input("请输入您取款金额:"))
        nowmoney = int(user.card.cardmoney)
        if getmoney > nowmoney:
            print("余额不足,取款失败......")
            return -1
        nowmoney -= getmoney
        user.card.cardmoney = nowmoney
        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 == True:
            print("该卡已经被锁定,请解锁后再使用其他操作......")
            return -1
            # 验证密码
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误,该卡已经被锁定......")
            user.card.cardlock = True
            return -1
        savemoney = int(input("请输入您存款金额:"))
        nowmoney = int(user.card.cardmoney)
        nowmoney += savemoney
        user.card.cardmoney = nowmoney
        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 == True:
            print("该卡已经被锁定,请解锁后再使用其他操作......")
            return -1
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误,该卡已经被锁定......")
            user.card.cardlock = True
            return -1
        print("账户:%s     余额:%d" % (user.card.cardStr, int(user.card.cardmoney)))
        tocardStr = input("请输入您希望转账的账户:")
        usertoid = self.allUsers.get(tocardStr)
        if not usertoid:
            print("该卡号不存在,查询失败......")
            return -1
        tomoney = int(input("请输入您希望转账的金额:"))
        nowmoney = int(user.card.cardmoney)
        tonowmoney = int(usertoid.card.cardmoney)
        if tomoney > nowmoney:
            print("余额不足,转账失败......")
            return -1
        nowmoney -= tomoney
        tonowmoney += tomoney
        user.card.cardmoney = nowmoney
        usertoid.card.cardmoney = tonowmoney
        print("转账成功,您目前余额为:%d" % user.card.cardmoney)

改密功能

        # 改密码
    def changePasswd(self):
        cardnum = input("请输入您的卡号:")
        user = self.allUsers.get(cardnum)
        if not user:
            print("该卡号不存在,查询失败......")
            return -1
        if user.card.cardlock == True:
            print("该卡已经被锁定,请解锁后再使用其他操作......")
            return -1
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误,该卡已经被锁定......")
            user.card.cardlock = True
            return -1
        newpasswd = input("请输入新密码:")
        newpasswd2 = input("请确认新密码:")
        if newpasswd != newpasswd2:
            print("两次密码输入不一致,密码修改失败")
            return -1
        user.card.cardpasswd = newpasswd
        print("密码修改成功......")

冻结功能

        # 锁定
    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
            # 锁定
        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
        temid = input("请输入您的身份证号:")
        if temid != user.idCard:
            print("身份证号输入错误,解锁失败......")
            return -1
            # 解锁
        user.card.cardlock = False
        print("解锁成功......")

补卡功能

        # 补卡
    def newCard(self):
        cardnum = input("请输入您的卡号:")
        user = self.allUsers.get(cardnum)
        if not user:
            print("该卡号不存在,查询失败......")
            return -1
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误......")
            return -1
            # 重新生成卡号
        newcard = self.creatcardStr()
        user.card.cardStr = newcard
        print("补卡办理成功,这是您的新卡号:%s" % user.card.cardStr)

销户功能

        # 销户
    def killUser(self):
        cardnum = input("请输入您的卡号:")
        user = self.allUsers.get(cardnum)
        if not user:
            print("该卡号不存在,查询失败......")
            return -1
        if not self.checkPasswd(user.card.cardpasswd):
            print("密码错误,销户失败......")
            return -1
        self.allUsers.pop(cardnum)
        print("该账户已经被销户......")

        # 验证密码,循环三次没有正确就输出错误
    def checkPasswd(self, realPasswd):
        for i in range(3):
            temPasswd = input("请确认密码:")
            if temPasswd == realPasswd:
                return True
        return False

开户功能卡号生成方式

        # 随机生成卡号
    def creatcardStr(self):
            # 验证密码是否重复
        while True:
            str = ""
            for i in range(6):
                ch = chr(random.randrange(ord('0'), ord('9') + 1))
                str += ch
                # 断是否重复
            if not self.allUsers.get(str):
                return str
class Person(object):
    def __init__(self,name,idCard,phone,card):
        self.name = name
        self.idCard = idCard
        self.phone = phone
        self.card = card
class Card(object):
    def __init__(self,cardStr,cardpasswd,cardmoney):
        self.cardStr = cardStr
        self.cardpasswd = cardpasswd
        self.cardmoney = cardmoney
        self.cardlock = False

主程序

def main():
    # 界面对象
    admin = Admin()

    # 管理员开机
    admin.AdminView()
    if admin.Check():
        return -1

    # 存储信息的文件是否存在
    if os.path.exists("userinfo.txt"):
        filepath = "userinfo.txt"
    else:
        open("userinfo.txt", "wb")
        filepath = "userinfo.txt"

    if os.path.getsize(filepath):
        f = open(filepath, "rb")
        allusers = pickle.load(f)
    else:
        allusers = "正在打开功能菜单"
        allusers = {}
    print((allusers))
    atm = ATM(allusers)

    while True:
        admin.FunctionView()
        option = input("请输入您的操作:")
        if option == '1':
            # 开户
            atm.CreatUser()
        elif option == '2':
            # 查询
            atm.searchUserInfo()
        elif option == '3':
            # 取款
            atm.getMoney()
        elif option == '4':
            # 存款
            atm.saveMoney()
        elif option == '5':
            # 转账
            atm.transferMoney()
        elif option == '6':
            # 改密码
            atm.changePasswd()
        elif option == '7':
            # 锁定
            atm.lockUser()
        elif option == '8':
            # 解锁
            atm.unlockUser()
        elif option == '9':
            # 补卡
            atm.newCard()
        elif option == '0':
            # 销户
            atm.killUser()
        elif option == 'q':
            # 退出
            if not admin.Check():
                # 将当前系统中的用户信息保存到文件中
                f = open(filepath, "wb")
                pickle.dump(atm.allUsers, f)
                f.close()
                return -1

        time.sleep(2)

启动

if __name__ == "__main__":
	main()

静态银行系统(二)

此银行系统有6种功能,分别是开户、查询、取款、存款、转账以及退出,每选择一个功能都要确保用户信息是否正确。

管理员登录页面

class Preson(object):
    def __init__(self):
        self.name = ''
        self.Passwd = ''
        self.cardCode = 0
        self.money = 0
class Bank(object):
    dict1 = {}
    list1 = []

开户功能

    def open(self):        #开户
        per1 = Preson()
        per1.name = input("请输入用户名:")
        per1.Passwd = input("请输入登录密码:")
        c = per1.name
        Bank.dict1[c] = per1
        print("注册成功")

查询功能

    def see(self):           #查询方法
        a = input("请输入您的用户名")
        if a not in Bank.dict1.keys():       #判断是否存在用户名
            print("用户名不存在,返回上一级")
            return -1
        if Bank.dict1[a].cardCode >= 3:      #判断错误次数是否已有三次
            print("已错误3次")
            print("*****返回上一级*****")
            return -1
        else:
            while Bank.dict1[a].cardCode < 3:
                b = input("请输入密码")
                if b == Bank.dict1[a].Passwd:          #判断密码是否正确
                    Bank.dict1[a].cardCode = 0
                    print("密码正确,您当前余额为:",Bank.dict1[a].money)
                    print("*****返回上一级*****")
                    return -1
                else:
                    print("密码错误,请重新输入")
                    Bank.dict1[a].cardCode += 1
            print("已输入错误3次")
            print("*****返回上一级*****")

存款功能

    def saves(self):         #存款方法
        a = input("请输入用户名")
        if a not in Bank.dict1.keys():
            print("用户名不存在,返回上一级")
            return -1
        if Bank.dict1[a].cardCode >= 3:
            print("已错误3次")
            print("*****返回上一级*****")
            return -1
        else:
            while Bank.dict1[a].cardCode < 3:
                b = input("请输入密码")
                if b == Bank.dict1[a].Passwd:
                    Bank.dict1[a].cardCode = 0
                    while True:
                        c= int(input("密码正确,请输入您的存款金额:"))
                        if c <= 0:
                            print("存款金额必须大于0")
                        else:
                            break
                    Bank.dict1[a].money += c
                    print("存款成功您当前余额为:", Bank.dict1[a].money)
                    print("*****正在返回上一级*****")
                    return -1
                else:
                    print("密码错误,请重新输入")
                    Bank.dict1[a].cardCode += 1
            print("已输入错误3次")
            print("*****返回上一级*****")
    def draw(self):     #取款
        a = input("请输入您的用户名")
        if a not in Bank.dict1.keys():
            print("用户名不存在,返回上一级")
            return -1
        if Bank.dict1[a].cardCode >= 3:
            print("已错误3次")
            print("*****返回上一级*****")
            return -1
        else:
            while Bank.dict1[a].cardCode < 3:
                b = input("请输入密码")
                if b == Bank.dict1[a].Passwd:
                    Bank.dict1[a].cardCode = 0
                    while True:
                        c = int(input("密码正确,请输入您的取款金额:"))
                        if c >  Bank.dict1[a].money:
                            print("余额不足,请重新输入取款金额")
                        elif c <= 0:
                            print("取款金额必须大于0")
                        else:
                            break
                    Bank.dict1[a].money -= c
                    print("取款成功您当前余额为:", Bank.dict1[a].money)
                    print("*****正在返回上一级*****")
                    return -1
                else:
                    print("密码错误,请重新输入")
                    Bank.dict1[a].cardCode += 1
            print("已输入错误3次")
            print("*****返回上一级*****")

转账功能

    def transfer(self):#转账
        a = input("请输入您的用户名")
        if a not in Bank.dict1.keys():
            print("用户名不存在,返回上一级")
            return -1
        if Bank.dict1[a].cardCode >= 3:
            print("已错误3次")
            print("*****返回上一级*****")
            return -1
        else:
            while Bank.dict1[a].cardCode < 3:
                b = input("请输入密码")
                if b == Bank.dict1[a].Passwd:
                    Bank.dict1[a].cardCode = 0
                    c = input("密码正确,请输入你想转账的卡号:")
                    if c not in Bank.dict1.keys():
                        print("用户名不存在,返回上一级")
                        return -1
                    if Bank.dict1[c].cardCode >= 3:
                        print("密码错误次数过多")
                        print(".*****返回上一级*****")
                        return -1
                    if a == c:
                        print("不能给自己转账,返回上一级")
                        return -1
                    else:
                        while True:
                            d = int(input("请输入您想转账的金额:"))
                            if d > Bank.dict1[a].money:
                                print("余额不足,请重新输入转账金额")
                            elif d <= 0:
                                print("转账金额必须大于0")
                            else:
                                break
                        Bank.dict1[a].money -= d
                        Bank.dict1[c].money += d
                        print("转账成功,您当前余额为:", Bank.dict1[a].money )
                    print("*****正在返回上一级*****")
                    return -1
                else:
                    print("密码错误,请重新输入")
                    Bank.dict1[a].cardCode += 1
            print("已输入错误3次")
            print("*****返回上一级*****")
    def changepw(self):
        a = input("请输入您的用户名")
        if a not in Bank.dict1.keys():
            print("用户名不存在,返回上一级")
            return -1
        if Bank.dict1[a].cardCode >= 3:
            print("已错误3次")
            print("*****返回上一级*****")
            return -1
        else:
            while Bank.dict1[a].cardCode < 3:
                b = input("请输入密码")
                if b == Bank.dict1[a].Passwd:
                    c = input("请输入新的密码....")
                    while c != input("请确认您的新密码"):
                        c = input("请输入新的密码....")
                    Bank.dict1[a].Passwd = c
                    print("密码重置成功")
                    print("*****返回上一级*****")
                    return -1
                else:
                    print("密码错误,请重新输入")
                    Bank.dict1[a].cardCode += 1
            print("已输入错误3次")
            print("*****返回上一级*****")

退出功能

    def exit1(self):
        exit()

主程序

    def admin_register(self):
        print("欢迎使用本系统")
        while 1:
            admin = input("请输入管理员账号:")
            password = input("请输入管理员密码:")
            if admin == "admin" and password == "12345":
                print("登录成功,正在跳转......")
                break
            else:
                print("账号或密码错误,请重新输入")
        while 1:
            print("*********************************")
            print("*\t1、开户\t\t\t2、查询\t\t*\n*\t3、存款\t\t\t4、取款\t\t*"
                  "\n*\t5、转账\t\t\t6、修改密码\t*\n*\t\t\t7、退出\t\t\t\t*")
            print("*********************************")
            a = input("请输入操作:")
            if a == "1":
                self.open()
            elif a == "2":
                self.see()
            elif a == "3":
                self.saves()
            elif a == "4":
                self.draw()
            elif a == "5":
                self.transfer()
            elif a == "6":
                self.changepw()
            elif a == "7":
                self.exit1()
            else:
                print("输入错误,请重新输入......")
a = Bank()
a.admin_register()

后记

这次的小项目学会了吗,多支持一下哦!

这两种Python写的银行系统喜欢那种呢?

如果你觉得文章写的还不错,欢迎你帮助宣传扩散一下,这将是我继续更新的动力,切忌不要白嫖,会让自己变得廉价,好的文章值得分享。

害怕找不到的话请记得点赞评论关注哦!

;