码迷,mamicode.com
首页 > 其他好文 > 详细

ATM

时间:2019-09-19 22:08:38      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:lag   main   from   enc   inf   cos   als   iphone   fun   

conf->settings.py:

import os

BASE_PATH = os.path.dirname(os.path.dirname(__file__))
DB_PATH = os.path.join(BASE_PATH, 'db')

lib-> start.py:

import os
import sys
from core import src

sys.path.append(
    os.path.dirname(__file__)
)
if __name__ == '__main__':
    src.run()

lib->common.py:

from functools import wraps


def login_auth(func):
    from core import src
    @wraps(func)
    def inner(*args, **kwargs):
        if src.user_info.get('user'):
            res = func(*args, **kwargs)
            return res
        else:
            print('未登录,请去登录!')
            src.login()

    return inner

db->db_handler.py:

import json
import os
from conf import settings

def select(username):
    user_path = os.path.join(settings.DB_PATH,
                             f'{username}.json')
    if os.path.exists(user_path):
        with open(user_path, 'r', encoding='utf-8')as f:
            user_dic = json.load(f)
            return user_dic

def save(user_dic):
    user_path = os.path.join(settings.DB_PATH,
                             f'{user_dic.get("username")}.json')
    with open(user_path, 'w', encoding='utf-8')as f:
        json.dump(user_dic, f)
        f.flush()

interface->bank_Interface.py:

from db import db_handler


def withdraw_interface(username, money):
    user_dic = db_handler.select(username)
    withdraw_money = money * 1.05
    if user_dic.get('balance') >= withdraw_money:
        user_dic['balance'] -= withdraw_money
        msg = f'{username}提现{money}元成功!'
        user_dic['bank_flow'].append(msg)
        db_handler.save(user_dic)
        return True, msg


def repay_interface(username, money):
    user_dic = db_handler.select(username)
    user_dic['balance'] += money
    msg = f'{username}还款{money}成功!'
    user_dic['bank_flow'].append(msg)
    db_handler.save(user_dic)
    return msg


def transfer_interface(current_user, to_user, money):
    to_user_dic = db_handler.select(to_user)
    if not to_user_dic:
        return False, '目标用户不存在'
    current_user_dic = db_handler.select(current_user)
    if current_user_dic.get('balance') >= money:
        current_user_dic['balance'] -= money
        to_user_dic['balance'] += money
        msg = f'{current_user}向{to_user}转账{money}元成功'
        to_user_dic['bank_flow'].append(msg)
        to_user_flow = f'{to_user}接收到{current_user}转账{money}元成功'
        to_user_dic['bank_flow'].append(to_user_flow)
        db_handler.save(current_user_dic)
        db_handler.save(to_user_dic)
        return True, msg
    return False, '余额不足,转账失败!'


def check_flow_interface(username):
    user_dic = db_handler.select(username)
    return user_dic.get('bank_flow')


def check_bal_interface(username):
    user_dic = db_handler.select(username)
    return user_dic.get('balance')


def pay_interface(username, cost):
    user_dic = db_handler.select(username)
    if user_dic.get('balance') >= cost:
        user_dic['balance'] -= cost
        flow = f'{username}购物支付{cost}成功!'
        user_dic['bank_flow'].append(flow)
        db_handler.save(user_dic)
        return True
    return False

interface->user_interface.py:

from db import db_handler
from core import src


def register_interface(username, password):
    user_dic = db_handler.select(username)
    if user_dic:
        return False, '用户名已存在'
    user_dic = {
        'username': username,
        'password': password,
        'balance': 50000,
        'bank_flow': [],
        'shop_car': {}
    }
    db_handler.save(user_dic)
    return True, f'{username}注册成功!'


def login_interface(username, password):
    user_dic = db_handler.select(username)
    if not user_dic:
        return False, '用户不存在'
    if user_dic.get('password') == password:
        return True, f'{username}登录成功!'
    else:
        return False, '密码错误'


def logout_interface():
    src.user_info['user'] = None
    return '注册成功!'

interface->shop_interface.py:

from interface import bank_interface
from db import db_handler


def shop_pay_interface(username, shop_car, cost):
    flag = bank_interface.pay_interface(username, cost)
    user_dic = db_handler.select(username)
    if flag:
        user_dic['shop_car'] = {}
        db_handler.save(user_dic)
        return True, '购物并支付成功!'
    else:
        user_dic['shop_car'] = shop_car
        db_handler.save(user_dic)
        return False, '支付失败,保存购物车'


def shopping_car_interface(username, shop_car):
    user_dic = db_handler.select(username)
    if shop_car:
        user_dic['shop_car'] = shop_car
        db_handler.save(user_dic)
        return True, '添加购物车成功!'
    else:
        return False, '购物车是空的!'


def check_shop_car_interface(username):
    user_dic = db_handler.select(username)
    return user_dic.get('shop_car')

core->src.py:

from interface import user_interface
from interface import bank_interface
from interface import shop_interface
from lib import common

user_info = {
    'user': None
}


def register():
    while True:
        username = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()
        re_password = input('请确认密码:').strip()
        if password == re_password:
            flag, msg = user_interface.register_interface(username, password)
            if flag:
                print(msg)
                break
            else:
                print(msg)


def login():
    while True:
        username = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()
        flag, msg = user_interface.login_interface(username, password)
        if flag:
            print(msg)
            user_info['user'] = username
            break
        else:
            print(msg)


@common.login_auth
def check_balance():
    print('查看余额')
    balance = bank_interface.check_bal_interface(user_info['user'])
    print(balance)


@common.login_auth
def withdraw():
    while True:
        money = input('请输入提现金额:').strip()
        if not money.isdigit():
            print('必须是数字:').strip()
            continue
        money = int(money)
        flag, msg = bank_interface.withdraw_interface(user_info.get('user'), money)
        if flag:
            print(msg)
            break
        else:
            print(msg)


@common.login_auth
def repay():
    while True:
        money = input('请输入还款金额:').strip()
        if not money.isdigit():
            print('必须是数字:')
            continue
        money = int(money)
        msg = bank_interface.repay_interface(user_info.get('user'), money)
        print(msg)
        break


@common.login_auth
def transfer():
    while True:
        to_user = input('请输入转账目标用户:').strip()
        money = input('请输入转账金额:').strip()
        if not money.isdigit():
            print('金额必须是数字:')
            continue
        money = int(money)
        flag, msg = bank_interface.transfer_interface(user_info.get('user'), to_user, money)
        if flag:
            print(msg)
            break
        else:
            print(msg)


@common.login_auth
def check_flow():
    flow_list = bank_interface.check_flow_interface(user_info['user'])
    if flow_list:
        for flow in flow_list:
            print(flow)


@common.login_auth
def shopping():
    good_list = [
        ['iphone6s', 3000],
        ['huawei p30p', 6000],
        ['xiaomi9', 3000],
        ['onesplus7', 3000]
    ]
    shopping_car = {}
    cost = 0
    user_bal = bank_interface.check_bal_interface(user_info.get('user'))
    while True:
        for index, goods in enumerate(good_list):
            print(index, goods)
        choice = input('请输入商品编号 or q退出购买:').strip()
        if choice == 'q':
            break
        if not choice.isdigit():
            print('必须是数字!')
            continue
        choice = int(choice)
        good_name, good_price = good_list[choice]
        if user_bal >= good_price:
            if good_name in shopping_car:
                shopping_car[good_name] += 1
            else:
                shopping_car[good_name] = 1
            cost += good_price
        else:
            print('用户余额不足!')
    if not cost:
        print('没有选择商品!')
        return
    sure = input('是否确认购买,输入y/n:').strip()
    if sure == 'y':
        flag, msg = shop_interface.shop_pay_interface(
            user_info.get('user'), shopping_car, cost)
        if flag:
            print(msg)
        else:
            print(msg)
    elif sure == 'n':
        flag, msg = shop_interface.shopping_car_interface(
            user_info.get('user'), shopping_car)
        if flag:
            print(msg)
        else:
            print(msg)


@common.login_auth
def check_shop_car():
    shop_car = shop_interface.check_shop_car_interface(user_info.get('user'))
    print(shop_car)


def logout():
    if user_info.get('user'):
        msg = user_interface.logout_interface()
        print(msg)


func_dic = {
    '1': register,
    '2': login,
    '3': check_balance,
    '4': withdraw,
    '5': repay,
    '6': transfer,
    '7': check_flow,
    '8': shopping,
    '9': check_shop_car,
    '10': logout,
}


def run():
    while True:
        print('''
        1.注册
        2.登录
        3.查看额度
        4.提现
        5.还款
        6.转账
        7.查看流水
        8.购物功能
        9.查看购物车
        10.注销
        q;退出
        ''')
        choice = input('请输入功能编号:').strip()
        if choice == 'q':
            break
        if choice not in func_dic:
            print('输入有误,请重新输入!')
            continue
        func_dic.get(choice)()

ATM

标签:lag   main   from   enc   inf   cos   als   iphone   fun   

原文地址:https://www.cnblogs.com/xwjhyy/p/11552520.html

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