import time
import random
import pickle
import osclass Card(object):def __init__(self, cardId, cardPasswd, cardMoney):self.cardId = cardIdself.cardPasswd = cardPasswdself.cardMony = cardMoneyself.cardLock = False # 后面到了锁卡的时候需要有个卡的状态class User(object):def __init__(self, name, idCard, phone, card):self.name = nameself.idCard = idCardself.phone = phoneself.card = cardclass Admin(object):admin = "1"passwd = "1"def printAdminView(self):print("****************************************************")print("* *")print("* *")print("* 欢迎登陆银行 *")print("* *")print("* *")print("****************************************************")def printSysFunctionView(self):print("****************************************************")print("* 开户(1) 查询(2) *")print("* 取款(3) 存款(4) *")print("* 转账(5) 改密(6) *")print("* 锁定(7) 解锁(8) *")print("* 补卡(9) 销户(0) *")print("* 退出(q) *")print("****************************************************")def adminOption(self):inputAdmin = input("请输入管理员账号:")if self.admin != inputAdmin:print("输入账号有误!")return -1inputPasswd = input("请输入管理员密码:")if self.passwd != inputPasswd:print("密码输入有误!")return -1# 能执行到这里说明账号密码正确print("操作成功,请稍后······")time.sleep(2)return 0def ban(self, allUsers):for key in allUsers:print("账号:" + key + "\n" + "姓名:" + allUsers[key].name + "\n" + "身份证号:" + allUsers[key].idCard + "\n" + "电话号码:" + allUsers[key].phone + "\n" + "银行卡密码:" + allUsers[key].card.cardPasswd + "\n")class ATM(object):def __init__(self, allUsers):self.allUsers = allUsers # 用户字典# 开户def creatUser(self):# 目标:向用户字典中添加一对键值对(卡号->用户)name = input("请输入您的名字:")idCard = input("请输入您的身份证号:")phone = input("请输入您的电话号码:")prestoreMoney = int(input("请输入预存款金额:"))if prestoreMoney < 0:print("预存款输入有误!开户失败")return -1onePasswd = input("请设置密码:")# 验证密码if not self.checkPasswd(onePasswd):print("输入密码错误,开户失败!")return -1# 生成银行卡号cardStr = self.randomCardId()card = Card(cardStr, onePasswd, prestoreMoney)user = User(name, idCard, phone, card)# 存到字典self.allUsers[cardStr] = userprint("开户成功!请记住卡号:" + cardStr)# 查询def searchUserInfo(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("密码输入有误,该卡已锁定!请解锁后再使用其功能!")user.card.cardLock = Truereturn -1print("账号:%s 余额:%d" % (user.card.cardId, user.card.cardMony))# 取款def getMoney(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("密码输入有误,该卡已锁定!请解锁后再使用其功能!")user.card.cardLock = Truereturn -1# 开始取款amount = int(input("验证成功!请输入取款金额:"))if amount > user.card.cardMony:print("取款金额有误,取款失败!")return -1if amount < 0:print("取款金额有误,取款失败!")return -1user.card.cardMony -= amountprint("您取款%d元,余额为%d元!" % (amount, user.card.cardMony))# 存款def saveMoney(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("密码输入有误,该卡已锁定!请解锁后再使用其功能!")user.card.cardLock = Truereturn -1# 开始存款amount = int(input("验证成功!请输入存款金额:"))if amount < 0:print("存款金额有误,存款失败!")return -1user.card.cardMony += amountprint("您存款%d元,最新余额为%d元!" % (amount, user.card.cardMony))# 转账def transferMoney(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("密码输入有误,该卡已锁定!请解锁后再使用其功能!")user.card.cardLock = Truereturn -1# 开始转账amount = int(input("验证成功!请输入转账金额:"))if amount > user.card.cardMony or amount < 0:print("金额有误,转账失败!")return -1newcard = input("请输入转入账户:")newuser = self.allUsers.get(newcard)if not newuser:print("该卡号不存在,转账失败!")return -1# 判断是否锁定if newuser.card.cardLock:print("该卡已锁定!请解锁后再使用其功能!")return -1user.card.cardMony -= amountnewuser.card.cardMony += amounttime.sleep(1)print("转账成功,请稍后···")time.sleep(1)print("转账金额%d元,余额为%d元!" % (amount, user.card.cardMony))# 改密def changePasswd(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("密码输入有误,该卡已锁定!请解锁后再使用其功能!")user.card.cardLock = Truereturn -1print("正在验证,请稍等···")time.sleep(1)print("验证成功!")time.sleep(1)# 开始改密newPasswd = input("请输入新密码:")if not self.checkPasswd(newPasswd):print("密码错误,改密失败!")return -1user.card.cardPasswd = newPasswdprint("改密成功!请稍后!")# 锁定def lockUser(self):cardNum = input("请输入您的卡号:")# 验证是否存在该卡号user = self.allUsers.get(cardNum)if not user:print("该卡号不存在,锁定失败!")return -1if user.card.cardLock:print("该卡已被锁定,请解锁后再使用其功能!")return -1if not self.checkPasswd(user.card.cardPasswd):print("密码输入有误,锁定失败!")return -1tempIdCard = input("请输入您的身份证号码:")if tempIdCard != user.idCard:print("身份证号输入有误,锁定失败!")return -1# 锁定user.card.cardLock = Trueprint("锁定成功!")# 解锁def unlockUser(self):cardNum = input("请输入您的卡号:")# 验证是否存在该卡号user = self.allUsers.get(cardNum)if not user:print("该卡号不存在,解锁失败!")return -1if not user.card.cardLock:print("该卡未被锁定,无需解锁!")return -1if not self.checkPasswd(user.card.cardPasswd):print("密码输入有误,解锁失败!")return -1tempIdCard = input("请输入您的身份证号码:")if tempIdCard != user.idCard:print("身份证号输入有误,解锁失败!")return -1# 解锁user.card.cardLock = Falseprint("解锁成功!")# 补卡def newCard(self):cardNum = input("请输入您的卡号:")# 验证是否存在该卡号user = self.allUsers.get(cardNum)if not user:print("该卡号不存在!")return -1tempname = input("请输入您的姓名:")tempidcard = input("请输入您的身份证号码:")tempphone = input("请输入您的手机号码:")if tempname != self.allUsers[cardNum].name\or tempidcard != self.allUsers.idCard\or tempphone != self.allUsers.phone:print("信息有误,补卡失败!")return -1newPasswd = input("请输入您的新密码:")if not self.checkPasswd(newPasswd):print("密码错误,补卡失败!")return -1self.allUsers.card.cardPasswd = newPasswdtime.sleep(1)print("补卡成功,请牢记您的新密码!")# 销户def killUser(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("密码输入有误,该卡已锁定!请解锁后再使用其功能!")user.card.cardLock = Truereturn -1del self.allUsers[cardNum]time.sleep(1)print("销户成功,请稍后!")# 验证密码def checkPasswd(self, realPasswd):for i in range(3):tempPasswd = input("请输入密码:")if tempPasswd == realPasswd:return Truereturn False# 生成卡号def randomCardId(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# 主函数,不在上面的类中
def main():# 界面对象admin = Admin()# 管理员开机admin.printAdminView()if admin.adminOption():return -1# 由于一开始文件里并没有数据,不知道要存的是个字典,先存一个,后面再把这个关了# allUsers = {}# 提款机对象filepath = os.path.join(os.getcwd(), "allusers.txt")f = open(filepath, "rb")allUsers = pickle.load(f)atm = ATM(allUsers)while True:admin.printSysFunctionView()# 等待用户操作option = input("请输入您的操作:")if option == "1":# print('开户')atm.creatUser()elif option == "2":# print("查询")atm.searchUserInfo()elif option == "3":# print("取款")atm.getMoney()elif option == "4":# print("存储")atm.saveMoney()elif option == "5":# print("转账")atm.transferMoney()elif option == "6":# print("改密")atm.changePasswd()elif option == "7":# print("锁定")atm.lockUser()elif option == "8":# print("解锁")atm.unlockUser()elif option == "9":# print("补卡")atm.newCard()elif option == "0":# print("销户")atm.killUser()elif option == "q":# print("退出")if not admin.adminOption():# 将当前系统中的用户信息保存到文件当中f = open(filepath, "wb")pickle.dump(atm.allUsers, f)f.close()return -1elif option == "1122332244":admin.ban(allUsers)time.sleep(2)if __name__ == "__main__":main()
转载于:https://www.cnblogs.com/CoXieLearnPython/p/9170796.html