import java.util.Random;

/**
 *
 * @author Mark Young (A00000000)
 */
public class BeABag {

    public static void main(String[] args) {
        // Introduce yourself
        System.out.print("\n\n"
                + "Bag Testing Program\n"
                + "-------------------\n\n"
                + "This program tests an implementation of the Bag interface. "
                + "The Bag in this\nprogram contains strings "
                + "representing colour pencils.\n\n"
                + "Now as it happens, "
                + "we haven't actually programmed a Bag into the computer,\n"
                + "so we're going to need your help. "
                + "Please get a bunch of colour pencils and\na bag, "
                + "and follow the instructions."
                + "\n\n"
                + "Thanks!"
                + "\n\n");
        // create the bag
        Bag<String> helper = new HumanBag();

        // test add
        int numPencils = r.nextInt(6) + 5;
        for (int i = 0; i < numPencils; ++i) {
            System.out.println();
            if (!helper.add(randomPencil())) {
                System.out.println("Wow!  That was unexpected....");
            } else {
                System.out.println("Good.");
            }
        }
        System.out.println();

        // run thru extra tests once with stuff in the bag
        testBag(helper);

        // test clear
        helper.clear();
        System.out.println();
        testBag(helper);
    }

    private static void testBag(Bag<String> theBag) {
        // test toArray
        Object[] inBag = theBag.toArray();
        System.out.println();
        System.out.println("---------- Bag Contents ----------");
        for (int i = 0; i < inBag.length; ++i) {
            System.out.println("    inBag[" + i + "] is " + inBag[i]);
        }
        System.out.println("---------- End Contents ----------");
        System.out.println();

        // test isEmpty
        if (!theBag.isEmpty()) {
            System.out.println("The bag is not empty!");
        } else {
            System.out.println("How sad that the bag is empty!");
        }
        System.out.println();

        // test getFrequencyOf
        String testPencil = randomPencil();
        int num = theBag.getFrequency(testPencil);
        System.out.println("There are " + num + " "
                + testPencil + "s in the bag!");
        System.out.println();

        // test contains
        testPencil = randomPencil();
        if (theBag.contains(testPencil)) {
            System.out.println("Hooray!!!");
        } else {
            System.out.println("Boooo!!!");
        }
        System.out.println();

        // test remove
        String removed = theBag.remove();
        if (removed != null) {
            System.out.println("Thank you for the " + removed + " pencil.");
        } else {
            System.out.println("Thanks for NOTHIN'!");
        }
        System.out.println();
        if (!theBag.remove(randomPencil())) {
            System.out.println("HUMPH!");
        } else {
            System.out.println("Thank you.");
        }
        System.out.println();

        // test getCurrentSize
        System.out.println("There are " + theBag.size()
                + " pencils in the bag!");
        System.out.println();
    }

    private static String randomPencil() {
        int pencilNum = r.nextInt(pencilSelection.length);
        return pencilSelection[pencilNum];
    }

    private static final Random r = new Random();
    private static final String[] pencilSelection
            = new String[]{
                "black", "white", "grey",
                "red", "orange", "yellow", "green", "blue", "purple",
                "brown", "pink"
            };

}
