-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoInputSource
More file actions
42 lines (35 loc) · 1.22 KB
/
DemoInputSource
File metadata and controls
42 lines (35 loc) · 1.22 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
package byow.InputDemo;
/**
* Created by hug.
* Demonstrates how a single interface can be used to provide input
* from they keyboard, from a random sequence, from a string, or whatever else.
*/
public class DemoInputSource {
private static final int KEYBOARD = 0;
private static final int RANDOM = 1;
private static final int STRING = 2;
public static void main(String[] args) {
int inputType = KEYBOARD;
InputSource inputSource;
if (inputType == KEYBOARD) {
inputSource = new KeyboardInputSource();
} else if (inputType == RANDOM) {
inputSource = new RandomInputSource(50L);
} else { // inputType == STRING
inputSource = new StringInputDevice("HELLO MY FRIEND. QUACK QUACK");
}
int totalCharacters = 0;
while (inputSource.possibleNextInput()) {
totalCharacters += 1;
char c = inputSource.getNextKey();
if (c == 'M') {
System.out.println("moo");
}
if (c == 'Q') {
System.out.println("done.");
break;
}
}
System.out.println("Processed " + totalCharacters + " characters.");
}
}