需求:实现开户、查询、存款、取款、转账、改密、锁卡、解卡、销户、退出这十个功能
首先,创建一个账户类account.py
class Account:
def __init__(self,name,aid,card):
self.name = name
self.aid = aid
self.card = card
def __str__(self):
return "[Account] name:%s aid:%s card:%s" % (self.name,self.aid,self.card)
__repr__ = __str__
然后,创建卡类card.py
class Card:
def __init__(self,cid,pwd):
self.cid = cid
self.pwd = pwd
self.balance = 10
self.iscolok = False
def __str__(self):
return "[Card] cid:%s pwd:%s balance:%d iscolok:%s" % (self.cid,self.pwd,self.balance,self.iscolok)
__repr__ = __str__
接下来是操作类operation.py
import random
from yinhangtest.card import Card
from yinhangtest.account import Account
import os
import pickle
class Operation:
def __init__(self):
self.load_user()
print(self.allusers)
def load_user(self):
if not os.path.exists("users.txt"):
self.allusers = {}
else:
with open("users.txt","rb") as fp:
self.allusers = pickle.load(fp)
def get_cardid(self):
while True:
cid = str(random.randint(100000,999999))
if cid not in self.allusers:
return cid
def create_account(self):
cid = self.get_cardid()
name = input("请输入名字:")
aid = input("请输入身份证号码:")
pwd = input("请输入卡的密码:")
card = Card(cid,pwd)
account = Account(name,aid,card)
self.allusers[cid] = account
self.exit()
print("开户成功!")
def get_card(self):
cid = input("请输入卡号:")
if cid not in self.allusers:
print("请输入正确的卡号")
return
user = self.allusers[cid]
card = user.card
return card
def search_money(self):
card = self.get_card()
if card != None:
print("卡的余额为:",card.balance)
def save_money(self):
card = self.get_card()
if card.iscolok == True:
print("此卡已被锁定")
return
if card != None:
money =int(input("请输入要存款金额:"))
card.balance += money
print("存款成功,卡中余额为:",card.balance)
self.exit()
def get_money(self):
card = self.get_card()
if card.iscolok==True:
print("此卡已被锁定")
return
if card != None:
money = int(input("请输入要取款金额:"))
if money > card.balance:
print("余额不足!")
else:
card.balance -= money
print("取款成功,卡中余额为:", card.balance)
self.exit()
def transfer(self):
card = self.get_card()
if card.iscolok == True:
print("此卡已被锁定")
return
if card == None:
return
while True:
other_cid = input("请输入对方的卡号:")
if other_cid not in self.allusers:
print("请输入正确的卡号")
else:
break
while True:
money = int(input("请输入转账金额:"))
if money>card.balance:
print("余额不足")
else:
card.balance -= money
other_card = self.allusers[other_cid].card
other_card.balance += money
print("转账成功")
self.exit()
break
def change_pwd(self):
card = self.get_card()
if card.iscolok == True:
print("此卡已被锁定")
return
if card == None:
return
pwd = input("请输入密码:")
card.pwd = pwd
print("改密成功")
self.exit()
def lock_card(self):
cid = input("请输入卡号:")
if cid not in self.allusers:
print("请输入正确的卡号")
return
user = self.allusers[cid]
while True:
aid = input("请输入身份证号:")
if aid != user.aid:
print("请重新输入")
else:
break
user.card.iscolok = True
print("锁卡成功")
self.exit()
def unlock_card(self):
cid = input("请输入卡号:")
if cid not in self.allusers:
print("请输入正确的卡号")
return
user = self.allusers[cid]
while True:
aid = input("请输入身份证号:")
if aid != user.aid:
print("请重新输入")
else:
break
user.card.iscolok = False
print("解卡成功")
self.exit()
def del_card(self):
cid = input("请输入卡号:")
if cid not in self.allusers:
print("请输入正确的卡号")
return
user = self.allusers[cid]
while True:
aid = input("请输入身份证号:")
if aid != user.aid:
print("请重新输入")
else:
break
user.card.balance = 0
self.allusers.pop(cid)
print("销户成功")
self.exit()
def exit(self):
with open("users.txt","wb") as fp:
pickle.dump(self.allusers,fp)
最后是测试类testmain.py
from yinhangtest.operation import Operation
def main():
print("*****欢迎进入本银行系统*****")
print("*****本系统功能操作如下*****")
print("*****1.开户 2.查询*****")
print("*****3.存款 4.取款*****")
print("*****5.转账 6.改密*****")
print("*****7.锁卡 8.解卡*****")
print("*****9.销户 0.退出*****")
opeartion = Operation()
while True:
selectnum = int(input("请输入对应的操作:"))
if selectnum == 0:
opeartion.exit()
break
elif selectnum == 1:
opeartion.create_account()
elif selectnum == 2:
opeartion.search_money()
elif selectnum == 3:
opeartion.save_money()
elif selectnum == 4:
opeartion.get_money()
elif selectnum == 5:
opeartion.transfer()
elif selectnum == 6:
opeartion.change_pwd()
elif selectnum == 7:
opeartion.lock_card()
elif selectnum == 8:
opeartion.unlock_card()
elif selectnum == 9:
opeartion.del_card()
else:
print("请输入正确的操作")
if __name__ == '__main__':
main()
注:
我这几个文件是建在一个名为yinghangtest的Python Package的包下的,所以我导包的时候可能和你们导包代码写的有一点点区别,如果你只是找着我的代码手打,可能不会出现问题,但如果直接复制粘贴,你将因为没有yinghangtest这个包而导入失败
另:这个项目里边还有一个关键功能我没做,那就是存取款和转账时本来是应该先核对密码的,而这里我没做,其实也不难,写一个核对密码的方法导入进去就行了