python3 - 商品管理的程序,商品信息都存在一个json串里面

商品管理的程序,商品信息都存在一个json串里面
1、查询商品信息 #校验商品是否存在
2、新增商品 # #校验商品是否存在 #校验价格是否合法
3、修改商品信息 ##校验商品是否存在
if chice =="1":
query_goods()
elif choice = ="2":
add_goods()

goods.json:

{
"iphonex":{
"num":100,
"price":1999.98,
"color":"red"
},
"car":{
"num":100,
"price":9999999,
"color":"black"
}
}

import json
FILE_NAME="goods.json"
def check_goods(name,newGood=None):
    with open(FILE_NAME, "r+", encoding="utf-8") as f:
        goods_dic = json.load(f)
    if newGood:
        goods_dic[name]=newGood.get(name) #don't use setdefault function, because cann't update the origibal valye
       
print(goods_dic)
        with open(FILE_NAME,"w+",encoding="utf-8") as fw:
            json.dump(goods_dic,fw,ensure_ascii=False,indent=4)
    else:
        #with open(FILE_NAME,"r+",encoding="utf-8") as f:
            #goods_dic=json.load(f)
       
if goods_dic.get(name):
            print(name + " 存在!")
            return goods_dic
        else:
            print(name +" 不存在!")
            return False

def check_price(price):
    if price.count(".")==0 and price.isdigit() and int(price)>=0:
        print("price is correct int!")
        return True
    elif price.count(".")==1:
        list=price.split(".")
        left = list[0]
        right=list[1]
        if left.isdigit() and right.isdigit():
            print("price is correct float!")
            return True
    else:
        print("price is illegal!")
        return False

choice = input("please input your choice:"
               " 1. check goods info."
               " 2. add goods info"
               " 3. edit goods info")
if choice =='1':
    goods_name = input("please input the goods name:").strip()
    good_dis=check_goods(goods_name)
    if good_dis:
        print(good_dis.get(goods_name))
elif choice=='2':
    name_add=input("please input the goods name you want to add:").strip()
    if check_goods(name_add):
        print("this goods name exist!")
    else:
        good_add_dic={}
        num = int(input("please input the number:"))
        color=input("please input the color:")
        price = input("please input the price:").strip()
        if check_price(price):
            info_add = {"num": num, "price": price, "color:": color}
            good_add_dic.setdefault(name_add,info_add)
            print("成功添加商品!商品信息为:"+str(good_add_dic))
            check_goods(name_add,good_add_dic)
        else:
            print("价格输入不正确!")
elif choice =="3":
    name_edit=input("please input the goods name you want to edit:")
    if check_goods(name_edit):
        goods_edit_dic=check_goods(name_edit) # get the goods dic
       
good_info=goods_edit_dic.get(name_edit) # get the edited good info
       
changeInfo=input("please choose the info you want to change:"
                         "1. num"
                         "2. color"
                         "3. price").strip()
        if changeInfo =='1':
            num1 =

转载于:https://www.cnblogs.com/yuanchunli/p/8872747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值