ATM 的实现:
需求: * 1.登陆 2.开户 * * 3.查询 4.取款 * * 5.存款 6.退出 * * 7.转账 8.改密 * * 9.锁卡 10.解锁 *
面向过程写法: 下面还有使用面向对象的写法
# 转账
def transfer_account(card_id):
count = 0
print("\03[1;34m欢迎来到转账界面!\033")
while True:
target_card_id = input("请输入要转账的账号>>")
if target_card_id not in card:
print("该卡号不存在,请输入正确的卡号")
continue
target_money = float(input("你要转多少钱给他>>"))
user_password = input("请输入支付密码>>>")
if user_password == card[card_id]['password']:
if card[card_id]['money'] - target_money > 0:
print("正在转账,请稍等")
time.sleep(0.5)
# 当前用户余额减少 target_money
card[card_id]['money'] -= target_money
card[target_card_id]['money'] += target_money
print("转账成功,当前余额%s" % card[card_id]['money'])
write_data(card)
else:
print("转账失败,余额不足")
else:
if count >= 3:
print("输入错误次数过多,锁定账号")
card[card_id]['lock'] = True
break
print("密码输入错误次数%s" % count + 1)
count += 1
user_cmd = input("是否返回上一层[yes/no]")
if user_cmd == 'yes':
break
# 改密
def modify_password(card_id):
print("欢迎来到改密码界面")
user_cmd = input("您确认要修改密码吗?")
if user_cmd == 'yes':
while True:
user_password = input("请输入你的密码>>")
user_confirm_pwd = input("请确认密码>>")
if user_password == user_confirm_pwd:
print("密码修改成功,即将返回上一层")
card[card_id]['password'] = user_password
time.sleep(0.5)
print("请记住密码%s******************"%user_password)
write_data(card)
break
else:
print("两次密码不一致!!! 重新输入")
else:
print("退出修改密码界面")
return
# 锁卡功能
# 锁卡
def lock_up(card_id):
print("欢迎来到锁卡界面")
password = input("请输入密码>>")
if card[card_id]['password'] == password:
print("正在锁卡.....")
time.sleep(0.5)
card[card_id]['lock'] = True
write_data(card)
print("锁卡成功,")
return
else:
print("请输入正确的密码")
# 解锁功能
def unlock(card_id):
print("欢迎来到解锁界面")
while True:
id=input("请输入身份证id>>")
phone=input("请输入手机号码>>")
if card[card_id]['id']==id and phone==card[card_id]['phone']:
card[card_id]['lock']=False
write_data(card)
print("解锁成功,正在返回上一层")
return
else:
print("请输入正确的信息")
continue
# 退出系统
def logout(card_id):
print("""\033[1;34m
**********************
* 欢迎下次光临, *
* 谢谢使用该系统 *
* *
**********************
\033""")
card_id['status'] = 0
exit()
# 存钱
def depoist(card_id=None):
if card_id == None:
print("请先登录")
return
if card[card_id]['status'] != 1:
print("请先登录!")
return
while True:
print("\033[1;34m欢迎来到存钱的界面\033")
user_depoist_money = float(input("你要存多少钱,请输入>>>"))
# card_id 是当前账号的一个字典
card[card_id]['money'] += user_depoist_money
write_data(card)
print("成功存入%s" % user_depoist_money)
user_cmd = input("是否返回上一层[yes/no] 输入1可查看当前信息")
if user_cmd == 'yes':
break
if user_cmd == '1':
show_info(card_id)
# 取钱
def withdraw(card_id=None):
if card_id == None:
print("请先登录")
return
if card[card_id]['status'] != 1:
print("请先登录!")
return
while True:
print("欢迎来到取钱界面")
if card[card_id]['money'] > 0:
withdraw_money = float(input("请输入要取款的金额>>>"))
card[card_id]['money'] -= withdraw_money
write_data(card)
print("取款成功剩余金额>%s" % card[card_id]['money'])
else:
print("没钱你还想取钱?")
user_cmd = input("是否返回上一层[yes/no] 输入1可查看当前信息")
if user_cmd == 'yes':
break
if user_cmd == '1':
show_info(card_id)
# 查看信息
def show_info(card_id=None):
# user_info={'alex':{'password':'123456','phone':123456,
# 'id':123465,'money':123456,'username':'alex',
# 'status':0}}
# card = {'123456': user_info['alex']}
if card_id == None:
print("请先登录")
return
while True:
print("当前信息为》》》》》》》》》》》》》》》")
id = card_id
phone = card[card_id]['phone']
username = card[card_id]['username']
password = card[card_id]['password']
money = card[card_id]['money']
print("""\033[1;31;43m
**************************
* 用户名:%s *
* 卡号:%s *
* 电话:%s *
* 身份证号码:%s *
* 余额:%s *
* 密码:%s *
**************************
\033""" % (username, card_id, phone, id, money, password))
user_cmd = input("返回上一层[yes/no]")
if user_cmd == 'yes':
break
# 开户
def account(card):
# user_info={'alex':{'password':'123456','phone':123456,
# 'id':123465,'money':123456,'username':'alex',
# 'status':0}}
user_info={}
print("\033[1;33m欢迎来到开户界面\033")
while True:
id = input("请输入您的身份证号码>>")
phone = input("请输入电话号码>>")
username = input("请输入用户名>>>")
password = input("请输入密码>>>")
status = 0
money = 0
user_info={'password': password, 'phone': phone, 'id': id, 'money': money, 'username': 'alex',
'status': status,'lock':False,}
# 产生卡号
while True:
random_card_id = random.sample([str(i) for i in range(0, 9)], 6)
if random_card_id not in card:
break
user_card_id = "".join(random_card_id)
card[user_card_id] = user_info
write_data(card)
print("\033[1;31m开户成功恭喜!\033")
choice('account')
user_cmd = input("请输入操作")
if user_cmd == '1':
login(user_card_id, password)
if user_cmd == '2':
logout(user_info[username])
if user_cmd == '4':
show_info(user_card_id)
if user_cmd == '3':
break
# 登录
def login(user_card_id, pwd):
# 累加用户登录失败次数
count=0
# 检查当前用户的密码是否正确
# card[user_card_id] 当前用户的卡
print("欢迎进入登录界面!")
while True:
if card[user_card_id]['password'] == pwd:
card[user_card_id]['status'] = 1
print("\033[1;32登录成功!欢迎%s\33" % card[user_card_id]['username'])
# 查看登录的操作
while True:
choice('login_choice')
user_cmd = input("请选择你的操作")
if user_cmd == '1':
show_info(user_card_id)
# 存钱
elif user_cmd == '2':
withdraw(user_card_id)
# 取钱
elif user_cmd == '3':
depoist(user_card_id)
# 退出
elif user_cmd == '4':
logout(card[user_card_id])
# 退出
elif user_cmd == '5':
return
# 转账
elif user_cmd == '6':
transfer_account(user_card_id)
# 改密码
elif user_cmd == '7':
modify_password(user_card_id)
else:
print("\033[1;31m请输入正确的选项\033")
else:
if count>=3:
print("\033[1;31m输入密码错误次数过多,锁定该卡号\033")
card[user_card_id]['lock']=True
return
pwd=input("密码错误请从新输入次数%s"%count)
count+=1
# 用户选择操作
def choice(cmd=None):
if cmd == None:
print("""
***********************
* 1.登陆 2.开户 *
* 3.查询 4.取款 *
* 5.存款 6.退出 *
* 7.转账 8.改密 *
* 9.锁卡 10.解锁 *
***********************
""")
user_cmd = input("\033[1;33m请输入您的选择>>>\033")
return user_cmd
# 登陆后的操作
if cmd == 'login_choice':
print("""
***********************
* 1.查询 2.取款 *
* 3.存款 4.退出系统 *
* 5.返回上一层 6.转账 *
* 7.改密 *
***********************
""")
if cmd == 'account':
print("""
* * * * * * * * * * *
* 1.登录 *
* 4.查询 *
* 2.退出系统 *
* 3. 返回上一层 *
* * * * * * * * * * *
""")
# 欢迎界面
def welcome():
print("""\033[1;34m
**********************
* *
* welcome to bank *
* *
**********************
\033""")
# 程序入口
def main():
fun_dic = {'1': login, '2': account, '3': show_info,
'4': depoist, '5': withdraw, '6': logout, }
welcome()
while True:
cmd = choice()
# 登录
if cmd == '1':
user_card_id = input("请输入卡号>>")
pasword = input("请输入密码>>")
if user_card_id in card:
# print(card[user_card_id])
if card[user_card_id]['lock'] == False:
login(user_card_id, pasword, )
else:
print("该卡号已被锁定,无法登陆")
else:
print("该卡号不存在")
user_res = input("您需要去开户[yes/no]")
if user_res == 'no':
pass
if user_res == 'yes':
account(card)
# 开户
if cmd == '2':
account(card)
# 查询
if cmd == '3':
print("不登录你看毛线啊")
print("\033[0;31m这是当前用户,你还想看什么,来来来什么都给你看\033")
print(card)
show_info()
# 取款
if cmd == '4':
print("请前往登录")
depoist()
# 存款
if cmd == '5':
print("请前往登录")
withdraw()
# 退出
if cmd == '6':
break
# 转账
if cmd == '7':
print("请前往登录")
# 改密
if cmd == '8':
print("请前往登录")
# 锁卡
if cmd == '9':
while True:
user_card_id=input("请输入卡号>>>")
user_password=input("请输入密码>>>")
if user_card_id in card:
if card[user_card_id]['lock']==True:
print("该用户已是锁定状态")
user_cmd=input("是否退出[yes/no]")
if user_cmd=='yes':
break
if card[user_card_id]['password']==user_password:
lock_up(user_card_id)
break
else:
print("密码错误请从新输入")
else:
print("卡号不存在")
user_cmd=input("返回上一层[yes]/no")
if user_cmd=='yes':
break
# 解锁
if cmd == '10':
while True:
user_card_id=input("请输入卡号>>>")
user_password=input("请输入密码>>>")
if user_card_id in card:
if card[user_card_id]['lock']==False:
print("该用户不需要解锁")
user_cmd=input("是否退出[yes/no]")
if user_cmd=='yes':
break
if card[user_card_id]['password']==user_password:
unlock(user_card_id)
break
else:
print("密码错误请从新输入")
else:
print("卡号不存在")
user_cmd=input("返回上一层[yes]/no")
if user_cmd=='yes':
break
# 初始化数据
def init_data():
with open(r'bank1','r',encoding="utf-8")as f:
res_data=f.read()
return res_data
# 写入数据
def write_data(card):
with open('bank1','w',encoding="utf-8")as f:
f.write(str(card))
# 读取数据
def read_data(path):
with open(path,'r',encoding="utf-8")as f:
res_data=f.read()
return res_data
if __name__ == '__main__':
import random
import time
# 测试数据
"""
card = {'123456': {'password': '123456', 'phone': '123456',
'id': '123465', 'money': 123456, 'username': 'alex',
'status': 0, 'lock': False}},
'111111':{'password': '123456', 'phone': '123456',
'id': '123465', 'money': 123456, 'username': 'egon',
'status': 0, 'lock': False}},
}
"""
# 从文件中读取数据 ------card
path=r'D:\bank1'
# 获取初始化数据 字符串
card=read_data(path)
# 得到字典数据类型 如果card为空串""则eval会报错 这里我没检测
card=eval(card)
# print(eval(card))
main()
有4个py文件
1.account.py (用户类)
2.card.py (卡类)
3.bank.py(银行类)
4.main.py(程序执行入口)
main.py
from day11.card import Card
from day11.account import Account
from day11.bank import Bank
from day11 import bank
import json
import godjson
# 实现的功能
"""
* 1.登陆 2.开户 *
* 3.查询 4.取款 *
* 5.存款 6.退出 *
* 7.转账 8.改密 *
* 9.锁卡 10.解锁 *
"""
# 主程序
def main():
while True:
user_cmd=big_bank.choice()
# 登录
if user_cmd=='1':
user_card_id=input("请输入卡号进行登录")
if user_card_id not in big_bank.accounts:
print("该卡号不存在!!!")
continue
if big_bank.accounts[user_card_id].card.lock==True:
print("该卡号已被锁")
user_password=input("请输入密码")
if user_password==big_bank.accounts[user_card_id].card.card_password:
big_bank.login(user_card_id)
# 开户
if user_cmd=='2':
big_bank.open_account()
# 退出
if user_cmd=='3':
big_bank.logout()
# 改密
if user_cmd=='4':
user_card_id = input("请输入你的卡号>>>")
if user_card_id not in big_bank.accounts:
print("请输入一个正确,并且存在的卡号!")
continue
identify_id=input("请输入你的身份证号码>>>")
phone=input("请输入你的电话号码>>>")
user_name=input("请输入你的名字>>>")
res=big_bank.modify_password(user_card_id,identify_id,phone,user_name)
if res==True:
print("改密成功")
else:
print("改密失败")
# 解锁
if user_cmd=='5':
user_card_id = input("请输入你的卡号")
big_bank.unlock(user_card_id)
if __name__ == '__main__':
# data={}
# test_card=Card('123456','123456',999999,1)
# test_card2=Card('1234562','123456',999999,1)
# test_user=Account('alex','123456','123456789',test_card)
# data[test_card.card_id]=test_user
# data[test_card2.card_id]=test_user
# 手动的反序列化 没有用object_hook
# 读取文件数据
data=bank.read_data('bank')
big_bank=Bank()
big_bank.accounts=data
# 程序开始
main()
account.py
"""
用户:
属性:name、身份证号码、电话号码、卡
"""
class Account:
def __init__(self,name,id,phone,card):
self.name=name
self.id=id
self.phone=phone
self.card=card
card.py
"""
卡:
属性:卡号、密码、余额、锁卡状态
"""
class Card:
def __init__(self,card_id,card_password,balance,status):
self.card_id=card_id
self.card_password=card_password
self.balance=balance
self.status=status
self.lock=False
# 将ATM系统改写成面向对象:
# ATM:
# 属性:所有用户信息
# 行为:
# 1.取款
# 2.存款
# 3.转账
# 4.改密
# 5.登陆
# 6.开户
# 7.锁卡
# 8.解锁
# 9.退出
from day11.card import Card
from day11.account import Account
import time
import random
import json
class Bank:
def __init__(self,):
self.accounts={}
# 存钱 isTrue
def depoist(self,money,user_card_id):
# 失败
self.accounts[user_card_id].card.balance+=money
wirte_data(self.accounts)
print("剩余余额%s" % self.accounts[user_card_id].card.balance)
# 取钱 isTrue
def withdraw(self,user_card_id):
money=float(input("请输入你要取走的金额"))
if money > self.accounts[user_card_id].card.balance:
return False
else:
self.accounts[user_card_id].card.balance-=money
wirte_data(self.accounts)
print("剩余余额%s"%self.accounts[user_card_id].card.balance)
return True
# 转账 isTrue
def transfer_money(self,user_card_id,target_account,money):
# print(type(money),"钱*"*120)
if self.accounts[user_card_id].card.balance>money:
self.accounts[user_card_id].card.balance-=money
self.accounts[target_account].card.balance+=money
wirte_data(self.accounts)
return True
else:
return False
# 改密
def modify_password(self,user_card_id,identify_id,phone,user_name):
if self.accounts[user_card_id].name==user_name and self.accounts[user_card_id].id==identify_id and self.accounts[user_card_id].phone==phone:
print("数据校验正确")
user_password=input("请输入要修改的密码>>>")
self.accounts[user_card_id].card.card_password=user_password
wirte_data(self.accounts)
return True
else:
return False
# 登录
def login(self,user_card_id):
self.welcome()
while True:
self.choice('login_choice')
user_choice=input("请输入你的选择")
# 查询
if user_choice=='1':
self.show_info(user_card_id)
# 取款
if user_choice=='2':
res=self.withdraw(user_card_id)
if res==True:
print("取钱成功")
else:
print("失败,可能是余额不足")
# 存款
if user_choice=='3':
money=float(input("请输入你要存入的金额"))
res=self.depoist(money,user_card_id)
if res==True:
print("存钱成功")
# 退出系统
if user_choice=='4':
self.logout()
# 返回上一层
if user_choice=='5':
break
# 转账
if user_choice=='6':
while True:
target_account=input("请输入要转账的账号>>>")
if target_account in self.accounts:
money=float(input("请输入金额>>>"))
res=self.transfer_money(user_card_id,target_account,money)
if res==True:
print("转账成功")
else:
print("失败")
else:
print("该账号不存在")
user_cmd=input("是否继续操作?[yes/no]")
if user_cmd=='no':
break
# 解锁
if user_choice=='7':
self.unlock(user_card_id)
# 开户
def open_account(self):
count=0
while True:
# 录入用户的数据
user_name=input("\033[1;34m请输入你的用户名>>>\033")
user_phone=input("请输入你的电话号码>>>")
user_identify=input("请输入你的身份证号码>>>")
user_passowrd=input("请输入你的账号密码>>>")
user_confirm_passowrd=input("请确认你的账号密码>>>")
user_balance = float(input("输入你预存的金额>>>"))
if user_passowrd!=user_confirm_passowrd:
print("\033[1;31m请输入两个一样的密码,或数据不完整\033")
count+=1
if count==5:
print("输入错误次数太多了")
return False
continue
# 产生一个随机卡号
while True:
user_random_card=random.sample([str(i) for i in range(0,9)],6)
user_random_card_id="".join(user_random_card)
if user_random_card_id in self.accounts:
continue
else:
# test_card = Card('123456', '123456', 999999, False)
# test_user = Account('alex', '123456', '123456789', test_card)
# 把该账号存入进银行
user_card=Card(user_random_card_id,user_passowrd,user_balance,1)
user_info_dic =Account(user_name,user_identify,user_phone,user_card)
self.accounts[user_random_card_id]=user_info_dic
wirte_data(self.accounts)
print("开户成功账号----%s"%user_random_card_id)
return True
# 锁卡 isTrue cur
def lock_card(self,user_card_id):
user_cmd=input("确认锁卡吗?[yes/no]")
if user_cmd=='yes':
self.accounts[user_card_id].card.lock=True
wirte_data(self.accounts)
print("锁卡成功!!!!")
return True
return False
# 解锁
def unlock(self,user_card_id):
if self.accounts[user_card_id].card.lock==True:
print("该用户已经被锁定了")
return True
while True:
user_identify_id=input("请输入身份证>>>")
user_phone=input("请输入用户的电话号码>>>")
user_name=input("请输入用户的姓名>>>")
if self.accounts[user_card_id].name == user_name and self.accounts[user_card_id].id == user_identify_id and self.accounts[user_card_id].phone == user_phone:
print("正在解锁。。。")
time.sleep(1)
self.accounts[user_card_id].card.lock = False
wirte_data(self.accounts)
print("解锁成功")
return True
else:
print("解锁失败请输入正确的信息")
# 退出
def logout(self):
print("欢迎使用该系统")
exit()
# 查询
def show_info(self,user_card_id=None):
if user_card_id==None:
print("""\033[1;31;43m
**************************
* 卡号:%s *
**************************
\033""" % (self.accounts,))
return
user_name=self.accounts[user_card_id].name
user_phone=self.accounts[user_card_id].phone
user_id=self.accounts[user_card_id].id
user_card_balance=self.accounts[user_card_id].card.balance
user_islogin=self.accounts[user_card_id].card.status
print("""\033[1;31;43m
**************************
* 用户名:%s *
* 卡号:%s *
* 电话:%s *
* 身份证号码:%s *
* 余额:%s *
* 登录状态:%s *
**************************
\033""" % (user_name, user_card_id, user_phone, user_id, user_card_balance, user_islogin))
return
@staticmethod
def choice(cmd=None):
if cmd == None:
print("""
***********************
* 1.登陆 2.开户 *
* 3.退出 4.解锁 *
* 5.改密 *
***********************
""")
user_cmd = input("\033[1;33m请输入您的选择>>>\033")
return user_cmd
# 登陆后的操作
if cmd == 'login_choice':
print("""
***********************
* 1.查询 2.取款 *
* 3.存款 4.退出系统 *
* 5.返回上一层 6.转账 *
***********************
""")
if cmd == 'account':
print("""
* * * * * * * * * * *
* 1.登录 *
* 4.查询 *
* 2.退出系统 *
* 3. 返回上一层 *
* * * * * * * * * * *
""")
@staticmethod
def welcome():
print("""\033[1;34m
**********************
* *
* welcome to bank *
* *
**********************
\033""")
# 写入数据
def wirte_data(data):
print(data)
res=json.dumps(data,default=user_obj2json)
with open('bank','w',encoding="utf8")as f:
f.write(res)
# 读取数据
def read_data(path):
with open(path,'r',encoding="utf8")as f:
str=f.read()
if str:
return reader(str)
return {}
# 反序列化
def user_json2obj(dic):
return Account(dic['name'],dic['id'],dic['phone'],Card(dic['card']['card_id'],dic['card']['card_password'],dic['card']['balance'],dic['card']['status']))
# 循环反序列化
def reader(res_data):
dic={}
res_data=json.loads(res_data)
# print(res_data)
# print(res_data,"*"*20)
for k,v in res_data.items():
dic[k]=user_json2obj(v)
# print(k,v)
return dic
# 序列化
def user_obj2json(obj):
print(obj)
res_obj={}
for key,value in obj.__dict__.items():
res_obj[key]=value
# print(res_obj)
return res_obj