|
| 1 | +package pgdp; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.Random; |
| 5 | + |
| 6 | +/** |
| 7 | + * This class provides convenience methods to make the barrier of entry into |
| 8 | + * programming easier. |
| 9 | + * <p> |
| 10 | + * <i>Diese Klasse stellt simple Methoden zur Verfügung um den Einstieg in die |
| 11 | + * Programmierung zu vereinfachen.</i> |
| 12 | + */ |
| 13 | +public class PinguLib { |
| 14 | + |
| 15 | + private static InputStream is = System.in; |
| 16 | + private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); |
| 17 | + |
| 18 | + /** |
| 19 | + * Prints the {@link String} to the console and breaks the line. |
| 20 | + * <p> |
| 21 | + * <i>Gibt den {@link String} auf der Konsole aus und beginnt eine neue |
| 22 | + * Zeile.</i> |
| 23 | + */ |
| 24 | + public static void write(String output) { |
| 25 | + System.out.println(output); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Prints the <code>int</code> to the console and breaks the line. |
| 30 | + * <p> |
| 31 | + * <i>Gibt den <code>int</code>-Wert auf der Konsole aus und beginnt eine neue |
| 32 | + * Zeile.</i> |
| 33 | + */ |
| 34 | + public static void write(int output) { |
| 35 | + write(String.valueOf(output)); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Prints the <code>double</code> to the console and breaks the line. |
| 40 | + * <p> |
| 41 | + * <i>Gibt den <code>double</code>-Wert auf der Konsole aus und beginnt eine |
| 42 | + * neue Zeile.</i> |
| 43 | + */ |
| 44 | + public static void write(double output) { |
| 45 | + write(String.valueOf(output)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Identical to {@link #write(String)}. |
| 50 | + * <p> |
| 51 | + * <i>Identisch zu {@link #write(String)}</i> |
| 52 | + */ |
| 53 | + public static void writeLineConsole(String output) { |
| 54 | + System.out.println(output); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Identical to {@link #write(int)}. |
| 59 | + * <p> |
| 60 | + * <i>Identisch zu {@link #write(int)}</i> |
| 61 | + */ |
| 62 | + public static void writeLineConsole(int output) { |
| 63 | + writeLineConsole(String.valueOf(output)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Identical to {@link #write(double)}. |
| 68 | + * <p> |
| 69 | + * <i>Identisch zu {@link #write(double)}</i> |
| 70 | + */ |
| 71 | + public static void writeLineConsole(double output) { |
| 72 | + writeLineConsole(String.valueOf(output)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Prints a line separator in the console. |
| 77 | + * <p> |
| 78 | + * <i>Gibt einen Zeilenumbruch auf der Konsole aus.</i> |
| 79 | + */ |
| 80 | + public static void writeLineConsole() { |
| 81 | + writeLineConsole(""); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Prints the {@link String} to the console. Does not end with a line break. |
| 86 | + * <p> |
| 87 | + * <i>Gibt den {@link String} auf der Konsole aus. Endet nicht mit einem |
| 88 | + * Zeilenumbruch.</i> |
| 89 | + */ |
| 90 | + public static void writeConsole(String output) { |
| 91 | + System.out.print(output); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Prints the <code>int</code> to the console. Does not end with a line break. |
| 96 | + * <p> |
| 97 | + * <i>Gibt den <code>int</code>-Wert auf der Konsole aus. Endet nicht mit einem |
| 98 | + * Zeilenumbruch.</i> |
| 99 | + */ |
| 100 | + public static void writeConsole(int output) { |
| 101 | + writeConsole(String.valueOf(output)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Prints the <code>double</code> to the console. Does not end with a line |
| 106 | + * break. |
| 107 | + * <p> |
| 108 | + * <i>Gibt den <code>double</code>-Wert auf der Konsole aus. Endet nicht mit |
| 109 | + * einem Zeilenumbruch.</i> |
| 110 | + */ |
| 111 | + public static void writeConsole(double output) { |
| 112 | + writeConsole(String.valueOf(output)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Reads a {@link String} from the console, and prompts the user by printing the |
| 117 | + * given <code>text</code> with a line break to the console. |
| 118 | + * <p> |
| 119 | + * |
| 120 | + * <i>Liest einen {@link String} von der Konsole ein, und fordert den oder die |
| 121 | + * Benutzerin durch die Ausgabe des übergebenen <code>text</code>es zur Eingabe |
| 122 | + * auf (mit Zeilenumbruch).</i> |
| 123 | + * |
| 124 | + * @param text the text to display on the console before reading an input. |
| 125 | + * @return the input string or <code>null</code>, if no input is available |
| 126 | + * (should normally not happen) |
| 127 | + */ |
| 128 | + public static String readString(String text) { |
| 129 | + // Exchange the reader in case System.in has changed. |
| 130 | + // This is necessary for testing, as for every test input, System.in is |
| 131 | + // changed. |
| 132 | + if (System.in != is) { |
| 133 | + is = System.in; |
| 134 | + bufferedReader = new BufferedReader(new InputStreamReader(is)); |
| 135 | + } |
| 136 | + try { |
| 137 | + System.out.println(text); |
| 138 | + return bufferedReader.readLine(); |
| 139 | + } catch (IOException e) { |
| 140 | + // We "hide" the exception in the method signature by rethrowing an |
| 141 | + // unchecked |
| 142 | + // exception |
| 143 | + throw new UncheckedIOException("Konnte Eingabe nicht lesen.", e); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Reads a {@link String} from the console, and prompts the user by printing the |
| 149 | + * line |
| 150 | + * |
| 151 | + * <pre> |
| 152 | + * Eingabe: |
| 153 | + * </pre> |
| 154 | + * |
| 155 | + * <i>Liest einen {@link String} von der Konsole ein, und fordert den oder die |
| 156 | + * Benutzerin dazu auf durch die Ausgabe von |
| 157 | + * |
| 158 | + * <pre> |
| 159 | + * Eingabe: |
| 160 | + * </pre> |
| 161 | + * |
| 162 | + * </i> |
| 163 | + */ |
| 164 | + public static String readString() { |
| 165 | + return readString("Eingabe:"); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Tries to read an <code>int</code> from the console, and retires if the input |
| 170 | + * was not a valid integer. It prompts the user by printing the given |
| 171 | + * <code>text</code> with a line break to the console. |
| 172 | + * <p> |
| 173 | + * <i>Versucht einen <code>int</code>-Wert von der Konsole einzulesen, und |
| 174 | + * wiederholt die Anfrage solange es nicht nicht um eine ganze Zahl handelt. Der |
| 175 | + * oder die Benutzerin wird durch die Ausgabe des übergebenen |
| 176 | + * <code>text</code>es zur Eingabe aufgefordert (mit Zeilenumbruch).</i> |
| 177 | + * |
| 178 | + * @see Integer#parseInt(String) |
| 179 | + */ |
| 180 | + public static int readInt(String text) { |
| 181 | + Integer x = null; |
| 182 | + do { |
| 183 | + String s = readString(text); |
| 184 | + if (s == null) { |
| 185 | + throw new IllegalStateException("Es ist keine Eingabe (mehr) verfügbar."); |
| 186 | + } |
| 187 | + try { |
| 188 | + x = Integer.parseInt(s.trim()); |
| 189 | + } catch (@SuppressWarnings("unused") NumberFormatException e) { |
| 190 | + // try again |
| 191 | + } |
| 192 | + } while (x == null); |
| 193 | + return x; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Works like {@link #readInt(String)}, but uses the following preset text to |
| 198 | + * prompt the user: |
| 199 | + * |
| 200 | + * <pre> |
| 201 | + * Geben Sie eine ganze Zahl ein: |
| 202 | + * </pre> |
| 203 | + * |
| 204 | + * <i>Funktioniert wie {@link #readInt(String)}, nutzt jedoch folgenden |
| 205 | + * vorgegebenen Text: |
| 206 | + * |
| 207 | + * <pre> |
| 208 | + * Geben Sie eine ganze Zahl ein: |
| 209 | + * </pre> |
| 210 | + * |
| 211 | + * </i> |
| 212 | + */ |
| 213 | + public static int readInt() { |
| 214 | + return readInt("Geben Sie eine ganze Zahl ein:"); |
| 215 | + } |
| 216 | +} |
0 commit comments