Source of OneWayNoRepeatsList.java


  1: 
  2: /**
  3:  An object of this class is a special kind of list. The list can
  4:  be written only from beginning to end. You can add to the end of
  5:  the list, but you cannot change individual entries. You can erase
  6:  the entire list and start over. No entry may appear more than once
  7:  on the list. You can use int variables as position markers into
  8:  the list. Position markers are similar to array indices, 
  9:  but are numbered starting with 1.
 10: */
 11: public class OneWayNoRepeatsList
 12: {
 13:     public static int START_POSITION = 1;
 14:     public static int DEFAULT_SIZE = 50;
 15:     //entry.length is the total number of items you have room
 16:     //for on the list. countOfEntries is the number of items 
 17:     //currently on the list.
 18:     private int countOfEntries; //can be less than entry.length.
 19:     private String[] entry; 
 20:     
 21:     public OneWayNoRepeatsList(int maximumNumberOfEntries)
 22:     {
 23:         entry = new String[maximumNumberOfEntries];
 24:         countOfEntries = 0; 
 25:     }
 26:     
 27:     /**
 28:      Creates an empty list with a capacity of DEFAULT_SIZE.
 29:     */
 30:     public OneWayNoRepeatsList( )
 31:     {
 32:         entry = new String[DEFAULT_SIZE];
 33:         countOfEntries = 0; 
 34:     }
 35:     
 36:     public boolean full( )
 37:     {
 38:         return (countOfEntries == entry.length);
 39:     }
 40:     
 41:     public boolean empty( )
 42:     {
 43:         return (countOfEntries == 0);
 44:     }
 45: 
 46:     /**
 47:      Precondition: List is not full.
 48:      Postcondition: If item was not on the list, 
 49:      it has been added to the list.
 50:     */
 51:     public void addItem(String item)
 52:     {
 53:         if (! onList(item))
 54:         {
 55:             if (countOfEntries == entry.length)
 56:             {
 57:                 System.out.println("Adding to a full list!");
 58:                 System.exit(0);
 59:             }
 60:             else
 61:             {
 62:                 entry[countOfEntries] = item;
 63:                 countOfEntries++;
 64:             }
 65:         }//else do nothing. Item is already on the list.
 66:     }
 67:     
 68:     /**
 69:      If the argument indicates a position on the list,
 70:      then the entry at that specified position is returned;
 71:      otherwise, null is returned.
 72:     */
 73:     public String getEntryAt(int position)
 74:     {
 75:         if ((1 <= position) && (position <= countOfEntries))
 76:             return entry[position - 1];
 77:         else
 78:             return null;
 79:     }
 80:     
 81:     /**
 82:      Returns true if position is the index of the
 83:      last item on the list; otherwise, returns false.
 84:     */
 85:     public boolean atLastEntry(int position)
 86:     {
 87:         return (position == countOfEntries);
 88:     }
 89: 
 90:     /**
 91:      Returns true if item is on the list;
 92:      otherwise, returns false. Does not differentiate 
 93:      between upper- and lowercase letters.
 94:     */
 95:     public boolean onList(String item) 
 96:     {
 97:          boolean found = false;
 98:          int i = 0;
 99:          while ((! found) && (i < countOfEntries)) 
100:          {
101:              if (item.equalsIgnoreCase(entry[i]))
102:                  found = true;
103:              else
104:                  i++;
105:          }
106:         return found;
107:     }
108:     
109:     public int maximumNumberOfEntries( )
110:     {
111:         return entry.length;
112:     }
113:     
114:     public int getNumberOfEntries( )
115:     {
116:         return countOfEntries;
117:     } 
118:     
119:     public void eraseList( )
120:     {
121:         countOfEntries = 0;
122:     }
123: }