You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
42 lines
1.2 KiB
package model;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class InventoryManager {
|
|
private Map<String, Integer> inventory;
|
|
|
|
public InventoryManager() {
|
|
inventory = new HashMap<>();
|
|
}
|
|
|
|
public void addOrUpdateInventory(String goodsId, int quantity) {
|
|
inventory.put(goodsId, inventory.getOrDefault(goodsId, 0) + quantity);
|
|
}
|
|
|
|
public void removeOrUpdateInventory(String goodsId, int quantity) {
|
|
if (inventory.containsKey(goodsId)) {
|
|
int currentQuantity = inventory.get(goodsId);
|
|
if (currentQuantity >= quantity) {
|
|
inventory.put(goodsId, currentQuantity - quantity);
|
|
} else {
|
|
throw new IllegalArgumentException("库存不足");
|
|
}
|
|
} else {
|
|
throw new IllegalArgumentException("货物不存在");
|
|
}
|
|
}
|
|
|
|
public int getInventoryQuantity(String goodsId) {
|
|
return inventory.getOrDefault(goodsId, 0);
|
|
}
|
|
|
|
public void processInboundRecord(InboundRecord record) {
|
|
addOrUpdateInventory(record.getGoodsId(), record.getQuantity());
|
|
}
|
|
|
|
public void processOutboundRecord(OutboundRecord record) {
|
|
removeOrUpdateInventory(record.getGoodsId(), record.getQuantity());
|
|
}
|
|
}
|