银行提款机系统
首先应该明确有哪些类和哪些属性,方法
用户身份证类
class IdCard(object):
def __init__(self, name, cardId):
self.__name = name
self.__cardId = cardId
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
self.__name = value
@property
def cardId(self):
return self.__cardId
@cardId.setter
def cardId(self, value):
self.__cardId = value
银行类
此处假设只有一个银行,用单例实现
def single(cls):
instance = {}
def inner(*args, **kwargs):
if cls not in instance:
instance[cls] = cls(*args, **kwargs)
return instance[cls]
return inner
@single
class Bank(object):
def __init__(self, name=None):
self.name = name
self.atmsList = []
self.usersDict = {}
self.cardsDict = {}
多个atm机类
from idCard import IdCard
from bank import Bank
from bankCard import BankCard
import random
class Atm(object):
def __init__(self, atmId, money):
self.atmId = atmId
self.__money = money
@property
def money(self):
if self.__money >= 0:
return self.__money
else:
return 0
@money.setter
def money(self, value):
self.__money = value
# 开户
def open_account(self):
userName = input("输入您的姓名:")
userCardId = input("输入您的身份证号码:")
sunckBank = Bank()
#判断银行中改用是否存储过信息
user = sunckBank.usersDict.get(userCardId)
if not user:
# 创建身份证对象
user = IdCard(userName, userCardId)
sunckBank.usersDict[userCardId] = user
#随机生成卡号
bankCardId = self.getBankCardNum()
passwd = input("设置密码:")
if not self.checkPasswd(passwd):
#验证密码失败
print("两次密码不一致,开户失败")
return
phone = input("请输入您的手机号:")
#创建银行卡
bankCard = BankCard(bankCardId,passwd, phone, user)
#将卡存入银行
sunckBank.cardsDict[bankCardId] = bankCard
print("开户成功,请牢记卡号%s"%(bankCardId))
#插卡
def insertCard(self):
bankCardId = input("请输入您的银行卡号码:")
#验证是否存在
if self.checkBankCardNum(bankCardId):
print("卡号不存在,请重新操作!")
return
sunckBank = Bank()
bankCard = sunckBank.cardsDict.get(bankCardId)
# 验证是否锁定
if bankCard.flag == 0:
print("该卡被锁定,请解锁后操作!")
return
if not self.checkPasswd(bankCard.passwd):
#密码输入错误,锁卡,结束
bankCard.flag = 0
print("三次密码错误,该卡已被锁定!")
return
return bankCard
# 存款
def deposit(self, card):
money = int(input("请输入存款金额:"))
card.money = card.money + money
#提款机的money也要增加
self.money = self.money + money
print("存款成功!")
# 取款
def withdrawal(self, card):
money = int(input("请输入取款金额:"))
if card.money < money:
print("余额不足(%d),取款失败!"%(card.money))
return
if self.money < money:
print("ATM机器现金不足(%d),取款失败!"%(self.money))
return
card.money = card.money - money
self.money = self.money - money
print("取款成功!")
# 查询
def query(self, card):
print("余额:%d"%(card.money))
# 转账
def transfer_accounts(self, card):
transferCardId = input("请输入对方卡号:")
if self.checkBankCardNum(transferCardId):
print("卡号不存在,转账失败!")
return
#显示对方用户名验证是否正确
sunckBank = Bank()
transferCard = sunckBank.cardsDict.get(transferCardId)
flag = input("用户名为%s,是否确认转账,输入y/Y表示转账,否则退出"%(transferCard.idCard.name))
if not (flag == "y" or flag == "Y"):
return
transferMoney = int(input("请输入转账金额:"))
if card.money < transferMoney:
print("余额不足,转账失败!")
return
card.money = card.money - transferMoney
transferCard.money = transferCard.money + transferMoney
print("转账成功!")
# 锁卡
def lock_card(self, card):
cardId = input("请输入您的身份证号码:")
if cardId != card.idCard.cardId:
print("身份验证失败,锁卡失败!")
return False
phone = input("请输入您的手机号码:")
if phone != card.phone:
print("手机验证失败,锁卡失败!")
return False
card.flag = 0
print("锁卡成功!")
return True
# 解卡
def unlock_card(self):
bankCardId = input("请输入您的银行卡号码:")
# 验证是否存在
if self.checkBankCardNum(bankCardId):
print("卡号不存在,请重新操作!")
return
sunckBank = Bank()
bankCard = sunckBank.cardsDict.get(bankCardId)
# 验证是否锁定
if bankCard.flag == 1:
print("该卡未被锁定,无需解锁!")
return
cardId = input("请输入您的身份证号码:")
if cardId != bankCard.idCard.cardId:
print("身份验证失败,解卡失败!")
return
phone = input("请输入您的手机号码:")
if phone != bankCard.phone:
print("手机验证失败,解卡失败!")
return
passwd = input("请输入密码:")
if bankCard.passwd != passwd:
print("密码错误,解卡失败!")
return
bankCard.flag = 1
print("解卡成功,可继续其他操作!")
# 改密
def change_password(self, card):
cardId = input("请输入您的身份证号码:")
if cardId != card.idCard.cardId:
print("身份验证失败,改密失败!")
return False
phone = input("请输入您的手机号码:")
if phone != card.phone:
print("手机验证失败,改密失败!")
return False
passwd = input("请设置新密码:")
if not self.checkPasswd(passwd):
print("两次密码不一致,修改失败!")
return False
card.passwd = passwd
print("密码修改成功,请重新登录")
return True
# 补卡
def reissue_card(self):
bankCardId = input("请输入您的银行卡号码:")
# 验证是否存在
if self.checkBankCardNum(bankCardId):
print("卡号不存在,请重新操作!")
return
sunckBank = Bank()
bankCard = sunckBank.cardsDict.get(bankCardId)
# 验证是否锁定
if bankCard.flag == 0:
print("该卡被锁定,请先解锁!")
return
cardId = input("请输入您的身份证号码:")
if cardId != bankCard.idCard.cardId:
print("身份验证失败,补卡失败!")
return
phone = input("请输入您的手机号码:")
if phone != bankCard.phone:
print("手机验证失败,补卡失败!")
return
passwd = input("请输入密码:")
if bankCard.passwd != passwd:
print("密码错误,补卡失败!")
return
#补卡操作
#获取新卡号
newCardId = self.getBankCardNum()
#删除原有键值对
sunckBank.cardsDict.pop(bankCardId)
#修改对象中的卡号
bankCard.cardId = newCardId
sunckBank.cardsDict[newCardId] = bankCard
print("补卡成功,请牢记新卡号:%s"%(newCardId))
# 销户
def closing_account(self, card):
cardId = input("请输入您的身份证号码:")
if cardId != card.idCard.cardId:
print("身份验证失败,销户失败!")
return False
phone = input("请输入您的手机号码:")
if phone != card.phone:
print("手机验证失败,销户失败!")
return False
sunckBank = Bank()
sunckBank.cardsDict.pop(card.cardId)
print("销户成功!")
return True
@classmethod
def view1(cls):
print("----------------------------------")
print("- welecome to guabank -")
print("- 开户(1) -")
print("- 插卡(2) -")
print("- 解锁(3) -")
print("- 补卡(4) -")
print("----------------------------------")
@classmethod
def view2(cls):
print("----------------------------------")
print("- welecome to guabank -")
print("- 查询(1) 转账(2) -")
print("- 存款(3) 取款(4) -")
print("- 锁卡(5) -")
print("- 改密(6) 注销(7) -")
print("- 退卡(8) -")
print("----------------------------------")
@staticmethod
def checkBankCardNum(bankCardId):
sunckBank = Bank()
card = sunckBank.cardsDict.get(bankCardId)
if card:
#有卡
return False
else:
return True
@staticmethod
def getBankCardNum():
while 1:
c = ""
for i in range(6):
c += random.choice("1234567890")
if Atm.checkBankCardNum(c):
return c
@staticmethod
def checkPasswd(passwd):
for i in range(3):
passwd1 = input("输入密码:")
if passwd == passwd1:
return True
return False
银行卡类
class BankCard():
def __init__(self, cardId, passwd, phone, idCard):
self.cardId = cardId
self.__passwd = passwd
self.__money = 0
self.__flag = 1
self.__phone = phone
self.__idCard = idCard
@property
def passwd(self):
return self.__passwd
@passwd.setter
def passwd(self, value):
self.__passwd = value
@property
def money(self):
return self.__money
@money.setter
def money(self, value):
self.__money = value
@property
def flag(self):
return self.__flag
@flag.setter
def flag(self, value):
self.__flag = value
@property
def phone(self):
return self.__phone
@phone.setter
def phone(self, value):
self.__phone= value
@property
def idCard(self):
return self.__idCard
@idCard.setter
def idCard(self, value):
self.__idCard = value
主模块
from atm import Atm
from bank import Bank
import uuid
def main(at):
while 1:
Atm.view1()
option1 = int(input("请输入操作:"))
#开户
if option1 == 1:
at.open_account()
elif option1 == 2:
#插卡
card = at.insertCard()
if card:
while 1:
Atm.view2()
option2 = int(input("请输入操作:"))
if option2 == 1:
at.query(card)
elif option2 == 2:
at.transfer_accounts(card)
elif option2 == 3:
at.deposit(card)
elif option2 == 4:
at.withdrawal(card)
elif option2 == 5:
if at.lock_card(card):
break
elif option2 == 6:
if at.change_password(card):
break
elif option2 == 7:
if at.closing_account(card):
break
elif option2 == 8:
print("请收好您的卡片!")
break
else:
print("输入有误!")
elif option1 == 3:
at.unlock_card()
elif option1 == 4:
at.reissue_card()
else:
print("输入有误!")