Skip to content

Commit

Permalink
Add 'Simple Math' solution
Browse files Browse the repository at this point in the history
  • Loading branch information
durimkryeziu committed Nov 9, 2024
1 parent f0af162 commit ab1e8e5
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Exercises for Programmers: 57 Challenges to Develop Your Coding Skills by Brian
- Exercise 2. [Counting the Number of Characters](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/characters-count)
- Exercise 3. [Printing Quotes](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/printing-quotes)
- Exercise 4. [Mad Lib](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/mad-lib)
- Exercise 5. Simple Math
- Exercise 5. [Simple Math](https://github.com/durimkryeziu/exercises-for-programmers-java/tree/main/simple-math)
- Exercise 6. Retirement Calculator

**Calculations**
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
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')
8 changes: 8 additions & 0 deletions simple-math/build.gradle
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)
}
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();
}
}
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;
}
}
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
""");
}
}

0 comments on commit ab1e8e5

Please sign in to comment.