zoukankan      html  css  js  c++  java
  • 管理商品demo

    1、写一个管理商品的程序
    # 1、商品存在文件里面
    # 2、添加商品的时候,商品存在的就不能添加了,数量只能是大于0的整数,价格可以是小数、整数,但是只能是大于0的
    # 商品名称
    # 商品价格
    # 商品数量
    # 3、删除的商品的时候输入商品名称,商品不存在,要提示
    # 4、修改商品的时候
    # 商品名称
    # 商品价格
    # 商品数量
    # 5、查看商品,输入商品名称,查询到商品的信息
    # choice = input('请输入你的选择:'
    # '1、添加商品'
    # '2、删除商品'
    # '3、修改商品信息'
    # '4、查看商品'
    # '5、退出'

    import json
    FILENAME='Product.json'
    def read_file():
    with open(FILENAME,encoding='utf-8')as fw:
    return json.load(fw)
    def write_file(d):
    with open(FILENAME,'w',encoding='utf-8')as fw:
    return json.dump(d,fw,indent=4,ensure_ascii=False)
    def check_int(num):
    num=str(num)
    if num.isdigit() and int(num)>0:
    return True
    def check_price(num):
    num=str(num)
    if num.count('.')==1:
    left,right=num.split('.')
    if left.isdigit() and right.isdigit() and int(right)>0:
    return True
    elif check_int(num):
    return True
    all_product=read_file()
    def add_product():
    name=input('输入商品名称:').strip()
    count=input('请输入数量:').strip()
    price=input('请输入价格').strip()
    if name and count and price:
    if not check_int(count):
    print('请输入正确的数量')
    elif not check_price(price):
    print('请输入正确的价格')
    elif name in all_product:
    print('商品已存在')
    else:
    all_product[name]={'price':price,'count':count}
    write_file(all_product)
    print('添加成功')
    else:
    print('不能为空')
    def edit_product():
    name=input('输入名称').strip()
    while True:
    if name in all_product:
    break
    elif name=='q':
    quit('退出')
    count= input('输入数量').strip()
    price= input('输入价格').strip()
    if name and count and price:
    all_product[name] = {'price': price, 'count': count}
    write_file(all_product)
    print('修改成功')
    else:
    print('不能为空')
    def del_product():
    name=input('商品名称').strip()
    if name not in all_product:
    print('商品不存在')
    else:
    all_product.pop(name)
    write_file(all_product)
    print('删除成功')
    def show_product():
    while True:
    name = input('商品名称').strip()
    if name in all_product:
    print(all_product.get(name))
    break
    elif name=='all':
    print(json.dumps(all_product,indent=4,ensure_ascii=False))

    func_map={
    '1':add_product,
    '2':del_product,
    '3':edit_product,
    '4':show_product
    }
    for i in range(4):
    choice=input('输入你的选择 :'
    '1添加 '
    '2删除 '
    '3修改 '
    '4查看 '
    )
    if choice in func_map:
    func_map[choice]()
    else:
    print('请输入正确选项')
  • 相关阅读:
    约瑟夫问题的JAVA实现(借鉴别人的代码+自己分析理解)
    ubuntu 与windows双系统记录
    重装windows7
    浏览器专题之缓存url请求
    js实现函数重载
    用python实现网上书店
    flex与bison应用实例
    前端小游戏之拼图功夫熊猫
    细说javascript的对象
    [LeetCode 1712] Ways to Split Array Into Three Subarrays
  • 原文地址:https://www.cnblogs.com/mzxs-jgm/p/10745565.html
Copyright © 2011-2022 走看看