import java.util.*;

public class Inventory
{
    public Inventory (boolean debug)
    {
        _bags = new Vector<Bag>();
        _debug = debug;
    }

    public final void insert (Bag b)
    {
        _bags.add(b);
    }

    public final int totalBagCount (Bag theBag)
    {
        Vector<Bag> checked = new Vector<Bag>();

        return checkTotalBags(theBag, checked);
    }

    public final int bagCount (Bag theBag)
    {
        Vector<Bag> checked = new Vector<Bag>();

        return checkBags(theBag, checked);
    }

    @Override
    public String toString ()
    {
        String str = "";
        Enumeration<Bag> iter = _bags.elements();

        while (iter.hasMoreElements())
        {
            Bag b = iter.nextElement();

            str += b.printRule()+"\n";

            //str += "    Total contained bags in "+b+": "+b.totalContainedBags()+"\n";
        }

        return str;
    }

    private final int checkBags (Bag theBag, Vector<Bag> checked)
    {
        int count = 0;
        Enumeration<Bag> iter = _bags.elements();
        Vector<Bag> indirect = new Vector<Bag>();

        while (iter.hasMoreElements())
        {
            Bag b = iter.nextElement();

            if (b.contains(theBag))
            {
                if (_debug)
                    System.out.println("A "+b+" bag contains a "+theBag+" bag.");

                if (!checked.contains(b))
                {
                    indirect.add(b);

                    checked.add(b);

                    count++;
                }
            }
        }

        if (indirect.size() > 0)
        {
            iter = indirect.elements();

            while (iter.hasMoreElements())
            {
                Bag b = iter.nextElement();

                count += checkBags(b, checked);
            }
        }

        return count;
    }

    private final int checkTotalBags (Bag theBag, Vector<Bag> checked)
    {
        int count = 0;
        Enumeration<Bag> iter = _bags.elements();
        Vector<Bag> indirect = new Vector<Bag>();

        while (iter.hasMoreElements())
        {
            Bag b = iter.nextElement();

            if (b.contains(theBag))
            {
                if (_debug)
                    System.out.println("A "+b+" bag contains "+b.quantity()+" copies of "+theBag+" bag.");

                if (!checked.contains(b))
                {
                    indirect.add(b);

                    checked.add(b);

                    if (_debug)
                        System.out.println("Adding "+b.totalContainedBags()+" from "+b);

                    count += b.totalContainedBags();
                }
            }
        }

        if (indirect.size() > 0)
        {
            iter = indirect.elements();

            while (iter.hasMoreElements())
            {
                Bag b = iter.nextElement();

                count += checkTotalBags(b, checked);
            }
        }

        return count;
    }


    private Vector<Bag> _bags;
    private boolean _debug;
}
@@ -319,33 +319,32 @@
 tor%3CBag%3E();%0A
-%0A
         
 return check
@@ -335,46 +335,167 @@
     
-return checkTotalBags(theBag, checked)
+int index = _bags.indexOf(theBag);%0A        Bag actualBag = _bags.elementAt(index);%0A%0A        System.out.println(%22Got %22+actualBag.printRule());%0A%0A        return 0
 ;%0A  
@@ -1976,1132 +1976,8 @@
  %7D%0A%0A
-    private final int checkTotalBags (Bag theBag, Vector%3CBag%3E checked)%0A    %7B%0A        int count = 0;%0A        Enumeration%3CBag%3E iter = _bags.elements();%0A        Vector%3CBag%3E indirect = new Vector%3CBag%3E();%0A%0A        while (iter.hasMoreElements())%0A        %7B%0A            Bag b = iter.nextElement();%0A%0A            if (b.contains(theBag))%0A            %7B%0A                if (_debug)%0A                    System.out.println(%22A %22+b+%22 bag contains %22+b.quantity()+%22 copies of %22+theBag+%22 bag.%22);%0A%0A                if (!checked.contains(b))%0A                %7B%0A                    indirect.add(b);%0A%0A                    checked.add(b);%0A%0A                    if (_debug)%0A                        System.out.println(%22Adding %22+b.totalContainedBags()+%22 from %22+b);%0A%0A                    count += b.totalContainedBags();%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (indirect.size() %3E 0)%0A        %7B%0A            iter = indirect.elements();%0A%0A            while (iter.hasMoreElements())%0A            %7B%0A                Bag b = iter.nextElement();%0A%0A                count += checkTotalBags(b, checked);%0A            %7D%0A        %7D%0A%0A        return count;%0A    %7D%0A%0A%0A
     

Update Inventory.java