-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8df85dd
commit 677afec
Showing
6 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plugins { | ||
id 'com.craftsmanshipinsoftware.common-conventions' | ||
} | ||
|
||
dependencies { | ||
testImplementation(libs.assertj.core) | ||
testImplementation(libs.junit.jupiter) | ||
} |
57 changes: 57 additions & 0 deletions
57
mad-lib/src/main/java/com/craftsmanshipinsoftware/madlib/MadLib.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.craftsmanshipinsoftware.madlib; | ||
|
||
import java.io.InputStream; | ||
import java.io.PrintStream; | ||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
class MadLib { | ||
|
||
private final InputStream inputStream; | ||
private final PrintStream printStream; | ||
|
||
MadLib(InputStream inputStream, PrintStream printStream) { | ||
Objects.requireNonNull(inputStream, "inputStream cannot be null"); | ||
Objects.requireNonNull(printStream, "printStream cannot be null"); | ||
this.inputStream = inputStream; | ||
this.printStream = printStream; | ||
} | ||
|
||
void printStory() { | ||
try (Scanner scanner = new Scanner(inputStream)) { | ||
String noun = promptForInput("Enter a noun: ", scanner); | ||
if (noun == null) { | ||
return; | ||
} | ||
String verb = promptForInput("Enter a verb: ", scanner); | ||
if (verb == null) { | ||
return; | ||
} | ||
String adjective = promptForInput("Enter an adjective: ", scanner); | ||
if (adjective == null) { | ||
return; | ||
} | ||
String adverb = promptForInput("Enter an adverb: ", scanner); | ||
if (adverb == null) { | ||
return; | ||
} | ||
this.printStream.println("Do you " + verb + " your " + adjective + " " + noun + " " + adverb + "? That's hilarious!"); | ||
} | ||
} | ||
|
||
@SuppressWarnings("PMD.SystemPrintln") | ||
private String promptForInput(String prompt, Scanner scanner) { | ||
System.out.print(prompt); | ||
String input = readInput(scanner); | ||
if (input == null || input.isBlank()) { | ||
this.printStream.println("Please enter something as input!"); | ||
return null; | ||
} | ||
return input; | ||
} | ||
|
||
private String readInput(Scanner scanner) { | ||
return scanner.hasNext() ? scanner.nextLine() : null; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
mad-lib/src/main/java/com/craftsmanshipinsoftware/madlib/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.craftsmanshipinsoftware.madlib; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
MadLib madLib = new MadLib(System.in, System.out); | ||
madLib.printStory(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
mad-lib/src/test/java/com/craftsmanshipinsoftware/madlib/MadLibTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.craftsmanshipinsoftware.madlib; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class MadLibTest { | ||
|
||
@ParameterizedTest(name = "Given input [{0}]") | ||
@ValueSource(strings = {"", "dog", "dog\nwalk", "dog\nwalk\nblue"}) | ||
void printStory_GivenInputIsMissing_ShouldAskForMissingInput(String input) { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
MadLib madLib = new MadLib(new ByteArrayInputStream(input.getBytes()), new PrintStream(outputStream)); | ||
madLib.printStory(); | ||
assertThat(outputStream).hasToString("Please enter something as input!" + System.lineSeparator()); | ||
} | ||
|
||
@Test | ||
void printStory_GivenAllNeededInput_ShouldPrintStory() { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
MadLib madLib = new MadLib(new ByteArrayInputStream("dog\nwalk\nblue\nquickly".getBytes()), new PrintStream(outputStream)); | ||
madLib.printStory(); | ||
assertThat(outputStream).hasToString("Do you walk your blue dog quickly? That's hilarious!" + System.lineSeparator()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = 'exercises-for-programmers-java' | ||
include('saying-hello', 'characters-count', 'printing-quotes') | ||
include('saying-hello', 'characters-count', 'printing-quotes', 'mad-lib') |