Skip to content

Commit 677afec

Browse files
committed
Add 'Mad Lib' solution
1 parent 8df85dd commit 677afec

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Exercises for Programmers: 57 Challenges to Develop Your Coding Skills by Brian
77
- Exercise 1. [Saying Hello](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/saying-hello)
88
- Exercise 2. [Counting the Number of Characters](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/characters-count)
99
- Exercise 3. [Printing Quotes](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/printing-quotes)
10-
- Exercise 4. Mad Lib
10+
- Exercise 4. [Mad Lib](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/mad-lib)
1111
- Exercise 5. Simple Math
1212
- Exercise 6. Retirement Calculator
1313

mad-lib/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
id 'com.craftsmanshipinsoftware.common-conventions'
3+
}
4+
5+
dependencies {
6+
testImplementation(libs.assertj.core)
7+
testImplementation(libs.junit.jupiter)
8+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.craftsmanshipinsoftware.madlib;
2+
3+
import java.io.InputStream;
4+
import java.io.PrintStream;
5+
import java.util.Objects;
6+
import java.util.Scanner;
7+
8+
class MadLib {
9+
10+
private final InputStream inputStream;
11+
private final PrintStream printStream;
12+
13+
MadLib(InputStream inputStream, PrintStream printStream) {
14+
Objects.requireNonNull(inputStream, "inputStream cannot be null");
15+
Objects.requireNonNull(printStream, "printStream cannot be null");
16+
this.inputStream = inputStream;
17+
this.printStream = printStream;
18+
}
19+
20+
void printStory() {
21+
try (Scanner scanner = new Scanner(inputStream)) {
22+
String noun = promptForInput("Enter a noun: ", scanner);
23+
if (noun == null) {
24+
return;
25+
}
26+
String verb = promptForInput("Enter a verb: ", scanner);
27+
if (verb == null) {
28+
return;
29+
}
30+
String adjective = promptForInput("Enter an adjective: ", scanner);
31+
if (adjective == null) {
32+
return;
33+
}
34+
String adverb = promptForInput("Enter an adverb: ", scanner);
35+
if (adverb == null) {
36+
return;
37+
}
38+
this.printStream.println("Do you " + verb + " your " + adjective + " " + noun + " " + adverb + "? That's hilarious!");
39+
}
40+
}
41+
42+
@SuppressWarnings("PMD.SystemPrintln")
43+
private String promptForInput(String prompt, Scanner scanner) {
44+
System.out.print(prompt);
45+
String input = readInput(scanner);
46+
if (input == null || input.isBlank()) {
47+
this.printStream.println("Please enter something as input!");
48+
return null;
49+
}
50+
return input;
51+
}
52+
53+
private String readInput(Scanner scanner) {
54+
return scanner.hasNext() ? scanner.nextLine() : null;
55+
}
56+
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.craftsmanshipinsoftware.madlib;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
MadLib madLib = new MadLib(System.in, System.out);
7+
madLib.printStory();
8+
}
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.craftsmanshipinsoftware.madlib;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.PrintStream;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.ValueSource;
11+
12+
class MadLibTest {
13+
14+
@ParameterizedTest(name = "Given input [{0}]")
15+
@ValueSource(strings = {"", "dog", "dog\nwalk", "dog\nwalk\nblue"})
16+
void printStory_GivenInputIsMissing_ShouldAskForMissingInput(String input) {
17+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
18+
MadLib madLib = new MadLib(new ByteArrayInputStream(input.getBytes()), new PrintStream(outputStream));
19+
madLib.printStory();
20+
assertThat(outputStream).hasToString("Please enter something as input!" + System.lineSeparator());
21+
}
22+
23+
@Test
24+
void printStory_GivenAllNeededInput_ShouldPrintStory() {
25+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
26+
MadLib madLib = new MadLib(new ByteArrayInputStream("dog\nwalk\nblue\nquickly".getBytes()), new PrintStream(outputStream));
27+
madLib.printStory();
28+
assertThat(outputStream).hasToString("Do you walk your blue dog quickly? That's hilarious!" + System.lineSeparator());
29+
}
30+
31+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rootProject.name = 'exercises-for-programmers-java'
2-
include('saying-hello', 'characters-count', 'printing-quotes')
2+
include('saying-hello', 'characters-count', 'printing-quotes', 'mad-lib')

0 commit comments

Comments
 (0)