import java.util.ArrayList;

/**
 * A collection of Bins
 * @author Robert Cohen
 */
public class SimpleBinCollection {
	private ArrayList<Bin> bins = new ArrayList<Bin>();

  /**
   * Default constructor. We could leave this out, and it would be
   * created for us just like this. 
   */
  public SimpleBinCollection() {
  }

  /**
   * Adds a box to the BinCollection.  The method uses the following algorithm:
   * If the box is bigger than the capacity of a bin, throw an exception.
   * Otherwise, scan through the bins to find a bin with enough available space.
   * If such a bin exists, add the box to the bin.
   * Otherwise, create a new bin and add the box to the new bin.
   * @param b The Box
   * @return status: 0 = OK, -1 = too-big
   */
  public boolean addBox(Box b)
  {
		if (b.height() > Bin.CAPACITY)
			return false;
		for (Bin bin: bins) {
			if (b.height() <= bin.availableSpace()) {
				bin.addBox(b);
				return true;
			}
		}
		Bin bin = new Bin(bins.size());
		bin.addBox(b);
		bins.add(bin);
		return true;
	}

  /**
	 * Returns the number of bins in the BinCollection
	 * 
	 * @return The number of bins
	 */
  public int size() {
    return bins.size();
  }

  /**
  * Overrides the default method.
  * @return A string representation of the BinCollection
  * with one line for each Bin
  */
  public String toString() {
    StringBuffer s = new StringBuffer();
    for (Bin bin: bins)
      s.append("" + bin + "\n");
    return s.toString();
  }
}
