Python(ATM機low版)
阿新 • • 發佈:2018-11-14
import os
class ATM:
@staticmethod
def regst():
while 1:
nm = input('請輸入你的名字:')
mm = input('請輸入你的密碼:')
mm1 = input('請再次輸入你的密碼:')
if mm == mm1:
print('註冊成功')
money=input('輸入你存的錢:')
user=User(nm,mm,money)
f1=open('zhanghu.txt',mode='a',encoding='utf-8')
f1.write(nm+' '+mm+' '+money)
f1.write('\n')
f1.flush()
f1.close()
break
else:
print('兩次密碼不一樣,重新註冊')
def quqian(self, status): # status 0 , 1
money2 = int(input('請輸入你想取的錢:'))
f4 = open('zhanghu.txt', mode='r', encoding='utf-8')
f5 = open('zhanghu1.txt', mode='w', encoding='utf-8')
for line in f4:
if line.split()[0]==self.name and line.split()[1]==self.mima:
if status == 0:
self.qian =int(line.split()[2])+int(money2)
else:
self.qian = int(line.split()[2]) - int(money2)
print('你的賬戶還有%s元'%self.qian)
f5.write(self.name + ' ' + self.mima + ' ' + str(self.qian))
f5.write('\n')
else:
f5.write(self.name + ' ' + self.mima + ' ' + self.qian)
f5.write('\n')
f4.flush()
f5.flush()
f4.close()
f5.close()
os.remove('zhanghu.txt')
os.rename('zhanghu1.txt', 'zhanghu.txt')
class User(ATM):
def __init__(self,name,mima,qian):
self.name=name
self.mima=mima
self.qian=qian
while 1:
n=input('註冊輸1,登入輸2,退出輸其他')
if n=='1':
fn=getattr(ATM,'regst')
fn()
elif n=='2':
na1=input('輸入你的名字:')
mi1=input('輸入你密碼:')
f6 = open('zhanghu.txt', mode='r', encoding='utf-8')
for line in f6:
if line.split()[0] == na1 and line.split()[1] == mi1:
user=User(line.split()[0],line.split()[1],line.split()[2])
f6.flush()
f6.close()
print('登入成功')
while 1:
n1=input('存錢輸1,取錢輸2,查詢輸3,退出輸其他')
if n1=='1':
user.quqian(0)
elif n1=='2':
user.quqian(1)
elif n1=='3':
user.chaxun()
else:
break
break
else:
print('登入不成功')