- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleApp.java
More file actions
Latest commit
258 lines (205 loc) · 7.03 KB
/
Copy pathExampleApp.java
File metadata and controls
258 lines (205 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
importjava.util.Random;
importcom.jmodule.def.BoundedCommand;
importcom.jmodule.def.Command;
importcom.jmodule.def.CommandLogic;
importcom.jmodule.def.Option;
importcom.jmodule.exec.ConsoleClient;
importcom.jmodule.exec.Module;
/*
* The classes in this file serve as an example of how to use the JModule API.
* In this case, we are writing a simple arithmetic program that has a module
* to perform arithmetic quiestions, and a module to quiz yourself.
*/
publicclassExampleApp {
// --------- REGULAR FUNCTIONS ----------
privatestaticintcorrect = 0;
privatestaticintincorrect = 0;
privatestaticintattempts = 0;
privatestaticvoidadd(String[] args) {
try {
inta = Integer.parseInt(args[0]);
intb = Integer.parseInt(args[1]);
System.out.println("Sum: " + (a + b) + "\n");
} catch (NumberFormatExceptionnfe) {
System.out.println("Invalid input!\n");
}
}
privatestaticvoidsubtract(String[] args) {
try {
inta = Integer.parseInt(args[0]);
intb = Integer.parseInt(args[1]);
System.out.println("Difference: " + (a - b) + "\n");
} catch (NumberFormatExceptionnfe) {
System.out.println("Invalid input!\n");
}
}
privatestaticvoidmultiply(String[] args) {
try {
intprod;
if (args.length == 0) {
prod = 0;
} else {
prod = 1;
for (Stringstr : args) {
intfac = Integer.parseInt(str);
prod *= fac;
}
}
System.out.println("Product: " + prod + "\n");
} catch (NumberFormatExceptionnfe) {
System.out.println("Invalid input!\n");
}
}
privatestaticvoidquizme() {
booleanisAdd = newRandom().nextBoolean();
Stringoperation = isAdd ? " + " : " - ";
inta = newRandom().nextInt(10);
intb = newRandom().nextInt(10);
System.out.println("What is " + a + operation + b + "? ");
while (true) {
attempts++;
try {
intanswer = isAdd ? a + b : a - b;
intresult = Integer.parseInt(System.console().readLine());
if (answer == result) {
System.out.println("Correct\n");
correct++;
} else {
System.out.println("Incorrect - correct answer was " + answer + ".\n");
incorrect++;
}
break;
} catch (NumberFormatExceptionnfe) {
System.out.println("Invalid input!");
System.out.println("What is " + a + operation + b + "? ");
}
}
}
publicstaticvoidmain(String[] args) {
// ----------- JMODULE FUNCTIONS -------------
// set up commands
// 'add' - 2 parameters
CommandaddCmd = newCommand("add", "Adds 2 numbers together",
newCommandLogic(newString[] {
"First number",
"Second number" }) {
@Override
publicvoidexecute(String[] args) {
add(args);
}
});
// 'subtract' - 2 parameters
CommandsubCmd = newCommand("subtract", "Subtracts 2 numbers",
newCommandLogic(newString[] {
"First number",
"Second number" }) {
@Override
publicvoidexecute(String[] args) {
subtract(args);
}
});
subCmd.addReference("sub"); // add alternate reference to this command
// edit usage info with resetUsage() and appendUsage()
subCmd.resetUsage("Usage: the same as above");
subCmd.appendUsage("Call this command by typing either 'subtract' or alternatively, 'sub'");
/*
* 'multiply' - This is an example of an bounded command that can have any
* number of parameters within a specified range. We can either leave the
* range open by only specifying a minimum in the constructor as such:
*
* BoundedCommmand(String name, String description, CommandLogic logic, int min)
*
* or have the range closed by specifying both a minimum and a maximum:
*
* BoundedCommmand(String name, String description, CommandLogic logic, int min, int max)
*
* Note that in a bounded or indefinite command, the String[] params in the CommandLogic
* constructor does not affect how the command runs, but only what shows in the command's
* usage info.
*
*/
CommandmultCmd = newBoundedCommand("multiply", "Multiplies 2 or more numbers",
newCommandLogic(newString[] {
"First number",
"Factors..." }) {
@Override
publicvoidexecute(String[] args) {
multiply(args);
}
// in this instance, we set the mininum number of parameters to 2 and
// leave the maximum open
}, 2);
multCmd.addReference("mult");
multCmd.addReference("mul");
// 'quizme' - no parameters
CommandquizCmd = newCommand("quizme", "Tests your knowledge of math", newCommandLogic() {
@Override
publicvoidexecute(String[] args) {
quizme();
}
});
quizCmd.addReference("qm"); // add an alternative reference to this command
// 'info' - no parameters
CommandinfoCmd = newCommand("info", "Tells you your quiz performance record", newCommandLogic() {
@Override
publicvoidexecute(String[] args) {
System.out.println("Correct answers: " + correct);
System.out.println("Incorrect answers: " + incorrect);
// define the behavior of the command when called with a specific option, signified by its flag
if (onOption('v')) {
System.out.println("Total attempts: " + attempts);
}
if (onOption('b')) {
System.out.println("Cracking open a [B]old one with the [B]oys");
}
System.out.println();
}
}.addOption(newOption('v', "Prints the total attempts, including invalid input")
.addReference("ver")
.addReference("verbose"))
.addOption(newOption('b', "Memes")
.addReference("boys"))
);
// set up modules - useful for grouping commands of similar type together
Modulemath = newModule("math");
math.addCommand(addCmd);
math.addCommand(subCmd);
math.addCommand(multCmd);
Modulequiz = newModule("quiz");
quiz.addCommand(quizCmd);
quiz.addCommand(infoCmd);
// append help page - this message will now show at the bottom of the help page
// for the 'quiz' module
quiz.appendHelpPage(
"\nWarning: the math quiz problems in this module are very advanced, so proceed with caution");
// set up console with home module 'math.' This means when the user runs the
// app, the 'math' module will be running.
ConsoleClientclient = newConsoleClient("Example Education App", math);
// add the module 'quiz'
client.addModule(quiz);
// Enable useful functions in the console - they are all set to false by default
// enable history logging - toggle with up and down arrows
client.enableHistoryLogging(true);
// enable showing the history index in the prompt (history logging must be enabled)
client.enableHistoryIndexDisplay(true);
// enable tab completion
client.enableTabCompletion(true);
// enable alerts
client.enableAlerts(true);
// customize prompt
client.setPromptDisplayName("ExampleApp-v1.0");
client.setModuleSeparator("/");
client.setPromptSeparator(">");
// print a welcome message
System.out.println("\nWelcome to the example app!\n");
// add a shutdown hook
client.addShutdownHook(newThread() {
@Override
publicvoidrun() {
System.out.println("Goodbye!\n");
}
});
// run the console client
client.runConsole();
}
}