小型ATM存取款系统是一个非常常见的小项目,适合于python入门学习。
本文实现的小型ATM存取款系统将使用文件来存储数据,并不涉及数据库。
首先,需要在同一目录下创建三个文件,分别是 ATM.py ,person.txt,card.txt 。
person.txt 用于存储用户信息。
card.txt 用于存储卡号信息。
然后,ATM.py 的代码实现如下:
import random
import time
class Views:
def welcome(self):
print('***********************************************')
print('* Welcome to bank! *')
def ATM_loading(self):
print('***********************************************')
print('请稍后,ATM自动存取款系统加载中...')
def ATM_main(self):
print('************************************************')
print('* (1)登录 (2)注册 *')
print('* (3)锁卡 (4)解卡 *')
print('* (5)补卡 (6)退出 *')
print('************************************************')
def user_main(self):
print('************************************************')
print('* (1)查询 (2)存钱 *')
print('* (3)取钱 (4)转账 *')
print('* (5)改密 (6)退出 *')
print('************************************************')
class Card:
cardid = None
password = None
money = None
islock = None
def __init__(self, cardid, password, money, islock):
self.cardid = cardid
self.password = password
self.money = money
self.islock = islock
def msg(self):
ret = f'{self.cardid} {self.password} {self.money} {self.islock}\n'
return ret
class Person:
name = None
userid = None
phone = None
cardid = None
def __init__(self, name, userid, phone, cardid):
self.name = name
self.userid = userid
self.phone = phone
self.cardid = cardid
def msg(self):
ret = f'{self.name} {self.userid} {self.phone} {self.cardid}\n'
return ret
class user_control:
user_msg = None
card_msg = None
def __init__(self, user_msg, card_msg):
self.user_msg = user_msg
self.card_msg = card_msg
def check_money(self):
print(f'用户名: {self.user_msg[0]}')
print(f'卡号: {self.user_msg[3]}')
print(f'余额: {self.card_msg[2]}')
def save_money(self):
num = int(input('请输入您需要存入的金额:'))
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != self.card_msg[0]]
self.card_msg[2] = str(int(self.card_msg[2]) + num)
line = f'{self.card_msg[0]} {self.card_msg[1]} {self.card_msg[2]} {self.card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
time.sleep(1)
print(f'成功存入{num}元')
def get_money(self):
num = int(input('请输入您需要取出的金额:'))
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != self.card_msg[0]]
self.card_msg[2] = str(int(self.card_msg[2]) - num)
line = f'{self.card_msg[0]} {self.card_msg[1]} {self.card_msg[2]} {self.card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
time.sleep(1)
print(f'成功取出{num}元')
def trans_money(self):
name = input('需要转给的用户名:')
cardid = input('需要转给的卡号:')
num = int(input('转账金额:'))
pwd = input('请输入您的密码:')
'''获取对方用户信息'''
fp = open('./person.txt', 'r', encoding='utf-8')
user_msg = None
for line in fp:
list = line.split()
if (list[0] != name):
continue
if (list[3] != cardid):
continue
user_msg = list
break
if user_msg == None:
print('用户名或者卡号信息错误!')
fp.close()
return None
fp.close()
if pwd != self.card_msg[1]:
time.sleep(1)
print('密码错误,转账失败!')
return None
else:
'''更新我方卡号信息'''
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != self.card_msg[0]]
self.card_msg[2] = str(int(self.card_msg[2]) - num)
line = f'{self.card_msg[0]} {self.card_msg[1]} {self.card_msg[2]} {self.card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
'''获取对方卡号信息'''
fc = open('./card.txt', 'r', encoding='utf-8')
card_msg = None
for line in fc:
list = line.split()
if (cardid != list[0]):
continue
card_msg = list
break
fc.close()
'''更新对方卡号信息'''
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != card_msg[0]]
card_msg[2] = str(int(card_msg[2]) + num)
line = f'{card_msg[0]} {card_msg[1]} {card_msg[2]} {card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
time.sleep(1)
print('转账成功!')
return None
def change_pwd(self):
'''输入原密码'''
oldpwd = input('请输入您的原密码:')
if oldpwd != self.card_msg[1]:
print('原密码错误,修改密码失败!')
return None
'''输入新密码'''
password1 = input('请输入您的新密码:')
while len(password1) < 6:
password1 = input('密码必须要6位')
password2 = input('请再次确认密码:')
while password1 != password2:
print('两次密码不一致!')
password1 = input('请输入您的新密码:')
while len(password1) < 6:
password1 = input('密码必须要6位')
password2 = input('请再次确认密码:')
'''更新卡号数据'''
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != self.card_msg[0]]
self.card_msg[1] = password1
line = f'{self.card_msg[0]} {self.card_msg[1]} {self.card_msg[2]} {self.card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
time.sleep(1)
print('修改密码成功!')
return None
class Controller:
def load(self):
name = input('请输入用户名:')
cardid = input('您的卡号是:')
password = input('请输入密码:')
fp = open('./person.txt', 'r', encoding='utf-8')
'''获取用户信息'''
user_msg = None
for line in fp:
list = line.split()
if (list[0] != name):
continue
if (list[3] != cardid):
continue
user_msg = list
break
if user_msg == None:
print('用户名或者卡号信息错误!')
fp.close()
return None
'''获取卡号信息'''
fc = open('./card.txt', 'r', encoding='utf-8')
pwd = None
card_msg = None
for line in fc:
list = line.split()
if (cardid != list[0]):
continue
card_msg = list
pwd = list[1]
break
if password != pwd:
print('密码不正确!')
fc.close()
fp.close()
return None
if card_msg[3] == '1':
print('此卡已被锁,无法登录!')
fc.close()
fp.close()
return None
fc.close()
fp.close()
'''进入用户界面'''
user = user_control(user_msg, card_msg)
view.user_main()
flag = 1
while 1:
if flag:
op = input('请输入要选择的选项:')
else:
op = input('请输入正确的选项:')
if op == '1':
user.check_money()
flag = 1
elif op == '2':
user.save_money()
flag = 1
elif op == '3':
user.get_money()
flag = 1
elif op == '4':
user.trans_money()
flag = 1
elif op == '5':
user.change_pwd()
flag = 1
elif op == '6':
return None
else:
flag = 0
time.sleep(1)
view.user_main()
def regiser(self):
fp = open('./person.txt', 'a+', encoding = 'utf-8')
card1 = str(random.randint(0, 1000000))
card2 = str(random.randint(0, 1000000))
print(f'请选择卡号: {card1}, {card2}')
cardid = input('我选择的卡号是:')
while cardid != card1 and cardid != card2:
print(f'请重新选择卡号: {card1}, {card2}')
cardid = input('我选择的卡号是:')
name = input('请输入用户名:')
userid = input('请输入身份证号:')
phone = input('请输入手机号:')
password1 = input('请输入密码:')
while len(password1) < 6:
password1 = input('密码必须要6位')
password2 = input('请再次确认密码:')
while password1 != password2 :
print('两次密码不一致!')
password1 = input('请输入密码:')
while len(password1) < 6:
password1 = input('密码必须要6位')
password2 = input('请再次确认密码:')
person = Person(name, userid, phone, cardid)
card = Card(cardid, password1, '0', '0')
fc = open('./card.txt', 'a', encoding='utf-8')
fc.write(card.msg())
fc.close()
fp.write(person.msg())
print('恭喜您注册成功!')
fp.close()
def lock(self, sts):
'''输入信息'''
name = input('请输入用户名:')
cardid = input('您的卡号是:')
password = input('请输入密码:')
fp = open('./person.txt', 'r', encoding='utf-8')
user_msg = None
for line in fp:
list = line.split()
if (list[0] != name):
continue
if (list[3] != cardid):
continue
user_msg = list
break
if user_msg == None:
print('用户名或者卡号信息错误!')
fp.close()
return None
'''检查密码'''
fc = open('./card.txt', 'r', encoding='utf-8')
pwd = None
card_msg = None
for line in fc:
list = line.split()
if (cardid != list[0]):
continue
card_msg = list
pwd = list[1]
break
if password != pwd:
print('密码不正确!')
fc.close()
fp.close()
return None
fc.close()
fp.close()
# user_msg, card_msg
'''锁卡'''
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != card_msg[0]]
card_msg[3] = sts
line = f'{card_msg[0]} {card_msg[1]} {card_msg[2]} {card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
time.sleep(1)
if sts == '1':
print('锁卡成功!')
else:
print('解卡成功!')
return None
def unlock(self):
self.lock('0')
return None
def new_card(self):
name = input('请输入用户名:')
cardid = input('请输入原卡号是:')
password = input('请输入密码:')
'''获取用户信息'''
fp = open('./person.txt', 'r', encoding='utf-8')
user_msg = None
for line in fp:
list = line.split()
if (list[0] != name):
continue
if (list[3] != cardid):
continue
user_msg = list
break
if user_msg == None:
print('用户名或者卡号信息错误!')
fp.close()
return None
'''获取卡号信息'''
fc = open('./card.txt', 'r', encoding='utf-8')
pwd = None
card_msg = None
for line in fc:
list = line.split()
if (cardid != list[0]):
continue
card_msg = list
pwd = list[1]
break
if password != pwd:
print('密码不正确!')
fc.close()
fp.close()
return None
fc.close()
fp.close()
# user_msg, card_msg
'''选择新卡号'''
card1 = str(random.randint(0, 1000000))
card2 = str(random.randint(0, 1000000))
print(f'请选择新的卡号: {card1}, {card2}')
cardid = input('我选择的卡号是:')
while cardid != card1 and cardid != card2:
print(f'请重新选择新的卡号: {card1}, {card2}')
cardid = input('我选择的卡号是:')
'''设置密码'''
password1 = input('请设置密码:')
while len(password1) < 6:
password1 = input('密码必须要6位')
password2 = input('请再次确认密码:')
while password1 != password2:
print('两次密码不一致!')
password1 = input('请设置密码:')
while len(password1) < 6:
password1 = input('密码必须要6位')
password2 = input('请再次确认密码:')
'''
新卡号:cardid
新密码:password1 or password2
'''
'''更新用户数据'''
fp = open('./person.txt', 'r', encoding='utf-8')
lines = fp.readlines()
fp.close()
lines = [line for line in lines if line.split()[3] != user_msg[3]]
line = f'{user_msg[0]} {user_msg[1]} {user_msg[2]} {cardid}\n'
lines.append(line)
fp = open('./person.txt', 'w', encoding='utf-8')
fp.close()
fp = open('./person.txt', 'a', encoding='utf-8')
for line in lines:
fp.write(line)
fp.close()
'''更新卡号信息'''
fc = open('./card.txt', 'r', encoding='utf-8')
lines = fc.readlines()
fc.close()
lines = [line for line in lines if line.split()[0] != card_msg[0]]
line = f'{cardid} {password1} {card_msg[2]} {card_msg[3]}\n'
lines.append(line)
fc = open('./card.txt', 'w', encoding='utf-8')
fc.close()
fc = open('./card.txt', 'a', encoding='utf-8')
for line in lines:
fc.write(line)
fc.close()
time.sleep(1)
print('补卡成功!')
return None
ATM = Controller()
view = Views()
view.welcome()
view.ATM_loading()
time.sleep(3)
view.ATM_main()
flag = 1
while 1:
if flag:
op = input('请输入要选择的选项:')
else:
op = input('请输入正确的选项:')
if op == '1':
ATM.load()
flag= 1
elif op == '2':
ATM.regiser()
flag = 1
elif op == '3':
ATM.lock('1')
flag = 1
elif op == '4':
ATM.unlock()
flag = 1
elif op == '5':
ATM.new_card()
flag = 1
elif op == '6':
exit(0)
else:
flag = 0
time.sleep(2)
view.ATM_main()