需求:实现开户、查询、存款、取款、转账、改密、锁卡、解卡、销户、退出这十个功能
首先,创建一个账户类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):
# 判断存放所有用户信息的文件 users.txt
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这个包而导入失败
另:这个项目里边还有一个关键功能我没做,那就是存取款和转账时本来是应该先核对密码的,而这里我没做,其实也不难,写一个核对密码的方法导入进去就行了