本文实例讲述了Python实现的银行系统模拟程序。分享给大家供大家参考,具体如下:
银行系统模拟程序
1、概述
"_blank" href="https://github.com/liangdongchang/pyBankSystem.git" rel="external nofollow" >https://github.com/liangdongchang/pyBankSystem.git
1、bankSystem.py文件
from view import View from atm import ATM from person import Person def func(view,atm,per): view.funcInterface() choice = input("请选择您要办理的业务:") if choice == '1': return per.checkMoney(atm) elif choice == '2': return per.saveMoney(atm) elif choice == '3': return per.getMoney(atm) elif choice == '4': return per.transferMoney(atm) elif choice == '5': return per.changePassword(atm) elif choice == '6': return per.unlockAccount(atm) elif choice == '7': return per.closeAccount(atm) elif choice == 'T': if per.exit(atm): return True else: print("输入有误!") def main(): # 管理员登录名为'admin',密码为'123' view = View("admin",'123') view.initface() atm = ATM() view.login() per = Person() while True: view.funcInit() choice = input("请选择您要办理的业务:") if choice == '1': per.newAccount(atm) elif choice == '2': if per.login(atm): while True: if func(view,atm,per) == None: continue else: break elif choice == '3': per.findBackPassword(atm) elif choice == '4': per.lockAccount(atm) elif choice == 'T': if per.exit(atm): # 管理员注销系统 if view.logout(): return True else: print("输入有误!") if __name__ == '__main__': main()
2、card.py文件:
''' 卡: 类名:Card 属性:卡号【6位随机】 密码 余额 绑定的身份证号 手机号 ''' class Card(object): def __init__(self, cardId, password, money,identityId,phoneNum,cardLock='False'): self.cardId = cardId self.password = password self.money = money self.identityId = identityId self.phoneNum = phoneNum self.cardLock = cardLock
3、readAppendCard.py文件:
''' 功能:读取文件cardInfo.txt的信息 方法:读、写、删 ''' from card import Card import json # 读 class ReadCard(Card): def __init__(self, cardId='', password='', money=0, identityId='', phoneNum='', cardLock=''): Card.__init__(self, cardId, password, money, identityId, phoneNum, cardLock) def dict2Card(self, d): return self.__class__(d["cardId"], d["password"], d["money"],d["identityId"],d["phoneNum"], d["cardLock"]) def read(self): # card对象转为字典 with open("cardinfo.txt","r",encoding="utf-8") as fr: cards = [] for re in fr.readlines(): cards.append(self.dict2Card(eval(re))) return cards # 写 class AppendCard(Card): def __init__(self): Card.__init__(self, cardId = '', password = '', money = 0, identityId = '', phoneNum = '', cardLock='') def card2Dict(self,card): return {"cardId": card.cardId, "password": card.password, "money": card.money, "identityId": card.identityId, "phoneNum": card.phoneNum, "cardLock": card.cardLock } def append(self,card,w= 'a'): # 默认是追加,如果w='w'就清空文件 if w == 'w': with open("cardinfo.txt", "w", encoding="utf-8") as fa: fa.write('') else: with open("cardinfo.txt", "a", encoding="utf-8") as fa: json.dump(card, fa, default=self.card2Dict) fa.write('\n') # 删 class Del(object): def del_(self,cardId): readcard = ReadCard() cards = readcard.read() for card in cards: # 删除输入的卡号 if cardId == card.cardId: cards.remove(card) break else: print("卡号不存在!") return False # 重新写入文件 appendcard = AppendCard() appendcard.append('',w='w') for card in cards: appendcard.append(card) return True
4、person.py
''' 人 类名:Person 行为:开户、查询、取款、存储、转账、改密、销户、退出 ''' class Person(object): def __init__(self,name='',identity='',phoneNum='',card=None): self.name = name self.identity = identity self.phoneNum = phoneNum self.card = card # 登录 def login(self,atm): card = atm.login() if card: self.card = card return True else: return False # 开户 def newAccount(self,atm): return atm.newAccount() #找回密码 def findBackPassword(self,atm): return atm.findBackPassword() # 查询余额 def checkMoney(self, atm): return atm.checkMoney(self.card) # 存钱 def saveMoney(self, atm): return atm.saveMoney(self.card) # 取钱 def getMoney(self, atm): return atm.getMoney(self.card) # 转账 def transferMoney(self, atm): return atm.transferMoney(self.card) # 销户 def closeAccount(self, atm): return atm.closeAccount(self.card) # 挂失 def lockAccount(self, atm): return atm.lockAccount() # 解锁 def unlockAccount(self, atm): return atm.unlockAccount(self.card) # 改密 def changePassword(self, atm): return atm.changePassword(self.card) # 退出系统 def exit(self, atm): return atm.exit()
5、view.py
''' 管理员界面 类名:View 属性:账号,密码 行为:管理员初始化界面 管理员登陆 系统功能界面 管理员注销 系统功能:开户 查询 取款 存储 转账 改密 销户 退出 ''' from check import Check import time class View(object): def __init__(self,admin,password): self.admin = admin self.password = password # 管理员初始化界面 def initface(self): print("*------------------------------------*") print("| |") print("| 管理员界面正在启动,请稍候... |") print("| |") print("*------------------------------------*") time.sleep(1) return #管理员登录界面 def login(self): print("*------------------------------------*") print("| |") print("| 管理员登陆界面 |") print("| |") print("*------------------------------------*") check = Check() check.userName(self.admin,self.password) print("*-------------登陆成功---------------*") print(" 正在跳转到系统功能界面,请稍候... ") del check time.sleep(1) return # 管理员注销界面 def logout(self): print("*------------------------------------*") print("| |") print("| 管理员注销界面 |") print("| |") print("*------------------------------------*") #确认是否注销 check = Check() if not check.isSure('注销'): return False check.userName(self.admin,self.password) print("*-------------注销成功---------------*") print(" 正在关闭系统,请稍候... ") del check time.sleep(1) return True #系统功能界面 ''' 系统功能:开户,查询,取款,存储,转账,销户,挂失,解锁,改密,退出 ''' def funcInit(self): print("*-------Welcome To Future Bank---------*") print("| |") print("| (1)开户 (2)登录 |") print("| (3)找回密码 (4)挂失 |") print("| (T)退出 |") print("| |") print("*--------------------------------------*") def funcInterface(self): print("*-------Welcome To Future Bank---------*") print("| |") print("| (1)查询 (5)改密 |") print("| (2)存款 (6)解锁 |") print("| (3)取款 (7)销户 |") print("| (4)转账 (T)退出 |") print("| |") print("*--------------------------------------*")
6、atm.py
''' 提款机: 类名:ATM 属性: 行为(被动执行操作):开户,查询,取款,存储,转账,销户,挂失,解锁,改密,退出 ''' from check import Check from card import Card from readAppendCard import ReadCard,AppendCard import random import time class ATM(object): def __init__(self): # 实例化相关的类 self.check = Check() self.readCard = ReadCard() self.appendCard = AppendCard() self.cards = self.readCard.read() # 显示功能界面 def funcShow(self,ope): if ope != "找回密码": print("*-------Welcome To Future Bank-------*") print("| %s功能界面 |"%ope) print("*------------------------------------*") else: # 显示找回密码界面 print("*-------Welcome To Future Bank-------*") print("| 找回密码功能界面 |") print("*------------------------------------*") # 卡号输入 def cardInput(self,ope=''): while True: cardId = input("请输入卡号:") password = input("请输入密码:") card = self.check.isCardAndPasswordSure(self.cards, cardId,password) if not card: print("卡号或密码输入有误!!!") if ope == 'login' or ope == 'lock': return False else: continue else: return card # 登录 def login(self): self.funcShow("登录") return self.cardInput('login') #找回密码 def findBackPassword(self): self.funcShow("找回密码") cardId = input("请输入卡号:") card = self.check.isCardIdExist(self.cards,cardId) if card: if not self.check.isCardInfoSure(card,"找回密码"): return newpassword = self.check.newPasswordInput() index = self.cards.index(card) self.cards[index].password = newpassword self.writeCard() print("找回密码成功!请重新登录!!!") time.sleep(1) return True else: print("卡号不存在!!!") return True # 开户 def newAccount(self): self.funcShow("开户") # 输入身份证号和手机号 pnum = self.check.phoneInput() iden = self.check.identifyInput() print("正在执行开户程序,请稍候...") while True: # 随机生成6位卡号 cardId = str(random.randrange(100000, 1000000)) # 随机生成的卡号存在就继续 if self.check.isCardIdExist(self.cards,cardId): continue else: break # 初始化卡号密码,卡里的钱,卡的锁定状态 card = Card(cardId, '888888', 0, iden, pnum , 'False') self.appendCard.append(card) print("开户成功,您的卡号为%s,密码为%s,卡余额为%d元!"%(cardId,'888888',0)) print("为了账户安全,请及时修改密码!!!") # 更新卡号列表 self.cards = self.readCard.read() return True # 查询 def checkMoney(self,card): self.funcShow("查询") if self.check.isCardLock(card): print("查询失败!") else: print("卡上余额为%d元!" %card.money) time.sleep(1) # 存款 def saveMoney(self,card): self.funcShow("存款") if self.check.isCardLock(card): print("存钱失败!") else: mon = self.check.moneyInput("存款") # 找到所有卡中对应的卡号,然后对此卡进行存款操作 index = self.cards.index(card) self.cards[index].money += mon print("正在执行存款程序,请稍候...") time.sleep(1) self.writeCard() print("存款成功!卡上余额为%d元!"%self.cards[index].money) time.sleep(1) # 取款 def getMoney(self,card): self.funcShow("取款") if self.check.isCardLock(card): print("取钱失败!") else: print("卡上余额为%d元!" %card.money) mon = self.check.moneyInput("取款") if mon: if mon > card.money: print("余额不足,您当前余额为%d元!"%card.money) time.sleep(1) else: print("正在执行取款程序,请稍候...") time.sleep(1) # 找到所有卡中对应的卡号,然后对此卡进行存款操作 index = self.cards.index(card) self.cards[index].money -= mon self.writeCard() print("取款成功!卡上的余额为%d元!"%self.cards[index].money) time.sleep(1) # 转账 def transferMoney(self,card): self.funcShow("转账") if self.check.isCardLock(card): #如果卡已锁定就不能进行转账操作 print("转账失败!") return while True: cardId = input("请输入对方的账号:") if cardId == card.cardId: print("不能给自己转账!!!") return cardOther = self.check.isCardIdExist(self.cards,cardId) #判断对方卡号是否存在 if cardOther == False: print("对方账号不存在!!!") return else: break while True: print("卡上余额为%d元"%card.money) mon = self.check.moneyInput("转账") if not mon: #输入的金额不对就返回 return if mon > card.money: #输入的金额大于卡上余额就返回 print("余额不足,卡上余额为%d元!" % card.money) return else: break print("正在执行转账程序,请稍候...") time.sleep(1) index = self.cards.index(card) # 找到所有卡中对应的卡号,然后对此卡进行转账操作 self.cards[index].money -= mon indexOther = self.cards.index(cardOther) #找到对卡卡号所处位置 self.cards[indexOther].money += mon self.writeCard() print("转账成功!卡上余额为%d元!" % self.cards[index].money) time.sleep(1) # 销户 def closeAccount(self,card): self.funcShow("销户") if not self.check.isCardInfoSure(card,"销户"): return if card.money >0: print("卡上还有余额,不能进行销户!!!") return if self.check.isSure("销户"): self.cards.remove(card) #移除当前卡号 self.writeCard() print("销户成功!") time.sleep(1) return True # 挂失 def lockAccount(self): self.funcShow("挂失") card = self.cardInput('lock') if not card: return if card.cardLock == "True": print("卡已处于锁定状态!!!") return if not self.check.isCardInfoSure(card,"挂失"): return if self.check.isSure("挂失"): index = self.cards.index(card) #找到所有卡中对应的卡号,然后对此卡进行挂失操作 self.cards[index].cardLock = "True" self.writeCard() print("挂失成功!") time.sleep(1) return True # 解锁 def unlockAccount(self,card): self.funcShow("解锁") if card.cardLock == 'False': print("无需解锁,卡处于正常状态!!!") return if not self.check.isCardInfoSure(card,"解锁"): return index = self.cards.index(card) self.cards[index].cardLock = "False" self.writeCard() print("解锁成功!") time.sleep(1) return True # 改密 def changePassword(self,card): self.funcShow("改密") if self.check.isCardLock(card): print("卡处于锁定状态,不能进行改密!!!") return if not self.check.isCardInfoSure(card,"改密"): return # 输入旧密码 while True: password = input("请输入旧密码:") if self.check.isPasswordSure(password,card.password): break else: print("卡号原密码输入错误!") return newpassword = self.check.newPasswordInput() index = self.cards.index(card) #找到所有卡中对应的卡号,然后对此卡进行改密操作 self.cards[index].password = newpassword self.writeCard() print("改密成功!请重新登录!!!") time.sleep(1) return True # 写入文件 def writeCard(self): self.appendCard.append('', w='w') #先清除原文件再重新写入 for card in self.cards: self.appendCard.append(card) # 退出 def exit(self): if self.check.isSure("退出"): return True else: return False
7、check.py
''' 验证类: 用户名、密码、卡号、身份证、手机号验证 使用正则表达式进行文本搜索 ''' import re class Check(object): def __init__(self): pass #用户验证 def userName(self,admin,password): self.admin = admin self.password = password while True: admin = input("请输入用户名:") password = input("请输入密码:") if admin != self.admin or password != self.password: print("用户名或密码输入有误,请重新输入!!!") continue else: return #是否确认某操作 def isSure(self,operate): while True: res = input("是否确认%s"%operate) if res not in ['yes','no']: print("输入有误,请重新输入!!!") continue elif res == 'yes': return True else: return False # 手机号验证 def phoneInput(self): # 简单的手机号验证:开头为1且全部为数字,长度为11位 while True: pnum = input("请输入您的手机号:") res = re.match(r"^1\d{10}$",pnum) if not res: print("手机号输入有误,请重新输入!!!") continue return pnum # 身份证号验证 def identifyInput(self): # 简单的身份证号验证:6位,只有最后一可以为x,其余必须为数字 while True: iden = input("请输入您的身份证号(6位数字):") res = re.match(r"\d{5}\d|x$",iden) if not res: print("身份证号输入有误,请重新输入!!!") continue return iden # 卡号是否存在 def isCardIdExist(self,cards,cardId): for card in cards: if cardId == card.cardId: return card else: return False # 卡号和密码是否一致 def isCardAndPasswordSure(self,cards,cardId,password): card = self.isCardIdExist(cards,cardId) if card: if card.password == password: return card return False # 密码二次确认是否正确 def isPasswordSure(self, newassword,oldpassword): if newassword == oldpassword: return True else: return False # 卡号完整信息验证 def isCardInfoSure(self,card,ope): phoneNum = input("请输入手机号:") iden = input("请输入身份证号:") if card.phoneNum == phoneNum and card.identityId == iden: return True print("%s失败!!!\n密码、手机号或身份证号与卡中绑定的信息不一致!!!"%ope) return False # 卡号是否锁定 def isCardLock(self,card): if card.cardLock == "True": print("此卡已挂失!") return True return False # 输入金额验证 def moneyInput(self,ope): mon = input("输入%s金额(100的倍数):"%ope) # 输入的钱必须是100的倍数 if re.match(r"[123456789]\d*[0]{2}$", mon): return int(mon) print("输入有误,%s金额必须是100的倍数!请重新输入!!!"%ope) return False def newPasswordInput(self): while True: newpassword = input("请输入新密码:") if not re.match(r"\d{6}$",newpassword): print("密码必须是6位的纯数字!!!") continue newpasswordAgain = input("请重复输入新密码:") if self.isPasswordSure(newpassword, newpasswordAgain): break else: print("两次输入不一致!") continue return newpassword
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。