具备以下功能:
登录(account ,password)
存款
取款
转账
查询余额
import xlrd
from xlutils.copy import copy
FILENAME = "atm.xls"
CURRENT_ACCOUNT = {}#当前登录的账号值
CURRENT_ACCOUNT_XY = []
#读取excel表格数据
def read_excel():
book = xlrd.open_workbook(FILENAME)
sheet = book.sheet_by_index(0)
accounts = [] #[{},{},{}]
for i in range(1,sheet.nrows):
a = {}#行
for j in range(sheet.ncols):
a[sheet.cell(0,j).value] = sheet.cell(i,j).value#键值在0行,j列
accounts.append(a)
return accounts
#登录
def login(account,password):
global CURRENT_ACCOUNT
#读取excel数据
accounts = read_excel()
#比较
flag = False
for a in range(len(accounts)):
# 判断状态是否未1,并且账号和密码都正确
if accounts[a]["status"]=='1' and (accounts[a]["account"]==account and accounts[a]["password"]==password):
flag = True
CURRENT_ACCOUNT = accounts[a]
CURRENT_ACCOUNT_XY.append(a+1)
CURRENT_ACCOUNT_XY.append(list(accounts[a].keys()).index("balance"))
break
return flag
#取款
def get_money(money):
if money%100==0:# 只能取整数
if CURRENT_ACCOUNT["balance"]>=money:
#修改当前账号值
CURRENT_ACCOUNT["balance"] -= money
#暂时不用修改excel数据,缓存
else:
print("余额不足")
else:
print("非法金额")
# 存款
def save_money(money):
if money%100==0:
CURRENT_ACCOUNT["balance"] += money
else:
print("非法金额")
#更新excel数据
def write_excel(xy,value):
book = xlrd.open_workbook(FILENAME)
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(0)
sheet_copy.write(xy[0],xy[1],value)
book_copy.save(FILENAME)
#转账
def trans_money(other_account,money):
# 判断是否抽取跨行手续费
accounts = read_excel()
#查找对方账户信息
other_account_temp = {}
other_account_xy = []
for a in range(len(accounts)):
if accounts[a]["account"] == other_account:
other_account_temp = accounts[a]
other_account_xy.append(a+1)#增加x值
other_account_xy.append(list(accounts[a].keys()).index("balance"))#寻找y值
break
if other_account_temp["bank"]==CURRENT_ACCOUNT["bank"]:#判断是不是同一银行
#同一个银行
if CURRENT_ACCOUNT["balance"]>=money:
CURRENT_ACCOUNT["balance"] -= money
other_account_temp["balance"] += money
else:
print("余额不足")
else:
#不同银行
if CURRENT_ACCOUNT["balance"]>=money*(1+0.002):
CURRENT_ACCOUNT["balance"] -= money*(1+0.002)
other_account_temp["balance"] += money
else:
print("余额不足")
# 更新excel
write_excel(other_account_xy,other_account_temp["balance"])
#退卡
def return_card():
write_excel(CURRENT_ACCOUNT_XY, CURRENT_ACCOUNT["balance"])
print("退卡成功")
#查询余额
def get_balance():
return CURRENT_ACCOUNT["balance"]
#运行测试
flag1 = False
for i in range(3):
account = input("请输入卡号:")
password = input("请输入密码:")
flag = login(account,password)
if flag:
flag1 = True
break
if not flag1 and i==2:
#锁卡
xy = []
accounts = read_excel(FILENAME)
for j in range(len(accounts)):
if accounts[j]["account"]==account:
xy.append(j+1)
xy.append(list(accounts[j].keys()).index("status"))
write_excel(xy,"0")
break
while flag:
f = input("1-取款 "
"2-存款 "
"3-转账 "
"4-查询余额 "
"5-退出"
)
if f == '1':
m = int(input("请输入取款金额:"))
get_money(m)
print(get_balance())
elif f == '2':
m = int(input("请输入存款金额:"))
save_money(m)
print(get_balance())
elif f == '3':
o = input("请输入对方账号:")
m = int(input("请输入转账金额:"))
trans_money(o,m)
print(get_balance())
elif f == '4':
print(get_balance())
elif f == '5':
return_card()
break
else:
print("请输入正确数字")