码迷,mamicode.com
首页 > 编程语言 > 详细

python模拟ATM系统

时间:2017-07-03 20:58:17      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:python模拟atm系统

一、环境及程序功能说明

      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

技术分享

$ python looks_bank.py

技术分享

技术分享


四、所有文件如下

$ls -alh

技术分享

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

本文出自 “学无止境,学以致用” 博客,请务必保留此出处http://dyc2005.blog.51cto.com/270872/1944150

python模拟ATM系统

标签:python模拟atm系统

原文地址:http://dyc2005.blog.51cto.com/270872/1944150

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!