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 bagCount (Bag theBag)
    {
        int count = 0;
        Enumeration<Bag> iter = _bags.elements();

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

            if (b.contains(theBag))
            {
                if (_debug)
                    System.out.println("Bag "+b+" containts "+theBag);

                count++;
            }
        }

        return count;
    }

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

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

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

        return str;
    }

    private Vector<Bag> _bags;
    private boolean _debug;
}
@@ -261,32 +261,497 @@
 g theBag)%0A    %7B%0A
+        Vector%3CBag%3E checked = new Vector%3CBag%3E();%0A%0A        return checkBags(theBag, checked);%0A    %7D%0A%0A    @Override%0A    public String toString ()%0A    %7B%0A        String str = %22%22;%0A        Enumeration%3CBag%3E iter = _bags.elements();%0A%0A        while (iter.hasMoreElements())%0A        %7B%0A            Bag b = iter.nextElement();%0A%0A            str += b.printRule()+%22%5Cn%22;%0A        %7D%0A%0A        return str;%0A    %7D%0A%0A    private final int checkBags (Bag theBag, Vector%3CBag%3E checked)%0A    %7B%0A
         int coun
@@ -798,32 +798,82 @@
 bags.elements();
+%0A        Vector%3CBag%3E indirect = new Vector%3CBag%3E();
 %0A%0A        while 
@@ -1070,25 +1070,27 @@
 ln(%22
-Bag
+A
  %22+b+%22
+ bag
  contain
 ts %22
@@ -1085,18 +1085,19 @@
  contain
-t
 s
+ a
  %22+theBa
@@ -1097,16 +1097,24 @@
 %22+theBag
++%22 bag.%22
 );%0A%0A    
@@ -1129,16 +1129,33 @@
     
+if (!checked.
 co
-u
 nt
-++;
+ains(b))
 %0A   
@@ -1163,17 +1163,21 @@
         
-%7D
+    %7B
 %0A       
@@ -1181,152 +1181,217 @@
     
-%7D%0A%0A        return count;%0A    %7D%0A%0A    @Override%0A    public String toString ()%0A    %7B%0A        String str = %22%22;%0A        Enumeration%3CBag%3E iter = _bags
+            indirect.add(b);%0A%0A                    checked.add(b);%0A%0A                    count++;%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (indirect.size() %3E 0)%0A        %7B%0A            iter = indirect
 .ele
@@ -1392,32 +1392,36 @@
 ct.elements();%0A%0A
+    
         while (i
@@ -1443,34 +1443,40 @@
 ents())%0A        
-%7B%0A
+    %7B%0A  
             Bag 
@@ -1463,32 +1463,34 @@
 %7B%0A              
+  
 Bag b = iter.nex
@@ -1518,34 +1518,57 @@
     
-str += b.printRule()+%22%5Cn%22;
+    count += checkBags(b, checked);%0A            %7D
 %0A   
@@ -1582,35 +1582,37 @@
 %0A        return 
-str
+count
 ;%0A    %7D%0A%0A    pri

Update Inventory.java