商品仓库管理系统

1.出库
2.入库
3.增加商品
4.删除商品
5.修改商品
6.展示所有商品

不多说直接上代码:

#菜单模块
def Displaymenu():
    print("-"*35)
    print("{: >18}".format("仓库管理系统"))
    print("{: >18}".format("1.商品入库"))
    print("{: >18}".format("2.商品出库"))
    print("{: >20}".format("3.增加商品信息"))
    print("{: >20}".format("4.删除商品信息"))
    print("{: >20}".format("5.修改商品信息"))
    print("{: >20}".format("6.查看商品信息"))
    print("{: >18}".format("0.退出系统"))
    print("-"*35)

goods_information=[]  #用列表存储商品信息

#增加商品信息
def add_goods_information():
    while True:
        new_num = input('请输入商品的编号:')
        new_name = input('请输入商品的名字:')
        new_amount = input('请输入商品的数量:')
        new_cost = input('请输入商品的进价:')
        new_price = input('请输入商品的售价:')
        new_goods = {}  #创建一个字典,存储一个商品的信息,最后把字典存入列表
        new_goods['num']=new_num
        new_goods['name']=new_name
        new_goods['amount']=new_amount
        new_goods['cost']=new_cost
        new_goods['price']=new_price
        goods_information.append(new_goods)

        a = input('是否继续增加商品?(输入1继续,输入0结束)')
        if a == '1':
            continue
        else:
            print('保存成功!')
            show_goods_information()
            break


#遍历列表,寻找到指定商品的编号
def find_goods_information(find_num):
    find_goods=-1                                     #先定义为-1,找不到则return这个-1
    for a in goods_information:                    #遍历列表
        if a['num'] == find_num:                   #找到指定商品的编号
            find_goods=goods_information.index(a)  #把找到的商品在列表的地址赋值给find_goods
    return find_goods                                 #返回值

#删除商品信息
def del_goods_information():
    del_num = input("请输入要删除的商品编号:")
    goods_addresss = find_goods_information(del_num)  #从遍历列表模块,返回要删除的商品的地址
    if goods_addresss == -1:                          #没找到的话,返回的值是-1
        print('商品不存在!')
    else:
        del goods_information[goods_addresss]         #用del直接删除掉找到的商品
        print('删除成功!')
        show_goods_information()

#修改商品信息
def modify_goods_information():
    modify_num  = input("请输入要修改的商品编号:")
    goods_address = find_goods_information(modify_num)  #从遍历列表模块,返回要修改的商品的地址
    if goods_address == -1:
        print('商品不存在!')
    else:
        new_name = input("请输入新的名字:")
        new_cost = input("请输入新的进价")
        new_price = input("请输入新的售价")
        goods_information[goods_address]['name'] = new_name    #把新输入的信息一个个重新保存到字典里
        goods_information[goods_address]['cost'] = new_cost
        goods_information[goods_address]['price'] = new_price
        print('修改成功!')
        show_goods_information()

#展示所有商品信息
def show_goods_information():
    print("-"*30)
    print("{: >15}".format("商品总览"))
    print("-"*30)
    print("{: >20}".format("编号   名字   数量   进价   售价"))
    for a in goods_information:
        print("{: >20}".format("%s    %s    %s    %s    %s")%(a['num'],a['name'],a['amount'],a['cost'],a['price']))

#出库
def out():
  while True:  #若商品不存在,执行continue
      out_num = input('请输入需要出库的商品编号:')
      goods_address = find_goods_information(out_num)  # 从遍历列表模块,返回要修改的商品的地址
      if goods_address == -1:
        print('商品不存在!')
        continue
      else:
            a = eval(input('请输入出库数量:'))
            print()
            # 判断是否有足够库存,否则报错,这里保存的时候都是字符串,先转换为int型再比较
            if int(goods_information[goods_address]['amount']) >= a:
                b = int(goods_information[goods_address]['amount'])  #赋值给b
                goods_information[goods_address]['amount'] = b-a  # b减a保存进列表内的字典
            else:
                print('商品仓库数量不足!')


#入库
def storage():

    while True:  # 若商品不存在,执行continue
        ru_num = input('请输入需要出库的商品编号:')
        goods_address = find_goods_information(ru_num)  # 从遍历列表模块,返回要修改的商品的地址
        if goods_address == -1:
            print('商品不存在!')
            continue
        else:
            a = eval(input('请输入出库数量:'))
            print()
            b = int(goods_information[goods_address]['amount'])  # 赋值给b
            goods_information[goods_address]['amount'] = b + a  # b加a保存进列表内的字典


#主函数
def main():
    while True:
        Displaymenu()
        a = input('请输入功能对应的数字:')
        if a == '1':
            storage()
        elif a == '2':
            out()
        elif a == '3':
            add_goods_information()
        elif a == '4':
            del_goods_information()
        elif a == '5':
            modify_goods_information()
        elif a == '6':
            show_goods_information()
        elif a == '0':
            quit_confirm = input('输入end确定退出程序')
            if quit_confirm == 'end':
                break
        else:
            print('输入有误,请重新输入:')

if __name__ == '__main__':   #作用请在csdn内搜索
    main()

用到的函数很少,不知道作用的,自行搜索,抄作业很简单,理解最重要

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐