用python写了一个ATM(自动取款机),共有十种功能,可用作大学生大作业。
具体实现效果:
1.功能模块
2.姓名、身份证号、电话号码的采集、设置密码、向注册的手机号发送信息(包括异常处理)
3.开卡、向注册的手机号发送注册成功信息,并向终端打印个人所有信息
5.改密、并向该账号注册的手机号发送验证码,验证码正确才能改密
7.注销
还有很多功能不便展示,源代码地址:ATM
完整源代码:
__auth__='穆穆max'
data='2021/1/4'
class User():
def __init__(self,name,idcard,phonenum,card):
self.name = name
self.idcard = idcard
self.phonenum = phonenum
self.card = card
class Card:
def __init__(self,cardnum,password,money,islock=False):
self.cardnum = cardnum
self.password = password
self.money = money
self.islock = islock
import random,time,datetime
class ATM:
userDict={
}
#是否登录
islogin = None
#尝试密码的次数
changshicishu = 0
@staticmethod
def welcome():
print('''
**********************
* *
* 欢迎来到中国农业银行 *
* ABC *
* *
**********************
''')
#静态方法 :普通方法和类无关
@staticmethod
def select():
print('''
**********************
* 1.登陆 2.开户 *
* 3.查询 4.取款 *
* 5.存款 0.退出 *
* 6.转账 7.改密 *
* 8.注销 9.解锁 *
* 10.忘记密码 *
**********************
''')
num = input("请选择服务项目:")
return num
@staticmethod
def getcardnum():
cardnum = "622848"
for x in range(10):
cardnum += str(random.randrange(0,10))
return cardnum
#开户 类方法: 给类使用 第一个参数为 cls
@classmethod
def openUser(cls):
simple_pas=[]
for i in range(10):
simple_pas.append(str(i)*6)
user_mas='''
+-------------------+
---{}
【中国农业银行】尊敬的{}
{},您已成功注册中国农业
银行账户{}
+-------------------+
'''
send_mas='''
+-------------------+
---{}
【中国农业银行】尊敬的{}
{},您正在申请中国农业银行
的账户,验证码 {},请勿告
诉他人
+-------------------+
'''
users='''
+-------------------+
姓 名: {}
性 别: {}
年 龄: {}
电 话: {}
身份证:{}
余 额: {}
卡 号:{}
+-------------------+
'''
while True:
try:
name = input("请输入您的姓名:")
idcard = input("请输入您的身份证号码:")
if len(idcard)!=18:
continue
if int(idcard[-2])%2==0:
sex='女士'
else:
sex='先生'
while True:
phonenum = input("请输入您的电话号码:")
if len(phonenum) != 11:
continue
yanzhengma = ""
for x in range(6):
yanzhengma += str(random.randrange(0, 10))
print(send_mas.format(phonenum,name,sex,yanzhengma))
print("已经向您的电话 {} 发送验证码".format(phonenum))
count = 0
while True:
recv=input("请您输入收到的验证码!").strip()
count+=1
if recv!=yanzhengma:
if count>2:
print("验证码输入3次错误!三秒后退出系统!")
time.sleep(3)
exit()
continue
else:
break
break
while True:
psd = input("请设置您的密码:").strip()
if len(psd)!=6 or psd in idcard or psd in simple_pas:
print("您的密码不应该与身份证号相同或者过于简单和格式不对")
continue
psd2 = input("请确认您的密码:")
print("密码保存成功!请您牢记!切勿告诉他人")
if psd == psd2:
mon = int(input("请输入您的预存余额:"))
if mon>0:
cardnum = cls.getcardnum()
card = Card(cardnum,psd,mon)
user = User(name,idcard,phonenum,card)
cls.userDict[cardnum] = user
print("开卡成功,您的卡号为%s,请牢记..."%cardnum)
print(user_mas.format(phonenum,name,sex,cardnum))
today=datetime.datetime.today()
age=int(today.year)-int(idcard[6:10])
print(users.format(name,sex,age,phonenum,idcard,mon,cardnum))
break
else:
print("预存余额非法,开卡失败...")
else:
print("两次输入密码不一致,开卡失败...")
except:
print("输入错误!")
exit(0)
finally:
break
#登录
@classmethod
def login(cls):
cardnum = input("请输入您的卡号:")
user = cls.userDict.get(cardnum)
if user:
while True:
psd = input("请输入您的密码:")
if psd == user.card.password:
print("登录成功!!!")
cls.changshicishu = 0
cls.islogin = cardnum
break
else:
cls.changshicishu += 1
#如果尝试次数大于三 将锁卡
if cls.changshicishu >= 3:
print("卡已被锁,请先解锁")
cls.islogin = False
print(cls.islogin)
break
else:
print("密码错误,请重新输入,还有%d次将锁卡"%(3-cls.changshicishu))
else:
print("卡号不存在,请查证后登录...")
# 查询
@classmethod
def search(cls):
print(cls.islogin)
if cls.islogin:
print("您当前的余额为%d"%(cls.userDict.get(cls.islogin).card.money))
else:
print("请先登录")
#取钱
@classmethod
def setmoney(cls):
if cls.islogin:
money = int(input("请输入您要取的金额:"))
print(cls.userDict.get(cls.islogin).card.money)
if cls.islogin:
if cls.userDict.get(cls.islogin).card.money - money >= 0 :
print("取款%d成功!"%money)
cls.userDict.get(cls.islogin).card.money -= money
print("您卡里的余额为%d"%cls.userDict.get(cls.islogin).card.money)
else:
print("余额不足,取款失败!")
else:
print("未登录,请登录后再试")
else:
print("请先登录")
#存钱
@classmethod
def getmoney(cls):
if cls.islogin:
while True:
gmoney = int(input("请输入您要存储的款项:"))
if gmoney > 0 :
cls.userDict.get(cls.islogin).card.money += gmoney
print("您卡上的余额为%d"%cls.userDict.get(cls.islogin).card.money)
break
else:
print("输入有误,请重新输入金额")
else:
print("请先登录!")
#转账
@classmethod
def zhuanzhang(cls):
if cls.islogin:
while 1:
kahao = input("您需要转到的卡号为:")
for x in cls.userDict.keys():
if x == kahao :
print("您要转的卡号为%s,持卡人为%s"%(x,cls.userDict[x].name))
amoney = int(input("您需要转的金额为"))
#转账人卡内余额减少
cls.userDict.get(cls.islogin).card.money -= amoney
#被转账人卡内余额增加
cls.userDict.get(x).card.money += amoney
else:
continue
print("卡号有误,请重新输入")
break
else:
print("请先登录!")
#改密码
@classmethod
def gaimi(cls):
yanzhengma = ""
for x in range(6):
yanzhengma += str(random.randrange(0, 10))
reset_password='''
+-------------------+
【中国农业银行】您正在尝试
修改密码,验证码为{}
请勿告诉他人
+-------------------+
'''
if cls.islogin:
count=0
while True:
count+=1
print('已经向您的手机号发送了手机号')
print(reset_password.format(yanzhengma))
is_true = input("请输入验证码:").strip()
if is_true==yanzhengma:
count_set_pas=0
while True:
count_set_pas+=1
mima1 = input("请输入您需要修改的密码:").strip()
mima2 = input("请再次输入修改的密码:").strip()
if mima1 == mima2:
cls.userDict.get(cls.islogin).card.password = mima1
print("您的新密码为%s"%mima1)
break
elif count_set_pas>2:
print("三次改密失败!三秒后退出系统")
time.sleep(3)
exit()
else:
continue
elif count>2:
print("三次验证码失败!三秒后退出系统")
time.sleep(3)
exit()
else:
print('验证码错误')
continue
break
else:
print("请先登录")
#锁卡
@classmethod
def suoka(cls):
if cls.changshicishu >=3:
print("卡以被锁,请先解锁")
#解锁
@classmethod
def jiesuo(cls):
print("请输入您的卡号和密码")
kahao1 = input("请输入卡号:")
mima3 = input("请输入密码:")
for x in cls.userDict.keys():
if kahao1 == x:
if mima3 == cls.userDict.get(x).card.password:
print("解锁成功")
cls.islogin = cls.userDict.get(x).card.cardnum
else:
print("账号或者密码不正确,请重试")
else:
print("账号或者密码不正确,请重试")
#注销
@classmethod
def zhuxiao(cls):
cls.islogin = False
print("注销成功")
if __name__ == '__main__':
ATM.welcome()
while True:
time.sleep(1)
print(ATM.userDict)
num = ATM.select()
if num == "2":
print("开户")
ATM.openUser()
elif num == "0":
print("退出")
break
elif num == "1":
print("登录")
ATM.login()
ATM.suoka()
elif num == "3":
print("查询")
ATM.search()
elif num == "4":
ATM.setmoney()
elif num == "5":
ATM.getmoney()
elif num == "6":
ATM.zhuanzhang()
elif num == "7":
ATM.gaimi()
elif num == "8":
ATM.zhuxiao()
elif num == "9":
ATM.jiesuo()
else:
print("选择有误请重新选择...")
如果对我的文章感兴趣,请为我点一个赞,如果有python的知识需要了解或探讨,可以加本人微信:cuiliang1666457052