public class BeABag
1: import java.util.Random;
3: /**
4: * A program to run the {@link HumanBag} thru its paces. It interacts with the
5: * user who holds, acts on, and answers questions about the contents of a Bag.
6: * <p>
7: * This program could also be used to put a different implementation thru its
8: * paces, but the output would be rather hard to understand, as it would not
9: * include any explanation of what events it is responding to.
10: *
11: * @author Mark Young (A00000000)
12: */
13: public class BeABag {
15: public static void main(String[] args) {
16: // Introduce yourself
17: System.out.print("\n\n"
18: + "Bag Testing Program\n"
19: + "-------------------\n\n"
20: + "This program tests an implementation of the Bag interface. "
21: + "The Bag in this\nprogram contains strings "
22: + "representing colour pencils.\n\n"
23: + "Now as it happens, "
24: + "we haven't actually programmed a Bag into the computer,\n"
25: + "so we're going to need your help. "
26: + "Please get a bunch of colour pencils and\na bag, "
27: + "and follow the instructions."
28: + "\n\n"
29: + "Thanks!"
30: + "\n\n");
31: // create the bag
32: Bag<String> helper = new HumanBag();
34: // test add
35: int numPencils = r.nextInt(6) + 5;
36: for (int i = 0; i < numPencils; ++i) {
37: System.out.println();
38: if (!helper.add(randomPencil())) {
39: System.out.println("Wow! That was unexpected....");
40: } else {
41: System.out.println("Good.");
42: }
43: }
44: System.out.println();
46: // run thru extra tests once with stuff in the bag
47: testBag(helper);
49: // test clear
50: helper.clear();
51: System.out.println();
52: testBag(helper);
53: }
55: private static void testBag(Bag<String> theBag) {
56: // test toArray
57: Object[] inBag = theBag.toArray();
58: System.out.println();
59: System.out.println("---------- Bag Contents ----------");
60: for (int i = 0; i < inBag.length; ++i) {
61: System.out.println(" inBag[" + i + "] is " + inBag[i]);
62: }
63: System.out.println("---------- End Contents ----------");
64: System.out.println();
66: // test isEmpty
67: if (!theBag.isEmpty()) {
68: System.out.println("The bag is not empty!");
69: } else {
70: System.out.println("How sad that the bag is empty!");
71: }
72: System.out.println();
74: // test count
75: String testPencil = randomPencil();
76: int num = theBag.count(testPencil);
77: System.out.println("There are " + num + " "
78: + testPencil + "s in the bag!");
79: System.out.println();
81: // test contains
82: testPencil = randomPencil();
83: if (theBag.contains(testPencil)) {
84: System.out.println("Hooray!!!");
85: } else {
86: System.out.println("Boooo!!!");
87: }
88: System.out.println();
90: // test remove
91: String removed = theBag.remove();
92: if (removed != null) {
93: System.out.println("Thank you for the " + removed + " pencil.");
94: } else {
95: System.out.println("Thanks for NOTHIN'!");
96: }
97: System.out.println();
98: if (!theBag.remove(randomPencil())) {
99: System.out.println("HUMPH!");
100: } else {
101: System.out.println("Thank you.");
102: }
103: System.out.println();
105: // test getCurrentSize
106: System.out.println("There are " + theBag.size()
107: + " pencils in the bag!");
108: System.out.println();
109: }
111: private static String randomPencil() {
112: int pencilNum = r.nextInt(pencilSelection.length);
113: return pencilSelection[pencilNum];
114: }
116: private static final Random r = new Random();
117: private static final String[] pencilSelection
118: = new String[]{
119: "black", "white", "grey",
120: "red", "orange", "yellow", "green", "blue", "purple",
121: "brown", "pink"
122: };
124: }