溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)

發(fā)布時間:2022-02-28 09:10:39 來源:億速云 閱讀:545 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)文章都會有所收獲,下面我們一起來看看吧。

題目要求:

請?jiān)O(shè)計一個商品管理系統(tǒng),程序要求根據(jù)用戶輸入的選項(xiàng)進(jìn)入相應(yīng)的功能模塊,并顯示相應(yīng)的提示信息。若用戶輸入其他選項(xiàng)時,提示“輸入有誤”。
程序的功能模塊有:

1、添加

程序讀取源文件路徑下的goodinformation.txt,若沒有就創(chuàng)建。當(dāng)添加的商品已存在時,顯示“該商品已存在”

2、查詢

根據(jù)用戶輸入的商品名稱,查詢商品信息,并輸出。當(dāng)查詢的商品不存在時,顯示“商品庫中無該商品”

3、統(tǒng)計

根據(jù)goodinformation.xt中的數(shù)據(jù),生成統(tǒng)計數(shù)據(jù),包括(商品種類,商品總數(shù),銷售總額,商品均價),并寫入到goodtotal.txt中去

4、退出

退出運(yùn)行。

提示:

字典、列表、函數(shù)、文件的打開和讀取等

要求如下:

1.如果具體功能代碼無法實(shí)現(xiàn),那么就把程序架構(gòu)完成,比如羅列需要的函數(shù),并且添加適當(dāng)?shù)淖⑨尅?br/>2.添加測試用例,即根據(jù)題目功能,可以寫明如果輸入為XXX,輸出 應(yīng)該為YYY。

代碼入下,為了增強(qiáng)程序的健壯性,使用了re模塊防止輸入輸出出現(xiàn)逗號標(biāo)點(diǎn)符號不識別的情況。

# 假設(shè)文件中的數(shù)據(jù)按照這樣來寫:藍(lán)莓,10,20(一行的數(shù)據(jù))
import re


def read_goods(fn):
    fdic = {}
    with open(fn, "r") as f:
        for i in f.readlines():
            goodl = list(re.split(r"[,,]", i))
            goodl = [x.strip() for x in goodl]
            fdic[goodl[0]] = goodl
    return fdic


def add_goods(fdic, fn):
    goods_list = list(re.split('[,,]', input("請輸入商品信息(商品名稱,單價,數(shù)量),直接輸入回車退出:")))
    if goods_list[0] == "":
        return 0
    elif len(goods_list) == 3:
        try:
            a = eval(goods_list[1]) + eval(goods_list[2])  # 防止輸入價格時輸入非數(shù)字符號
        except NameError:
            print("價格請輸入數(shù)字符號")
        else:
            if goods_list[0] in fdic.keys():
                print("該商品已存在")
            else:
                fdic[goods_list[0]] = goods_list
                with open(fn, "a") as f:
                    f.writelines(','.join(goods_list))
                    f.write("\n")
            add_goods(fdic, fn)
    else:
        if goods_list[0] in fdic.keys():
            print("該商品已存在")
        else:
            print("輸入錯誤請重新輸入")
        add_goods(fdic, fn)


def find_goods(fdic):
    while True:
        good_name = input("請輸入查詢商品名稱,直接輸入回車退出:")
        if good_name == "":
            break
        else:
            for k in fdic.keys():
                if k == good_name:
                    print("{},{:.2f}".format(k, eval(fdic[k][2])))
                    find_goods(fdic)
                    return 0
            print("商品庫中無該商品")


def count(fdic, fn):
    type_amount = len(fdic)
    good_amount, total_sales, sum_price, ave_price = 0, 0, 0, 0
    for v in fdic.values():
        good_amount += eval(v[2])
        total_sales += eval(v[2]) * eval(v[1])
        sum_price += eval(v[1])
    try:
        ave_price = sum_price / type_amount
        with open(fn, "w") as f:
            text = "商品種類: " + str(type_amount) + \
                   "\n商品總數(shù): " + str(good_amount) + \
                   "\n銷售總額: " + str(total_sales) + \
                   "\n商品均價: " + str(ave_price)
            f.write(text)
    except ZeroDivisionError:
        with open(fn, "w+") as f:
            f.seek(0)
            text = "商品種類: 0\n商品總數(shù): 0 \n銷售總額: 0\n商品均價: 0"
            f.write(text)

    return print("商品統(tǒng)計數(shù)據(jù)已寫入統(tǒng)計文件")


def main():
    goodinfo = "C:\\Users\\13935\\Desktop\\goodinformation.txt"  # 換成自己的路徑
    goodtotal = "C:\\Users\\13935\\Desktop\\goodtotle.txt"  # 換成自己的路徑
    goods_dict = read_goods(goodinfo)
    print("1. 查詢商品\n2. 添加商品\n3. 統(tǒng)計商品\n4. 退出\n")
    while True:
        try:
            info = eval(input('請輸入您的選擇:'))
            if info == 1:
                find_goods(goods_dict)
            elif info == 2:
                add_goods(goods_dict, goodinfo)
            elif info == 3:
                count(goods_dict, goodtotal)
            elif info == 4:
                break
            else:
                print("輸入錯誤請重新輸入")
        except NameError:
            print("輸入錯誤請重新輸入")
    return 0


main()

運(yùn)行結(jié)果:

怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)

生成兩個文件:

怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)

本要求截圖入下

怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)

怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)

怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)

關(guān)于“怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“怎么用python實(shí)現(xiàn)庫存商品管理系統(tǒng)”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI