Skip to content

Commit 59e228a

Browse files
committed
Use line-io in simple-math
1 parent 9886eb5 commit 59e228a

4 files changed

Lines changed: 59 additions & 72 deletions

File tree

simple-math/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ dependencies {
77
implementation enforcedPlatform(libs.spring.boot.dependencies)
88
implementation(libs.spring.boot.starter.webmvc)
99

10+
implementation(project(':line-io'))
11+
testImplementation(testFixtures(project(':line-io')))
12+
1013
testImplementation(libs.spring.boot.starter.webmvc.test)
1114
testImplementation(libs.assertj.core)
1215
testImplementation(libs.junit.jupiter)
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package dev.delivercraft.math.console;
22

3+
import dev.delivercraft.io.InputStreamLineReader;
4+
import dev.delivercraft.io.LineReader;
5+
import dev.delivercraft.io.LineWriter;
6+
import dev.delivercraft.io.OutputStreamLineWriter;
7+
38
public final class Main {
49

510
void main() {
6-
SimpleMath simpleMath = new SimpleMath(System.in, System.out);
11+
LineReader lineReader = new InputStreamLineReader(System.in);
12+
LineWriter lineWriter = new OutputStreamLineWriter(System.out);
13+
SimpleMath simpleMath = new SimpleMath(lineReader, lineWriter);
714
simpleMath.printOutput();
815
}
916
}

simple-math/src/main/java/dev/delivercraft/math/console/SimpleMath.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
package dev.delivercraft.math.console;
22

3-
import java.io.InputStream;
4-
import java.io.PrintStream;
3+
import dev.delivercraft.io.LineReader;
4+
import dev.delivercraft.io.LineWriter;
5+
56
import java.util.Objects;
6-
import java.util.Scanner;
77

88
class SimpleMath {
99

10-
private final PrintStream printStream;
10+
private final LineReader lineReader;
1111

12-
private final InputStream inputStream;
12+
private final LineWriter lineWriter;
1313

14-
SimpleMath(InputStream inputStream, PrintStream printStream) {
15-
Objects.requireNonNull(inputStream, "inputStream must not be null");
16-
Objects.requireNonNull(printStream, "printStream must not be null");
17-
this.printStream = printStream;
18-
this.inputStream = inputStream;
14+
SimpleMath(LineReader lineReader, LineWriter lineWriter) {
15+
Objects.requireNonNull(lineReader, "lineReader must not be null");
16+
Objects.requireNonNull(lineWriter, "lineWriter must not be null");
17+
this.lineReader = lineReader;
18+
this.lineWriter = lineWriter;
1919
}
2020

2121
void printOutput() {
22-
try (Scanner scanner = new Scanner(this.inputStream)) {
23-
String firstInput = promptForInput("What is the first number? ", scanner);
24-
PositiveInteger firstNumber = PositiveInteger.of(firstInput);
25-
String secondInput = promptForInput("What is the second number? ", scanner);
26-
PositiveInteger secondNumber = PositiveInteger.of(secondInput);
27-
this.printStream.println(formattedOutput(firstNumber, secondNumber));
28-
}
22+
String firstInput = promptForInput("What is the first number? ");
23+
PositiveInteger firstNumber = PositiveInteger.of(firstInput);
24+
String secondInput = promptForInput("What is the second number? ");
25+
PositiveInteger secondNumber = PositiveInteger.of(secondInput);
26+
this.lineWriter.writeLine(formattedOutput(firstNumber, secondNumber));
2927
}
3028

31-
@SuppressWarnings("PMD.SystemPrintln")
32-
private String promptForInput(String prompt, Scanner scanner) {
33-
System.out.print(prompt);
34-
String input = scanner.hasNext() ? scanner.nextLine() : null;
29+
private String promptForInput(String prompt) {
30+
this.lineWriter.write(prompt);
31+
String input = this.lineReader.readLine();
3532
if (input == null || input.isBlank()) {
3633
throw new IllegalArgumentException("Input must not be empty!");
3734
}

simple-math/src/test/java/dev/delivercraft/math/console/SimpleMathTest.java

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,52 @@
11
package dev.delivercraft.math.console;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
5-
6-
import java.io.ByteArrayInputStream;
7-
import java.io.ByteArrayOutputStream;
8-
import java.io.PrintStream;
3+
import dev.delivercraft.io.CapturingLineWriter;
4+
import dev.delivercraft.io.LineReader;
5+
import dev.delivercraft.io.LineWriter;
6+
import dev.delivercraft.io.StubLineReader;
97
import org.junit.jupiter.api.Test;
108
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.CsvSource;
10+
import org.junit.jupiter.params.provider.NullAndEmptySource;
1111
import org.junit.jupiter.params.provider.ValueSource;
1212

13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
15+
1316
class SimpleMathTest {
1417

1518
@ParameterizedTest
16-
@ValueSource(strings = {"", " ", "10\n", "\n5"})
19+
@NullAndEmptySource
20+
@ValueSource(strings = {" "})
1721
void inputIsRequired(String input) {
18-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream(input.getBytes()),
19-
new PrintStream(new ByteArrayOutputStream()));
22+
LineReader lineReader = () -> input;
23+
SimpleMath simpleMath = new SimpleMath(lineReader, new CapturingLineWriter());
2024

2125
assertThatIllegalArgumentException()
2226
.isThrownBy(simpleMath::printOutput)
2327
.withMessage("Input must not be empty!");
2428
}
2529

26-
@Test
27-
void firstInputMustBeANumber() {
28-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("abc".getBytes()),
29-
new PrintStream(new ByteArrayOutputStream()));
30-
31-
assertThatIllegalArgumentException()
32-
.isThrownBy(simpleMath::printOutput)
33-
.withMessage("Please enter a valid number! Input: abc");
34-
}
35-
36-
@Test
37-
void firstNumberMustBePositive() {
38-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("-10\n5".getBytes()),
39-
new PrintStream(new ByteArrayOutputStream()));
40-
41-
assertThatIllegalArgumentException()
42-
.isThrownBy(simpleMath::printOutput)
43-
.withMessage("Please enter a positive number! Input: -10");
44-
}
45-
46-
@Test
47-
void secondInputMustBeANumber() {
48-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\nasdf".getBytes()),
49-
new PrintStream(new ByteArrayOutputStream()));
50-
51-
assertThatIllegalArgumentException()
52-
.isThrownBy(simpleMath::printOutput)
53-
.withMessage("Please enter a valid number! Input: asdf");
54-
}
55-
56-
@Test
57-
void secondNumberMustBePositive() {
58-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n-5".getBytes()),
59-
new PrintStream(new ByteArrayOutputStream()));
30+
@ParameterizedTest
31+
@CsvSource({
32+
"abc, 5, Please enter a valid number! Input: abc",
33+
"-10, 5, Please enter a positive number! Input: -10",
34+
"10, asdf, Please enter a valid number! Input: asdf",
35+
"20, -5, Please enter a positive number! Input: -5"
36+
})
37+
void numbersMustBeValidPositiveNumbers(String firstInput, String secondInput, String expectedMessage) {
38+
LineReader lineReader = new StubLineReader(firstInput, secondInput);
39+
SimpleMath simpleMath = new SimpleMath(lineReader, new CapturingLineWriter());
6040

6141
assertThatIllegalArgumentException()
6242
.isThrownBy(simpleMath::printOutput)
63-
.withMessage("Please enter a positive number! Input: -5");
43+
.withMessage(expectedMessage);
6444
}
6545

6646
@Test
6747
void secondNumberMustNotBeZero() {
68-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n0".getBytes()),
69-
new PrintStream(new ByteArrayOutputStream()));
48+
LineReader lineReader = new StubLineReader("10", "0");
49+
SimpleMath simpleMath = new SimpleMath(lineReader, new CapturingLineWriter());
7050

7151
assertThatIllegalArgumentException()
7252
.isThrownBy(simpleMath::printOutput)
@@ -75,13 +55,13 @@ void secondNumberMustNotBeZero() {
7555

7656
@Test
7757
void sumDifferenceProductAndQuotientIsPrinted() {
78-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
79-
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n5".getBytes()),
80-
new PrintStream(outputStream));
58+
LineReader lineReader = new StubLineReader("10", "5");
59+
LineWriter lineWriter = new CapturingLineWriter();
60+
SimpleMath simpleMath = new SimpleMath(lineReader, lineWriter);
8161

8262
simpleMath.printOutput();
8363

84-
assertThat(outputStream).hasToString("""
64+
assertThat(lineWriter.toString()).containsOnlyOnce("""
8565
10 + 5 = 15
8666
10 - 5 = 5
8767
10 * 5 = 50

0 commit comments

Comments
 (0)