Source of HumanBag.java


  1: import java.util.Arrays;
  2: import java.util.Collection;
  3: import java.util.Iterator;
  4: import java.util.List;
  5: import java.util.Scanner;

  7: /**
  8:  * An implementation of the BagInterface that uses a human being (the user) to
  9:  * control the bag and answer all questions about it. The human is responsible
 10:  * for making sure that everything works as it should. An object of this class
 11:  * simply passes the requests on to the user, and returns the responses to the
 12:  * client.
 13:  * <p>
 14:  * To simplify the implementation, only Strings are allowed in the HumanBag.
 15:  * <p>
 16:  * Requests to the user are prefixed with greater-than signs (>>>).
 17:  *
 18:  * @author Mark Young (A00000000)
 19:  */
 20: public class HumanBag implements Bag<String> {

 22:     private Scanner human = new Scanner(System.in);

 24:     @Override
 25:     public int size() {
 26:         System.out.print(">>> How many items are in the bag? ");
 27:         int numInBag = human.nextInt();
 28:         human.nextLine();
 29:         return numInBag;
 30:     }

 32:     @Override
 33:     public boolean isEmpty() {
 34:         System.out.print(">>> Is the bag empty? ");
 35:         String answer = human.next().toUpperCase();
 36:         human.nextLine();
 37:         return answer.startsWith("Y");
 38:     }

 40:     @Override
 41:     public boolean add(String newEntry) {
 42:         System.out.println(">>> Add a " + newEntry + " to the bag. ");
 43:         System.out.print(">>> Type NO if you can't, "
 44:                 + "or press ENTER when you're done: ");
 45:         String answer = human.nextLine().toUpperCase();
 46:         return !answer.equals("NO");
 47:     }

 49:     @Override
 50:     public String remove() {
 51:         System.out.println(">>> Remove something from the bag. ");
 52:         System.out.print(">>> Tell me what it is "
 53:                 + "(leave blank if there was nothing in the bag): ");
 54:         String answer = human.nextLine();
 55:         if ("".equals(answer)) {
 56:             return null;
 57:         } else {
 58:             return answer;
 59:         }
 60:     }

 62:     @Override
 63:     public boolean remove(Object anEntry) {
 64:         System.out.println(">>> Remove a " + anEntry + " from the bag. ");
 65:         System.out.print(">>> Type NO if you can't, "
 66:                 + "or press ENTER when you're done: ");
 67:         String answer = human.nextLine().toUpperCase();
 68:         return !answer.equals("NO");
 69:     }

 71:     @Override
 72:     public void clear() {
 73:         System.out.println(">>> Dump everything out of the bag.");
 74:         System.out.print(">>> Press ENTER when you're done....");
 75:         human.nextLine();
 76:     }

 78:     @Override
 79:     public int getFrequency(String anEntry) {
 80:         System.out.print(">>> How many " + anEntry + "s are in the bag? ");
 81:         int numInBag = human.nextInt();
 82:         human.nextLine();
 83:         return numInBag;
 84:     }

 86:     @Override
 87:     public boolean contains(Object anEntry) {
 88:         System.out.print(">>> Is there a " + anEntry + " in the bag? ");
 89:         String answer = human.next().toUpperCase();
 90:         human.nextLine();
 91:         return answer.startsWith("Y");
 92:     }

 94:     @Override
 95:     public String[] toArray() {
 96:         int inBag = this.size();
 97:         String[] contents = new String[inBag];
 98:         if (inBag > 0) {
 99:             System.out.println(">>> Tell me what each of the items is.");
100:             System.out.println(">>> Enter one item per line "
101:                     + "until you're done.");
102:             for (int i = 0; i < inBag; ++i) {
103:                 System.out.print(" >> item #" + (i + 1) + " >> ");
104:                 contents[i] = human.nextLine();
105:             }
106:             System.out.println(">>> Thank you!");
107:             System.out.println(">>> Make sure all those items are "
108:                     + "still/back in the bag.");
109:             System.out.print(">>> Press ENTER when you're ready....");
110:             human.nextLine();
111:         }
112:         return contents;
113:     }

115:     @SuppressWarnings("unchecked")
116:     @Override
117:     public <T> T[] toArray(T[] starter) {
118:         int mySize = this.size();
119:         if (starter.length < mySize) {
120:             starter = Arrays.copyOf(starter, mySize);
121:         }
122:         String[] myContents = toArray();
123:         for (int i = 0; i < mySize; ++i) {
124:             starter[i] = (T)myContents[i];
125:         }

127:         // terminate with a null, if possible
128:         if (starter.length > mySize) {
129:             starter[mySize] = null;
130:         }
131:         return starter;
132:     }

134:     @Override
135:     public Iterator<String> iterator() {
136:         List<String> myList = Arrays.asList(toArray());
137:         return myList.iterator();
138:     }

140: }