前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python写的ATM小程序

Python写的ATM小程序

作者头像
py3study
发布2020-01-07 15:19:19
7600
发布2020-01-07 15:19:19
举报
文章被收录于专栏:python3python3

很久没写博客了,近两年的时间一直在搞Openstack相关的东西,有时间我也会把我认为值得写得一些技术和经验分享出来。

以后准备好好学学Python了,要不快没饭吃了

j_0009.gif
j_0009.gif

,这两个礼拜看了一些视频教程和书籍,遂拿这个ATM小程序练练手。

文件结构:

程序共有6个py文件和3个文本文件

cashin.py  -- 还款模块 goods_list  -- 商品列表 login.py -- 主文件 menu.py -- 菜单模块 printlist.py  -- 记录打印模块 record_tran.txt  -- 用户操作记录 shopping.py -- 购买模块 user_list -- 用户列表 withdraw.py -- 提现模块

源代码:

login.py

代码语言:javascript
复制
#!/usr/bin/python

import sys
from menu import menu_show

while True:
    user = str(raw_input("\033[1;32;40mPlease input your name:\033[0m"))
    f = open('user_list','r+')
    for line in f.readlines():
        n = str(line.split()[0])
        p = str(line.split()[1])
        if user == n:
            while True:
                password = str(raw_input("\033[1;32;40mPlease input your password:\033[0m"))
                if password != p:
                    print "\033[1;31;40mThe password is incorrect\033[0m"
                    continue
                else:
                    print "\033[1;32;40myes let you in.\033[0m"
                    global money
                    money_total = 15000
                    while True:
                        print "\033[1;33;40mYou total money is: \033[0m",money_total
                        money_total = menu_show(user,money_total)
        else:
            print "\033[1;31;40mThe user is not vaild, please re-input:\033[0m"
            continue

menu.py

代码语言:javascript
复制
#!/usr/bin/python

import sys
import os
from shopping import show_shopping_list
from printlist import print_list
from withdraw import with_draw
from cashin import cash_in

def menu_show(n,mo):
    print "Welcome %s, This is the ATM system:" %n
    print "Select what you want to do:"
    print " 1. Shopping"
    print " 2. Withdraw"
    print " 3. Cash in"
    print " 4. Print list"
    print " 5. Exit"
    user_input = int(raw_input("Input 0 ~ 5 "))
    global current_user
    current_user = n
    mo = menu_select(user_input,mo)
    return mo

def menu_select(input,money):
    if input == 1:
        money = show_shopping_list(current_user,money)
    if input == 2:
        money = with_draw(current_user,money)
    if input == 3:
        money = cash_in(current_user,money)
    if input == 4:
        print_list(current_user)
    if input == 5:
        print "\033[1;33;40mThank you for using ATM, Good bye!\033[0m"
        sys.exit()
    return money

shopping.py

代码语言:javascript
复制
#!/usr/bin/python

import sys
import time

def show_shopping_list(n,m):
    
    #read the file to a dictionary
    goods_dict={}
    goods = open('goods_list','r+')
    for line in goods.readlines():
        key = line.split()[0]
        value = line.split()[1]+' '+line.split()[2]
        goods_dict[key] = value
    
    #print the goods list
    print "The current user is %s." %n
    print goods_dict   
    
    #decrease the money
    buy_number = int(raw_input("Please select which one you want to buy:"))
    goods_name = goods_dict[`buy_number`].split()[0]
    goods_value = int(goods_dict[`buy_number`].split()[1])
    
    m = calculate(int(m),goods_value)
    
    #record the log
    shopping_file_write(n,goods_name,goods_value,0)
    
    return m


def calculate(qian,pri):
    new_qian = qian - pri
    return new_qian
    
def shopping_file_write(user,name,price,fee):
    tran_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    spath = "record_tran.txt"
    f = open(spath,'a')
    f.write("%s|%s|%s|%d|%d  \n"  %(user,tran_time,name,price,fee))
    f.close()

withdraw.py

代码语言:javascript
复制
#!/usr/bin/python

import time

#withdraw function

def with_draw(n,m):
    print "Hello %s, you account have %d RMB." %(n,m)
    print "Attention: There are 5% fee per withdraw !"
    draw = int(raw_input("Please input how much money you want to with draw:"))
    m = m - draw * 1.05
    
    #record to the log
    
    withdraw_file_write(n,draw)
    
    return m

def withdraw_file_write(user,withdraw):
    shouxufei = cash * 0.05
    tran_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    spath = "record_tran.txt"
    f = open(spath,'a')
    f.write("%s|%s|withdraw|%d|%d  \n" %(user,tran_time,withdraw,shouxufei))
    f.close()

cashin.py

代码语言:javascript
复制
#!/usr/bin/python

import time

#cash in function

def cash_in(n,m):
    print "Hello %s, your account have %d RMB now." %(n,m)
    cash = int(raw_input("Please input how much money you want to save in :"))
    m = m + cash
    cashin_file_write(n,cash)
    return m

def cashin_file_write(user,cashin):
    tran_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    spath = "record_tran.txt"
    f = open(spath,'a')
    f.write("%s|%s|cashin|%d|0  \n"  %(user,tran_time,cashin))
    f.close()

printlist.py

代码语言:javascript
复制
#!/usr/bin/python

#select current user's bill

def print_list(n):
    print "Hello %s, Here is your bill:" %n
    print "Account|Date|Goods|Price|Fee"
    spath1 = "./record_tran.txt"
    f1 = open(spath1,'r')
    for line in f1.readlines():
        if line.split('|')[0] == n:
            print line

user_list

代码语言:javascript
复制
user1 123
user2 456

goods_list

代码语言:javascript
复制
1  Car  250000
2  Clothes   399
3  Shoes  199
4  Iphone5s   4999
5  Coffee   35
6  Foods    68
7  Bicycle   1688

example of record_tran.txt

代码语言:javascript
复制
user1|2014-07-08 16:59:27|Iphone5s|4999|0  
user2|2014-07-08 16:59:31|withdraw|1000|50  
user1|2014-07-08 16:59:56|withdraw|500|25  
user2|2014-07-09 14:38:33|cashin|3456|0  
user2|2014-07-09 14:39:10|Coffee|35|0
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档