思路:ATM是一个单独程序,提供给消费的是一个接口core下的settlement.py,只做了个人的,没写管理的模块
Shopping也是一个单独的,只做了一个购物的消费模块,没写商家模块,偷懒用了银行的数据库,用户名和密码都是用的一套的
具体目录如下:
atm.py:
import os
import sys
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# print(base_dir)
sys.path.append(base_dir)
from core import main
if __name__ == '__main__':
main.run()
settings.py:
import os
import sys
import logging
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATABASE = {
'engine': 'file_storage',
'name':'accounts',
'path':"%s\db" % BASE_DIR
}
LOG_LEVEL = logging.INFO
LOG_TYPES = {
'transaction':'transactions.log',
'access':'access.log',
}
TRANSACTION_TYPE = {
'repay':{'action':'plus','interest':0},
'withdraw':{'action':'minus','interest':0.05},
'transfer':{'action':'minus','interest':0.05},
'consume':{'action':'minus','interest':0},
}
accounts.py:
import json,os,sys,time
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from core import db_handler
from conf import settings
def load_current_balance(account_id):
"""
:param account_id:
:return:返回账号的资金和其它信息
"""
db_path = db_handler.db_handler(settings.DATABASE)
account_file = "%s\%s.json" %(db_path,account_id)
with open(account_file) as f:
acc_data = json.load(f)
return acc_data
def dump_account(account_data):
db_path = db_handler.db_handler(settings.DATABASE)
account_file = "%s\%s.json" %(db_path,account_data['id'])
with open(account_file,'w') as f:
acc_data = json.dump(account_data,f)
return True
auth.py:
import os
import sys
import json
import time
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from core import db_handler
from conf import settings
from core import logger
def acc_auth(account,password):
"""
:param account: 信用卡账号
:param password: 信用卡密码
:return:
"""
db_path = db_handler.db_handler(settings.DATABASE) #数据库存储的目录
account_file = "%s\%s.json" %(db_path,account) #个人的json数据
# print(account_file)
if os.path.isfile(account_file): #还必须判断这个文件是否存在
with open(account_file) as f:
account_data = json.load(f) #还原成字典
if account_data['password'] == password:
#mktime() 接收struct_time对象作为参数,返回用秒数来表示时间的浮点数,
# time.strptime(string[, format])根据指定的格式把一个时间字符串解析为时间元组
exp_time_stamp = time.mktime(time.strptime(account_data['expire_date'],"%Y-%m-%d"))
if time.time() > exp_time_stamp:
print("