Skip to content

Commit fd94672

Browse files
committed
Add 'Printing Quotes' solution
1 parent 574e62c commit fd94672

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Exercises for Programmers: 57 Challenges to Develop Your Coding Skills by Brian
66
**Input, Processing, and Output**
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)
9-
- Exercise 3. Printing Quotes
9+
- Exercise 3. [Printing Quotes](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/printing-quotes)
1010
- Exercise 4. Mad Lib
1111
- Exercise 5. Simple Math
1212
- Exercise 6. Retirement Calculator

printing-quotes/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation(libs.assertj.core)
11+
testImplementation(libs.junit.jupiter)
12+
}
13+
14+
tasks.named('test') {
15+
useJUnitPlatform()
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.craftsmanshipinsoftware.prntqts;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
QuotesPrinter quotesPrinter = new QuotesPrinter(System.in, System.out);
7+
quotesPrinter.displayQuote();
8+
}
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.craftsmanshipinsoftware.prntqts;
2+
3+
import java.io.InputStream;
4+
import java.io.PrintStream;
5+
import java.util.Objects;
6+
import java.util.Scanner;
7+
8+
class QuotesPrinter {
9+
10+
private final InputStream inputStream;
11+
private final PrintStream printStream;
12+
13+
QuotesPrinter(InputStream inputStream, PrintStream printStream) {
14+
Objects.requireNonNull(inputStream, "inputStream must not be null");
15+
Objects.requireNonNull(printStream, "printStream must not be null");
16+
this.inputStream = inputStream;
17+
this.printStream = printStream;
18+
}
19+
20+
void displayQuote() {
21+
try (Scanner scanner = new Scanner(this.inputStream)) {
22+
askForQuote();
23+
String quote = scanner.hasNext() ? scanner.nextLine() : null;
24+
askForAuthor();
25+
String author = scanner.hasNext() ? scanner.nextLine() : null;
26+
if (quote == null || quote.isBlank()) {
27+
this.printStream.println("Please enter the quote!");
28+
} else if (author == null || author.isBlank()) {
29+
this.printStream.println("Please enter the author!");
30+
} else {
31+
this.printStream.printf("%s says, \"%s\"%n", author, quote);
32+
}
33+
}
34+
}
35+
36+
private void askForQuote() {
37+
System.out.print("What is the quote? ");
38+
}
39+
40+
private void askForAuthor() {
41+
System.out.print("Who said it? ");
42+
}
43+
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.craftsmanshipinsoftware.prntqts;
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+
10+
class QuotesPrinterTest {
11+
12+
@Test
13+
void displayQuote_GivenNoQuote_ShouldAskToEnterTheQuote() {
14+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
15+
QuotesPrinter quotesPrinter = new QuotesPrinter(new ByteArrayInputStream(new byte[0]), new PrintStream(outputStream));
16+
17+
quotesPrinter.displayQuote();
18+
19+
assertThat(outputStream).hasToString("Please enter the quote!" + System.lineSeparator());
20+
}
21+
22+
@Test
23+
void displayQuote_GivenQuoteWithoutAuthor_ShouldAskToEnterTheAuthor() {
24+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
25+
QuotesPrinter quotesPrinter = new QuotesPrinter(new ByteArrayInputStream("To be or not to be".getBytes()), new PrintStream(outputStream));
26+
27+
quotesPrinter.displayQuote();
28+
29+
assertThat(outputStream).hasToString("Please enter the author!" + System.lineSeparator());
30+
}
31+
32+
@Test
33+
void displayQuote_GivenQuoteAndAuthor_ShouldPrintQuoteWithAuthor() {
34+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
35+
QuotesPrinter quotesPrinter = new QuotesPrinter(new ByteArrayInputStream("To be or not to be\nWilliam Shakespeare".getBytes()),
36+
new PrintStream(outputStream));
37+
38+
quotesPrinter.displayQuote();
39+
40+
assertThat(outputStream).hasToString("William Shakespeare says, \"To be or not to be\"" + System.lineSeparator());
41+
}
42+
}

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')
2+
include('saying-hello', 'characters-count', 'printing-quotes')

0 commit comments

Comments
 (0)