You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仓库类receive()方法问题:Bin容量达10时无法创建新Bin

Fixing Your Warehouse receive() Method: Handling Bin Capacity Limits

Hey there! Let's figure out why your bin logic isn't creating new bins when the total item count hits 10, and why you're only seeing that single item output. Here's a step-by-step breakdown of what to adjust:

1. Core Logic: Check Bin Capacity Before Adding Items

The key mistake is likely that you're adding items to the current bin without first checking if it's already full. You need to add a check that triggers a new bin creation whenever the active bin's total count reaches 10.

Example Corrected Code (Java, since warehouse/bin patterns are common here)

First, let's outline the proper structure for your Warehouse, Bin, and Item classes:

public class Warehouse {
    private List<Bin> bins = new ArrayList<>();
    private Bin currentActiveBin;

    // Initialize with the first bin when the warehouse is created
    public Warehouse() {
        currentActiveBin = new Bin();
        bins.add(currentActiveBin);
    }

    public void receive(Item item) {
        // Check if current bin is at max capacity (10 items)
        if (currentActiveBin.getTotalItemCount() >= 10) {
            // Create a new bin and set it as the active one
            currentActiveBin = new Bin();
            bins.add(currentActiveBin);
            System.out.println("Created new bin - current bin is now full!");
        }

        // Add the item to the active bin
        currentActiveBin.addItem(item);
        // Print the item description (fixes your single-output issue)
        System.out.println(item.getFullDescription());
    }

    // Optional: Print all bins to verify counts
    public void printAllBins() {
        int binIndex = 1;
        for (Bin bin : bins) {
            System.out.println("\nBin #" + binIndex + " | Total items: " + bin.getTotalItemCount());
            for (Item item : bin.getItems()) {
                System.out.println("- " + item.getFullDescription());
            }
            binIndex++;
        }
    }
}

class Bin {
    private List<Item> items = new ArrayList<>();
    private int totalItemCount = 0;

    public void addItem(Item item) {
        items.add(item);
        totalItemCount++; // Critical: Update the count every time you add an item
    }

    public int getTotalItemCount() {
        return totalItemCount;
    }

    public List<Item> getItems() {
        return items;
    }
}

class Item {
    private String type;
    private String style;
    private String size;

    public Item(String type, String style, String size) {
        this.type = type;
        this.style = style;
        this.size = size;
    }

    public String getFullDescription() {
        return type + " – " + style + " (size " + size + ")";
    }
}

2. Key Fixes Explained

  • Pre-add Capacity Check: The receive() method first checks if the active bin is full. If yes, it creates a new bin and switches to it before adding the item.
  • Proper Count Tracking: The Bin class increments totalItemCount every time an item is added—this ensures your capacity check is accurate (if you forgot this step, your code would never trigger a new bin).
  • Consistent Output: Every time you call receive(), it prints the item's description, so you'll see all items being added instead of just one.

3. Testing the Logic

In your main method, try adding multiple items to see the bin switching work:

public static void main(String[] args) {
    Warehouse warehouse = new Warehouse();
    // Add 12 items to trigger a new bin
    for (int i = 0; i < 12; i++) {
        warehouse.receive(new Item("Dress Shoe", "Loafer", "10½"));
    }
    // Print all bins to verify counts
    warehouse.printAllBins();
}

You'll see the first bin fill up with 10 items, then a new bin is created for the remaining 2.

Quick Troubleshooting Checks

  • Did you forget to update the bin's total count when adding items? That's the most common culprit for the "no new bin" issue.
  • Is your initial bin being created properly? If currentActiveBin is null when you first call receive(), you'll get errors—initialize it in the warehouse constructor.

内容的提问来源于stack exchange,提问作者Mi Le

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:36:33