模拟银行业务
print("*********************************************")
print("* 1.开户 2.查询 *")
print("* 3.存款 4.取款 *")
print("* 5.转账 6.改密 *")
print("* 7.锁卡 8.解卡 *")
print("* 9.补卡 0.退出 *")
print("*********************************************")
导包
import time
import os
import pickle
import random
定义类
'''
类:
card #卡
person #用户
view #界面
ATM #ATM机功能-
class Card:
def __init__(self,cardid,password,money):
self.cardid = cardid
self.password = password
self.money = money
self.isclock = False #是否上锁
class Person:
def __init__(self,name,userid,phone,card):
self.name = name
self.userid = userid
self.phone = phone
self.card = card
class View:
def login(self):
name = input("请输入管理员账号:")
password = input("请输入管理员密码:")
if name =="admin" and password == "hong": #初始化了账号和密码
self.show()
time.sleep(2)
return True
else:
print("管理员账号或密码不正确")
return False
def show(self):
print("*********************************************")
print("* *")
print("* *")
print("* 欢迎进入银行系统 *")
print("* *")
print("* *")
print("*********************************************")
def operation_show(self):
print("*********************************************")
print("* 1.开户 2.查询 *")
print("* 3.存款 4.取款 *")
print("* 5.转账 6.改密 *")
print("* 7.锁卡 8.解卡 *")
print("* 9.补卡 0.退出 *")
print("*********************************************")
'''
功能类:
class Operation:
def __init__(self):
self.load_user()
print(self.user_dict)
以下所有函数都包含在Operation类中:
0.首先使用文本用来存储信息以及访问信息
def load_user(self):
#加载文件之前判断文件是否存在
if os.path.exists("user.txt"):
with open("user.txt","rb") as f:
self.user_dict = pickle.load(f)
else:
self.user_dict = {}
def save(self):
'''保存用户'''
with open("user.txt","wb") as f:
pickle.dump(self.user_dict,f)
1.开卡
def create(self):
'''开户'''
name = input("请输入持卡人姓名:")
user_id = input("请输入身份证号码:")
phone = input("请设置预留手机号:")
password = self.get_password()
cardid = self.get_cardid()
#有信息之后就可以生成一张卡
card = Card(cardid,password,0)
#通过卡找到用户
user = Person(name,user_id,phone,card)
self.user_dict[cardid] = user
print("开卡成功!您的卡号为:%s,卡内余额为:%d元" % (cardid,card.money))
self.save()
time.sleep(2)
def get_password(self):
'''设置密码'''
while True:
pwd1 = input("请设置银行卡密码:")
pwd2 = input("请重新输入密码:")
if pwd1 == pwd2:
return pwd1
else:
print("密码输入不一致!")
time.sleep(2)
def get_cardid(self):
'''生成卡号'''
while True:
cardid = random.randint(100000,999999)
if cardid not in self.user_dict:
return cardid
2.查询
def select(self):
''' 查询功能'''
id = int(input("请输入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被锁定,无法使用!")
time.sleep(1)
else:
password = input("请输入该银行卡密码:")
if password == self.user_dict[id].card.password:
print("卡号:%s 持卡人:%s 卡内余额:%d" % (id, self.user_dict[id].name, self.user_dict[id].card.money))
time.sleep(3)
else:
print("密码错误!")
time.sleep(1)
3.存款
def in_money(self):
'''存钱'''
id = int(input("请输入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被锁定,无法使用!")
time.sleep(1)
else:
password = input("请输入该银行卡密码:")
if password == self.user_dict[id].card.password:
money = int(input("请选择存储款数:"))
self.user_dict[id].card.money += money
print("存款成功!卡号:%s 持卡人:%s 卡内余额:%d元" % (id,self.user_dict[id].name,self.user_dict[id].card.money))
time.sleep(3)
self.save()
else:
print("密码错误!")
time.sleep(1)
4.取款
def out_money(self):
'''取款'''
id = int(input("请输入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被锁定,无法使用!")
time.sleep(1)
else:
password = input("请输入该银行卡密码:")
if password == self.user_dict[id].card.password:
money = int(input("请选择取款数:"))
if money>self.user_dict[id].card.money:
print("银行卡余额不足,无法取款!")
else:
self.user_dict[id].card.money -= money
print("取款成功!卡号:%s 持卡人:%s 卡内余额:%d元" % (id,self.user_dict[id].name,self.user_dict[id].card.money))
time.sleep(3)
self.save()
else:
print("密码错误!")
time.sleep(1)
5.转账
def changge(self):
'''转账'''
id = int(input("请输入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被锁定,无法使用!")
time.sleep(1)
else:
id2 = int(input("请输入转账接收者的卡号"))
if id2 not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id2].card.isclock:
print("卡被锁定,无法使用!")
time.sleep(1)
else:
password = input("请输入您的银行卡密码:")
if password == self.user_dict[id].card.password:
money = int(input("请选转账数:"))
if money>self.user_dict[id].card.money:
print("银行卡余额不足,无法转账!")
else:
self.user_dict[id].card.money -= money
print("转账成功!卡号:%s 持卡人:%s 卡内余额:%d元" % (id,self.user_dict[id].name,self.user_dict[id].card.money))
self.user_dict[id2].card.money += money
time.sleep(3)
self.save()
else:
print("密码错误!")
time.sleep(1)
6.改密
def chang_password(self):
'''修改密码'''
id = int(input("请输入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被锁定,无法使用!")
time.sleep(1)
else:
password = input("请输入该原银行卡密码:")
if password == self.user_dict[id].card.password:
self.user_dict[id].card.password = self.get_password()
print("密码修改成功!")
time.sleep(3)
self.save()
else:
print("密码错误!")
time.sleep(1)
7.锁卡
def lock(self):
'''冻结卡'''
id = int(input("请输入要锁定(冻结)的卡号:"))
if self.user_dict[id].card.isclock:
print("该卡号已被锁定,无需重复锁定!")
time.sleep(1)
else:
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
phone = input("请输入预留手机号:") #此处也可以要获取验证码
if phone == self.user_dict[id].phone:
self.user_dict[id].card.isclock = True
print("卡号锁定(冻结)完成!")
self.save()
time.sleep(1)
else:
print("预留手机号码错误!")
time.sleep(1)
8.解卡
def unlock(self):
'''解冻卡'''
id = int(input("请输入要解锁的卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
phone = input("请输入预留手机号:") #此处也可以要获取验证码
if phone == self.user_dict[id].phone:
self.user_dict[id].card.isclock = False
print("卡号解锁完成!")
self.save()
time.sleep(1)
else:
print("预留手机号码错误!")
time.sleep(1)
else:
print("该卡号未被锁定,无需解锁!")
time.sleep(1)
9.补卡
注:补卡后原来的卡将会删除,并且原来卡里的钱需转移到新卡
def add_card(self):
'''补卡'''
id = int(input("请输入丢失的卡的卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
name = input("请输入持卡人姓名:")
user_id = input("请输入身份证号码:")
phone = input("请设置预留手机号:")
password = self.get_password()
cardid = self.get_cardid()
# 有信息之后就可以生成一张卡
card = Card(cardid, password, self.user_dict[id].card.money)
#删除原来的卡
del self.user_dict[id]
# 通过卡找到用户
user = Person(name, user_id, phone, card)
self.user_dict[cardid] = user
print("补卡卡成功!您的卡号为:%s,卡内余额为:%d元" % (cardid, card.money))
self.save()
time.sleep(2)
10.主框架
def main():
v = View()
if v.login() == True:
while True:
o = Operation()
v.operation_show()
choice = input("请输入业务序号:")
if choice == "1": #开户
o.create()
elif choice == "2": #查询
o.select()
elif choice == "3": #存款
o.in_money()
elif choice == "4": #取款
o.out_money()
elif choice == "5": #转账
o.changge()
elif choice == "6": #改密
o.chang_password()
elif choice == "7": #锁卡
o.lock()
elif choice == "8": #解卡
o.unlock()
elif choice == "9": #补卡
o.add_card()
elif choice == "0": #退出
o.save()
break
if __name__ == '__main__':
main()