python模拟ATM系统

发布时间:2019-09-18 07:22:09编辑:auto阅读(1457)

    一、环境及程序功能说明

          python 2.7.12模拟与银行ATM功能,主要功能如下:

           1,登录查询余额

           2,转账功能

           3,提现功能

           4,流水查询

           5,密码修改

           6,还款功能

           7,退出(切换账号)

           采用pickle存储账号密码相关信息,以账号名为日志文件名存储该账号的流水日志~

    为了实现测试,先写一个提供测试文件的小脚本,代码如下:

    #coding:utf-8
    import pickle
    import time
    import os
    IOSTIME = '%Y-%m-%d %X'
    Now = time.strftime(IOSTIME,time.localtime())
    Counts = {'9168':['1234','0',15000,[Now]],'9198':['4567','0',10000,[Now]],'9999':['6789','1',12000,[Now]]}
    bank_count = open("banks_counts.pkl",'w')
    pickle.dump(Counts,bank_count)
    os.chdir(".")
    for log in Counts.keys():
        os.popen("rm -rf %s" %log)
    
    print("测试数据已经生成:\n:%s" %Counts)

    二、ATM系统实现

    #!/usr/bin/env python
    #coding=utf8
    import pickle
    import sys
    import time
    import os
    IOSTIME = '%Y-%m-%d %X'
    Now = time.strftime(IOSTIME,time.localtime())
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    ##现在时间
    def NOW():
        global Now
        Now = time.strftime(IOSTIME,time.localtime())
        return Now
    
    ##读入数据
    with open('banks_counts.pkl') as bank_infos:
        UserInfos = pickle.load(bank_infos)
        print UserInfos
    
    ##完成写入功能
    def Write_DB():
        with open('banks_counts.pkl','w') as New_bank_infos:
            pickle.dump(UserInfos,New_bank_infos)
    
    #for userinfo in UserInfos.keys():
    #    print UserInfos[userinfo]     
    
    ##查询金额
    def LookHow(HM):
        print "现在时间是: %s,银行余额: %s RMB!" %(NOW(),HM)
    #提现功能    
    def TakeCrash(HOW,WHO):
        ShouXuFei = HOW * 0.01                    #手续费为1/100
        if (HOW + ShouXuFei) > WHO:
            print "余额不足提取!"
        else:
            WHO = WHO - HOW- ShouXuFei
            print "您要提取的金额为:%s,手续费: %s,余额为:%s RMB!" %(HOW,ShouXuFei,WHO)
            with open(UserCard,'a+') as f:
                f.write("%s:%s提款%s\n" %(NOW(),UserCard,HOW))
        UserInfos[UserCard][2] = WHO
    
    ####转账功能
    def ZhuangZ(From,TO,How):
        print From,TO
        if How > From:
            print "%s 账号余额不足!" %How
        else:
           From = From - How
           TO = TO + How
           print "转出金额: %s RMB,余额为: %s RMB" %(How,From)
    
        UserInfos[UserCard][2] = From
        UserInfos[TO_Card][2] = TO
        with open(UserCard,"a+") as f1:
           f1.write("%s:%s扣款%s,输出到:%s!\n" %(NOW(),UserCard,How,TO_Card))
        with open(TO_Card,"a+") as f2:
           f2.write("%s:%s收到来自%s的%s款!\n" %(NOW(),TO_Card,UserCard,How))
    
    ###还款功能
    def AddMoney(HOW,WHO):
        if HOW > 0:
            print("确定还款:%s" %HOW)
            UserInfos[WHO][2] = UserInfos[WHO][2] + HOW
            with open(WHO,"a+") as f:
                f.write("%s:还款%s\n" %(NOW(),HOW))
    
    ##流水查询
    def Logs(WHO):
        if os.path.isfile(os.path.join(os.getcwd(),WHO)):
            with open(WHO,'r') as f:
                for line in f.readlines():
        else:
            print("该账号还没有产生交易流水!")
    
    
    ##最后一次访问时间
    def LastAccess(WHO):
        UserInfos[WHO][3] = NOW()
    #    Write_DB()
    
    ##密码修改
    def ChgPwd(WHO,NewPass):
        UserInfos[WHO][0] = NewPass
        print("密码修改成功!")
    
    Contex = ["查询余额","转账","提现","流水","还款","修改密码","退出"]
    
    ErrorCount = 0
    while ErrorCount < 3 :
        for userinfo in UserInfos.keys():
            UserCard = raw_input("[输入quit|exit退出系统]请输入您的银行卡:").strip()
    #        if UserCard == 'quit':sys.exit("您选择退出!")
            if UserCard == '':continue
            elif UserCard == 'quit' or UserCard == 'exit':
                sys.exit("您选择退出!")
            elif UserCard not in UserInfos.keys():
                print("您输入的银行卡不存在!")
                continue
    #        else:
    #           print("您输入的有误!")  
            UserPwd = raw_input("[输入quit|exit退出系统]请输入您的密码:").strip()
            if  UserPwd == 'quit' or UserPwd == "exit":
                sys.exit("您选择退出!")
            try:
                if  UserInfos[UserCard][1] != '0':
                    print "%s 账号被锁定!" % UserCard
                    ErrorCount +=1
                    continue
            except KeyError:
                    print "%s 账号不存在!" %UserCard
                    ErrorCount +=1
                    continue
            if  len(UserPwd) == 0 or UserPwd !=  UserInfos[UserCard][0]:
                print "密码错误!"
                ErrorCount +=1
                continue
            else:
                print "%s 登录成功!您最后一次访问在: %s"  %(UserCard,UserInfos[UserCard][3])
                LastAccess(UserCard)   #登录成功,写入访问时间
    
                while True:
                    print(("请选择你需要的服务:").center(90,"+"))
                    print
                    for i in range(len(Contex)):
                        print i,Contex[i].decode("utf-8"),'\t',
                    print
                    print("+" * 80 + '\n')
                    choice = raw_input("[按6或输入quit退出系统]请选择您的业务编号:")
                    if len(choice) == 0:continue
                    elif choice == 'quit' or int(choice) == 6 :
                       Write_DB()
                       print("您选择退出系统!")
                       break
    
                    elif int(choice) == 0:
                        LookHow(UserInfos[UserCard][2])
                    elif int(choice) == 1:
                        TO_Card = raw_input("请输入对方银行卡:").strip()
                        if len(TO_Card) == 0 or TO_Card == UserInfos[UserCard]:
                            print "账号错误!"
                            continue
                        HowZ = raw_input("输入转账金额:").strip()
                        if len(HowZ) == 0:
                            print "转账金额不能为0!"
                        try:
                            UserInfos[TO_Card]
                        except KeyError:
                             print "对方账号: %s 不存在!" % TO_Card
                             continue
                        else:
                             UserInfos[TO_Card][2] = int(UserInfos[TO_Card][2])
                             ZhuangZ(UserInfos[UserCard][2],UserInfos[TO_Card][2],int(HowZ))
    
                    elif int(choice) == 2:
                        HowTake = raw_input("请选择提取金额:")
                        HowTake = int(HowTake)
                        TakeCrash(HowTake,UserInfos[UserCard][2])
                        Write_DB()
                    elif int(choice) == 4:
    #                    print UserCard
                        HowAdd = int(raw_input("请输入您要还款的金额:"))
                        AddMoney(HowAdd,UserCard)
                        Write_DB()
                    elif int(choice) == 3:
                        Logs(UserCard)
                    elif int(choice) == 5:
                        NewPass = raw_input("请输入新的密码:")
                        NewPass2 = raw_input("请再输入一次新密码:")
                        if NewPass == NewPass2:
                            ChgPwd(UserCard,NewPass)
                            Write_DB()
                        else:
                            sys.exit("两次输入的密码不一致!退出密码修改!")
    
    else:
        print("你的账号已锁,请到柜台解锁!")
        UserInfos[UserCard][1] = '1'
        UserInfos[UserCard][3] = NOW()
        Write_DB()

    三、功能测试如

    $python creat_bank.py

    wKioL1laFaPzSIytAAIE4DjPGt4003.png

    $ python looks_bank.py

    wKiom1laFIbAn_ebAAvuN1jbwTI233.png-wh_50

    wKioL1laFIzwlAeGAArIeqxBeek607.png-wh_50


    四、所有文件如下

    $ls -alh

    wKiom1laEsOCXzWYAAG2lkVgBwY725.png

    写着用来练习,如有错误之处欢迎指正,交流!

关键字

上一篇: python+selenium+auto

下一篇: 内建校验器3