python基础篇 12-函数+文件读写+json练习作业

需求:

写一个管理商品的程序,商品文件格式在a.json里面
提供商品的增删改查功能
choice = input('请输入你的选择:1、查看商品 2、新增商品 3、修改商品 4、删除商品')
#1、查看商品,输入商品名称,print单个商品的信息,价格、数量,输入all,查看所有商品
#2、新增商品,输入商品名称、数量、价格,数量是大于0的整数,价格必须是大于0的数值,
#如果商品存在,无法添加
#3、修改商品,输入商品名称、数量、价格,商品存在才可以修改 数量是大于0的整数,价格必须是大于0的数值,
#4、输入商品名称,如果存在,删除

import json
def read_file(file_name):
    with open(file_name,encoding='utf-8') as fr:
        return json.load(fr)        #返回是字典
def write_file(file_name,content:dict):
    with open(file_name,'w',encoding='utf-8') as fw:
        json.dump(content,fw,ensure_ascii=False,indent=4)       #入参是字典 写入文件是json串

def valid_int(count):
    if str(count).isdigit():
        if int(count) > 0:
            return True
def valid_price(price):
    try:
        price = float(price)   #确保是小数
        if price and not str(price).startswith('-'):   # 去掉0.0 和负小数 保留正小数
            return True
    except Exception as e:
        print(f"{price}必须是大于0的数")

def valid_name(name,product_info:dict):
    return True if name in product_info else False

def get_product_detail(file_name):
    name = input("请输入产品名称:").strip()
    if name:
        product_info = read_file(file_name)
        if name == 'all':
            print(product_info)
        elif valid_name(name,product_info):
            print(product_info.get(name))
        else:
            print(f"{name}产品不存在!")
    else:
        print(f"{name}不能为空!")
def add_product(file_name):
    name = input("请输入产品名称:").strip()
    count = input("请输入产品数量:")
    price = input("请输入产品价格:")
    if name and count and price:
        product_info = read_file(file_name)

        if not valid_name(name,product_info):
            if not valid_int(count):
                print(f"{name}产品的数量{count}必须为大于正整数!")
            elif not valid_price(price):
                print(f"{name}产品的价格{price}必须为大于0的数!")
            else:
                add_info={name:{'count':count,'price':price}}
                product_info.update(add_info)
                write_file(file_name,product_info)
                print(f"添加产品信息:{add_info}成功!")
        else:
            print(f"{name}产品已经存在!")
    else:
        print(f"{name} 、{count}、{price}都不能为空!")
def modify_product(file_name):
    name = input("请输入产品名称:").strip()
    count = input("请输入产品数量:")
    price = input("请输入产品价格:")
    if name and count and price:
        product_info = read_file(file_name)
        if valid_name(name,product_info):
            if not valid_int(count):
                print(f"{name}产品的数量{count}必须为大于正整数!")
            elif not valid_price(price):
                print(f"{name}产品的价格{price}必须为大于0的数!")
            else:
                update_info={name:{'count':count,'price':price}}
                product_info.update(update_info)
                write_file(file_name,product_info)
                print(f"更新产品信息:{product_info}成功!")
        else:
            print(f"{name}产品不存在!")
    else:
        print(f"{name} 、{count}、{price}都不能为空!")
def delete_product(file_name):
    name = input("请输入产品名称:").strip()
    if name:
        product_info = read_file(file_name)
        if valid_name(name,product_info):
            product_info.pop(name)
            write_file(file_name,product_info)
            print(f"删除产品{name}成功!")
        else:
            print(f"{name}不存在!")
    else:
        print(f"{name}不能为空!")
file_name
= r'a.json' choice = input('1、查看商品 2、新增 3、修改 4、删除') func_map = {'1':get_product_detail,'2':add_product,'3':modify_product,'4':delete_product} if choice in func_map: func_map[choice](file_name=file_name) else: print("请输入正确的选项!")

 

posted @ 2021-12-26 20:48  捞铁  Views(73)  Comments(0Edit  收藏  举报