Python 配件调拨代码实例 - 库存管理系统
以下是配件调拨的 Python 代码实例,该代码实现了一个简单的库存管理系统,包括调拨、入库、出库等功能:
class Inventory:
def __init__(self):
self.stock = {}
def add_item(self, item, quantity):
if item in self.stock:
self.stock[item] += quantity
else:
self.stock[item] = quantity
def remove_item(self, item, quantity):
if item in self.stock:
if self.stock[item] >= quantity:
self.stock[item] -= quantity
else:
print(f'{item} inventory not enough')
else:
print(f'{item} not found in inventory')
def transfer_item(self, item, quantity, from_location, to_location):
if item in self.stock:
if from_location in self.stock[item]:
if self.stock[item][from_location] >= quantity:
if to_location in self.stock[item]:
self.stock[item][to_location] += quantity
else:
self.stock[item][to_location] = quantity
self.stock[item][from_location] -= quantity
else:
print(f'{item} inventory not enough in {from_location}')
else:
print(f'{item} not found in {from_location}')
else:
print(f'{item} not found in inventory')
def add_location(self, location):
for item in self.stock:
self.stock[item][location] = 0
def remove_location(self, location):
for item in self.stock:
del self.stock[item][location]
def print_inventory(self):
for item, locations in self.stock.items():
print(f'{item}:')
for location, quantity in locations.items():
print(f' {location}: {quantity}')
# Example usage
inventory = Inventory()
# Add items to inventory
inventory.add_item('laptop', 10)
inventory.add_item('phone', 20)
# Add locations to inventory
inventory.add_location('warehouse 1')
inventory.add_location('warehouse 2')
# Transfer items between locations
inventory.transfer_item('laptop', 5, 'warehouse 1', 'warehouse 2')
# Print inventory
inventory.print_inventory()
# Remove a location from inventory
inventory.remove_location('warehouse 1')
# Remove an item from inventory
inventory.remove_item('phone', 15)
# Print inventory
inventory.print_inventory()
该代码使用了一个字典来存储库存信息,其中每个键表示一个物品,对应的值是一个字典,表示该物品在不同位置的库存数量。调拨、入库、出库等操作都是在这个字典上进行的。该代码还实现了添加、删除位置和物品的功能,并提供了一个简单的输出方法来查看当前库存状态。
原文地址: https://www.cveoy.top/t/topic/lMp0 著作权归作者所有。请勿转载和采集!