需求文档.md
# card 类 存储卡类
卡号 , 密码 , 余额 , 锁定状态
cardid password money islock
# person 类 用户类
用户名,身份证,手机号,卡
name,userid,phone,card
# view 类 视图类
登录 , 打印欢迎界面 , 打印操作界面
# operation 类 操作类
把十个功能都写在操作类当中
# main 调用十个功能
(1)开户 : register
(2)查询 : query
(3)存钱 : save_money
(4)取钱 : get_money
(5)转账 : trans_money
(6)改密 : change_pwd
(7)锁卡 : lock
(8)解卡 : unlock #
(9)补卡 : new_card #
(10)退出: save
ATM_Demo/
├── 需求文档.md # 程序需求文档
├── user.txt # 数据库文件
├── userid.txt # 数据库文件
├── main.py # 程序运行主文件
├── package/ # 功能包
├── card.py
├── operation.py
├── person.py
├── view.py
from package.view import View
from package.operation import Operation
class Main():
@staticmethod
def run():
if View.login():
obj=Operation()
while True:
choice = input("请选择需要办理的业务")
if choice == "1":
obj.register()
elif choice == "2":
obj.query()
elif choice == "3":
obj.save_money()
elif choice == "4":
obj.get_money()
elif choice == "5":
obj.trans_money()
elif choice == "6":
obj.change_pwd()
elif choice == "7":
obj.lock()
elif choice == "8":
obj.unlock()
elif choice == "9":
obj.new_card()
elif choice == "0":
obj.save()
break
else:
print("您输入的业务指令有误,请重新选择")
if __name__ == "__main__":
Main.run()
# ### card 类
# 卡号: cardid 密码:password 余额:money 锁定状态:islock
class Card():
def __init__(self,cardid,password,money):
self.cardid = cardid
self.password = password
self.money = money
#默认新开的卡都不锁
self.islock = False
# ### operation 操作类
import re
import pickle
import os
import random
import time
from .card import Card
from .person import Person
class Operation():
def __init__(self):
#加载数据 卡号: 用户对象
self.load_user()
#加载数据 身份证:卡号
self.load_userid()
def load_user(self):
#判断文件是否存在,存在时候就拿数据,否则初始化字典
if os.path.exists("user.txt"):
with open("user.txt",mode="rb") as fp:
self.user_dict = pickle.load(fp)
else:
self.user_dict = {}
print(self.user_dict)
def load_userid(self):
if os.path.exists("userid.txt"):
with open("userid.txt",mode="rb") as fp:
self.userid_dict = pickle.load(fp)
else:
self.userid_dict = {}
print(self.userid_dict)
#(0)保存退出 save
def save(self):
with open("user.txt",mode="wb") as fp:
pickle.dump(self.user_dict,fp)
with open("userid.txt",mode="wb") as fp:
pickle.dump(self.userid_dict,fp)
#(1)开户 register
def register(self):
name = self.get_name()
userid=self.get_userid()
phone=self.get_phone()
#获取密码
password = self.get_cwd()
# 获取卡号
cardid = self.get_cardid()
#余额默认为10
money=10
#创建一张新卡
card=Card(cardid,password,money)
#创建一个用户
user=Person(name,userid,phone,card)
#存储数据(卡号:用户对象)
self.user_dict[cardid]=user
#存储数据(身份证号:卡号)
self.userid_dict[userid]=cardid
#给提示
print("恭喜{}开卡成功,您的卡号为{},卡内余额{}元".format(name,cardid,money))
#匹配姓名(不能为空)
def get_name(self):
while True:
name = input("请输入你的姓名(不能为空):")
res = re.findall("^\S+$",name)
if res == []:
print("您输入的姓名格式不对")
else:
return name
#匹配身份证
def get_userid(self):
while True:
userid=input("请输入您的身份证号:")
res = re.findall('^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$',userid)
if res == []:
print("您输入的身份证号码格式不对")
else:
if userid not in self.userid_dict:
return userid
else:
print("您的身份证号码已有关联卡")
#匹配手机号:
def get_phone(self):
while True:
phone = input("请输入您的手机号:")
res = re.findall("^1[3-9]\d{9}$",phone)
if res == []:
print("您输入的手机号格式不对")
else:
return phone
#获取密码
def get_cwd(self):
while True:
pwd1 = input("请输入您的密码(长度6位,不能含有空格)")
res = re.findall("^\S{6}$",pwd1)
if res != []:
pwd2 = input("请确认您的密码")
if pwd1 == pwd2:
#return 直接终止当前函数,后面的代码不执行
return pwd1
else:
print("两次密码不一致,请重新输入")
else:
print("你输入的密码不满足要求")
#获取新密码
def get_newcwd(self):
res = True
while res:
pwd1 = input("请输入您的新密码(长度6位不能为空),按q退出改密模式")
if pwd1.upper() == "Q":
res = False
break
else:
pwd2 = input("请确认您的新密码")
if pwd1 == pwd2:
#return 直接终止当前函数,后面的代码不执行
return pwd1
else:
print("两次密码不一致,请重新输入新密码")
#可以设置按q退出
#获取卡号
def get_cardid(self):
while True:
cardid = str(random.randrange(100000,1000000))
if cardid not in self.user_dict:
return cardid
#(2)查询
def query(self):
card = self.getcard_info()
#card 要么返回False 要么返回真实对象
if not card:
print("对不起,你的卡不存在")
else:
#判断是是否被锁住
if card.islock:
print("对不起您的卡被锁了")
else:
if self.check_pwd(card):
print("你的卡余额是{}元".format(card.money))
#获取卡信息
def getcard_info(self):
cardid = input("请输入你的卡号")
if cardid not in self.user_dict:
return False
else:
#获取用户对象
user = self.user_dict[cardid]
#通过用户对象 -> 卡对象
return user.card
#获取转账卡信息
def getaccount_info(self):
cardid = input("请输入你要转账的对方卡号")
if cardid not in self.user_dict:
return False
else:
#获取用户对象
user = self.user_dict[cardid]
#通过用户对象 -> 卡对象
return user.card
# 超过三次直接锁卡
def check_pwd(self,card):
count = 1
while count < 4:
pwd = input("请输入密码:")
if pwd == card.password:
return True
else:
#剩余次数 = 总次数 - 已经循环的次数
print("密码错误,您还剩余%d次机会" % (3-count))
if count == 3:
card.islock = True
print("抱歉,因为密码错了三次,你的卡被锁了")
count += 1
# 验证身份信息
def check_id(self,card):
userid = input("请输入您的身份证号码:")
if userid == self.user_dict[card.cardid].userid:
return True
else:
print("身份证号不正确")
#(3)存钱
def save_money(self):
card = self.getcard_info()
if not card:
print("抱歉您的卡不存在")
else:
#判断是否锁定
if card.islock:
print("对不起,您的卡被锁住了")
else:
#获取用户对象
user = self.user_dict[card.cardid]
print("您的卡用户名:%s" % (user.name))
key_sure = input("确认存款请按1,按任意键返回上一层!")
if key_sure == "1":
str_money = input("请输入您的存款金额:")
if str_money.isdecimal():
money = int(str_money)
#充值
card.money += money
print("成功存入%s元" % (money))
else:
print("你输入的金额有误")
#(7)锁卡
def lock(self):
card = self.getcard_info()
if not card:
print("抱歉,您的卡不存在")
else:
num = input("1.使用密码冻结 2.使用身份冻结")
if num == "1":
if self.check_pwd(card):
card.islock = True
print("====锁卡成功=====")
elif num == "2":
userid = input("请输入您的身份证号:")
user = self.user_dict[card.cardid]
if userid == user.userid:
card.islock = True
print("====锁卡成功=====")
else:
print("====锁卡失败=====")
#(8) 解卡
def unlock(self):
card = self.getcard_info()
if not card:
print("抱歉,您的卡不存在")
else:
if self.check_id(card):
key_sure = input("确认解卡请按1,按任意键返回上一层!")
if key_sure == "1":
card.islock = False
print("===解卡成功===")
else:
print("=====解卡失败=====")
else:
print("=====解卡失败=====")
#(4) 取钱
def get_money(self):
card = self.getcard_info()
if not card:
print("抱歉您的卡不存在")
else:
#判断是否锁定
if card.islock:
print("对不起,您的卡被锁住了")
else:
if self.check_pwd(card):
#获取用户对象
user = self.user_dict[card.cardid]
print("您的卡用户名:%s" % (user.name))
key_sure = input("确认取钱请按1,按任意键返回上一层!")
if key_sure == "1":
str_money = input("请输入您的取款金额:")
if str_money.isdecimal():
money = int(str_money)
if money <= card.money:
#充值
card.money -= money
print("成功取出%s元,余额%s元" % (money,card.money))
else:
print("卡余额不足")
else:
print("你输入的金额有误")
#(5)转账
def trans_money(self):
card = self.getcard_info()
if not card:
print("抱歉您的卡不存在")
else:
#判断是否锁定
if card.islock:
print("对不起,您的卡被锁住了")
else:
if self.check_pwd(card):
account = self.getaccount_info()
if not card:
print("抱歉转账账户不存在或者格式有误")
else:
#判断是否锁定
if account.islock:
print("对不起,对方账户被锁住了")
elif account.cardid == card.cardid:
print("对不起,不可以自己给自己转账")
else:
user = self.user_dict[card.cardid]
print("您的卡用户名:%s" % (user.name))
key_sure = input("确认转账请按1,按任意键返回主菜单!")
if key_sure == "1":
str_money = input("请输入您的转账金额:")
if str_money.isdecimal():
money = int(str_money)
if money <= card.money:
#充值
card.money -= money
account.money += money
print("成功转账%s元,余额%s元" % (money,card.money))
else:
print("卡余额不足")
else:
print("你输入的金额有误")
#(6)改密
def change_pwd(self):
card = self.getcard_info()
if not card:
print("抱歉,您的卡不存在")
else:
num = input("1.使用原密码改密 2.使用身份证改密")
if num == "1":
if self.check_pwd(card):
card.password = self.get_newcwd()
print("====改密成功=====")
elif num == "2":
userid = input("请输入您的身份证号:")
user = self.user_dict[card.cardid]
if userid == user.userid:
card.password = self.get_newcwd()
print("====改密成功=====")
else:
print("=====身份证不匹配,改密失败=======")
else:
print("====输入数字不对,改密失败=====")
#(9)补卡
def new_card(self):
card = self.getcard_info()
if not card:
print("抱歉,您的卡不存在")
else:
#验证身份证号和密码
if self.check_pwd(card) and self.check_id(card):
#创建新卡
#1. 获取卡号
cardid_new = self.get_cardid()
time.sleep(1)
print("已冻结原卡并获取新卡,接下来请为新卡设置密码")
#2.设置获取密码
password_new = self.get_cwd()
#3.余额继承
money_new=card.money
#创建一张新卡
card_new = Card(cardid_new,password_new,money_new)
#提取用户
user = self.user_dict[card.cardid]
user.card = card_new
#存储数据(卡号:用户对象)
self.user_dict[cardid_new] = user
#存储数据(身份证号:卡号)
self.userid_dict[user.userid]=cardid_new
card_new.islock = False
#删除原卡
self.user_dict.pop(card.cardid)
print("恭喜{}补卡成功,您的新卡号为{},卡内余额{}元".format(user.name,cardid_new,money_new))
else:
print("补卡失败")
### #### person类
#姓名: name 身份证: userid 手机号:phone 卡:card
class Person():
def __init__(self,name,userid,phone,card):
self.name = name
self.userid = userid
self.phone = phone
self.card = card
# ### view类
import time
class View():
def login():
name = input("请输入管理员账户:")
pwd = input("请输入管理员密码:")
if name == "admin" and pwd == "111":
welcomeview()
#延时0.5s
time.sleep(0.5)
operationview()
return True
else:
print("管理员账号或密码不正确")
def welcomeview():
print("*"*40)
print("*"*1+" "*38+"*"*1)
print("*"*1+" "*38+"*"*1)
print("*"*1+" "*8+"Welcome To Oldboy Bank"+" "*8+"*"*1)
print("*"*1+" "*38+"*"*1)
print("*"*1+" "*38+"*"*1)
print("*"*40)
def operationview():
print("*"*40)
print("*"*1+" "*10+"开户(1) 查询(2)"+" "*10+"*"*1)
print("*"*1+" "*10+"存钱(3) 取钱(4)"+" "*10+"*"*1)
print("*"*1+" "*10+"转账(5) 改密(6)"+" "*10+"*"*1)
print("*"*1+" "*10+"锁卡(7) 解卡(8)"+" "*10+"*"*1)
print("*"*1+" "*10+"补卡(9) 退出(0)"+" "*10+"*"*1)
print("*"*40)