欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ATM程序-第一版-第二部分(未经视频指导)

程序员文章站 2022-05-02 22:17:29
...

3 自动增加利息

#!/usr/bin/env python
#--author lisheng--

import time
import os
import sys
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_path)

from core import auxiliary

def timeup():
    while True:
        time.sleep(60)
        t1 = time.strftime("%y-%m-%d %H:%M:%S", time.localtime())
        t2 = t1.split(" ")[1]
        t2 = t2.split(":")[1]
        print(t1)
        print(t2)
        if "6" in t2:
            auxiliary.auto_repayment()

timeup()

4 辅助函数(读写文件以及记录日志的函数)

#!/usr/bin/env python
#Author:lisheng

import time
import sys
import json
import os
path_a = "%s/db/accounts" % (os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
path = "%s/db/product" %(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
path1 = "%s/log/" %(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_path)

def product_list():
    #从商品文件中读取商品列表
    car = []
    product1 = open("%s/product" %(path), "r")   #读出商品列表,并赋给car列表
    for i1 in product1.readlines():
        i1 = i1.strip()
        pro = i1.split(" ")[0]
        pri = i1.split(" ")[1]
        car.append(["{ss}".format(ss=pro), "{ssd}".format(ssd=pri)])
    product1.close()
    return car

def write_cre(name,summ):
    #消费后写入信用卡更新信息
    f = open("%s/db/accounts/%s.json" %(base_path,name),"r+")
    fi = json.loads(f.read())
    f.close()
    f = open("%s/db/accounts/%s.json" %(base_path,name),"w")
    f.write("")
    fi["Amount of money"] = float(fi["Amount of money"])-float(summ)
    f.write(json.dumps(fi))
    f.close()

def write_sal(name,summ):
    # 消费后写入工资卡更新信息
    f = open("%s/db/accounts/%s_salary.json" %(base_path,name),"r+")
    fi = json.loads(f.read())
    f.close()
    f = open("%s/db/accounts/%s_salary.json" %(base_path,name),"w")
    f.write("")
    fi["money"] = float(fi["money"])-float(summ)
    f.write(json.dumps(fi))
    f.close()

def write_sal_add(name1,summ1):
    #有收入时,更新工资卡更新信息
    f = open("%s/db/accounts/%s_salary.json" %(base_path,name1),"r+")
    fi = json.loads(f.read())
    f.close()
    f = open("%s/db/accounts/%s_salary.json" %(base_path,name1),"w")
    f.write("")
    fi["money"] = float(fi["money"])+float(summ1)
    f.write(json.dumps(fi))
    f.close()

def write_cre_add(name,summ):
    #还款时,更新信用卡信息
    f = open("%s/db/accounts/%s.json" %(base_path,name),"r+")
    fi = json.loads(f.read())
    f.close()
    f = open("%s/db/accounts/%s.json" %(base_path,name),"w")
    f.write("")
    fi["Amount of money"] = float(fi["Amount of money"])+float(summ)
    print("add")
    f.write(json.dumps(fi))
    f.close()

def credit_card(name):
    #查询信用卡信息并返回
    path = "%s/db/accounts" % (os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    dir = os.listdir(path)
    dir = iter(dir)
    for i in dir:
        if "%s.json" % (name) == i:
            f = open("%s/db/accounts/%s.json" % (base_path,name), "r")
            fi = json.loads(f.read())
            f.close()
            return fi
    else:
        return {"Amount of money":"没有信用卡,开一个去!快!"}

def salary(name):
    # 查询工资卡信息并返回
    f = open("%s/db/accounts/%s_salary.json" %(base_path,name),"r")
    fi = json.loads(f.read())
    f.close()
    return fi

def conpare_cre(name,summ):
    #比较消费金额与信用卡余额,并调用write_cre(name,summ)更新信用卡信息
    cre = credit_card(name)
    if float(cre["Amount of money"]) >= summ:
        write_cre(name,summ)
    elif float(cre["Amount of money"]) < summ:
        print("超出信用卡透支额度,不要太贪!要不就去掉一些东西")

def conpare_sal(name,summ):
    # 比较消费金额与工资卡余额,并调用write_sal(name,summ)更新工资卡信息
    sal = salary(name)
    if float(sal["money"]) >= summ:
        write_sal(name,summ)
    elif float(sal["money"]) < summ:
        print("你的账户里钱不够,回去再挣一些吧!")

def conpare_sal_add(name,summ,name1,summ1):
    # 比较转出金额与被转工资卡余额,并调用write_sal_add(name1,summ1)更新工资卡信息
    sal = salary(name)
    if float(sal["money"]) >= summ:
        write_sal_add(name1,summ1)
    elif float(sal["money"]) < summ:
        print("你的账户里钱不够,回去再挣一些吧!")

def conpare_cre_add(name,summ):
    # 比较转出金额与被转工资卡余额,并调用write_cre_add(name,summ)更新信用卡信息
    sal = salary(name)
    if float(sal["money"]) >= summ:
        write_cre_add(name,summ)
    elif float(sal["money"]) < summ:
        print("你的账户里钱不够,回去再挣一些吧!")

def write_sal_Withdraw_money_log(name,mon,balance):
    #工资卡取钱日志
    f = open("%ssal_Withdraw_money_log.txt" %(path1),"a",encoding="utf-8")
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f.write("%s %s's Withdraw money log! " %(t,name))
    f.write("取出金额:%s 余额:%s\n" %(mon,balance))
    f.close()

def write_sal_Transfer_accounts_log(name,name1,mon,mon1):
    #转账日志
    f = open("%ssal_Transfer_accounts_log.txt" %(path1),"a",encoding="utf-8")
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f.write("%s %s's Transfer accounts log! " %(t,name))
    f.write("转出金额:%s 余额:%s 转入账号:%s\n" %(mon,mon1,name1))
    f.close()

def write_cash_advance_log(name,mon,mon1):
    #信用卡提现
    f = open("%scash_advance_log.txt" %(path1),"a",encoding="utf-8")
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f.write("%s %s's cash advance log! " %(t,name))
    f.write("提现金额:%s 余额:%s\n" %(mon,mon1))
    f.close()

def write_repayment_log(name,mon,mon1,mon2):
    f = open("%scash_repayment_log.txt" %(path1),"a",encoding="utf-8")
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f.write("%s %s's repayment log! " %(t,name))
    f.write("还款金额:%s 工资卡余额:%s 信用卡待还金额:%s\n" %(mon,mon1,mon2))
    f.close()

def write_cre_shopping_log(name,shopping_car):
    #购物车-信用卡消费记录
    f = open("%scredit_card_log.txt" %(path1),"a",encoding="utf-8")
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f.write("%s %s's shopping list! " % (t, name))
    f.write("%s\n" % (shopping_car))
    f.close()

def write_sal_shopping_log(name,shopping_car):
    #购物车-工资卡消费记录
    f = open("%ssal_log.txt" %(path1),"a",encoding="utf-8")
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f.write("%s %s's shopping list! " % (t, name))
    f.write("%s\n" % (shopping_car))
    f.close()

def Withdraw_money():
    #取钱
    while True:
        input_user = input("(输入q退出)输入你的名字,开始取钱:")
        dir = os.listdir(path_a)
        dir = iter(dir)
        if input_user == "q":
            break
        input_password = input("密码:")
        for i in dir:
            if "%s_salary.json" % (input_user) == i:
                if salary(input_user)["password"] == input_password:
                    print("你的工资卡里现在有:%s" % (salary(input_user)["money"]))
                    while True:
                        mon = input("想挥霍多少钱?")
                        if mon == "q":
                            break
                        elif mon.isdigit():
                            mon = float(mon)
                            conpare_sal(input_user,mon)
                            print("看看你工资卡里剩几个子儿了:%s" % (salary(input_user)["money"]))
                            write_sal_Withdraw_money_log(input_user,mon,salary(input_user)["money"])
                            break

def Transfer_accounts():
    #转账
    while True:
        input_user = input("(输入q退出)输入你的名字,开始转账:")
        dir = os.listdir(path_a)
        dir = iter(dir)
        if input_user == "q":
            break
        for i in dir:
            if "%s_salary.json" % (input_user) == i:
                input_password = input("密码:")
                if salary(input_user)["password"] == input_password:
                    print("你的工资卡里现在有:%s" % (salary(input_user)["money"]))
                    while True:
                        user_tr = input("想转入的账户:")
                        dir = os.listdir(path_a)
                        dir = iter(dir)
                        if user_tr == "q":
                            break
                        for i1 in dir:
                            if "%s_salary.json" % (user_tr) == i1:
                                mon = input("想转多少钱?")
                                if mon.isdigit():
                                    mon = float(mon)
                                    conpare_sal(input_user,mon)
                                    conpare_sal_add(input_user,mon,user_tr,mon)
                                    print("看看你工资卡里剩几个子儿了:%s" % (salary(input_user)["money"]))
                                    write_sal_Transfer_accounts_log(input_user,user_tr,mon,salary(input_user)["money"])
                                    break

def cash_advance():
    #信用卡提现
    while True:
        input_user = input("(输入q退出)输入你的名字,开始从信用卡提现:")
        dir = os.listdir(path_a)
        dir = iter(dir)
        if input_user == "q":
            break
        input_password = input("密码:")
        for i in dir:
            if "%s.json" % (input_user) == i:
                if salary(input_user)["password"] == input_password:
                    print("你的信用卡可以透支:%s" % (credit_card(input_user)["Amount of money"]))
                    while True:
                        mon = input("想从你的信用卡里取多少钱?(不要忘了有5%的手续费)")
                        if mon == "q":
                            break
                        elif mon.isdigit():
                            mon = float(mon)
                            if mon > 7500:
                                print("银行说了,此信用卡最高只能提现7500")
                            elif mon >= credit_card(input_user)["Amount of money"]:
                                print("不要想入非非了,老兄!信用卡可用提现金额不够!")
                            elif credit_card(input_user)["Amount of money"] - mon - mon*0.05 > 0:
                                mon1 = mon + mon*0.05
                                conpare_cre(input_user,mon1)
                                print("假装你刚从提款机取了%s" %(mon))
                                print("你的信用卡现在可以透支:%s" % (credit_card(input_user)["Amount of money"]))
                                write_cash_advance_log(input_user,mon,credit_card(input_user)["Amount of money"])
                                break
                            else:
                                print("不要耍我,能取出多少,你自己没个B数!")

def repayment():
    #信用卡还款
    while True:
        input_user = input("(输入q退出)输入你的名字,开始还账:")
        dir = os.listdir(path_a)
        dir = iter(dir)
        if input_user == "q":
            break
        for i in dir:
            if "%s_salary.json" % (input_user) == i:
                dir = os.listdir(path_a)
                dir = iter(dir)
                for i1 in dir:
                    if "%s.json" % (input_user) == i1:
                        input_password = input("密码:")
                        if salary(input_user)["password"] == input_password:
                            print("你的工资卡里现在有:%s" % (salary(input_user)["money"]))
                            print("你的信用卡待还:%s" % (15000-credit_card(input_user)["Amount of money"]))
                            while True:
                                mon = input("想还多少钱?")
                                if mon.isdigit():
                                    mon = float(mon)
                                    if salary(input_user)["money"] - mon >= 0:
                                        conpare_sal(input_user,mon)
                                        conpare_cre_add(input_user,mon)
                                        print("看看你工资卡里剩几个子儿了:%s" % (salary(input_user)["money"]))
                                        print("你的信用卡待还:%s" % (15000-credit_card(input_user)["Amount of money"]))
                                        write_repayment_log(input_user,mon,salary(input_user)["money"],15000-credit_card(input_user)["Amount of money"])
                                        break
                                    elif salary(input_user)["money"] - mon < 0:
                                        print("这不是信用卡,不能透支,大哥!")
                                if mon == "q":
                                    break

def check_credit_card(i):
    #如果检查到没有还款,则增加利息
    f = open("%s/%s.json" %(path_a,i),"r")
    acc = json.loads(f.read())
    f.close()
    if float(acc["Amount of money"]) < 15000:
        f = open("%s/%s.json" %(path_a,i),"w")
        f.write("")
        f.close()
        f = open("%s/%s.json" %(path_a,i),"w")
        acc["Amount of money"] = acc["Amount of money"] - (15000 - acc["Amount of money"])*0.05
        f.write(json.dumps(acc))
        print(acc)
        f.close()

def auto_repayment():
    #找出所有信用卡账户,并传送给check_credit_card
    dir = os.listdir(path_a)
    dir = iter(dir)
    time.sleep(2)
    for i in dir:
        if "_" in i:
            pass
        else:
            i = i.split(".")[0]
            print(i)
            check_credit_card(i)

5 商品列表文件格式
iphone 6000
ak47 5000
tv 3500
three_star 100
pan 200
ss 555
tv4500 4500
tt 20
wuzi 4000
disk 100

6 账户文件格式
{“username”: “lz”, “password”: “1234”, “sex”: “male”, “Amount of money”: 15000}