1   // joi/4/dictionary/Lookup.java                         
2   //                                                            
3   //                                                            
4   // Copyright 2003 Bill Campbell and Ethan Bolker                         
5                                                               
6   /**
7    * On line word lookup.
8    *
9   * @see Dictionary
10   * @see Definition
11   *
12   * @version 4
13   */
14  
15  public class Lookup
16  {
17      private static Terminal t = new Terminal();
18      private static Dictionary dictionary = new Dictionary();
19  
20      /**
21       * Helper method to fill the dictionary with some simple
22       * Definitions.
23       *
24       * A real Dictionary would live in a file somewhere.
25       */
26  
27      private static void fillDictionary()
28      {
29          dictionary.addEntry( "shape",
30              new Definition(  "a geometric object in a plane" ) );
31          dictionary.addEntry( "quadrilateral",
32              new Definition(  "a polygonal shape with four sides" ) );
33          dictionary.addEntry( "rectangle",
34              new Definition(  "a right-angled quadrilateral" ) );
35          dictionary.addEntry( "square",
36              new Definition(  "a rectangle having equal sides" ) );
37      }
38  
39      /**
40       * Helper method to print the Definition of a single word,
41       * or a message if the word is not in the Dictionary.
42       *
43       * @param word the word whose definition is wanted.
44       */
45  
46      private static void printDefinition(String word)
47      {
48          Definition definition = dictionary.getEntry(word);
49          if (definition == null) {
50              t.println("sorry, no definition found for " + word);
51          }
52          else {
53              t.println(definition.toString());
54          }
55      }
56  
57      /**
58       * Run the Dictionary lookup. 
59       *
60       * Parse command line arguments for words to look up,
61       * "all" prints the whole Dictionary.
62       *
63       * Then prompt for more words, "quit" to finish.
64       *
65       * For example,
66       * <pre>
67       *
68       * %> java Lookup shape square circle
69       * shape:
70       * a geometric object in a plane
71       * square:
72       * a rectangle having equal sides
73       * circle:
74       * sorry, no definition found for circle
75       * 
76       * look up words, "quit" to quit
77       * word> rectangle
78       * a right-angled quadrilateral
79       * word> quit
80       * %>
81       * </pre>
82       *
83       * @param args the words that we want looked up, supplied as
84       *             command line arguments.  If the word "all" is
85       *             included, all words are looked up.
86       */
87  
88      public static void main( String[] args )
89      {
90          // fill the dictionary (not a big one!)
91          fillDictionary();
92          
93          // look up some words 
94          String word;
95  
96          // words specified on command line
97          for (int i = 0; i < args.length; i++ ) {
98              word = args[i];
99              if (word.equals("all")) {
100                 t.println("The whole dictionary (" + 
101                           dictionary.getSize() + " entries):");
102                 t.println("-------------------");
103                 t.println(dictionary.toString());
104                 t.println("-------------------");
105             }
106             else {
107                 t.println(word + ":");
108                 printDefinition(word);
109             }
110         }
111 
112         // words entered interactively
113         t.println("\nlook up words, \"quit\" to quit");
114         while (true) {
115             word = t.readWord("word> ");
116             if (word.equals("quit")) {
117                 break;
118             }
119             printDefinition(word);
120         }
121     }
122 }
123