使用 Python 中的 JSON 進行庫存管理


使用 JSON 進行庫存管理:簡介

任何處理和跟蹤物品或產品的公司都需要有效的庫存管理。為了確保有足夠的商品供應給客戶並避免庫存過剩或缺貨,它包括監控和控制商品進出流程的過程。在本教程中,我們將探討在 Python 中使用 JSON 進行庫存管理。

使用 JSON 進行庫存管理

定義

JSON,或 JavaScript 物件表示法,是一種簡單的資料交換格式,對人和機器都易於閱讀和編寫。它通常用於在多個應用程式和計算機語言之間共享資料。JSON 是一種文字格式,使用鍵值對,其語法類似於 Python 字典。

語法

{"Key 1":"value 1",
"Key 2":"value 2",
.
.
"Key n":"value n"}

JSON 的語法易於閱讀和理解。它由包含在花括號中的鍵值對組成。逗號用於分隔每個鍵值對。值可以是任何有效的 JSON 資料型別,包括字串、數字、布林值、陣列或另一個 JSON 物件。鍵始終是字串。

演算法

  • 步驟 1 - 為庫存中的物件建立一個 JSON 物件。

  • 步驟 2 - 開啟檔案或 API 並讀取 JSON 資料。

  • 步驟 3 - 根據需要調整庫存資料(例如,新增新專案和更新數量)。

  • 步驟 4 - 將更新後的庫存資料寫回檔案或 API。

  • 步驟 5 - 根據需要重複此過程。

方法

  • 方法 1 - 使用 Python 的內建 JSON 模組進行 JSON 庫存管理

  • 方法 2 - 使用自定義 Inventory 類進行 JSON 庫存管理

方法 1:使用 Python 的內建 JSON 模組進行 JSON 庫存管理

在這種方法中,我們將使用 Python 的內建 JSON 模組讀取和寫入 JSON 檔案中的資料。我們將構建一個簡單的庫存管理系統,以新增、刪除和檢查庫存中的物件。以下是關鍵 -

示例

import json

# Load the inventory data from the file
with open('inventory.json', 'r') as f:
   inventory = json.load(f)

# Function to add a new item to the inventory
def add_item():
   item_name = input('Enter item name: ')
   item_price = int(input('Enter item price: '))
   item_quantity = int(input('Enter item quantity: '))
   inventory[item_name] = {
      'price': item_price,
      'quantity': item_quantity
   }
   save_inventory()

# Function to remove an item from the inventory
def remove_item():
   item_name = input('Enter item name: ')
   if item_name in inventory:
      del inventory[item_name]
      save_inventory()
   else:
      print('Item not found in inventory')

# Function to display the inventory
def display_inventory():
   print('{:<15} {:<10} {:<10}'.format('Item', 'Price', 'Quantity'))
   for item_name, item_data in inventory.items():
      print('{:<15} {:<10} {:<10}'.format(item_name, item_data['price'], item_data['quantity']))

# Function to save the inventory to the file
def save_inventory():
   with open('inventory.json', 'w') as f:
      json.dump(inventory, f)

# Main program loop
while True:
   print('1. Add item')
   print('2. Remove item')
   print('3. Display inventory')
   print('4. Exit')
   choice = int(input('Enter choice: '))
   if choice == 1:
      add_item()
   elif choice == 2:
      remove_item()
   elif choice == 3:
      display_inventory()
   elif choice == 4:
      break
   else:
      print('Invalid choice')

輸出

1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 1
Enter item name: Apple
Enter item price: 10
Enter item quantity: 20
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 1
Enter item name: Banana
Enter item price: 20
Enter item quantity: 30
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 3
Item           Price      Quantity  
Apple          10         20        
Banana         20         30        
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 2
Enter item name: Apple
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 3
Item           Price      Quantity  
Banana         20         30        
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 4

方法 2:使用自定義 Inventory 類進行 JSON 庫存管理

在這種方法中,我們將構建一個特殊的 Inventory 類,它將包含庫存資料並提供新增、刪除和檢視庫存專案的方法。Python 語言的內建 JSON 模組將用於讀取和寫入 JSON 檔案中的資料。以下是關鍵 -

示例

import json

class Inventory:
def _init_(self, filename):
   self.filename = filename
   # Load the inventory data from the file
   with open(filename, 'r') as f:
      self.data = json.load(f)

def add_item(self, name, price, quantity):
   self.data[name] = {'price': price, 'quantity': quantity}
   self.save()

def remove_item(self, name):
   if name in self.data:
      del self.data[name]
      self.save()
   else:
      print('Item not found in inventory')

def display_inventory(self):
   print('{:<15} {:<10} {:<10}'.format('Item', 'Price', 'Quantity'))
   for item_name, item_data in self.data.items():
      print('{:<15} {:<10} {:<10}'.format(item_name, item_data['price'], item_data['quantity']))

def save(self):
   with open(self.filename, 'w') as f:
      json.dump(self.data, f)
#Create an inventory object
inventory = Inventory('inventory.json')

#Main program loop
while True:
print('1. Add item')
print('2. Remove item')
print('3. Display inventory')
print('4. Exit')
choice = int(input('Enter choice: '))
if choice == 1:
name = input('Enter item name: ')
price = int(input('Enter item price: '))
quantity = int(input('Enter item quantity: '))
inventory.add_item(name, price, quantity)
elif choice == 2:
name = input('Enter item name: ')
inventory.remove_item(name)
elif choice == 3:
inventory.display_inventory()
elif choice == 4:
break
else:
print('Invalid choice')

輸出

1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 1
Enter item name: Apple
Enter item price: 10
Enter item quantity: 20
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 1
Enter item name: Banana
Enter item price: 20
Enter item quantity: 30
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 3
Item           Price      Quantity  
Apple          10         20        
Banana         20         30        
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 2
Enter item name: Apple
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 3
Item           Price      Quantity  
Banana         20         30        
1. Add item
2. Remove item
3. Display inventory
4. Exit
Enter choice: 4

結論

總之,庫存控制是管理公司的一個重要組成部分。可以使用 Python 和 JSON 建立一個簡單而有效的庫存管理系統,該系統可以輕鬆地儲存和處理庫存資料。在本例中,我們演示瞭如何使用兩種不同的 Python 實現策略進行基於 JSON 的庫存管理。第一種方法透過將庫存資料寫入字典來建立 JSON 檔案。雖然這種方法簡單易用,但它存在一些重大缺點。例如,它不支援跟蹤隨時間推移的庫存變化或支援多個庫存。第二種方法使用類來包含庫存資料,並具有新增、刪除和顯示專案的方法。

由於這種方法比第一種方法更靈活和可擴充套件,因此更容易擴充套件以處理多個庫存和維護庫存歷史記錄。策略的選擇將取決於庫存管理系統的特定需求。兩種方法都有優點和缺點。無論採用哪種方法,JSON 和 Python 都提供了強大的組合來維護簡單高效的庫存資料。

更新於: 2023年10月12日

387 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告