Skip to content

Commit ab1e8e5

Browse files
committed
Add 'Simple Math' solution
1 parent f0af162 commit ab1e8e5

File tree

6 files changed

+189
-2
lines changed

6 files changed

+189
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Exercises for Programmers: 57 Challenges to Develop Your Coding Skills by Brian
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)
1010
- Exercise 4. [Mad Lib](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/mad-lib)
11-
- Exercise 5. Simple Math
11+
- Exercise 5. [Simple Math](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/simple-math)
1212
- Exercise 6. Retirement Calculator
1313

1414
**Calculations**

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

simple-math/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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.craftsmanshipinsoftware.math;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
SimpleMath simpleMath = new SimpleMath(System.in, System.out);
7+
simpleMath.printOutput();
8+
}
9+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.craftsmanshipinsoftware.math;
2+
3+
import java.io.InputStream;
4+
import java.io.PrintStream;
5+
import java.util.Objects;
6+
import java.util.Scanner;
7+
8+
class SimpleMath {
9+
10+
private final PrintStream printStream;
11+
private final InputStream inputStream;
12+
13+
SimpleMath(InputStream inputStream, PrintStream printStream) {
14+
Objects.requireNonNull(inputStream, "inputStream must not be null");
15+
Objects.requireNonNull(printStream, "printStream must not be null");
16+
this.printStream = printStream;
17+
this.inputStream = inputStream;
18+
}
19+
20+
void printOutput() {
21+
try (Scanner scanner = new Scanner(inputStream)) {
22+
String firstInput = promptForInput("What is the first number? ", scanner);
23+
int firstNumber = validatedInput(firstInput);
24+
String secondInput = promptForInput("What is the second number? ", scanner);
25+
int secondNumber = validatedInput(secondInput);
26+
if (secondNumber == 0) {
27+
throw new IllegalArgumentException("Cannot divide by zero!");
28+
}
29+
this.printStream.println(output(firstNumber, secondNumber));
30+
}
31+
}
32+
33+
@SuppressWarnings("PMD.SystemPrintln")
34+
private String promptForInput(String prompt, Scanner scanner) {
35+
System.out.print(prompt);
36+
String input = readInput(scanner);
37+
if (input == null || input.isBlank()) {
38+
throw new IllegalArgumentException("Input must not be empty!");
39+
}
40+
return input;
41+
}
42+
43+
private String readInput(Scanner scanner) {
44+
return scanner.hasNext() ? scanner.nextLine() : null;
45+
}
46+
47+
private static int validatedInput(String input) {
48+
try {
49+
int number = Integer.parseInt(input);
50+
if (number < 0) {
51+
throw new IllegalArgumentException("Please enter a positive number! Input: " + number);
52+
}
53+
return number;
54+
} catch (NumberFormatException e) {
55+
throw new IllegalArgumentException("Please enter a valid number! Input: " + input, e);
56+
}
57+
}
58+
59+
private static String output(int firstNumber, int secondNumber) {
60+
return String.format("""
61+
%d + %d = %d
62+
%d - %d = %d
63+
%d * %d = %d
64+
%d / %d = %d""",
65+
firstNumber, secondNumber, add(firstNumber, secondNumber),
66+
firstNumber, secondNumber, subtract(firstNumber, secondNumber),
67+
firstNumber, secondNumber, multiply(firstNumber, secondNumber),
68+
firstNumber, secondNumber, divide(firstNumber, secondNumber));
69+
}
70+
71+
private static int add(int firstNumber, int secondNumber) {
72+
return firstNumber + secondNumber;
73+
}
74+
75+
private static int subtract(int firstNumber, int secondNumber) {
76+
return firstNumber - secondNumber;
77+
}
78+
79+
private static int multiply(int firstNumber, int secondNumber) {
80+
return firstNumber * secondNumber;
81+
}
82+
83+
private static int divide(int firstNumber, int secondNumber) {
84+
return firstNumber / secondNumber;
85+
}
86+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.craftsmanshipinsoftware.math;
2+
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;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.ValueSource;
12+
13+
class SimpleMathTest {
14+
15+
@ParameterizedTest
16+
@ValueSource(strings = {"", " ", "10\n", "\n5"})
17+
void inputIsRequired(String input) {
18+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream(input.getBytes()), new PrintStream(new ByteArrayOutputStream()));
19+
20+
assertThatIllegalArgumentException()
21+
.isThrownBy(simpleMath::printOutput)
22+
.withMessage("Input must not be empty!");
23+
}
24+
25+
@Test
26+
void firstInputMustBeANumber() {
27+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("abc".getBytes()), new PrintStream(new ByteArrayOutputStream()));
28+
29+
assertThatIllegalArgumentException()
30+
.isThrownBy(simpleMath::printOutput)
31+
.withMessage("Please enter a valid number! Input: abc");
32+
}
33+
34+
@Test
35+
void firstNumberMustBePositive() {
36+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("-10\n5".getBytes()), new PrintStream(new ByteArrayOutputStream()));
37+
38+
assertThatIllegalArgumentException()
39+
.isThrownBy(simpleMath::printOutput)
40+
.withMessage("Please enter a positive number! Input: -10");
41+
}
42+
43+
@Test
44+
void secondInputMustBeANumber() {
45+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\nasdf".getBytes()), new PrintStream(new ByteArrayOutputStream()));
46+
47+
assertThatIllegalArgumentException()
48+
.isThrownBy(simpleMath::printOutput)
49+
.withMessage("Please enter a valid number! Input: asdf");
50+
}
51+
52+
@Test
53+
void secondNumberMustBePositive() {
54+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n-5".getBytes()), new PrintStream(new ByteArrayOutputStream()));
55+
56+
assertThatIllegalArgumentException()
57+
.isThrownBy(simpleMath::printOutput)
58+
.withMessage("Please enter a positive number! Input: -5");
59+
}
60+
61+
@Test
62+
void secondNumberMustNotBeZero() {
63+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n0".getBytes()), new PrintStream(new ByteArrayOutputStream()));
64+
65+
assertThatIllegalArgumentException()
66+
.isThrownBy(simpleMath::printOutput)
67+
.withMessage("Cannot divide by zero!");
68+
}
69+
70+
@Test
71+
void sumDifferenceProductAndQuotientIsPrinted() {
72+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
73+
SimpleMath simpleMath = new SimpleMath(new ByteArrayInputStream("10\n5".getBytes()), new PrintStream(outputStream));
74+
75+
simpleMath.printOutput();
76+
77+
assertThat(outputStream).hasToString("""
78+
10 + 5 = 15
79+
10 - 5 = 5
80+
10 * 5 = 50
81+
10 / 5 = 2
82+
""");
83+
}
84+
}

0 commit comments

Comments
 (0)