自己写的练手小程序,练习面向对象的概念,代码中都有注释,刚学的同学也可以很容易看懂(需要自己用pickle模块新建一个database.txt文件,用来存储用户数据信息)。
import random
from card import Card
from user import User
import time
class ATM(object):
def __init__(self, allUsersInfo):
self.allUsersInfo = allUsersInfo
def checkPwd(self, realPwd):
myList = [3, 2, 1, 0]
for i in myList:
if i == 0:
return False
rePass = input('请再次输入密码:')
if realPwd == rePass:
return True
elif (i - 1) != 0:
print('输入错误,你还有%d次输入的机会!' % (i - 1))
def randomId(self):
while True:
str = ''
for i in range(6):
ch = chr(random.randrange(ord('0'), ord('9') + 1))
str += ch
if not self.allUsersInfo.get(str):
return str
def createUser(self):
try:
name = input('请输入姓名:')
idCard = input('请输入身份证号码:')
phone = input('请输入电话号码:')
preMoney = int(input('请输入预存金额:'))
if preMoney < 0:
print('预存金额有误,开户失败!')
return -1
onePwd = input('请输入密码:')
twoPwd = self.checkPwd(onePwd)
if not twoPwd:
print('确认密码错误,开户失败!')
return -1
cardStr = self.randomId()
card = Card(cardStr, onePwd, preMoney)
user = User(name, idCard, phone, card)
self.allUsersInfo[cardStr] = user
time.sleep(1)
print('开户成功,请牢记卡号({})...'.format(cardStr))
except Exception as e:
print('抱歉,系统出现故障,暂时无法操作!')
return
def isExistence(self, cardStrInp):
if self.allUsersInfo.get(cardStrInp):
return True
else:
print('卡号不存在!')
return False
def secretOption(self, cardStrInp):
myList = [3, 2, 1, 0]
for i in myList:
if i == 0:
return False
passInp = input('请输入密码:')
if passInp == self.allUsersInfo[cardStrInp].cardInfo.cardPwd:
return True
elif (i - 1) != 0:
print('输入错误,你还有%d次输入的机会!' % (i - 1))
def searchUserInfo(self):
cardStrInp = input('请输入卡号:')
isExist = self.isExistence(cardStrInp)
if isExist:
if not self.allUsersInfo[cardStrInp].cardInfo.isLock:
if self.secretOption(cardStrInp):
print('账号:%s 余额:%d' % (cardStrInp, self.allUsersInfo[cardStrInp].cardInfo.cardMoney))
return 0
else:
print('密码输入错误超过3次,卡号已被锁定,请先解锁!')
self.allUsersInfo[cardStrInp].cardInfo.isLock = True
return -1
else:
print('该卡号已被锁定,请先解锁!')
return -1
def getMoney(self):
cardStrInp = input('请输入卡号:')
isExist = self.isExistence(cardStrInp)
if isExist:
if not self.allUsersInfo[cardStrInp].cardInfo.isLock:
if self.secretOption(cardStrInp):
getMoney = int(input('请输入取款金额:'))
if getMoney > self.allUsersInfo[cardStrInp].cardInfo.cardMoney:
print('余额不足,取款失败!')
return -1
else:
self.allUsersInfo[cardStrInp].cardInfo.cardMoney -= getMoney
print('取款成功,卡上余额:%d' % self.allUsersInfo[cardStrInp].cardInfo.cardMoney)
return 0
else:
print('密码输入错误超过3次,卡号已被锁定,请先解锁!')
self.allUsersInfo[cardStrInp].cardInfo.isLock = True
return -1
else:
print('该卡号已被锁定,请先解锁!')
return -1
def saveMoney(self):
cardStrInp = input('请输入卡号:')
isExist = self.isExistence(cardStrInp)
if isExist:
if not self.allUsersInfo[cardStrInp].cardInfo.isLock:
if self.secretOption(cardStrInp):
saveMoney = int(input('请输入存款金额:'))
if saveMoney <= 0:
print('无效的存款金额!')
return -1
else:
self.allUsersInfo[cardStrInp].cardInfo.cardMoney += saveMoney
print('存款成功,卡内余额为%d!' % self.allUsersInfo[cardStrInp].cardInfo.cardMoney)
else:
print('密码输入错误超过3次,卡号已被锁定,请先解锁!')
self.allUsersInfo[cardStrInp].cardInfo.isLock = True
return -1
else:
print('该卡号已被锁定,请先解锁!')
return -1
def transMoney(self):
cardInp = input('请输入卡号:')
isExist = self.isExistence(cardInp)
if isExist:
if not self.allUsersInfo[cardInp].cardInfo.isLock:
if self.secretOption(cardInp):
transCardInp = input('请输入要转账的卡号:')
isExistTrans = self.isExistence(transCardInp)
if isExistTrans:
if not self.allUsersInfo[transCardInp].cardInfo.isLock:
if transCardInp != cardInp:
transMoneyInp = int(input('请输入要转账的金额:'))
if transMoneyInp > self.allUsersInfo[cardInp].cardInfo.cardMoney:
print('余额不足,转账失败!')
return -1
else:
self.allUsersInfo[cardInp].cardInfo.cardMoney -= transMoneyInp
self.allUsersInfo[transCardInp].cardInfo.cardMoney += transMoneyInp
print('转账成功,卡内余额为%d!' % self.allUsersInfo[cardInp].cardInfo.cardMoney)
else:
print('无法转入自己的账户!')
return -1
else:
print('对方卡号已被锁定,无法转账,请先解锁!')
return -1
else:
print('密码输入错误超过3次,卡号已被锁定,请先解锁!')
self.allUsersInfo[cardInp].cardInfo.isLock = True
return -1
else:
print('该卡号已被锁定,请先解锁!')
return -1
def changePwd(self):
cardStrInp = input('请输入卡号:')
isExist = self.isExistence(cardStrInp)
if isExist:
if not self.allUsersInfo[cardStrInp].cardInfo.isLock:
if self.secretOption(cardStrInp):
changePwd = input('请输入新密码:')
if changePwd != self.allUsersInfo[cardStrInp].cardInfo.cardPwd:
myList = [3, 2, 1, 0]
for i in myList:
if i == 0:
print('确认密码错误次数超过3次,修改密码失败!')
return -1
passInp = input('请确认新密码:')
if passInp == changePwd:
print('密码修改成功!')
self.allUsersInfo[cardStrInp].cardInfo.cardPwd = changePwd
return 0
elif (i - 1) != 0:
print('输入错误,你还有%d次输入的机会!' % (i - 1))
else:
print('新密码不允许和旧密码相同!')
return -1
else:
print('密码输入错误超过3次,卡号已被锁定,请先解锁!')
self.allUsersInfo[cardStrInp].cardInfo.isLock = True
return -1
else:
print('该卡号已被锁定,请先解锁!')
return -1
def unlockUser(self):
cardStrInp = input('请输入卡号:')
isExist = self.isExistence(cardStrInp)
if isExist:
if self.allUsersInfo[cardStrInp].cardInfo.isLock:
if self.secretOption(cardStrInp):
idCardInp = input('请输入身份证号:')
if idCardInp == self.allUsersInfo[cardStrInp].idCard:
print('解锁成功!')
self.allUsersInfo[cardStrInp].cardInfo.isLock = False
return 0
else:
print('身份证号信息不匹配,解锁失败!')
return -1
else:
print('密码输出错误,解锁失败!')
return -1
else:
print('该卡号未被锁定,无需解锁!')
return -1
class Card(object):
def __init__(self, cardStr, cardPwd, cardMoney):
self.cardStr = cardStr
self.cardPwd = cardPwd
self.cardMoney = cardMoney
self.isLock = False
from view import View
import pickle
from atm import ATM
import time
def main():
admin = View()
admin.printAdminView()
if admin.adminOption():
return -1
file = open('database.txt', 'rb')
allUsersInfo = pickle.load(file)
file.close()
print('*' * 10)
print('usersInfo:', allUsersInfo)
atm = ATM(allUsersInfo)
while True:
admin.printSysFunctionView()
option= input('请输入操作选项(数字):')
if option == "1":
atm.createUser()
elif option == "2":
atm.searchUserInfo()
elif option == "3":
atm.getMoney()
elif option == "4":
atm.saveMoney()
elif option == "5":
atm.transMoney()
elif option == "6":
atm.changePwd()
elif option == "7":
atm.unlockUser()
elif option == "8":
return 0
else:
print('无效的操作选项!')
return -1
f = open('database.txt', "wb")
pickle.dump(allUsersInfo, f)
f.close()
if __name__ == '__main__':
main()
class User(object):
def __init__(self, name, idCard, phone, cardInfo):
self.name = name
self.idCard = idCard
self.phone = phone
self.cardInfo = cardInfo
import time
class View(object):
admin = '1'
pwd = '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("*****************************************************************")
def adminOption(self):
inputAdmin = input('请输入管理员账号:')
if inputAdmin != self.admin:
print('账号错误!')
return -1
inputPwd = input('请输入管理员密码:')
if inputPwd != self.pwd:
print('密码错误!')
return -1
print('操作成功,请稍后...')
time.sleep(2)
return 0