仅在达到限制时才将新元素添加到arraylist

时间:2018-02-16 22:44:05

标签: java arraylist

我正在尝试为仓库类编写一个receive()方法,但是没有显示我想要的结果。主要方法是:

public static void main( String[] args )
{
    Warehouse w = new Warehouse( 10 ); 
    Footwear d0 = new DressShoe( "Loafer", 10.5, "1234-13" ); 
    Footwear b = new Boot( "Riding", 8, "1234-5" ); 
    Footwear c = new CasualShoe( "Sandal", 9.5, "1234-0" ); 
    Footwear d1 = new DressShoe( "Wing-tip", 10, "1234-3" ); 
    w.receive( d0, 4 ); 
    w.receive( b, 25 ); 
    w.receive( c, 18 ); 
    w.receive( d1, 5 ); 
    System.out.println(w);
}

我希望它打印出来,每个Bin的总数量应该是10(数量是最右边的数字)。

Bin B0:
Dress Shoe – Loafer (size 10½), SKU 1234-13: 4
Casual Shoe – Sandal (size 9½), SKU 1234-0: 6
Bin B1:
Boot – Riding (size 8), SKU 1234-5: 10
Bin B2:
Boot – Riding (size 8), SKU 1234-5: 10
Bin B3:
Boot – Riding (size 8), SKU 1234-5: 5
Casual Shoe – Sandal (size 9½), SKU 1234-0: 2
Dress Shoe – Wing-tip (size 10), SKU 1234-3: 3
Bin B4:
Casual Shoe – Sandal (size 9½), SKU 1234-0: 10
Bin B5:
Dress Shoe – Wing-tip (size 10), SKU 1234-3: 2

但我的除了“Dress Shoe - Loafer(尺寸10½)”之外不打印任何其他东西,当总数量为10时它不会创建新的Bin。我的看起来像

Bin B0:
Dress Shoe - Loafer (size 10½), SKU 1234-13: 52
Bin B1:
Dress Shoe - Loafer (size 10½), SKU 1234-13: 48
Bin B2:
Dress Shoe - Loafer (size 10½), SKU 1234-13: 23
Bin B3:
Dress Shoe - Loafer (size 10½), SKU 1234-13: 5
Bin B4:

到目前为止,这些是我的课程。

public class Warehouse
{
private int myBinMax;
private ArrayList<Footwear> myCatalog;
private ArrayList<Bin> myBins;

public Warehouse(int binMax)
{
    myBinMax = binMax;
    myCatalog = new ArrayList<Footwear>();
    myBins = new ArrayList<Bin>(5);
    for(int i=0; i<5; i++)
    {
        addBin();
    }
}
public void addBin()
{
    myBins.add( new Bin( "B" + myBins.size() ) );
}
public String toString()
{
    String s = "";
    for ( Bin bin : myBins )
    {
        s += "Bin " + bin.getName() + ":\n";
        for (int i = 0; i < bin.getContents().size(); i++) {
            s += Lab04Runner.lookupFootwear(myCatalog, 
          bin.getContents().get(i).getSKU()) + ", " + 
          bin.getContents().get(i) + "\n";
        }
    }
    return s;
}
public void receive(Footwear object, int a)
{
    if (!myCatalog.contains(object))
    {
        myCatalog.add(object);
    }
    int min = Integer.MAX_VALUE;
    String name;
    for (Bin bin : myBins)
    {
        for( int i =0; i<myCatalog.size(); i++)
        {
        if (bin.totalQuantity() < min)
        {
            if (bin.totalQuantity() == 0)
            {
                min = 0;
                name = bin.getName();
                BinItem bi = new BinItem(myCatalog.get(i).getSKU(), a);    
                bin.add(bi);
            }
            else
            {
                min = bin.totalQuantity();
                name = bin.getName();
                BinItem bi = new BinItem(myCatalog.get(i).getSKU(), a);    
                bin.add(bi);
            }
            if (bin.totalQuantity() == myBinMax)
            {
                addBin();
                name = myBins.get(myBins.size() - 1).getName();
                BinItem bi = new BinItem( myCatalog.get(i).getSKU(), 
                  a);
                myBins.get(myBins.size()-1).add(bi);
            }
        }
    }
    }
}
}

public class Lab04Runner
{
public static String lookupFootwear( ArrayList<Footwear> catalog, 
String sku )
{
    int n = Integer.parseInt(sku.replace("-", ""));
    for ( Footwear fw : catalog )
    {
        if (n <= 123499 && n >= 12340)
        {
            return "" + fw;
        }
    }

    return "SKU " + sku + " not in catalog";
}

public class Footwear
{
private String myStyle;
private double mySize;
private String mySKU;
public Footwear(String s, double r, String k)
{
    myStyle = s;
    mySize = r;
    mySKU = k;
}
public String getStyle()
{
    return myStyle;
}
public double getSize()
{
    return mySize;
}
public String getSKU()
{
    return mySKU;
}
public String getType()
{
    return "Unspecified";
}
public String printSize()
{
    if (mySize - (int)mySize == 0)
    {
        return "" +(int)mySize;
    }
    else
    {
        return "" +(int)mySize + "\u00bd";
    }
}    
public String toString()
{
    return getType() + " - " + getStyle() + " (size " + printSize() + 
")";
}
public class Bin
{
private String myName;
private ArrayList<BinItem> myContents;

public Bin( String name )
{
    myName = name;
    myContents = new ArrayList<BinItem>();
}
public String getName() 
{
    return myName; 
}
public ArrayList<BinItem> getContents()
{
    return myContents;
}
public void remove( int i )
{
    myContents.remove( i );
}
public int totalQuantity()
{
    int total = 0;
    for ( BinItem b : myContents )
    {
        total += b.getQuantity();
    }
    return total;
}
public void add( BinItem b )
{
    String sku = b.getSKU();
    int n = b.getQuantity();
    BinItem item;
    for ( int i = 0; i < myContents.size(); i++ )
    {
        item = myContents.get( i );
        if ( sku.equals( item.getSKU() ) )
        {
            int m = item.getQuantity();
            myContents.remove( i );
            myContents.add( new BinItem( sku, m+n ) );
            return;
        }
    }

    myContents.add( b );
}
public String toString()
{
    String s = "Bin " + myName + ":";
    for ( BinItem b : myContents )
    {
        s += "\n" + b;
    }
    return s;
}
public class BinItem
{
private String mySKU;
private int myQuantity;

public BinItem(String sku, int quantity)
{
    mySKU = sku;
    myQuantity = quantity;
}

public String getSKU()
{
    return mySKU;
}

public int getQuantity()
{
    return myQuantity;
}

public String toString()
{
    return "SKU " + getSKU() + ": " + getQuantity();
}  
}

我如何阻止它添加,而是在arraylist中添加一个新元素?另外我如何让它打印其他对象而不仅仅是第一个?

1 个答案:

答案 0 :(得分:0)

您不会计算可以放入Bin的剩余物品。您始终添加所有项目。因此,解决方案是将项目平均添加到现有的箱柜中。当还有剩余项目时,您必须添加另一个bin并再次调用receive方法将剩余项目添加到新bin。

public void receive(Footwear object, int a) {
    if (!myCatalog.contains(object)) {
        myCatalog.add(object);
    }
    int amountOfRemainingItems = 0;
    amountOfRemainingItems = distributeEqually(object, a);
    if(amountOfRemainingItems > 0) {
        addBin();
        receive(object, amountOfRemainingItems);
    }
}

private int distributeEqually(Footwear object, int amountOfRemainingItems) {
    for (Bin bin : myBins) {
        if(amountOfRemainingItems == 0) {
            break;
        }
        if (bin.totalQuantity() < myBinMax) {
            int itemsToAdd = calculateItemsToAdd(bin, amountOfRemainingItems);
            amountOfRemainingItems -= itemsToAdd;
            BinItem bi = new BinItem(object.getSKU(), itemsToAdd);
            bin.add(bi);
        } 
    }
    return amountOfRemainingItems;
}

private int calculateItemsToAdd(Bin bin, int amountOfremainingItems) {
    return (myBinMax - bin.totalQuantity()) >= amountOfremainingItems ? amountOfremainingItems : 
        myBinMax - bin.totalQuantity();
}

我认为可能会有一个更快的解决方案,因为你每次都会迭代所有的垃圾箱。但它可能是你可以建立起来的第一个解决方案。