当前位置: 代码迷 >> 综合 >> 编写程序,实现一个基于面向思想的、具有开户、查询、取款、存款、转账、锁定、解锁和退出功能的银行管理系统。
  详细解决方案

编写程序,实现一个基于面向思想的、具有开户、查询、取款、存款、转账、锁定、解锁和退出功能的银行管理系统。

热度:58   发布时间:2023-11-29 10:25:47.0
import time
class Admin:adminU = '1'  # 管理员的账号adpwd = '1'  # 管理员的密码def printAdminView(self):print("******************************************")print("***                                    ***")print("***                                    ***")print("***         欢迎登录银行系统              ***")print("***                                    ***")print("***                                    ***")print("******************************************")def printsysFunctionView(self):print("***********************************************")print("***                                       ***")print("***    1.开户(1)                2.查询(2)   ***")print("***    3.取款(3)                4.存款(4)   ***")print("***    5.转账(5)                6.锁定(6)   ***")print("***    7.解锁(7)                           ***")print("***                                        ***")print("***    退出(Q)                              ***")print("***                                        ***")print("***********************************************")def adminOption(self):adminInput = input("请输入管理员账户:")if self.adminU != adminInput:print("管理员账户输入错误......")return -1passwordInput = input("请输入密码:")if self.adpwd != passwordInput:print("输入密码有误......")return -1else:print("操作成功,请稍后......")return 0import randomclass ATM:def __init__(self, alluser):self.alluser = alluserdef randomiCardId(self):  # 随机生成开户卡号while True:str_data = ''  # 存储卡号for i in range(6):  # 随机生成6位卡号ch = chr(random.randrange(ord('0'), ord('9') + 1))str_data += chif not self.alluser.get(str):  # 判断卡号是否重复return str_datadef creatUser(self):# 目标向用户字典中添加一个键值对(卡号、用户对象)name = input("请输入姓名:")Uid = input("请输入身份证号:")phone = input("请输入手机号:")prestMoney = float(input("请输入预存金额:"))if prestMoney <= 0:print("预存款输入有误,开户失败")return -1oncePwd = input("请输入密码:")passWord = input("请再次输入密码:")if passWord != oncePwd:print("两次密码输入不同......")return -1print("密码设置成功,请牢记密码:  %s " % passWord)cardId = self.randomiCardId()card = Card(cardId, oncePwd, prestMoney)  # 创建卡user = User(name, Uid, phone, card)  # 创建用户self.alluser[cardId] = user  # 存入用户字典print("您的开户已完成,请牢记开户账号: %s" % cardId)def checkpwg(self, realPwd):for i in range(3):psd2 = input("请输入密码:")if realPwd == psd2:return Trueprint("密码输错三次,系统自动退出......")return Falsedef lockCard(self):inptcardId = input("请输入您的卡号:")user = self.alluser.get(inptcardId)if not self.alluser.get(inptcardId):print("此卡号不存在...锁定失败!")return -1if user.card.cardLock:print("该卡已经被锁定,无需再次锁定!")return -1if not self.checkpwg(user.card.cardPwd):  # 验证密码print("密码错误...锁定失败!!")return -1user.card.cardLock = Trueprint("该卡被锁定成功!")def searchUser(self, base=1):if base == 2:inptcardId = input("请输入转出主卡号:")elif base == 3:inptcardId = input("请输入转入卡号:")elif base == 1:inptcardId = input("请输入您的卡号:")user = self.alluser.get(inptcardId)# 如果卡号不存在,下面代码就会执行if not self.alluser.get(inptcardId):print("此卡号不存在...查询失败!")return -1if user.card.cardLock:print("该用户已经被锁定...请解卡后使用!")return -1if not self.checkpwg(user.card.cardPwd):  # 验证密码print("密码错误过多...卡已经被锁定,请解卡后使用!")user.card.cardLock = Truereturn -1if not base == 3:  # 查询转入账户   不打印余额print("账户: %s   余额: %.2f  " %(user.card.cardId, user.card.money))return userdef getMoney(self):userTF = self.searchUser()if userTF != -1:if userTF.card.cardId != '':inptMoney = float(input("请输入取款金额:"))if inptMoney > int(userTF.card.money):print("输入的金额大于余额,请先查询余额!")return -1userTF.card.money = \float(userTF.card.money) - inptMoneyprint("取款成功!  账户: %s   余额: %.2f  " %(userTF.card.cardId, userTF.card.money))else:return -1def saveMoney(self):userTF = self.searchUser()if userTF != -1:if not userTF.card.cardLock == True:if userTF.card.cardId != '':inptMoney = float(input("请输入要存入得金额:"))if inptMoney < 0:print("请输入正确金额")else:userTF.card.money += inptMoneyprint("存款成功!  账户: %s   余额: %.2f  " %(userTF.card.cardId, userTF.card.money))else:return -1def transferMoney(self):MasterTF = self.searchUser(base=2)if (MasterTF == -1):return -1userTF = self.searchUser(base=3)if (userTF == -1):return -1in_tr_Money = float(input("请输入转账金额:"))if MasterTF.card.money >= in_tr_Money:str = input("您确认要继续转账操作吗(y/n)?:")if str.upper() == "Y":MasterTF.card.money -= in_tr_MoneyuserTF.card.money += in_tr_Moneyprint("转账成功!  账户: %s   余额: %.2f  " %(MasterTF.card.cardId, MasterTF.card.money))else:print("转账失败,中止了操作")else:print("转账失败,余额不足!  账户: %s   余额: %.2f  " %(MasterTF.card.cardId, MasterTF.card.money))def unlockCard(self):inptcardId = input("请输入您的卡号:")user = self.alluser.get(inptcardId)while 1:if not self.alluser.get(inptcardId):print("此卡号不存在...解锁失败!")return -1elif not user.card.cardLock:print("该卡未被锁定,无需解锁!")breakelif not self.checkpwg(user.card.cardPwd):print("密码错误...解锁失败!!")return -1user.card.cardLock = False  # 解锁print("该卡 解锁 成功!")breakimport randomclass ATM:def __init__(self, alluser):self.alluser = alluserdef randomiCardId(self):  # 随机生成开户卡号while True:str_data = ''  # 存储卡号for i in range(6):  # 随机生成6位卡号ch = chr(random.randrange(ord('0'), ord('9') + 1))str_data += chif not self.alluser.get(str):  # 判断卡号是否重复return str_datadef creatUser(self):# 目标向用户字典中添加一个键值对(卡号、用户对象)name = input("请输入姓名:")Uid = input("请输入身份证号:")phone = input("请输入手机号:")prestMoney = float(input("请输入预存金额:"))if prestMoney <= 0:print("预存款输入有误,开户失败")return -1oncePwd = input("请输入密码:")passWord = input("请再次输入密码:")if passWord != oncePwd:print("两次密码输入不同......")return -1print("密码设置成功,请牢记密码:  %s " % passWord)cardId = self.randomiCardId()card = Card(cardId, oncePwd, prestMoney)  # 创建卡user = User(name, Uid, phone, card)  # 创建用户self.alluser[cardId] = user  # 存入用户字典print("您的开户已完成,请牢记开户账号: %s" % cardId)def checkpwg(self, realPwd):for i in range(3):psd2 = input("请输入密码:")if realPwd == psd2:return Trueprint("密码输错三次,系统自动退出......")return Falsedef lockCard(self):inptcardId = input("请输入您的卡号:")user = self.alluser.get(inptcardId)if not self.alluser.get(inptcardId):print("此卡号不存在...锁定失败!")return -1if user.card.cardLock:print("该卡已经被锁定,无需再次锁定!")return -1if not self.checkpwg(user.card.cardPwd):  # 验证密码print("密码错误...锁定失败!!")return -1user.card.cardLock = Trueprint("该卡被锁定成功!")def searchUser(self, base=1):if base == 2:inptcardId = input("请输入转出主卡号:")elif base == 3:inptcardId = input("请输入转入卡号:")elif base == 1:inptcardId = input("请输入您的卡号:")user = self.alluser.get(inptcardId)# 如果卡号不存在,下面代码就会执行if not self.alluser.get(inptcardId):print("此卡号不存在...查询失败!")return -1if user.card.cardLock:print("该用户已经被锁定...请解卡后使用!")return -1if not self.checkpwg(user.card.cardPwd):  # 验证密码print("密码错误过多...卡已经被锁定,请解卡后使用!")user.card.cardLock = Truereturn -1if not base == 3:  # 查询转入账户   不打印余额print("账户: %s   余额: %.2f  " %(user.card.cardId, user.card.money))return userdef getMoney(self):userTF = self.searchUser()if userTF != -1:if userTF.card.cardId != '':inptMoney = float(input("请输入取款金额:"))if inptMoney > int(userTF.card.money):print("输入的金额大于余额,请先查询余额!")return -1userTF.card.money = \float(userTF.card.money) - inptMoneyprint("取款成功!  账户: %s   余额: %.2f  " %(userTF.card.cardId, userTF.card.money))else:return -1def saveMoney(self):userTF = self.searchUser()if userTF != -1:if not userTF.card.cardLock == True:if userTF.card.cardId != '':inptMoney = float(input("请输入要存入得金额:"))if inptMoney < 0:print("请输入正确金额")else:userTF.card.money += inptMoneyprint("存款成功!  账户: %s   余额: %.2f  " %(userTF.card.cardId, userTF.card.money))else:return -1def transferMoney(self):MasterTF = self.searchUser(base=2)if (MasterTF == -1):return -1userTF = self.searchUser(base=3)if (userTF == -1):return -1in_tr_Money = float(input("请输入转账金额:"))if MasterTF.card.money >= in_tr_Money:str = input("您确认要继续转账操作吗(y/n)?:")if str.upper() == "Y":MasterTF.card.money -= in_tr_MoneyuserTF.card.money += in_tr_Moneyprint("转账成功!  账户: %s   余额: %.2f  " %(MasterTF.card.cardId, MasterTF.card.money))else:print("转账失败,中止了操作")else:print("转账失败,余额不足!  账户: %s   余额: %.2f  " %(MasterTF.card.cardId, MasterTF.card.money))def unlockCard(self):inptcardId = input("请输入您的卡号:")user = self.alluser.get(inptcardId)while 1:if not self.alluser.get(inptcardId):print("此卡号不存在...解锁失败!")return -1elif not user.card.cardLock:print("该卡未被锁定,无需解锁!")breakelif not self.checkpwg(user.card.cardPwd):print("密码错误...解锁失败!!")return -1user.card.cardLock = False  # 解锁print("该卡 解锁 成功!")breakclass Card:def __init__(self,cardId,cardPwd,money):self.cardId = cardIdself.cardPwd = cardPwdself.money = moneyself.cardLock = Falseclass User:def __init__(self, name, id, phone, card):self.name = nameself.id = idself.phone = phoneself.card = cardclass HomePage:def __init__(self):self.allUserD = {}  # 使用字典存储数据self.atm = ATM(self.allUserD)self.admin = Admin()  # 管理员开机界面def saveUser(self):self.allUserD.update(self.atm.alluser)print("数据存盘成功")def main(self):self.admin.printAdminView()resL = self.admin.adminOption()if not resL:while True:self.admin.printsysFunctionView()option = input("请输入您的操作:")if option not in ("1", "2", "3", "4", "5","6", "7", "S", "Q", "q"):print("输入操作项有误,请仔细确认!")time.sleep(1)if option == "1":  # 开户self.atm.creatUser()elif option == "2":  # 查询self.atm.searchUser()elif option == "3":  # 取款self.atm.getMoney()elif option == "4":  # 存储self.atm.saveMoney()elif option == "5":  # 转账self.atm.transferMoney()elif option == "6":  # 锁定self.atm.lockCard()elif option == "7":  # 解锁self.atm.unlockCard()elif option.upper() == "Q":if not (self.admin.adminOption()):self.saveUser()print('退出系统')return -1if __name__ == "__main__":homepage = HomePage()homepage.main()
  相关解决方案