This is my solution to the problem, but when I test it I get an IndexOutOfBounds Exception. I compared it to the teacher’s solution and it seems that the only difference is that in my solution I traverse backwards. Can someone tell me where I’ve gone wrong?
public static void combineSales(ArrayList combined)
{
/* ----------------------------------- TO DO -----------------------------------
* Find duplicate products and combine their total quantities sold. Remove
* the duplicate products from the combined list.
* -----------------------------------------------------------------------------
*/
for(int o = combined.size() - 1; o > -1; o--)
{
for(int c = o-1; c > -1; c--)
{
Product p = combined.get(o);
Product p2 = combined.get(c);
if(p.equals(p2))
{
p.setQuantity(p.getQuantity() + p2.getQuantity());
combined.remove(c);
}
}
}
}