importosimporttime
BASE_DIR= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'/DataAccess/'#设置路径
user_status =False#用户转账接口
defATM_UserTranster(UserID):#用户转账接口
#此程序接口不适用与管理数据量较大的数据,采用的是一次读取文件所有数据操作方式
with open(BASE_DIR +'ATM_Useropt.txt', 'a')as f:
info= UserID + ' ' + '转账' + ' ' + time.strftime('%Y-%m-%d-%X') + '\n'f.write(info)
a=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
a.append(line.strip().split(' '))for i,index inenumerate(a):if index[0] ==UserID:if index[4] == '0':print("当前账户被冻结,无法转账!")returnBalance= int(index[5])
pos=iprint("账户\033[32;1m%s\033[0m可供转账余额为 :%s" %(UserID, Balance))break
whileTrue:
UserCardID= input('请输入待转账用户卡号输入q退出 :')ifUserCardID.isdigit():for index ina :if index[0] ==UserCardID:
Value= input("请输入转账金额输q退出:")ifValue.isdigit():
Value=int(Value)if Value>Balance:print("操作失败,余额不足!")else:
index[5]=str(int(index[5])+Value)
a[pos][5]=str(Balance-Value)
with open(BASE_DIR+ 'ATM_UserCard.txt', 'w') as f:for line ina:
f.write(' '.join(line)+'\n')print("转账成功!当前账户余额为 :\033[32;1m%s\033[0m"%a[pos][5])return
elif 'q' ==Value:break
else:pass
elif 'q' ==UserCardID:break
else:pass
#提现接口
defATM_UserWithdrawal(UserID):
with open(BASE_DIR+'ATM_Useropt.txt', 'a')as f:
info= UserID+ ' '+'提现' + ' ' + time.strftime('%Y-%m-%d-%X') + '\n'f.write(info)
a=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
a.append(line.strip().split(' '))for i,index inenumerate(a):if index[0] ==UserID:if index[4] == '0':print("当前账户被冻结,无法提现!")returnBalance= int(index[5])print("账户\033[32;1m%s\033[0m可供提现金额为 :%s" %(UserID, Balance))break
whileTrue:
Value=input("请输入要提现的金额输入q退出:")ifValue.isdigit():
Value=int(Value)if Value>Balance:print("余额不足,提现失败")else:
Balance= Balance-Valueprint("提现成功,账户余额为 \033[32;1m%s\033[0m"%Balance)elif 'q'==Value:
index[5] =str(Balance)
with open(BASE_DIR+ 'ATM_UserCard.txt', 'w') as f:for line ina:
f.write(' '.join(line) + '\n')print("谢谢使用!")return
break
else:pass
#流水
defATM_UserWater(UserName,UserID):
with open(BASE_DIR+ 'ATM_Useropt.txt', 'a')as f:
info= UserID + ' ' + '查流水' + ' ' + time.strftime('%Y-%m-%d-%X') + '\n'f.write(info)
user_info=[]
with open(BASE_DIR+ 'ATM_UserInfo.txt', 'r')as f:for line inf:
user_info.append(line.strip().split(' '))
T=Falsefor line inuser_info:if UserName ==line[0]:
T=Trueprint('用户 :\033[32;1m%s\033[0m 购物清单 :%s 消费 :%s 日期 :%s' % (line[0], line[1], line[2], line[3]))if T ==False:print("用户\033[32;1m%s\033[0m无消费记录!" %UserName)#操作记录
defATM_UserOpt(UserID):
with open(BASE_DIR+ 'ATM_Useropt.txt', 'a')as f:
info= UserID + ' ' + '查操作记录' + ' ' + time.strftime('%Y-%m-%d-%X') + '\n'f.write(info)
user_info=[]
with open(BASE_DIR+ 'ATM_Useropt.txt', 'r')as f:for line inf:
user_info.append(line.strip().split(' '))
T=Falsefor line inuser_info:if UserID ==line[0]:
T=Trueprint('用户 :\033[32;1m%s\033[0m 操作内容 :%s 日期 :%s' % (line[0],line[1],line[2]))if T ==False:print("用户\033[32;1m%s\033[0m 无操作记录!" %UserID)#还款接口
defATM_UserReimbursement(UserID):
with open(BASE_DIR+ 'ATM_Useropt.txt', 'a')as f:
info= UserID + ' ' + '还款' + ' ' + time.strftime('%Y-%m-%d-%X') + '\n'f.write(info)
a=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
a.append(line.strip().split(' '))for i, index inenumerate(a):if index[0] ==UserID:if index[4] == '0':print("当前账户被冻结,无法操作!")returnBalance= 15000-int(index[5])if Balance<0:print("账户\033[32;1m%s\033[0m无需还款!"%UserID)return
print("账户\033[32;1m%s\033[0m需要还款金额为 :%s" %(UserID, Balance))break
whileTrue:
Value= input("请输入还款金额输入q退出 :")ifValue.isdigit():
Value=int(Value)if(int(index[5])+Value)>int(index[3]):
index[5] = str(int(index[5])+Value)print("还款成功,当前账户活期余额为 :%s,剩余可用信用额度为 :%s"%(int(index[5])-int(index[3]),index[3]))else:
index[5] = str(int(index[5]) +Value)print("还需还款金额为 :%s,当前可用额度为 :%s" %(int(index[3])-int(index[5]),index[5]))elif Value=='q':
with open(BASE_DIR+ 'ATM_UserCard.txt', 'w') as f:for line ina:
f.write(' '.join(line) + '\n')print("谢谢使用!")break
else:pass
#用户刷卡接口#参数:Count_Value消费金额
defATM_ExpensePort(Count_Value):
UserInfo=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
UserInfo.append(line.strip().split(' '))whileTrue:
UserID= input("请输入账户名称:")
Password= input("请输入密码:")
t=Truefor line inUserInfo:if line[0] == UserID and line[1] ==Password:
t=Falseif line[4] == '1':print("账户当前可用额度为:\033[32;1m%s\033[0m" % line[5])if int(Count_Value) > int(line[5]):print("账户额度不足!")else:
line[5] = str(int(line[5]) -int(Count_Value))print("此次消费:\033[32;1m%s\033[0m,当前额度为:\033[32;1m%s\033[0m" % (Count_Value, line[5]))
with open(BASE_DIR+ 'ATM_UserCard.txt', 'w')as f:for line inUserInfo:
f.write(' '.join(line) + '\n')returnTrueelse:print("帐号已被封锁,请到柜台处理!")returnFalseif t ==True:print("账户或密码错误!")pass
#认证装饰器
deflogin(func):def inuc(*args, **kwargs):
_username= "alex" #假装这是DB里存的用户信息
_password = "123456" #假装这是DB里存的用户信息
globaluser_statusif user_status ==False:
username= input("username:")
password= input("password:")if username == _username and password ==_password:print("登入成功!")
user_status=Trueif user_status ==True:
func(*args, **kwargs)returninuc#添加账户
@logindefATM_UserAdd():
a=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
a.append(line.strip().split(' '))whileTrue:
Value= input("请输入要注册的账号,密码,用户名,以逗号隔开,输入q退出 :")if Value =='q':break
else:
b=Value.split(',')
f=Truefor line ina:if line[0]==b[0]:
f=Falsebreak
if f==False:print("账户已存在!")pass
else:
b.extend(['15000','1','15000',time.strftime( '%Y-%m-%d-%X')])
with open(BASE_DIR+ 'ATM_UserCard.txt','a')as f:
f.write(' '.join(b) + '\n')print("用户注册成功!")break
#用户额度管理
@logindefAccount_Manage():
a=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
a.append(line.strip().split(' '))
Over_Flag=FalsewhileTrue:if Over_Flag ==True:breakUserId= input("请输入用户账户:")ifUserId.isdigit():
F=Falsefor line ina:if UserId ==line[0]:
F=Trueprint("用户当前额度为:\033[32;1m%s\033[0m"%line[3])whileTrue:
Value= input("请输入更新额度,输入q退出:")ifValue.isdigit():
line[3] =Valueprint("额度修改成功!")elif Value=='q':
with open(BASE_DIR+ 'ATM_UserCard.txt','w')as f:for lines ina:
f.write(' '.join(lines)+'\n')
Over_Flag=Truebreak
else:print("输入有误!")break
if F ==False:print("账户不存在!")elif UserId == 'q':break
else:print("输入有误!")pass
#账户冻结
@logindefuser_freeze():
a=[]
with open(BASE_DIR+ 'ATM_UserCard.txt', 'r') as f:for line inf:
a.append(line.strip().split(' '))whileTrue:
UserId= input("请输入用户账户:")ifUserId.isdigit():for line ina:if UserId ==line[0]:if line[4]=='1':print("账户当前状态为:\033[32;1m%s\033[0m" %('正常'))else:print("账户当前状态为:\033[32;1m%s\033[0m" %('冻结'))whileTrue:
Flag= input("1.冻结,2.解冻,q.退出:")ifFlag.isdigit():if Flag =='1':
line[4] = '0'
print("该账户已冻结!")elif Flag =='2':
line[4] = '1'
print("该账户已解冻!")else:pass
elif Flag == 'q':
with open(BASE_DIR+ 'ATM_UserCard.txt','w')as f:for lines ina:
f.write(' '.join(lines)+'\n')break
else:pass
elif Flag == 'q':break
else:pass
#查询数据库中的数据
defsearch_data_access():
goods=[]
with open(BASE_DIR+'goods_info.txt','r')as f:for line inf:
goods.append(line.strip().split(' '))for i inrange(len(goods)):print('%s 价格 :%s 库存 :%s'%(goods[i][0],goods[i][1],goods[i][2]))#添加商品操作#参数说明:goodname 商品名 prace 价格 Num 库存量
defadd_goods_opt(GoodName,prace,Num):
goods=[]
with open(BASE_DIR+'goods_info.txt','r')as f:for line inf:
goods.append(line.strip().split(' '))
T=Falsefor line ingoods:if line[0] ==GoodName:
T=Trueprint("商品已存在,添加失败!")returnInfo= GoodName+' '+str(prace)+' '+str(Num)+'\n'with open(BASE_DIR+ 'goods_info.txt', 'a')as f:
f.write(Info)print("添加商品%s成功!" %GoodName )#删除商品操作
defdelet_goods_opt(GoodName):
goods=[]
with open(BASE_DIR+'goods_info.txt','r')as f:for line inf:
goods.append(line.strip().split(' '))for line ingoods:if GoodName ==line[0]:print("删除商品%s成功!"%line[0])
goods.remove(line)
with open(BASE_DIR+ 'goods_info.txt', 'w')as f:for line ingoods:
f.write(' '.join(line) + '\n')return
print("没有此商品信息,删除失败!")#修改商品参数#参数说明:goodname 商品名 prace 价格 Num 库存量
defChange_goods_info(GoodName,prace,Num):
goods=[]
with open(BASE_DIR+'goods_info.txt','r')as f:for line inf:
goods.append(line.strip().split(' '))for line ingoods:if line[0] ==GoodName:
line[1] =str(prace)
line[2] =str(Num)
with open(BASE_DIR+'goods_info.txt','w')as f:for lines ingoods:
f.write(' '.join(lines) + '\n')print("商品%s参数修改成功!"%(GoodName))return
print("数据库中没有此商品,修改失败!")#查用户余额
defuser_printAccountBalance(UserName):
user_info=[]
with open(BASE_DIR+ 'users_value.txt', 'r')as f:for line inf:
user_info.append(line.strip().split(' '))for line inuser_info:if line[0] ==UserName:print("\033[32;1m%s\033[0m的账户余额 :\033[32;1m%s\033[0m"%(UserName,line[1]))return
#充值#UserName :用户名,充值金额 :新老用户标识 新用户为True老用户为False
defuser_TopUp(UserName,Value):
user_info=[]
with open(BASE_DIR+'users_value.txt', 'r')as f:for line inf:
user_info.append(line.strip().split(' '))for line inuser_info:if line[0] ==UserName:
line[1]=str(int(line[1])+Value)
with open(BASE_DIR+'users_value.txt', 'w')as f:for lines inuser_info:
f.write(' '.join(line) + '\n')print("充值成功\033[32;1m%s\033[0m当前的账户余额 :\033[32;1m%s\033[0m" % (UserName, line[1]))returnuser_info= UserName + ' ' + str(Value) + '\n'with open(BASE_DIR+ 'users_value.txt', 'a')as f:
f.write(user_info)print("新用户充值成功\033[32;1m%s\033[0m当前的账户余额 :\033[32;1m%s\033[0m" %(UserName, Value))#查询用户消费记录
defuser_RecordsConsumption(UserName):
user_info=[]
with open(BASE_DIR+'users_info.txt', 'r')as f:for line inf:
user_info.append(line.strip().split(' '))
T=Falsefor line inuser_info:if UserName ==line[0]:
T=Trueprint('用户 :\033[32;1m%s\033[0m 购物清单 :%s 消费 :%s 日期 :%s'%(line[0],line[1],line[2],line[3]))if T==False:print("用户\033[32;1m%s\033[0m无消费记录!"%UserName)#购物车
defuser_ShoopCar(Username):
goods_info=[]
ShoopCar= [] #创建购物车列表
expense =0
Salary=0
Count=0
with open(BASE_DIR+ 'goods_info.txt', 'r')as f:for line inf:
goods_info.append(line.strip().split(' '))
with open(BASE_DIR+ 'users_value.txt', 'r')as f:for line inf:if line.split(' ')[0] ==Username:
Salary= int(line.split(' ')[1])whileTrue:for i, index inenumerate(goods_info):print(i, index)
getNum= input("请输入要购买的商品编号,输入c结算,输入q退出:")if getNum.isdigit():# getNum=int(getNum)
Count+= int(goods_info[getNum][1])
ShoopCar.append(goods_info[getNum][0])
expense+= int(goods_info[getNum][1])#消费入库
if int(goods_info[getNum][2]) >0:
goods_info[getNum][2] = str(int(goods_info[getNum][2])-1)print("当前已消费 :\033[32;1m%s\033[0m"%Count)elif getNum=='c':#结算
whileTrue:
opt= input("结算方式:1.余额,2.刷卡,3.退出 :")ifopt.isdigit():if opt=='1':if Salary 
else:print("付款成功,当前余额为:\033[32;1m%s\033[0m"%(Salary-Count))
with open(BASE_DIR+ 'users_info.txt', 'a')as f:
f.write(Username+ ' ' + ','.join(ShoopCar) + ' ' + str(expense) + ' ' + time.strftime('%Y-%m-%d-%X') + '\n')
with open(BASE_DIR+'goods_info.txt', 'w')as f:for line ingoods_info:
f.write(' '.join(line)+'\n')
value_info=[]
with open(BASE_DIR+'users_value.txt', 'r')as f:for line inf:
value_info.append(line.strip().split(' '))for line invalue_info:if line[0] ==Username:
line[1] = str(Salary-Count)
with open(BASE_DIR+ 'users_value.txt', 'w')as f:for line invalue_info:
f.write(' '.join(line) + '\n')elif opt == '2':#刷卡
if ATM_ExpensePort(Count)==True:#刷卡接口
with open(BASE_DIR + 'ATM_UserInfo.txt', 'a')as f:
f.write(Username+ ' ' + ','.join(ShoopCar) + ' ' + str(expense) + ' ' + time.strftime('%Y-%m-%d-%X') + '\n')
with open(BASE_DIR+ 'users_info.txt', 'a')as f:
f.write(Username+ ' ' + ','.join(ShoopCar) + ' ' + str(expense) + ' ' + time.strftime('%Y-%m-%d-%X') + '\n')
with open(BASE_DIR+'goods_info.txt', 'w')as f:for line ingoods_info:
f.write(' '.join(line)+'\n')elif opt == '3':return
else:pass
break
elif getNum=='q':#退出
break
else:pass