Commit f0985fd7 authored by Colin DAMON's avatar Colin DAMON
Browse files

Full format (missing hook)

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