商品管理系统-作业

需求:

3、写一个商品管理的程序
功能1:添加商品
功能2:删除商品信息
功能3:修改商品信息
功能4:查看商品,输入all,查看所有商品,输入单个商品名称查看单个商品信息

商品格式存在文件中,goods.json
例子是在goods.py

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
FILE_NAME = 'goods.json'
def read_product():
    with open(FILE_NAME,'a+',encoding='utf-8') as  fr:
        fr.seek(0)
        result = fr.read()
        if result:
            return json.loads(result)
        return {}
 
def write_product(product):
    with open(FILE_NAME,'w',encoding='utf-8') as  fr:
        json.dump(product,fr,ensure_ascii=False,indent=4)
 
def get_product_name():
    for i in range(3):
        name = input("请输入商品名称:").strip()
        if name:
            return name
        else:
            print('商品名称不能为空')
    else:
        quit("错误次数过多")
 
def show():
    name = get_product_name()
    product = read_product()
    if name == 'all':
        print(product)
    elif product.get(name):
        print("商品信息是%s"%product.get(name))
    else:
        print('商品不存在!')
 
def delete():
    name = get_product_name()
    product = read_product()
    if product.get(name):
        product.pop(name)
        print("商品已经被删除")
        write_product(product)
    else:
        print('商品不存在!')
 
def check_count(count:str):
    if count.isdigit():
        if int(count)>0:
            return int(count) #1
    #None
 
def check_price(price:str):
    count = check_count(price)
    if count:
        return count
    else:
        if price.count('.')==1 and price.replace('.','').isdigit():
            return float(price) if float(price)>0 else None
 
def add():
    name = get_product_name()
    price = input("price:").strip()
    count = input("count:").strip()
    color = input("color:").strip()
    price = check_price(price)
    count = check_count(count)
    if  price and count and color :
        product = read_product()
        if product.get(name):
            print('无法添加')
        else:
            d = {"price":price,'count':count,'color':color}
            product[name] = d
            print("添加成功!")
            write_product(product)
    else:
        print("价格/数量/颜色不合法")
 
def modify():
    name = get_product_name()
    price = input("price:").strip()
    count = input("count:").strip()
    color = input("color:").strip()
    price = check_price(price)
    count = check_count(count)
    if  price and count and color :
        product = read_product()
        if product.get(name):#商品存在可以修改
            d = {"price": price, 'count': count, 'color': color}
            product[name] = d
            print("修改成功!")
            write_product(product)
        else:
            print('商品不存在!')
    else:
        print("价格/数量/颜色不合法")
 
# choice = input("请输入:1、添加2、修改、3、查看4、删除、other、退出").strip()
# if choice == "1":
#     add()
# elif choice == "2":
#     modify()
# elif choice=="3":
#     show()
# elif show()=="4":
#     delete()
# else:
#     quit()
 
choice = input("请输入:1、添加2、修改、3、查看4、删除、other、退出:").strip()
func_map = {'1':add,'2':modify,'3':show,'4':delete}
if choice in func_map:
    func_map.get(choice)()
else:
    quit("退出程序!")

  

posted @   MLing  阅读(287)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2017-05-16 Python中基本的读文件和简单数据处理
2017-05-16 上传和下载文件
2017-05-16 fiddler与Charles的区别
2017-05-16 修改请求和返回报文
2017-05-16 charles抓包--手机端
点击右上角即可分享
微信分享提示