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

python-code-16

时间:2018-06-18 23:22:42      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:path   1.0   操作   lis   als   alt   数字   .com   hdr   

模拟实现一个ATM + 购物商城程序

  1. 额度 15000或自定义
  2. 实现购物商城,买东西加入 购物车,调用信用卡接口结账
  3. 可以提现,手续费5%
  4. 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
  5. 支持多账户登录
  6. 支持账户间转账
  7. 记录每月日常消费流水
  8. 提供还款接口
  9. ATM记录操作日志 
  10. 提供管理接口,包括添加账户、用户额度,冻结账户等。。。
  11. 用户认证用装饰器

技术分享图片

技术分享图片
def login():
    pass

def register():
    pass

def check_balance():
    pass

def transfer():
    pass

def repay():
    pass

def withdraw():
    pass

def check_records():
    pass

def shopping():
    pass

def check_shopping_cart():
    pass

func_dic = {
    1:login,
    2:register,
    3:check_balance,
    4:transfer,
    5:repay,
    6:withdraw,
    7:check_records,
    8:shopping,
    9:check_shopping_cart
}

msg = ‘‘‘
    1 登录
    2 注册
    3 查看余额
    4 转账
    5 还款
    6 提现
    7 查看流水
    8 购物
    9 查看购买商品
    0 退出
‘‘‘
Readme
技术分享图片
import os
import sys

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_DIR)

from core import src

if __name__ == __main__:
    src.run()
bin-start
技术分享图片
import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # 得到上一层的上一层目录也就是根目录
BASE_DB_LOCAL = os.path.join(BASE_DIR, db)  # 把根目录 和 db 目录拼接起来
BASE_LOG_LOCAL=os.path.join(BASE_DIR, log)
logfile_name = log.log  # log文件名

standard_format = [%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]                   [%(levelname)s][%(message)s] #其中name为getlogger指定的名字

simple_format = [%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s

id_simple_format = [%(levelname)s][%(asctime)s] %(message)s

# 定义日志输出格式 结束


# 如果不存在定义的日志目录就创建一个
if not os.path.isdir(BASE_LOG_LOCAL):
    os.mkdir(BASE_LOG_LOCAL)

# log文件的全路径
logfile_path = os.path.join(BASE_LOG_LOCAL, logfile_name)

# log配置字典
LOGGING_DIC = {
    version: 1,
    disable_existing_loggers: False,
    formatters: {
        standard: {
            format: standard_format
        },
        simple: {
            format: simple_format
        },
    },
    filters: {},
    handlers: {
        #打印到终端的日志
        console: {
            level: DEBUG,
            class: logging.StreamHandler,  # 打印到屏幕
            formatter: simple
        },
        #打印到文件的日志,收集info及以上的日志
        default: {
            level: DEBUG,
            class: logging.handlers.RotatingFileHandler,  # 保存到文件
            formatter: standard,
            filename: logfile_path,  # 日志文件
            maxBytes: 1024*1024*5,  # 日志大小 5M
            backupCount: 5,
            encoding: utf-8,  # 日志文件的编码,再也不用担心中文log乱码了
        },
    },
    loggers: {
        #logging.getLogger(__name__)拿到的logger配置
        ‘‘: {
            handlers: [default, console],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            level: DEBUG,
            propagate: True,  # 向上(更高level的logger)传递
        },
    },
}
conf-setting
技术分享图片
import os
import json
from conf import setting
from interface import user
from lib import common
from interface import bank
from interface import shop

user_data = {
    name:None,
    is_auth:False
}
def login():
    ‘‘‘
    用户名,密码
    判断用户名是否存在(返回用户字典或者空(false))  判断密码是否正确
    已登录状态 不允许在登录 不允许在注册
    密码错误3次 锁定
    :return:
    ‘‘‘
    if user_data[is_auth]:
        print(user is login)
        return
    print(登录)
    count = 0
    while True:
        name = input(input your name>>: ).strip()
        user_dic = user.get_userinfo_interface(name)
        if count ==3:
            user.lock_user_interface(name)
            print(user is locked)
            break
        if user_dic:
            password=input(input your password).strip()
            if user_dic[pwd] == password and not user_dic[locked]:
                print(login success)
                user_data[name] = name
                user_data[is_auth] = True
                break
            else:
                count += 1
                print(password error or locked)

def register():
    if user_data[is_auth]:
        print(user is login)
        return
    print(注册)
    while True:
        name_inp = input(input your name).strip()
        if user.get_userinfo_interface(name_inp):
            print(user is exists)
            continue
        else:
            pwd_inp = input(input your password>>: ).strip()
            pwd_inp_check = input(check your input password>>: ).strip()
            if pwd_inp != pwd_inp_check:
                print(password is not equal)
                continue
            else:
                user.register(name_inp,pwd_inp)
                print(register success)
                break

@common.login_auth
def check_balance():
    print(查询余额)
    account = bank.get_account(user_data[name])
    print(%s的余额:%s %(user_data[name],account))

@common.login_auth
def transfer():
    print(转账)
    while True:
        to_user = input(transfer user>>: ).strip()
        if q == to_user:break
        if to_user == user_data[name]:
            print(can not transfer to yourself)
            continue
        to_user_dic = user.get_userinfo_interface(to_user)
        if to_user_dic:
            transfer_account=input(transfer account>>: ).strip()
            if transfer_account.isdigit():
                transfer_account = int(transfer_account)
                user_account = bank.get_account(user_data[name])
                if user_account >= transfer_account:
                    bank.transfer_interface(user_data[name],to_user,transfer_account)
                    break
                else:
                    print(余额不足)
                    continue
            else:
                print(必须是数字)
                continue
        else:
            print(用户不存在)
            continue

@common.login_auth
def repay():
    print(还款)
    while True:
        account = input(input repay account(q to quit)>>: ).strip()
        if q == account:break
        if account.isdigit():
            account = int(account)
            bank.repay_interface(user_data[name],account)
            break
        else:
            print(must input number)


@common.login_auth
def withdraw():
    print(取款)
    while True:
        account = input(input withdraw account>>: ).strip()
        if account.isdigit():
            user_account = bank.get_account(user_data[name])
            account = int(account)
            if user_account >= account*1.05:
                bank.withdraw_interface(user_data[name],account)
                print(withdraw success)
                break
            else:
                print(余额不足)
        else:
            print(只能输入数字)

@common.login_auth
def check_records():
    print(查看流水)
    bank_flow = bank.check_bankflow_interface(user_data[name])
    for record in bank_flow:
        print(record)

@common.login_auth
def shopping():
    ‘‘‘
    打印商品信息,根据编号购买 判断编号
    输入编号,输入数量 判断余额是否充足 (现金额度+信用额度)
    够直接扣款,购物车加入日志字段,不够提示
    :return:
    ‘‘‘
    product_list = [[Iphone7, 5800],
                    [Coffee, 30],
                    [疙瘩汤, 10],
                    [Python Book, 99],
                    [Bike, 199],
                    [ViVo X9, 2499],

                    ]
    shopping_cart = []
    while True:
        for k,v in enumerate(product_list):
            product_list_info = 编号:%s  商品:%s  价格:%s %(k,product_list[k][0],product_list[k][1])
            print(product_list_info)

        choice = input(输入购买编号(q to quit)>>: ).strip()
        if choice == q:break
        if not choice.isdigit():continue
        choice = int(choice)
        if not 0 <= choice <= len(product_list):continue

        product_count = input(输入购买数量>>: ).strip()
        if not product_count.isdigit():continue
        product_count = int(product_count)

        product_name = product_list[choice][0]
        product_price = product_list[choice][1]
        product_count = product_count

        for index in shopping_cart:
            if index[product_name] == product_name:
                index[product_count] += product_count
                break
        else:
            shopping_info = {product_name:product_name,product_price:product_price,product_count:product_count}
            shopping_cart.append(shopping_info)

        choice_continue = input(结账Y/y 继续任意键>>: ).strip()
        if choice_continue == Y or choice_continue == y:
            print(shopping_cart)
            #花费的钱数
            spend_money = 0
            for index in shopping_cart:
                spend_money += index[product_price] * index[product_count]
            user_account = bank.get_account(user_data[name])
            if user_account >= spend_money:
                user_account -= spend_money
                shop.shopping_interface(user_data[name],user_account,shopping_cart)
                break
            else:
                print(余额不足)
                shopping_cart = []

@common.login_auth
def check_shopping_cart():
    print(查看购物信息)
    shopping_flow = shop.check_shopping_cart_interface(user_data[name])
    print(shopping_flow)


func_dic = {
    1:login,
    2:register,
    3:check_balance,
    4:transfer,
    5:repay,
    6:withdraw,
    7:check_records,
    8:shopping,
    9:check_shopping_cart
}

msg = ‘‘‘
    1 登录
    2 注册
    3 查看余额
    4 转账
    5 还款
    6 提现
    7 查看流水
    8 购物
    9 查看购买商品
    0 退出
‘‘‘

def run():
    while True:
        print(msg)
        choice = input(请选择功能>>: ).strip()
        if choice == 0:break
        if choice not in func_dic:continue
        func_dic[choice]()
core-src
技术分享图片
import os
from conf import setting
import json

def update(user_dic):
    path_file = os.path.join(setting.BASE_DIR,db,%s.json%user_dic[name])
    with open(path_file, w, encoding=utf-8) as f:
        json.dump(user_dic, f)
        f.flush()


def select(name):
    path_file = os.path.join(setting.BASE_DIR, db, %s.json % name)
    if os.path.exists(path_file):
        with open(path_file,r,encoding=utf-8) as f:
            return json.load(f)
    else:
        return  False
db-db_handler
技术分享图片
from db import db_handler
from lib import common

logger_bank = common.get_logger(bank)

def get_account(name):
    user_dic = db_handler.select(name)
    return user_dic[account]

def withdraw_interface(name,account):
    user_dic = db_handler.select(name)
    user_dic[account] -= account*1.05
    user_dic[bankflow].append(%s withdraw %s yuan %(name,account))
    db_handler.update(user_dic)
    logger_bank.info(%s 取款 %s %(name,account))

def repay_interface(name,account):
    user_dic = db_handler.select(name)
    user_dic[account] += account
    user_dic[bankflow].append(%s repay %s yuan % (name, account))
    db_handler.update(user_dic)
    logger_bank.info(%s 还款 %s %(name,account))

def transfer_interface(from_user,to_user,account):
    from_user_dic = db_handler.select(from_user)
    to_user_dic = db_handler.select(to_user)

    from_user_dic[account] -=account
    to_user_dic[account] += account

    from_user_dic[bankflow].append(%s transfer %s %s yuan % (from_user,to_user,account))
    to_user_dic[bankflow].append(%s accept %s %s yuan % (to_user,from_user,account))

    db_handler.update(from_user_dic)
    db_handler.update(to_user_dic)

    logger_bank.info(%s transfer %s yuan to %s %(from_user,account,to_user))

def check_bankflow_interface(name):
    user_idc = db_handler.select(name)
    return user_idc[bankflow]
interface-bank
技术分享图片
from db import db_handler
from lib import common

logger_shopping = common.get_logger(shopping)

def shopping_interface(name,account,shopping_cart):
    user_dic = db_handler.select(name)
    user_dic[account] = account
    user_dic[shopping_cart].append(shopping_cart)
    db_handler.update(user_dic)
    logger_shopping.info(%s 购物信息 %s %(name,shopping_cart))

def check_shopping_cart_interface(name):
    user_dic = db_handler.select(name)
    return user_dic[shopping_cart]
interface-shop
技术分享图片
import os
from conf import setting
from db import db_handler
from lib import common

logger_user = common.get_logger(user)

def get_userinfo_interface(name):
    user_dic = db_handler.select(name)
    return user_dic

def register(name,pwd,account=15000):
    user_dic = {name: name, pwd: pwd, locked: False, account: account, creidt: account,bankflow:[],shopping_cart:[]}
    db_handler.update(user_dic)
    logger_user.info(%s 注册成功 %name)

def lock_user_interface(name):
    user_dic = get_userinfo_interface(name)
    user_dic[locked] = True
    db_handler.update(user_dic)
interface-user
技术分享图片
from core import src
import logging.config
from conf import setting

def login_auth(func):
    def wrapper(*args,**kwargs):
        if not src.user_data[is_auth]:
            src.login()
        else:
            return func(*args,**kwargs)
    return wrapper


def get_logger(name):
    logging.config.dictConfig(setting.LOGGING_DIC)
    logger = logging.getLogger(name)
    return logger
lib-common
技术分享图片
[2018-06-18 03:22:41,717][MainThread:8492][task_id:user][user.py:15][INFO][xjj4 注册成功]
[2018-06-18 03:49:33,482][MainThread:9828][task_id:bank][bank.py:14][INFO][xjj1 取款 100]
[2018-06-18 04:31:52,852][MainThread:6256][task_id:bank][bank.py:33][INFO][xjj1 transfer 10 yuan to xjj2]
[2018-06-18 04:33:10,783][MainThread:176][task_id:user][user.py:15][INFO][xjj 注册成功]
[2018-06-18 04:33:48,104][MainThread:176][task_id:user][user.py:15][INFO][xjj1 注册成功]
[2018-06-18 04:34:24,324][MainThread:176][task_id:bank][bank.py:33][INFO][xjj transfer 100 yuan to xjj1]
[2018-06-18 04:34:38,395][MainThread:176][task_id:bank][bank.py:33][INFO][xjj transfer 100 yuan to xjj1]
[2018-06-18 04:36:08,862][MainThread:11232][task_id:user][user.py:15][INFO][q 注册成功]
[2018-06-18 04:36:59,710][MainThread:11232][task_id:bank][bank.py:15][INFO][xjj 取款 100]
[2018-06-18 04:37:06,059][MainThread:11232][task_id:bank][bank.py:15][INFO][xjj 取款 100]
[2018-06-18 04:52:00,922][MainThread:4464][task_id:bank][bank.py:22][INFO][xjj 还款 100]
[2018-06-18 04:52:16,966][MainThread:4464][task_id:bank][bank.py:37][INFO][xjj transfer 1000 yuan to xjj1]
[2018-06-18 15:40:43,834][MainThread:11768][task_id:user][user.py:15][INFO][xjj2 注册成功]
[2018-06-18 16:38:22,487][MainThread:11960][task_id:user][user.py:15][INFO][xjj 注册成功]
[2018-06-18 16:38:54,313][MainThread:11960][task_id:shopping][shop.py:11][INFO][xjj 购物信息 [{product_name: Coffee, product_price: 30, product_count: 10}, {product_name: 疙瘩汤, product_price: 10, product_count: 10}]]
[2018-06-18 16:57:56,783][MainThread:6424][task_id:user][user.py:15][INFO][xjj1 注册成功]
[2018-06-18 17:03:09,624][MainThread:10096][task_id:shopping][shop.py:11][INFO][xjj 购物信息 [{product_name: Python Book, product_price: 99, product_count: 6}]]
[2018-06-18 17:04:18,088][MainThread:10096][task_id:bank][bank.py:15][INFO][xjj 取款 100]
[2018-06-18 17:04:32,079][MainThread:10096][task_id:bank][bank.py:22][INFO][xjj 还款 100]
[2018-06-18 17:04:59,107][MainThread:10096][task_id:bank][bank.py:37][INFO][xjj transfer 10 yuan to xjj1]
[2018-06-18 17:05:45,315][MainThread:4400][task_id:bank][bank.py:37][INFO][xjj1 transfer 11 yuan to xjj]
[2018-06-18 17:05:51,839][MainThread:4400][task_id:bank][bank.py:22][INFO][xjj1 还款 1000]
[2018-06-18 17:06:00,839][MainThread:4400][task_id:bank][bank.py:15][INFO][xjj1 取款 700]
[2018-06-18 17:07:18,609][MainThread:4400][task_id:shopping][shop.py:11][INFO][xjj1 购物信息 [{product_name: Iphone7, product_price: 5800, product_count: 1}, {product_name: 疙瘩汤, product_price: 10, product_count: 12}, {product_name: Coffee, product_price: 30, product_count: 3}]]
log-log.log
技术分享图片
{"name": "xjj", "pwd": "123", "locked": false, "account": 14002.0, "creidt": 15000, "bankflow": ["xjj withdraw 100 yuan", "xjj repay 100 yuan", "xjj transfer xjj1 10 yuan", "xjj accept xjj1 11 yuan"], "shopping_cart": [[{"product_name": "Coffee", "product_price": 30, "product_count": 10}, {"product_name": "\u7599\u7629\u6c64", "product_price": 10, "product_count": 10}], [{"product_name": "Python Book", "product_price": 99, "product_count": 6}]]}
db-xjj.json
技术分享图片
{"name": "xjj1", "pwd": "123", "locked": false, "account": 9254.0, "creidt": 15000, "bankflow": ["xjj1 accept xjj 10 yuan", "xjj1 transfer xjj 11 yuan", "xjj1 repay 1000 yuan", "xjj1 withdraw 700 yuan"], "shopping_cart": [[{"product_name": "Iphone7", "product_price": 5800, "product_count": 1}, {"product_name": "\u7599\u7629\u6c64", "product_price": 10, "product_count": 12}, {"product_name": "Coffee", "product_price": 30, "product_count": 3}]]}
db-xjj1.json

 

python-code-16

标签:path   1.0   操作   lis   als   alt   数字   .com   hdr   

原文地址:https://www.cnblogs.com/xujinjin18/p/9196610.html

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