Source of HumanBag.java


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

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

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

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

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

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

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

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

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

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

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

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

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

127:     @Override
128:     public Iterator<String> iterator() {
129:         List<String> myList = Arrays.asList(toArray());
130:         return myList.iterator();
131:     }

133: }