python实现atm机基本操作及购物车

一.需求分析

  ATM机要为用户提供转账,提现,还款,付款,消费流水,操作记录等操作接口

  ATM机要为管理员提供创建用户,冻结解冻,修改额度的功能

  ATM机管理员认证使用装饰器来实现

  购物车要提供管理员和用户两个接口

  用户接口需要提供,查询余额,充值,查询用户消费记录,购物等操作接口

  商户接口需要提供,上架,下架,修改,查看上架货品等操作接口

二.流程图

三.代码实现

  工程的创建按照较为简便的格式来描述,大致目录结构如图:

ATMandShopCar/
|-- bin/
|   |--
|-- DataAccess
| |--ATM_UserCard.txt
|  |--ATM_UserInfo.txt
|  |--ATM_UserOpt.txt
|  |--goods_info.txt
|  |--users_info.txt
|  |--users_value.txt
|  
|-- ShopingCar/ | |-- tests/ | | |-- __init__.py | | |-- test_main.py | |--ATM.py
|  |--DataAccess.py
|  |--ShoopingCar.py
| |-- __init__.py | |-- main.py | |-- docs/ | |-- conf.py | |-- abc.rst | |-- setup.py |-- requirements.txt |-- README

  上述目录中最主要的部分是:
DataAccess和ShopingCar两个文件夹,DataAccess主要存放用户操作数据,俗称数据库,本工程使用文本作为存储数据的载体。ShopingCar为程序处理.py,DataAccess.py主要用于对数据库的操作,ATM.py和ShoopingCar.py分别处理相应的atm机和购物车的逻辑。main.py为主函数,处理程序主逻辑。

  DataAccess.py程序代码如下:
import  os
import  time
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'/DataAccess/'#设置路径
user_status = False

# 用户转账接口
def ATM_UserTranster(UserID):
    #用户转账接口
    #此程序接口不适用与管理数据量较大的数据,采用的是一次读取文件所有数据操作方式
    with open(BASE_DIR +'ATM_Useropt.txt', 'a')as f:
        info = UserID + ' ' + '转账' + ' ' + time.strftime('%Y-%m-%d-%X') + '
'
        f.write(info)
    a = []
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            a.append(line.strip().split(' '))
        for i,index in enumerate(a):
            if index[0] == UserID:
                if index[4] == '0':
                    print("当前账户被冻结,无法转账!")
                    return
                Balance = int(index[5])
                pos=i
                print("账户33[32;1m%s33[0m可供转账余额为 :%s" % (UserID, Balance))
                break
    while True:
        UserCardID = input('请输入待转账用户卡号输入q退出 :')
        if UserCardID.isdigit():
            for index in a :
                if index[0] == UserCardID:
                    Value = input("请输入转账金额输q退出:")
                    if Value.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 in a:
                                    f.write(' '.join(line)+'
')
                            print("转账成功!当前账户余额为 :33[32;1m%s33[0m"%a[pos][5])
                            return
                    elif 'q' == Value:
                        break
                    else:
                        pass
        elif 'q' == UserCardID:
            break
        else:
            pass
#提现接口
def ATM_UserWithdrawal(UserID):
    with open(BASE_DIR +'ATM_Useropt.txt', 'a')as f:
        info = UserID+ ' '+'提现' + ' ' + time.strftime('%Y-%m-%d-%X') + '
'
        f.write(info)
    a = []
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            a.append(line.strip().split(' '))
        for i,index in enumerate(a):
            if index[0] == UserID:
                if index[4] == '0':
                    print("当前账户被冻结,无法提现!")
                    return
                Balance = int(index[5])
                print("账户33[32;1m%s33[0m可供提现金额为 :%s" % (UserID, Balance))
                break
    while True:
        Value=input("请输入要提现的金额输入q退出:")
        if Value.isdigit():
            Value = int(Value)
            if Value>Balance:
                print("余额不足,提现失败")
            else:
                Balance = Balance-Value
                print("提现成功,账户余额为 33[32;1m%s33[0m"%Balance)
        elif 'q'==Value:
            index[5] = str(Balance)
            with open(BASE_DIR + 'ATM_UserCard.txt', 'w') as f:
                for line in a:
                    f.write(' '.join(line) + '
')
            print("谢谢使用!")
            return
            break
        else:
            pass
#流水
def ATM_UserWater(UserName,UserID):
    with open(BASE_DIR + 'ATM_Useropt.txt', 'a')as f:
        info = UserID + ' ' + '查流水' + ' ' + time.strftime('%Y-%m-%d-%X') + '
'
        f.write(info)
    user_info = []
    with open(BASE_DIR + 'ATM_UserInfo.txt', 'r')as f:
        for line in f:
            user_info.append(line.strip().split(' '))
    T = False
    for line in user_info:
        if UserName == line[0]:
            T = True
            print('用户 :33[32;1m%s33[0m 购物清单 :%s 消费 :%s 日期 :%s' % (line[0], line[1], line[2], line[3]))
    if T == False:
        print("用户33[32;1m%s33[0m无消费记录!" % UserName)
#操作记录
def ATM_UserOpt(UserID):
    with open(BASE_DIR + 'ATM_Useropt.txt', 'a')as f:
        info = UserID + ' ' + '查操作记录' + ' ' + time.strftime('%Y-%m-%d-%X') + '
'
        f.write(info)
    user_info = []
    with open(BASE_DIR + 'ATM_Useropt.txt', 'r')as f:
        for line in f:
            user_info.append(line.strip().split(' '))
    T = False
    for line in user_info:
        if UserID == line[0]:
            T = True
            print('用户 :33[32;1m%s33[0m 操作内容 :%s 日期 :%s' % (line[0],line[1],line[2]))
    if T == False:
        print("用户33[32;1m%s33[0m 无操作记录!" % UserID)

#还款接口
def ATM_UserReimbursement(UserID):
    with open(BASE_DIR + 'ATM_Useropt.txt', 'a')as f:
        info = UserID + ' ' + '还款' + ' ' + time.strftime('%Y-%m-%d-%X') + '
'
        f.write(info)
    a = []
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            a.append(line.strip().split(' '))
        for i, index in enumerate(a):
            if index[0] == UserID:
                if index[4] == '0':
                    print("当前账户被冻结,无法操作!")
                    return
                Balance = 15000-int(index[5])
                if Balance<0:
                    print("账户33[32;1m%s33[0m无需还款!"%UserID)
                    return
                print("账户33[32;1m%s33[0m需要还款金额为 :%s" % (UserID, Balance))
                break
        while True:
            Value = input("请输入还款金额输入q退出 :")
            if Value.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 in a:
                        f.write(' '.join(line) + '
')
                print("谢谢使用!")
                break
            else:
                pass
# 用户刷卡接口
# 参数:Count_Value消费金额
def ATM_ExpensePort(Count_Value):
    UserInfo = []
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            UserInfo.append(line.strip().split(' '))
    while True:
        UserID = input("请输入账户名称:")
        Password = input("请输入密码:")
        t = True
        for line in UserInfo:
            if line[0] == UserID and line[1] == Password:
                t = False
                if line[4] == '1':
                    print("账户当前可用额度为:33[32;1m%s33[0m" % line[5])
                    if int(Count_Value) > int(line[5]):
                        print("账户额度不足!")
                    else:
                        line[5] = str(int(line[5]) - int(Count_Value))
                        print("此次消费:33[32;1m%s33[0m,当前额度为:33[32;1m%s33[0m" % (Count_Value, line[5]))
                        with open(BASE_DIR + 'ATM_UserCard.txt', 'w')as f:
                            for line in UserInfo:
                                f.write(' '.join(line) + '
')
                    return True
                else:
                    print("帐号已被封锁,请到柜台处理!")
                    return False
        if t == True:
            print("账户或密码错误!")
            pass

#认证装饰器
def login(func):
    def inuc(*args, **kwargs):
        _username = "alex"  # 假装这是DB里存的用户信息
        _password = "123456"  # 假装这是DB里存的用户信息
        global  user_status
        if user_status == False:
            username = input("username:")
            password = input("password:")
            if username == _username and password == _password:
                print("登入成功!")
                user_status = True
        if user_status == True:
            func(*args, **kwargs)

    return inuc

#添加账户
@login
def ATM_UserAdd():
    a=[]
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            a.append(line.strip().split(' '))
    while True:
        Value = input("请输入要注册的账号,密码,用户名,以逗号隔开,输入q退出 :")
        if Value =='q':
            break
        else:
            b=Value.split(',')
            f = True
            for line in a:
                if line[0]==b[0]:
                    f = False
                    break
            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) + '
')
                    print("用户注册成功!")
                break
#用户额度管理
@login
def Account_Manage():
    a = []
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            a.append(line.strip().split(' '))
    Over_Flag = False
    while True:
        if Over_Flag == True:
            break
        UserId = input("请输入用户账户:")
        if UserId.isdigit():
            F = False
            for line in a:
                if UserId == line[0]:
                    F = True
                    print("用户当前额度为:33[32;1m%s33[0m"%line[3])
                    while True:
                        Value = input("请输入更新额度,输入q退出:")
                        if Value.isdigit():
                            line[3] = Value
                            print("额度修改成功!")
                        elif Value=='q':
                            with open(BASE_DIR + 'ATM_UserCard.txt','w')as f:
                                for lines in a:
                                    f.write(' '.join(lines)+'
')
                            Over_Flag = True
                            break
                        else:
                            print("输入有误!")
                    break
            if F == False:
                print("账户不存在!")
        elif UserId == 'q':
            break
        else:
            print("输入有误!")
            pass
#账户冻结
@login
def user_freeze():
    a=[]
    with open(BASE_DIR + 'ATM_UserCard.txt', 'r') as f:
        for line in f:
            a.append(line.strip().split(' '))
    while True:
        UserId = input("请输入用户账户:")
        if UserId.isdigit():
            for line in a:
                if UserId == line[0]:
                    if line[4]=='1':
                        print("账户当前状态为:33[32;1m%s33[0m" %('正常'))
                    else:
                        print("账户当前状态为:33[32;1m%s33[0m" %('冻结'))
                    while True:
                        Flag = input("1.冻结,2.解冻,q.退出:")
                        if Flag.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 in a:
                                    f.write(' '.join(lines)+'
')
                            break
                        else:
                            pass
        elif Flag == 'q':
            break
        else:
            pass


#查询数据库中的数据
def search_data_access():
    goods=[]
    with open(BASE_DIR+'goods_info.txt','r')as f:
        for line in f:
            goods.append(line.strip().split(' '))
    for i in range(len(goods)):
        print('%s 价格 :%s 库存 :%s'%(goods[i][0],goods[i][1],goods[i][2]))
#添加商品操作
#参数说明:goodname 商品名 prace 价格 Num 库存量
def add_goods_opt(GoodName,prace,Num):
    goods = []
    with open(BASE_DIR+'goods_info.txt','r')as f:
        for line in f:
            goods.append(line.strip().split(' '))
    T = False
    for line in goods:
        if line[0] == GoodName:
            T = True
            print("商品已存在,添加失败!")
            return

    Info = GoodName+' '+str(prace)+' '+str(Num)+'
'
    with open(BASE_DIR + 'goods_info.txt', 'a')as f:
        f.write(Info)
    print("添加商品%s成功!" %GoodName )
#删除商品操作
def delet_goods_opt(GoodName):
    goods=[]
    with open(BASE_DIR+'goods_info.txt','r')as f:
        for line in f:
            goods.append(line.strip().split(' '))
    for line in goods:
        if GoodName == line[0]:
            print("删除商品%s成功!"%line[0])
            goods.remove(line)
            with open(BASE_DIR + 'goods_info.txt', 'w')as f:
                for line in goods:
                    f.write(' '.join(line) + '
')
            return
    print("没有此商品信息,删除失败!")
#修改商品参数
#参数说明:goodname 商品名 prace 价格 Num 库存量
def Change_goods_info(GoodName,prace,Num):
    goods = []
    with open(BASE_DIR+'goods_info.txt','r')as f:
        for line in f:
            goods.append(line.strip().split(' '))
    for line in goods:
        if line[0] == GoodName:
            line[1] = str(prace)
            line[2] = str(Num)
            with open(BASE_DIR+'goods_info.txt','w')as f:
                for lines in goods:
                    f.write(' '.join(lines) + '
')
            print("商品%s参数修改成功!"%(GoodName))
            return
    print("数据库中没有此商品,修改失败!")

#查用户余额
def user_printAccountBalance(UserName):
    user_info=[]
    with open(BASE_DIR + 'users_value.txt', 'r')as f:
        for line in f:
            user_info.append(line.strip().split(' '))
    for line in user_info:
        if line[0] == UserName:
            print("33[32;1m%s33[0m的账户余额 :33[32;1m%s33[0m"%(UserName,line[1]))
            return
#充值
# UserName :用户名,充值金额 :新老用户标识 新用户为True老用户为False
def user_TopUp(UserName,Value):
    user_info = []
    with open(BASE_DIR+'users_value.txt', 'r')as f:
        for line in f:
            user_info.append(line.strip().split(' '))
    for line in user_info:
        if line[0] == UserName:
            line[1]=str(int(line[1])+Value)
            with open(BASE_DIR+'users_value.txt', 'w')as f:
                for lines in user_info:
                    f.write(' '.join(line) + '
')
            print("充值成功33[32;1m%s33[0m当前的账户余额 :33[32;1m%s33[0m" % (UserName, line[1]))
            return
    user_info = UserName + ' ' + str(Value) + '
'
    with open(BASE_DIR + 'users_value.txt', 'a')as f:
        f.write(user_info)
    print("新用户充值成功33[32;1m%s33[0m当前的账户余额 :33[32;1m%s33[0m" % (UserName, Value))
#查询用户消费记录
def user_RecordsConsumption(UserName):
    user_info = []
    with open(BASE_DIR+'users_info.txt', 'r')as f:
        for line in f:
            user_info.append(line.strip().split(' '))
    T = False
    for line in user_info:
        if UserName == line[0]:
            T=True
            print('用户 :33[32;1m%s33[0m 购物清单 :%s 消费 :%s 日期 :%s'%(line[0],line[1],line[2],line[3]))
    if T==False:
        print("用户33[32;1m%s33[0m无消费记录!"%UserName)
#购物车
def user_ShoopCar(Username):
    goods_info = []
    ShoopCar = [] # 创建购物车列表
    expense = 0
    Salary = 0
    Count = 0
    with open(BASE_DIR + 'goods_info.txt', 'r')as f:
        for line in f:
            goods_info.append(line.strip().split(' '))
    with open(BASE_DIR + 'users_value.txt', 'r')as f:
        for line in f:
            if line.split(' ')[0] == Username:
                Salary = int(line.split(' ')[1])
    while True:
        for i, index in enumerate(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("当前已消费 :33[32;1m%s33[0m"%Count)
        elif getNum=='c':#结算
            while True:
                opt = input("结算方式:1.余额,2.刷卡,3.退出 :")
                if opt.isdigit():
                    if opt=='1':
                        if Salary < Count:
                            print("余额不足")
                            pass
                        else:
                            print("付款成功,当前余额为:33[32;1m%s33[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') + '
')
                            with open(BASE_DIR+'goods_info.txt', 'w')as f:
                                for line in  goods_info:
                                    f.write(' '.join(line)+'
')
                            value_info = []
                            with open(BASE_DIR+'users_value.txt', 'r')as f:
                                for line in  f:
                                    value_info.append(line.strip().split(' '))
                                for line in value_info:
                                    if line[0] == Username:
                                        line[1] = str(Salary-Count)
                            with open(BASE_DIR + 'users_value.txt', 'w')as f:
                                for line in value_info:
                                    f.write(' '.join(line) + '
')
                    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') + '
')
                            with open(BASE_DIR + 'users_info.txt', 'a')as f:
                                f.write(Username + ' ' + ','.join(ShoopCar) + ' ' + str(expense) + ' ' + time.strftime('%Y-%m-%d-%X') + '
')
                            with open(BASE_DIR+'goods_info.txt', 'w')as f:
                                for line in  goods_info:
                                    f.write(' '.join(line)+'
')
                    elif opt == '3':
                        return
                    else:
                        pass
            break
        elif getNum=='q':#退出
            break
        else:
            pass

  ATM.py程序代码如下:

from ATMandShoopCar.ShoopingCar import DataAccess
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'/DataAccess/'#设置路径

#ATM操作
def ATM_Opt():
    a=[]
    with open(BASE_DIR+'ATM_UserCard.txt','r')as f:
        for line in f:
            a.append(line.strip().split(' '))
    while True:
        opt = input("1.用户选项,2.管理员选项,输入q退出:")
        if opt.isdigit():
            if opt =='1':#用户选项
                while True:
                    username=input("请输入用户账号,输入q退出:")
                    if username=='q':break
                    password=input("请输入密码,输入q退出:")
                    if  password=='q':
                        break
                    T = False
                    for line in a:
                        if line[0]==username and line[1]==password:
                            T = True
                            print("登入成功!!!")
                            while True:
                                Chose =input("1.转账,2.提现,3.消费流水,4.还款,5.用户操作记录,输入q退出: ")
                                if Chose.isdigit():
                                    if Chose=='1':
                                        DataAccess.ATM_UserTranster(username)#转账
                                    elif Chose=='2':
                                        DataAccess.ATM_UserWithdrawal(username)#提现
                                    elif Chose=='3':
                                        Username=input("请输入用户名:")
                                        DataAccess.ATM_UserWater(Username,username)#消费流水
                                    elif Chose=='4':
                                        DataAccess.ATM_UserReimbursement(username)#还款
                                    elif Chose=='5':
                                        DataAccess.ATM_UserOpt(username)#用户操作记录
                                    else:
                                        pass

                                elif Chose=='q':
                                    break
                                else:
                                    pass
                            break
                    if T ==False:
                        print("账号密码错误!!")
            elif opt=='2':#管理员选项
                while True:
                    Chose = input("1.添加账户,2.用户额度管理,3.账户冻结,输入q退出: ")
                    if Chose.isdigit():
                        if Chose == '1':
                            DataAccess.ATM_UserAdd()#账户添加
                        elif Chose =='2':
                            DataAccess.Account_Manage()
                        elif Chose =='3':
                            DataAccess.user_freeze()
                        else:
                            pass

                    elif Chose =='q':
                        global user_status
                        user_status = False
                        break
                    else:
                        pass
            else:
                pass
        elif opt=='q':
            break
        else:
            pass

  ShoopingCar.py程序代码如下:

from ATMandShoopCar.ShoopingCar import DataAccess

def ShoopingCar_Opt():
    while True:
        Num=input("1.用户界面,2.商家界面,输入q退出 :")
        if Num.isdigit():
            Num = int(Num)
            if Num == 1:
                username = input("Please Input The Username :")
                DataAccess.user_printAccountBalance(username)
                while True:
                    UserChoose = input("1.查询购物记录,2.购物,3.充值,输入q返回上级目录 :")
                    if UserChoose.isdigit():
                        UserChoose = int(UserChoose)
                        if UserChoose == 1:
                            DataAccess.user_RecordsConsumption(username)#查询购物记录
                            break
                        elif UserChoose == 2:
                            DataAccess.user_ShoopCar(username)#购物车
                            break
                        elif UserChoose == 3:
                            value = int(input("请输入充值金额 :"))
                            DataAccess.user_TopUp(username, value)#充值
                    elif UserChoose=='q':
                        break
                    else:
                        pass

            elif Num == 2:#商家界面
                DataAccess.search_data_access()#打印商品信息和库存
                while True:
                    UserChoose = input("1.添加商品,2.修改商品信息,3.删除商品,输入q返回上级目录 :")
                    if UserChoose.isdigit():
                        UserChoose = int(UserChoose)
                        if UserChoose==1:
                            goodsname=input("请输入商品名 :")
                            goodsvalue=int(input("请输入商品价格 :"))
                            goodnum = int(input("请输入商品数量 :"))
                            DataAccess.add_goods_opt(goodsname, goodsvalue, goodnum)#商品添加操作
                        elif UserChoose==2:
                            goodsname = input("请输入要修改的商品名 :")
                            goodsvalue = int(input("请输入商品价格 :"))
                            goodnum = int(input("请输入商品数量 :"))
                            DataAccess.Change_goods_info(goodsname, goodsvalue, goodnum)
                        elif UserChoose==3:
                            goodsname = input("请输入要删除的商品名 :")
                            DataAccess.delet_goods_opt(goodsname)
                    elif UserChoose=='q':
                        break
                    else:
                        pass
            else:
                pass
        elif Num == 'q':
            break
        else:
            pass

  main.py程序代码如下:

from ATMandShoopCar.ShoopingCar import ShoopingCar
from ATMandShoopCar.ShoopingCar import ATM


def main():
    while True:
        opt=input("1.ATM,2.购物车,输入q退出:")
        if opt.isdigit():
            if opt =='1':
                ATM.ATM_Opt()
            elif opt=='2':
                ShoopingCar.ShoopingCar_Opt()
            else:
                pass
        elif opt=='q':
            break
        else:
            pass

main()

  数据库ATM_Use'rCard.txt格式如图:

  数据库ATM_Use'rInfo.txt格式如图:

  数据库ATM_UserOpt.txt格式如图:

  数据库goods_info.txt格式如图:

  数据库users_info.txt格式如图:

 数据库users_value.txt格式如图:



 
原文地址:https://www.cnblogs.com/wangxingwei/p/9593376.html