Convert 20_Buzzword to Java

Provide the output PrintStream dependency to UserInterface in the
constructor instead of have it hard coded to System.out.
This commit is contained in:
Brax Antti (Oy Samlink Ab)
2022-01-12 14:33:55 +02:00
parent 3e9f1354b3
commit c8633c6051
2 changed files with 27 additions and 20 deletions

View File

@@ -10,7 +10,7 @@ public class Buzzword {
) { ) {
final BuzzwordSupplier buzzwords = new BuzzwordSupplier(); final BuzzwordSupplier buzzwords = new BuzzwordSupplier();
final UserInterface userInterface = new UserInterface( final UserInterface userInterface = new UserInterface(
scanner, buzzwords); scanner, System.out, buzzwords);
userInterface.run(); userInterface.run();
} }
} }

View File

@@ -1,5 +1,4 @@
import static java.lang.System.out; import java.io.PrintStream;
import java.util.Scanner; import java.util.Scanner;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -14,6 +13,11 @@ public class UserInterface implements Runnable {
*/ */
private final Scanner input; private final Scanner input;
/**
* Output to the user.
*/
private final PrintStream output;
/** /**
* The buzzword generator. * The buzzword generator.
*/ */
@@ -23,35 +27,38 @@ public class UserInterface implements Runnable {
* Create a new user interface. * Create a new user interface.
* *
* @param input The input scanner with which the user gives commands. * @param input The input scanner with which the user gives commands.
* @param output The output to show messages to the user.
* @param buzzwords The buzzword supplier. * @param buzzwords The buzzword supplier.
*/ */
public UserInterface(final Scanner input, public UserInterface(final Scanner input,
final Supplier<String> buzzwords) { final PrintStream output,
final Supplier<String> buzzwords) {
this.input = input; this.input = input;
this.output = output;
this.buzzwords = buzzwords; this.buzzwords = buzzwords;
} }
@Override @Override
public void run() { public void run() {
out.println(" BUZZWORD GENERATOR"); output.println(" BUZZWORD GENERATOR");
out.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); output.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
out.println(); output.println();
out.println(); output.println();
out.println(); output.println();
out.println("THIS PROGRAM PRINTS HIGHLY ACCEPTABLE PHRASES IN"); output.println("THIS PROGRAM PRINTS HIGHLY ACCEPTABLE PHRASES IN");
out.println("'EDUCATOR-SPEAK' THAT YOU CAN WORK INTO REPORTS"); output.println("'EDUCATOR-SPEAK' THAT YOU CAN WORK INTO REPORTS");
out.println("AND SPEECHES. WHENEVER A QUESTION MARK IS PRINTED,"); output.println("AND SPEECHES. WHENEVER A QUESTION MARK IS PRINTED,");
out.println("TYPE A 'Y' FOR ANOTHER PHRASE OR 'N' TO QUIT."); output.println("TYPE A 'Y' FOR ANOTHER PHRASE OR 'N' TO QUIT.");
out.println(); output.println();
out.println(); output.println();
out.println("HERE'S THE FIRST PHRASE:"); output.println("HERE'S THE FIRST PHRASE:");
do { do {
out.println(buzzwords.get()); output.println(buzzwords.get());
out.println(); output.println();
out.print("?"); output.print("?");
} while ("Y".equals(input.nextLine().toUpperCase())); } while ("Y".equals(input.nextLine().toUpperCase()));
out.println("COME BACK WHEN YOU NEED HELP WITH ANOTHER REPORT!"); output.println("COME BACK WHEN YOU NEED HELP WITH ANOTHER REPORT!");
} }
} }