I'm trying to implement a bin packing algorithm using Next Fit online. So I don't know the size of the boxes before hand. Anyway I had a Box class with height and width. A column class which represents and stack of boxes and a Truck class which holds the stacks of boxes and a boolean if the truck is full or not.
However when I'm in my test class I generate a list of boxes and a list of trucks to hold the boxes. I add the boxes to each truck, when the truck is almost out of space, the final box tries to get added, and it doesn't fit, so the boolean isFull is set to true, but the box that went into the method is then lost. How can I add the box to the truck and if its full, use the same box on a call on the next truck?
Many Thanks
code
public void addBoxesNextFit(Box b)
{
if(truck.isEmpty()) // easy first case
{
*make a new column*
*add box to column and add column to truck*
}
else
{
*Get the last column in list*
if(remainingHeight in column > BoxesHeight && widthOftheColumn > boxesWidth)
{
*add box to column*
*add column to truck*
boxesInTruck++;
}
else if(there is enough space to make another column with the box)
{
*Make new column*
*add box to column and column to truck*
boxesInTruck++;
widthofAllColumns += b.getWidth(); //update width of all columns
}
else
{
truckFull = true; // if u can't add the box to 1 of the columns and you can't create a new column
} // then the truck is full
}
}