public class TestMenu
1: //TestMenu.java
2: //A test driver for the Menu class.
4: import java.io.*;
6: public class TestMenu
7: {
8: private static BufferedReader keyboard =
9: new BufferedReader(new InputStreamReader(System.in));
10: private static PrintWriter screen =
11: new PrintWriter(System.out, true);
14: private static void pause()
15: throws IOException
16: {
17: screen.print("This is a driver pause -----> " +
18: "Press Enter to continue ... ");
19: screen.flush();
20: keyboard.readLine();
21: }
24: private static void DisplayMessage(String message)
25: throws IOException
26: {
27: String linesAbove = new String("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
28: String linesBelow = new String("\n\n\n\n\n\n\n\n\n\n");
29: screen.println(linesAbove);
30: screen.println(message);
31: screen.println(linesBelow);
32: pause();
33: }
36: public static void main(String[] args)
37: throws IOException
38: {
39: DisplayMessage("First, a default constructor creates a " +
40: "\"blank\" menu, which we then display\n(on " +
41: "the following screen) to show what a menu " +
42: "with no title and no options\n(i.e., a " +
43: "\"default menu\") looks like when displayed.\n");
44: Menu menu1 = new Menu();
45: menu1.display();
46: pause();
48: DisplayMessage("Next we show the error we get if we try " +
49: "to add to an existing menu\na title that's " +
50: "too long.");
51: String longTitle = new String("A Title That is Way, Way, Way, " +
52: "Too Long for Any Reasonable Menu");
53: menu1.setTitle(longTitle);
55: //Or, what if we try to use a too-long title in the constructor?
56: Menu menu = new Menu(longTitle);
58: //Add a title to the default menu and then re-display the menu:
59: menu1.setTitle("A Brand New Menu");
60: menu1.display();
61: pause();
63: //Observe what happens when we attempt to get a menu choice
64: //from a "title-only" menu with no options.
65: int menuChoice = menu1.getChoice();
66: screen.println("The menu choice returned was " +
67: menuChoice + ".");
68: pause();
70: //Declare a second menu object and initialize it with a title.
71: //Then add several options, displaying the menu after adding
72: //each one. Notice how the menu display remains "centered".
74: Menu menu2 = new Menu("Main Menu");
75: menu2.display();
76: pause();
78: menu2.addOption("Quit");
79: menu2.display();
80: pause();
82: menu2.addOption("Get information");
83: menu2.display();
84: pause();
86: menu2.addOption("Do something");
87: menu2.display();
88: pause();
90: menu2.addOption("Do something really, really " +
91: "very long and involved");
92: menu2.display();
93: pause();
95: String longOption =
96: new String("This option is going to be too long " +
97: "to be added to any menu, for sure!");
98: menu2.addOption(longOption);
100: menu2.addOption("Do something shorter");
101: menu2.addOption("Do something else altogether");
103: //Loop while the user does not choose the Quit option, i.e.
104: //while the user does not choose option 1 from the menu, and
105: //while the user does not enter 3 invalid options in a row.
106: boolean finished;
107: do
108: {
109: menu2.display();
110: menuChoice = menu2.getChoice();
111: finished = (menuChoice == 1 || menuChoice == -1);
112: screen.println("\nThe menu choice returned was " +
113: menuChoice + ".");
114: if (menuChoice != 1 && menuChoice != -1)
115: screen.println("\nWe are just printing the menu " +
116: "choice here, but we could be doing " +
117: "most anything.");
118: else
119: screen.println();
120: pause();
121: }
122: while (!finished);
124: //Create a third menu with 20 options (the maximum number).
125: //Note how the periods following the option numbers line up,
126: //and continue to line up when the option number goes from
127: //one digit to two digits.
128: Menu menu3 = new Menu("Test Menu");
129: menu3.addOption("Option 1 permits you to do a first thing, " +
130: "which is to quit");
131: menu3.addOption("Option 2 allows you to do this");
132: menu3.addOption("Option 3 lets you do something just " +
133: "a bit longer");
134: menu3.addOption("Option 4 just lets you do it");
135: menu3.addOption("Option 5 lets you do that");
136: menu3.addOption("Option 6 lets you do it now, not later, " +
137: "if you please");
138: menu3.addOption("Option 7 lets you do whatever");
139: menu3.addOption("Option 8 requires you to do it quickly " +
140: "if you can");
141: menu3.addOption("Option 9 lets you do it and take your time");
142: menu3.addOption("Option 10 lets you do it over and over again");
143: menu3.addOption("Option 11 lets you do it with style");
144: menu3.addOption("Option 12 requires it to be done by Monday");
145: menu3.addOption("Option 13 asks that you do it next week");
146: menu3.addOption("Option 14 demands that it be done yesterday");
147: menu3.addOption("Option 15 doesn't care if it's done at all");
148: menu3.addOption("Option 16 lets you do it whenever you like");
149: menu3.addOption("Option 17 requires it to happend every Friday");
150: menu3.addOption("Option 18 suggests you do it Tuesdays " +
151: "and Thursdays");
152: menu3.addOption("Option 19 asks for a full report " +
153: "in the morning");
154: menu3.addOption("Option 20 asks you to undo it");
155: menu3.display();
156: pause();
158: //Finally, note what happens when you attempt to add another
159: //option to a menu that already contains the maximum 20 options.
160: menu3.addOption("And yet another option ... ");
163: //Loop while the user does not choose the Quit option, i.e.
164: //while the user does not choose option 1 from the menu, and
165: //while the user does not enter 3 invalid options in a row.
166: //This loop allows the testing of option values up to and
167: //including the maximum.
168: do
169: {
170: menu3.display();
171: menuChoice = menu3.getChoice();
172: finished = (menuChoice == 1 || menuChoice == -1);
173: screen.println("\nThe menu choice returned was " +
174: menuChoice + ".");
175: pause();
176: }
177: while (!finished);
179: //Create a fourth menu, again with 20 options and, on this
180: //occasion, a very long title. Note the position of that title
181: //on the line. Note as well, in passing, how the options are
182: //created and added. This is not very useful, of course, since
183: //it is only possible with options of this form.
184: Menu menu4 = new Menu("Another Test Menu with a Very, Very, " +
185: "Very, Very Long Title");
187: String commonText = new String("Option ");
188: String newOption;
189: for (int i=1; i<=20; i++)
190: {
191: newOption = commonText + new Integer(i).toString();
192: menu4.addOption(newOption);
193: }
194: menu4.display();
195: pause();
198: //Create a fifth, rather bizarre menu as one more example
199: //to show that its display is at least reasonable.
200: Menu menu5 = new Menu("A Final Test Menu with a Very, " +
201: "Very, Very, Very Long Title");
202: for (int i=1; i<=19; i++)
203: {
204: newOption = commonText + new Integer(i).toString();
205: menu5.addOption(newOption);
206: }
207: menu5.display();
208: pause();
209: menu5.addOption("And this of course is a very, very, " +
210: "very long option");
211: menu5.display();
212: pause();
213: }
214: }