# -*- coding: utf-8 -*-
import pickle
from SetForOperations import Bank_Card
class RegisterAndCheck(object):
@classmethod
def register(cls):
card = input("please input bank card:")
password = input("please input card\'s password:")
name = input("please input customer\'s name:")
ID = input("please input customer\'s ID:")
balance = input("please input card\'s balance:")
dat = ""
card = Bank_Card(name, card, password, ID, balance, dat)
return card
@classmethod
def check(cls, cards):
id = input("请输入银行卡号:")
flag1 = 1
for b in cards:
if b.getCard() == id:
while flag1 <= 3:
Password = input("请输入银行卡号密码:")
if b.getPassword() == Password:
print "输入正确,进入以下操作:"
return b
else:
print "输入密码错误,请重新输入"
flag1 += 1
break
print "银行卡号或密码不正确"
return None
class SimpleOperationForBankcard(object):
def __init__(self, card):
self.card = card
def modifyPassword(self):
"""
修改密码
"""
newPass = input("请输入新密码:")
renewPass = input("请再次输入新密码:")
if newPass == renewPass:
self.card.setPassword(newPass)
print "modify Password Success !"
return True
else:
print "modify Password Failure !"
return False
##存取金额
def takeMoney(self, moneyString):
money = float(moneyString)
if money <= 0.0:
print "输入金额有误,不能进行此操作"
return False
if money > float(self.card.balance):
print "您的余额不够,不能进行此操作."
return False
balance = float(self.card.balance) - money ##字符窜不能进行加减乘除操作,要把字符串转化为数字
self.card.balance = str(balance)
print "取款成功!"
return True
def saveMoney(self, moneyString):
money = float(moneyString)
if money <= 0.0:
print "输入金额有误,不能进行此操作"
return False
balance = float(self.card.balance) + money
self.card.setBalance(str(balance))
print "存款成功!"
return True
def getCard1(self):
return self.card
class BankServer(object):
@classmethod
def transferMoney(cls, SourceCard, strForObjectCardId, cards): #有错误,无法操作两个对象
i = 0
cards.remove(SourceCard)
while i < cards.__len__():
cardId = cards.__getitem__(i)
if cardId.getCard() == strForObjectCardId:
moneyString = input("please input transfer money --->")
if float(moneyString) > float(SourceCard.balance):
print "余额不足,请重新输入:"
return False
sourceBalance = float(SourceCard.getBalance()) - float(moneyString)
objectBalance = float(cardId.getBalance()) + float(moneyString)
SourceCard.setBalance(str(sourceBalance))
cardId.setBalance(str(objectBalance))
cards.pop(i)
cards.insert(i, cardId)
cards.append(SourceCard)
print "transfer money success !"
return True
i += 1
print "transfer money failure !"
return False
class SimpleOperateFile(object):
@classmethod
def readFile(cls):
cards = []
try:
#从文件里读出
f=open(r'F:\work\mnt\e\backup\Bank.txt','rb')
try:
while True:
bank_card = pickle.load(f) #只能返回一个对象,不能返回多个对象
if bank_card == None:
break
cards.append(bank_card)
except Exception as e:
print (e)
f.close()
return cards
except Exception as e:
print "chu xian yi chang!"
@classmethod
def writeFile(cls, cards):
try:
#写进文件里
#Bank_Card_list.sort() #sort()方法返回为空,方法里有参数一定有返回值
f=open(r'F:\work\mnt\e\backup\Bank.txt','wb') #关键是用二进制方式把对象写进文件
cards.sort()
for p in cards: #for后面不能用一对小括号
pickle.dump(p,f) #只能放进去一个对象,不能多个
f.close()
except(Exception):
print('chu xian yi chang !')
if __name__ =="__main__":
print "-------1 表示银行卡注册------->>"
print "-------2 表示银行卡存款------->>"
print "-------3 表示银行卡取款------->>"
print "-------4 表示修改银行卡密码------->>"
print "-------5 表示查询用户信息------->>"
print "-------6 表示本银行内转账------->>"
print "-------7 表示管理员查询用户------->>"
print "-------8 表示退出------->>"
while True:
cards = SimpleOperateFile.readFile()
value = int(input("请输入数字------>>"))
#注册
if value == 1:
newcard = RegisterAndCheck.register()
flag = cards.__contains__(newcard)
if not flag:
cards.append(newcard)
#存款
elif value == 2:
card = RegisterAndCheck.check(cards)
saveMoneyString = input("please input save money-->")
SimpleOperationForBankcard(card).saveMoney(saveMoneyString)
#取款
elif value == 3:
card = RegisterAndCheck.check(cards)
takeMoneyString = input("please input take money-->")
SimpleOperationForBankcard(card).takeMoney(takeMoneyString)
#修改密码
elif value == 4:
card = RegisterAndCheck.check(cards)
flag = 1
while flag <= 3:
status = SimpleOperationForBankcard(card).modifyPassword()
if status:
break
else:
flag += 1
#查询用户信息
elif value == 5:
card = RegisterAndCheck.check(cards)
print card
#银行内转账
elif value == 6:
card = RegisterAndCheck.check(cards)
transferCardId = input("please input object card--->")
BankServer.transferMoney(card, transferCardId, cards)
#退出
elif value == 7:
administratorPassword = input("please input administrator\'s password---> ")
if administratorPassword == "wangshijie":
for p in cards:
print p
elif value == 8:
break
SimpleOperateFile.writeFile(cards)
status = input("Are you stict to Operate others, Yes or No? ")
if status == "No":
break
评论4
最新资源