//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Example 5.11 on page 102
//  Testing the Bag class

import java.util.Iterator;

public class Ex0511
{ public static void main(String[] args)
  { String[] states = { "Maine", "Idaho", "Iowa", "Ohio", "Utah" };
    Bag myCollection = new Bag(states);
    print(myCollection);
    myCollection.add("Alaska");
    print(myCollection);
    if (myCollection.remove("Ohio")) print(myCollection);
    else System.out.println("Object \"Ohio\" was not found.");
    Iterator it = myCollection.iterator();
    while (it.hasNext())
    { String s = (String)it.next();
      System.out.println("\ts = \""+s+"\"");
      if (s.charAt(0) == 'I')
      { it.remove();
        System.out.println("\tObject \""+s+"\" removed.");
      }
    }
    print(myCollection);
  }

  private static void print(Bag collection)
  { System.out.println("size() = " + collection.size());
    Object[] objects = collection.toArray();
    for (int i=0; i<objects.length; i++)
      System.out.println("\tobjects[" + i + "] = " + objects[i]);
    if (collection.contains("Iowa"))
      System.out.println("\tContains \"Iowa\"");
    else System.out.println("\tDoes not contains \"Iowa\"");
    String[] states = { "Maine", "Idaho", "Iowa", "Ohio", "Utah" };
    Bag newCollection = new Bag(states);
    if (newCollection.containsAll(collection))
      System.out.println("\tnewC contains myC");
    else System.out.println("\tnewC does not contain myC");
    if (collection.containsAll(newCollection))
      System.out.println("\tmyC contains newC");
    else System.out.println("\tmyC does not contain newC");
    if (collection.equals(newCollection))
      System.out.println("\tmyC equals newC");
    else System.out.println("\tmyC does not equal newC");
    if (newCollection.equals(collection))
      System.out.println("\tnewC equals myC");
    else System.out.println("\tnewC does not equal myC");
  }
}
