-
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
f0af162
commit ab1e8e5
Showing
6 changed files
with
189 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = 'exercises-for-programmers-java' | ||
include('saying-hello', 'characters-count', 'printing-quotes', 'mad-lib') | ||
include('saying-hello', 'characters-count', 'printing-quotes', 'mad-lib', 'simple-math') |
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) | ||
} |
9 changes: 9 additions & 0 deletions
9
simple-math/src/main/java/com/craftsmanshipinsoftware/math/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.math; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
SimpleMath simpleMath = new SimpleMath(System.in, System.out); | ||
simpleMath.printOutput(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
simple-math/src/main/java/com/craftsmanshipinsoftware/math/SimpleMath.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,86 @@ | ||
package com.craftsmanshipinsoftware.math; | ||
|
||
import java.io.InputStream; | ||
import java.io.PrintStream; | ||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
class SimpleMath { | ||
|
||
private final PrintStream printStream; | ||
private final InputStream inputStream; | ||
|
||
SimpleMath(InputStream inputStream, PrintStream printStream) { | ||
Objects.requireNonNull(inputStream, "inputStream must not be null"); | ||
Objects.requireNonNull(printStream, "printStream must not be null"); | ||
this.printStream = printStream; | ||
this.inputStream = inputStream; | ||
} | ||
|
||
void printOutput() { | ||
try (Scanner scanner = new Scanner(inputStream)) { | ||
String firstInput = promptForInput("What is the first number? ", scanner); | ||
int firstNumber = validatedInput(firstInput); | ||
String secondInput = promptForInput("What is the second number? ", scanner); | ||
int secondNumber = validatedInput(secondInput); | ||
if (secondNumber == 0) { | ||
throw new IllegalArgumentException("Cannot divide by zero!"); | ||
} | ||
this.printStream.println(output(firstNumber, secondNumber)); | ||
} | ||
} | ||
|
||
@SuppressWarnings("PMD.SystemPrintln") | ||
private String promptForInput(String prompt, Scanner scanner) { | ||
System.out.print(prompt); | ||
String input = readInput(scanner); | ||
if (input == null || input.isBlank()) { | ||
throw new IllegalArgumentException("Input must not be empty!"); | ||
} | ||
return input; | ||
} | ||
|
||
private String readInput(Scanner scanner) { | ||
return scanner.hasNext() ? scanner.nextLine() : null; | ||
} | ||
|
||
private static int validatedInput(String input) { | ||
try { | ||
int number = Integer.parseInt(input); | ||
if (number < 0) { | ||
throw new IllegalArgumentException("Please enter a positive number! Input: " + number); | ||
} | ||
return number; | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("Please enter a valid number! Input: " + input, e); | ||
} | ||
} | ||
|
||
private static String output(int firstNumber, int secondNumber) { | ||
return String.format(""" | ||
%d + %d = %d | ||
%d - %d = %d | ||
%d * %d = %d | ||
%d / %d = %d""", | ||
firstNumber, secondNumber, add(firstNumber, secondNumber), | ||
firstNumber, secondNumber, subtract(firstNumber, secondNumber), | ||
firstNumber, secondNumber, multiply(firstNumber, secondNumber), | ||
firstNumber, secondNumber, divide(firstNumber, secondNumber)); | ||
} | ||
|
||
private static int add(int firstNumber, int secondNumber) { | ||
return firstNumber + secondNumber; | ||
} | ||
|
||
private static int subtract(int firstNumber, int secondNumber) { | ||
return firstNumber - secondNumber; | ||
} | ||
|
||
private static int multiply(int firstNumber, int secondNumber) { | ||
return firstNumber * secondNumber; | ||
} | ||
|
||
private static int divide(int firstNumber, int secondNumber) { | ||
return firstNumber / secondNumber; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
simple-math/src/test/java/com/craftsmanshipinsoftware/math/SimpleMathTest.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,84 @@ | ||
package com.craftsmanshipinsoftware.math; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
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 SimpleMathTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"", " ", "10\n", "\n5"}) | ||
void inputIsRequired(String input) { | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream(input.getBytes()), new PrintStream(new ByteArrayOutputStream())); | ||
|
||
assertThatIllegalArgumentException() | ||
.isThrownBy(simpleMath::printOutput) | ||
.withMessage("Input must not be empty!"); | ||
} | ||
|
||
@Test | ||
void firstInputMustBeANumber() { | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("abc".getBytes()), new PrintStream(new ByteArrayOutputStream())); | ||
|
||
assertThatIllegalArgumentException() | ||
.isThrownBy(simpleMath::printOutput) | ||
.withMessage("Please enter a valid number! Input: abc"); | ||
} | ||
|
||
@Test | ||
void firstNumberMustBePositive() { | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("-10\n5".getBytes()), new PrintStream(new ByteArrayOutputStream())); | ||
|
||
assertThatIllegalArgumentException() | ||
.isThrownBy(simpleMath::printOutput) | ||
.withMessage("Please enter a positive number! Input: -10"); | ||
} | ||
|
||
@Test | ||
void secondInputMustBeANumber() { | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\nasdf".getBytes()), new PrintStream(new ByteArrayOutputStream())); | ||
|
||
assertThatIllegalArgumentException() | ||
.isThrownBy(simpleMath::printOutput) | ||
.withMessage("Please enter a valid number! Input: asdf"); | ||
} | ||
|
||
@Test | ||
void secondNumberMustBePositive() { | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n-5".getBytes()), new PrintStream(new ByteArrayOutputStream())); | ||
|
||
assertThatIllegalArgumentException() | ||
.isThrownBy(simpleMath::printOutput) | ||
.withMessage("Please enter a positive number! Input: -5"); | ||
} | ||
|
||
@Test | ||
void secondNumberMustNotBeZero() { | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n0".getBytes()), new PrintStream(new ByteArrayOutputStream())); | ||
|
||
assertThatIllegalArgumentException() | ||
.isThrownBy(simpleMath::printOutput) | ||
.withMessage("Cannot divide by zero!"); | ||
} | ||
|
||
@Test | ||
void sumDifferenceProductAndQuotientIsPrinted() { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n5".getBytes()), new PrintStream(outputStream)); | ||
|
||
simpleMath.printOutput(); | ||
|
||
assertThat(outputStream).hasToString(""" | ||
10 + 5 = 15 | ||
10 - 5 = 5 | ||
10 * 5 = 50 | ||
10 / 5 = 2 | ||
"""); | ||
} | ||
} |