day18作业

ATM(1.0)

from db import db_handler
import os


def admin_interface(name):
    user_file_path = r'D:\py\ATM\db\%s.json' % name
    if os.path.exists(user_file_path):
        to_dic = db_handler.select(name)
        to_dic['locked'] = True
        db_handler.save(to_dic)
        return True, '已成功冻结该用户'
    else:
        return False, '该用户不存在'


def checking(user_name):
    user_file_path = r'D:\py\ATM\db\%s.json' % user_name
    if os.path.exists(user_file_path):
        user_dic = db_handler.select(user_name)
        if user_dic['locked'] == True:
            print('该用户已被冻结'.center(20, '*'))
            return True
admin.py
import json
import os
from db import db_handler
from lib import common


def check_bal_interface():
    user_name = common.name['name']
    user_dict = db_handler.select(user_name)
    return '您的余额为:%s' % user_dict['bal']


def withdraw_interface(money):
    user_name = common.name['name']
    user_dict = db_handler.select(user_name)
    money2 = money * 1.05
    if user_dict['bal'] >= money2:
        user_dict['bal'] = user_dict['bal'] - money2
        db_handler.save(user_dict)
        return True, '取款 %s 成功' % money
    return False, '余额不足无法取款'


def repay_interface(money):
    user_name = common.name['name']
    user_dict = db_handler.select(user_name)
    user_dict['bal'] += money
    db_handler.save(user_dict)
    return '还款 %s 成功' % money


def transfer_interface(name, money):
    user_name = common.name['name']
    if name == user_name:
        return False, '用户不能是自己'
    user_dic = db_handler.select(user_name)
    to_dic = db_handler.select(name)
    user_file_path = r'D:\py\ATM\db\%s.json' % name
    if os.path.exists(user_file_path):
        user_dic['bal'] = user_dic['bal'] - money
        to_dic['bal'] += money
        db_handler.save(user_dic)
        db_handler.save(to_dic)
        return True, '向用户 %s 转账 %s 成功' % (name, money)
    return False, '用户不存在,请重新输入'


def check_flow_interface():
    user_name = common.name['name']
    db_handler.bank_flow_select(user_name)
    return ''
bank.py
from lib import common
from db import db_handler

shopping_dic = {
    '1': ['商品A', 10],
    '2': ['商品B', 100],
    '3': ['商品C', 101],
    '4': ['商品D', 1050],
    '5': ['商品E', 140],
    '6': ['商品F', 20000],
}


def shopping_interface(choice):
    if choice in shopping_dic:
        shop_num = input('请输入购买数量').strip()
        shop_num = int(shop_num)
        shop_name = shopping_dic[choice][0]
        name = common.name['name']
        db_handler.shopping_save(name, shop_name, shop_num)
        return True, '加入购物车成功!'
    else:
        return False, '暂无此商品,请重新输入'


def check_shop_interface():
    user_name = common.name['name']
    user_dic = db_handler.select(user_name)
    return '您的购物车为:\n%s' % user_dic['shop_car']
shop.py
import os
import json
from db import db_handler
from lib import common


def register_interface(name, password):
    user_dic = db_handler.select(name)
    if user_dic:
        return False, '用户已存在,请重新输入'
    user_info = {'name': name, 'password': password, 'bal': 15000, 'shop_car': [], 'bank_flow': [],
                 'locked': False}
    db_handler.save(user_info)
    return True, '注册成功'


def login_interface(user_name, user_password):
    user_dic = db_handler.select(user_name)
    if user_dic:
        if user_dic['name'] == user_name and user_dic['password'] == user_password:
            common.name['name'] = user_name
            return True, '登录成功'
        else:
            return False, '密码错误'
    else:
        return False, '用户名不存在'
user.py
import os
import sys

path = os.path.dirname(__file__)

sys.path.append(path)

from core import src

if __name__ == '__main__':
    src.run()
start.py
import json
import os
from lib import common
from interface import user
from interface import bank
from interface import shop
from interface import admin

def register():
    print('注册功能'.center(20, '*'))
    while True:
        user_name = input('请输入用户名:').strip()
        user_pwd = input('请输入密码:').strip()
        re_user_pwd = input('请再次输入密码:').strip()
        if user_pwd == re_user_pwd:
            flag, msg = user.register_interface(user_name, user_pwd)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('两次密码不一致,请重新输入')


def login():
    print('登录功能'.center(20, '*'))
    while True:
        user_name = input('请输入用户名:').strip()
        flag = admin.checking(user_name)
        if flag:
            break
        else:
            user_pwd = input('请输入密码:').strip()
            flag, msg = user.login_interface(user_name, user_pwd)
            if flag:
                print(msg)
                break
            else:
                print(msg)
@common.deco
def check_bal():
    print('查看余额'.center(20, '*'))
    msg = bank.check_bal_interface()
    print(msg)


@common.deco
@common.otter('转账')
def transfer():
    while True:
        print('转账功能'.center(20, '*'))
        t_name = input('请输入转账对象:').strip()
        t_money = input('请输入转账金额:').strip()
        t_money = int(t_money)
        flag, msg = bank.transfer_interface(t_name, t_money)
        if flag:
            print(msg)
            break
        else:
            print(msg)
         


@common.deco
@common.otter('还款')
def repay():
    while True:
        print('还款功能'.center(20, '*'))
        money = input('请输入取款金额:').strip()
        if not money.isdigit():
            print('请输入数字')
            continue
        money = int(money)
        msg = bank.repay_interface(money)
        print(msg)
        break


@common.deco
@common.otter('取款(含百分之5手续费)')
def withdraw():
    while True:
        print('取款功能'.center(20, '*'))
        money = input('请输入取款金额:').strip()
        if not money.isdigit():
            print('请输入数字')
            continue
        money = int(money)
        flag, msg = bank.withdraw_interface(money)
        if flag:
            print(msg)
            break
        else:
            print(msg)
            break


@common.deco
def check_flow():
    print('查看流水'.center(20, '*'))
    msg = bank.check_flow_interface()
    print(msg)

@common.deco
@common.otter('购物')
def shopping():
    while True:
        print('购物功能'.center(20, '*'))
        print('0   退出')
        for k in shop.shopping_dic:
            print('商品编号:%s  商品名:%s  商品价格:%s' % (k, shop.shopping_dic[k][0], shop.shopping_dic[k][1]))
        choice = input('请输入您想要购买的商品编号:').strip()
        if choice == '0':
            break
        else:
            flag, msg = shop.shopping_interface(choice)
            if flag:
                print(msg)
            else:
                print(msg)
@common.deco
def check_shop():
    print('查看购物车'.center(20, '*'))
    msg = shop.check_shop_interface()
    print(msg)


@common.deco
def Administration():
    print('管理'.center(20, '*'))
    t_name = input('请输入冻结对象:').strip()
    flag, msg = admin.admin_interface(t_name)
    if flag:
        print(msg)
    else:
        print(msg)

func_dic = {
    '1': [register, '注册功能'],
    '2': [login, '登录功能'],
    '3': [check_bal, '查看余额'],
    '4': [transfer, '转账功能'],
    '5': [repay, '还款功能'],
    '6': [withdraw, '取款功能'],
    '7': [check_flow, '查看流水'],
    '8': [shopping, '购物功能'],
    '9': [check_shop, '查看购物车'],
    '10': [Administration, '管理']
}


def run():
    while True:
        print('0   退出功能')
        for k in func_dic:
            print('%s   %s' % (k, func_dic[k][1]))
        choice = input('请输入您的指令编号:').strip()
        if choice == '0':
            break
        elif choice in func_dic:
            func_dic[choice][0]()
        else:
            print('未识别的指令')
src.py
import json
import os


def select(name):
    user_file_path = r'D:\py\ATM\db\%s.json' % name
    if os.path.exists(user_file_path):
        with open(user_file_path, 'r', encoding='utf-8') as f:
            user_dic = json.load(f)
            return user_dic


def save(user_dic):
    user_file_path = r'D:\py\ATM\db\%s.json' % user_dic['name']
    with open(user_file_path, 'w', encoding='utf-8') as f:
        json.dump(user_dic, f)


def login(mag):
    user_file_path = r'D:\py\ATM\log\access.log'
    with open(user_file_path, 'a', encoding='utf-8') as f:
        f.write(mag)


def bank_flow_save(msg, name):
    user_dic = select(name)
    user_dic['bank_flow'].append(msg)
    save(user_dic)


def bank_flow_select(name):
    user_dic = select(name)
    print('您的流水为:')
    for i in user_dic['bank_flow']:
        print(i, end='')


def shopping_save(name, shop_name, shop_num):
    dic = {}
    user_dic = select(name)
    shop_car = user_dic['shop_car']
    if shop_car:
        for i in shop_car:
            if shop_name in i:
                dic[shop_name] = i[shop_name] + 1
                shop_car.remove(i)
                break
            else:
                dic[shop_name] = shop_num
    else:
        dic[shop_name] = shop_num
    shop_car.append(dic)
    save(user_dic)

# shop_car = [{'b':11}, {'a':23}, {'c':33}]
# dic = {}
# for i in shop_car:
#     if 'a' in i:
#         dic['a'] = i['a'] + 1
#         print(dic)
#         shop_car.remove(i)
# shop_car.append(dic)
# print(shop_car)
db_handler.py
common.py
posted @ 2020-07-27 20:35  画堂晨起  阅读(69)  评论(0编辑  收藏  举报