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('输入你的选择\n:'
'1添加\n'
'2删除\n'
'3修改\n'
'4查看\n'
)
if choice in func_map:
func_map[choice]()
else:
print('请输入正确选项')

管理商品demo的更多相关文章

  1. 基于ZKWeb + Angular 4.0的开源管理后台Demo

    这是一套基于ZKWeb网页框架和Angular 4.0编写的开源管理后台Demo,实现了前后端分离和模块化开发, 地址是: https://github.com/zkweb-framework/ZKW ...

  2. 微信公众号开发系统入门教程(公众号注册、开发环境搭建、access_token管理、Demo实现、natapp外网穿透)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a1786223749/article/ ...

  3. 1.0 多控制器管理(附:Demo)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”       控制器 :   一个iOS的app很少只由一个控制器组成,除非这个app极其简 ...

  4. C# Process类_进程管理器Demo

    Process用于管理计算机的进程,下面给出一个C#进程管理器的DEMO. namespace ProcessManager { public partial class Form1 : Form { ...

  5. 【vue】MongoDB+Nodejs+express+Vue后台管理项目Demo

    ¶项目分析 一个完整的网站服务架构,包括:   1.web frame ---这里应用express框架   2.web server ---这里应用nodejs   3.Database ---这里 ...

  6. Spring事务管理的demo

    事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败,最为典型的就是银行转账的案例: A要向B转账,现在A,B各自账户中有1000元,A要给B转200元,那么这个转账就必须保证是一个事务,防止中 ...

  7. PHP.38-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除、修改

    商品分类删除 1.删除商品时,根据商品id删除扩展分类表数据 商品扩展分类修改 1.在控制器GoodsController.class.php/edit()中根据商品id取出对应的所有扩展分类 2.在 ...

  8. 使用java自带线程池管理器demo

    1.程序入口:DabianTest package com.lbh.myThreadPool.present; import java.util.concurrent.ExecutorService; ...

  9. 三级设置页面管理测试demo

    #include "PhoneBookWindow.h"#include "xWindow/xWindow.h"#include "hardwareD ...

随机推荐

  1. Mybatis(二)总结

    1. 输入映射(就是映射文件中可以传入哪些参数类型) 1)基本类型 2)pojo类型 3)Vo类型2. 输出映射(返回的结果集可以有哪些类型) 1)基本类型 2)pojo类型 3)List类型3. 动 ...

  2. 【Checkio Exercise】Robot Sort

    Robot Sort All of the refined ingots should be sorted by size in each lot while passing by on a conv ...

  3. fabric 更详尽的用法

    项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上. 修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A,B,C,D, ...

  4. Uncertainty

    I did'nt know where i was supposed to be, until i'd actually arrived.

  5. VS2013的x86汇编语言开发环境配置

    转载:https://blog.csdn.net/infoworld/article/details/45085415 转载:https://blog.csdn.net/u014792304/arti ...

  6. 要想成为前端大神,那些你不得不知晓的web前端命名规范。

    一.Web语义化 1.1 H5的语义化 对于经验资深的前端er,在给web布局时,相信都会很注重标签和命名的规范.尤其是随着html5的普及发展,更是把web前端语义化推向一个新的台阶上.比如html ...

  7. 获取动态代理生成的.class文件

    生成代理类,并写入硬盘:配置系统属性sun.misc.ProxyGenerator.saveGeneratedFile为true,代理类生成时将自动将生成的代理类写入硬盘 ? 1 2 3 4 5 6 ...

  8. centos7 安装xinetd,telnet

    安装方式:yum [root@master ~]# yum list |grep telnettelnet-server.x86_64                    1:0.17-59.el7 ...

  9. .NET Core 如何上传文件及处理大文件上传

    当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发ASP.NET Core MVC的Controller中的A ...

  10. 2017 Russian Code Cup (RCC 17), Final Round

    2017 Russian Code Cup (RCC 17), Final Round A Set Theory 思路:原题转换一下就是找一个b数组,使得b数组任意两个数的差值都和a数组任意两个数的差 ...