Skip to content
Snippets Groups Projects
Commit f0985fd7 authored by Colin DAMON's avatar Colin DAMON
Browse files

Full format (missing hook)

parent 3cf36dae
No related branches found
No related tags found
1 merge request!12Resolve "Mutualise java build"
...@@ -5,26 +5,21 @@ import java.util.function.BinaryOperator; ...@@ -5,26 +5,21 @@ import java.util.function.BinaryOperator;
public final class Calculator { public final class Calculator {
private Calculator() { private Calculator() {}
}
public static String sum(String first, String second) { public static String sum(String first, String second) {
return operate(first, second, BigDecimal::add); return operate(first, second, BigDecimal::add);
} }
public static String substract(String first, public static String substract(String first, String second) {
String second) {
return operate(first, second, BigDecimal::subtract); return operate(first, second, BigDecimal::subtract);
} }
private static String operate(String first, String second, private static String operate(String first, String second, BinaryOperator<BigDecimal> operation) {
BinaryOperator<BigDecimal> operation) {
notNull(first); notNull(first);
notNull(second); notNull(second);
return operation return operation.apply(toBigDecimal(first), toBigDecimal(second)).toPlainString();
.apply(toBigDecimal(first), toBigDecimal(second))
.toPlainString();
} }
private static BigDecimal toBigDecimal(String value) { private static BigDecimal toBigDecimal(String value) {
...@@ -40,5 +35,4 @@ public final class Calculator { ...@@ -40,5 +35,4 @@ public final class Calculator {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
} }
...@@ -8,16 +8,12 @@ public class CalculatorTest { ...@@ -8,16 +8,12 @@ public class CalculatorTest {
@Test @Test
void shouldNotSumWithNullFirstInput() { void shouldNotSumWithNullFirstInput() {
assertThatThrownBy(() -> Calculator.sum(null, "3")) assertThatThrownBy(() -> Calculator.sum(null, "3")).isExactlyInstanceOf(IllegalArgumentException.class);
.isExactlyInstanceOf(
IllegalArgumentException.class);
} }
@Test @Test
void shouldNotSumWithNullSecondInput() { void shouldNotSumWithNullSecondInput() {
assertThatThrownBy(() -> Calculator.sum("3", null)) assertThatThrownBy(() -> Calculator.sum("3", null)).isExactlyInstanceOf(IllegalArgumentException.class);
.isExactlyInstanceOf(
IllegalArgumentException.class);
} }
@Test @Test
...@@ -42,48 +38,36 @@ public class CalculatorTest { ...@@ -42,48 +38,36 @@ public class CalculatorTest {
@Test @Test
void shouldSumFloats() { void shouldSumFloats() {
assertThat(Calculator.sum("1.01", "2.02")) assertThat(Calculator.sum("1.01", "2.02")).isEqualTo("3.03");
.isEqualTo("3.03");
} }
@Test @Test
void shouldSumCommaSeparatedFloats() { void shouldSumCommaSeparatedFloats() {
assertThat(Calculator.sum("1,01", "2,02")) assertThat(Calculator.sum("1,01", "2,02")).isEqualTo("3.03");
.isEqualTo("3.03");
} }
@Test @Test
void shouldSumNegativeCommaSeparatedFloats() { void shouldSumNegativeCommaSeparatedFloats() {
assertThat(Calculator.sum("-1,01", "-2,02")) assertThat(Calculator.sum("-1,01", "-2,02")).isEqualTo("-3.03");
.isEqualTo("-3.03");
} }
@Test @Test
void shouldNotSubstractNullInputs() { void shouldNotSubstractNullInputs() {
assertThatThrownBy( assertThatThrownBy(() -> Calculator.substract(null, "3")).isExactlyInstanceOf(IllegalArgumentException.class);
() -> Calculator.substract(null, "3"))
.isExactlyInstanceOf(
IllegalArgumentException.class);
} }
@Test @Test
void shouldNotSubstractWithNullSecondInputs() { void shouldNotSubstractWithNullSecondInputs() {
assertThatThrownBy( assertThatThrownBy(() -> Calculator.substract("3", null)).isExactlyInstanceOf(IllegalArgumentException.class);
() -> Calculator.substract("3", null))
.isExactlyInstanceOf(
IllegalArgumentException.class);
} }
@Test @Test
void shouldSubstractCommaSeparatedFloats() { void shouldSubstractCommaSeparatedFloats() {
assertThat(Calculator.substract("1,01", "2,02")) assertThat(Calculator.substract("1,01", "2,02")).isEqualTo("-1.01");
.isEqualTo("-1.01");
} }
@Test @Test
void shouldSumTooBigIntegers() { void shouldSumTooBigIntegers() {
assertThat(Calculator assertThat(Calculator.sum(String.valueOf(Integer.MAX_VALUE), "2,02")).isEqualTo("2147483649.02");
.sum(String.valueOf(Integer.MAX_VALUE), "2,02"))
.isEqualTo("2147483649.02");
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment