MCPcopy Create free account
hub / github.com/Berkeley-CS61B/skeleton-sp23 / DemoInputSource

Class DemoInputSource

proj3/byow/InputDemo/DemoInputSource.java:8–42  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

6 * from they keyboard, from a random sequence, from a string, or whatever else.
7 */
8public class DemoInputSource {
9 private static final int KEYBOARD = 0;
10 private static final int RANDOM = 1;
11 private static final int STRING = 2;
12
13 public static void main(String[] args) {
14 int inputType = KEYBOARD;
15
16 InputSource inputSource;
17
18 if (inputType == KEYBOARD) {
19 inputSource = new KeyboardInputSource();
20 } else if (inputType == RANDOM) {
21 inputSource = new RandomInputSource(50L);
22 } else { // inputType == STRING
23 inputSource = new StringInputDevice("HELLO MY FRIEND. QUACK QUACK");
24 }
25
26 int totalCharacters = 0;
27
28 while (inputSource.possibleNextInput()) {
29 totalCharacters += 1;
30 char c = inputSource.getNextKey();
31 if (c == 'M') {
32 System.out.println("moo");
33 }
34 if (c == 'Q') {
35 System.out.println("done.");
36 break;
37 }
38 }
39
40 System.out.println("Processed " + totalCharacters + " characters.");
41 }
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected