鱼C论坛

 找回密码
 立即注册
查看: 2575|回复: 0

[作品展示] 商品管理器

[复制链接]
发表于 2020-11-11 18:53:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
直接上代码 ^  -   ^
  1. #1、添加
  2.     #1、商品名称
  3.         #1、要从文件里面把所有的商品读出来
  4.     #2、价格
  5.         #1、写一个方法判断是否为合理的价格
  6.     #3、数量
  7.         #整数
  8.    # product = {
  9.    #      "爱疯差":{
  10.    #        "price":999.98,
  11.    #        "count":5
  12.    #      },
  13.    #    "car":{
  14.    #      "price":32423432,
  15.    #      "count":10
  16.    #    }
  17.    #  }
  18.    # product['mac'] = {"price":9999,"count":5}

  19.     # write(product)
  20.     # 写入文件,最新的商品写进去
  21. #2、删除
  22.     # 1、商品名称
  23.         # 1、要从文件里面把所有的商品读出来
  24. # product = {
  25. #     "爱疯差": {
  26. #         "price": 999.98,
  27. #         "count": 5
  28. #     },
  29. #
  30. # }
  31. # product.pop('car')

  32. #3、查询
  33.     # 1、要从文件里面把所有的商品读出来

  34. FILENAME = 'product.json'
  35. import json
  36. import os
  37. while True:
  38.     def get_product():
  39.         with open(FILENAME,'a+',encoding='utf-8') as fr:
  40.             fr.seek(0)
  41.             content = fr.read()
  42.             if content:
  43.                 res = json.loads(content)
  44.             else:
  45.                 res = {}
  46.         return res

  47.     def is_price(s):
  48.         s=str(s)
  49.         if s.count('.')==1:
  50.             left,right = s.split('.')
  51.             if left.isdigit() and right.isdigit():
  52.                 print('正小数')
  53.                 return float(s)
  54.         elif s.isdigit():
  55.             if int(s)>0:
  56.                 print('大于0的整数')
  57.                 return int(s)
  58.         return False

  59.     def is_count(s):
  60.         if s.isdigit():
  61.             if int(s)>0:
  62.                 return int(s)

  63.     def write_product(product_dic):
  64.         with open(FILENAME,'w',encoding='utf-8') as fw:
  65.             json.dump(product_dic,fw,ensure_ascii=False,indent=4)

  66.     def add():
  67.         all_products = get_product()
  68.         pname = input('请输入产品名称:').strip()
  69.         price = input('请输入产品价格:').strip()
  70.         count = input('请输入产品数量:').strip()
  71.         if not pname or not price or not count:#为空的时候干啥
  72.             print('不能为空!')
  73.         elif pname in all_products:
  74.             print('商品已经存在')
  75.         elif not is_price(price):
  76.             print('价格不合法,只能是大于0的数值')
  77.         elif not is_count(count):
  78.             print('数量不合法!')
  79.         else:
  80.             all_products[pname] = {"price": float(price), "count": int(count)}
  81.             write_product(all_products)
  82.             print('添加商品成功')
  83.             return
  84.         return add()



  85.         # if pname and price and count: #不为空的时候,我干啥。。

  86.     def delete():
  87.         all_products = get_product()
  88.         pname = input('请输入产品名称:').strip()
  89.         if not pname :#为空的时候干啥
  90.             print('不能为空!')
  91.         elif pname not in all_products:
  92.             print('商品不存在')
  93.         else:
  94.             all_products.pop(pname)
  95.             write_product(all_products)
  96.             print('删除商品成功')
  97.             return
  98.         return delete()

  99.     def show():
  100.         all_products = get_product()
  101.         if all_products:
  102.             print(all_products)
  103.         else:
  104.             print('暂时还没有商品!')


  105.     choice = input('1、添加商品\n'
  106.                    '2、删除商品\n'
  107.                    '3、显示已有的商品 \n'
  108.                    '4、退出 \n')

  109.     func_map = {"1":add,"2":delete,"3":show,"4":quit}
  110.     if choice in func_map:
  111.         func_map[choice]()
  112.     else:
  113.         print('输入有误!')


  114. # if choice =="1":
  115. #     add()
  116. # elif choice=="2":
  117. #     delete()
  118. # elif choice=="3":
  119. #     show()
  120. # elif choice=="4":
  121. #     quit("程序退出")
  122. # else:
  123. #     print('输入错误!')

  124. # def a():
  125. #     print('asdfdfs')
  126. #
  127. # b = a
  128. # b()
  129. #函数即变量

复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 12:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表