Compare commits
7 Commits
1defc49f79
...
be0fa22bc4
| Author | SHA1 | Date | |
|---|---|---|---|
| be0fa22bc4 | |||
| b1d910927e | |||
| d4091a34c5 | |||
| 6f7c6d5f80 | |||
| a63e577071 | |||
| a0a34428d7 | |||
| 62fd349421 |
@@ -2,6 +2,7 @@ package com.example;
|
|||||||
|
|
||||||
import com.example.util.AddFunction;
|
import com.example.util.AddFunction;
|
||||||
import com.example.util.CalcFunction;
|
import com.example.util.CalcFunction;
|
||||||
|
import com.example.util.SubtractFunction;
|
||||||
|
|
||||||
public class LoopTestApplication {
|
public class LoopTestApplication {
|
||||||
|
|
||||||
@@ -9,5 +10,7 @@ public class LoopTestApplication {
|
|||||||
System.out.println("============== Calculator =============");
|
System.out.println("============== Calculator =============");
|
||||||
CalcFunction add = new AddFunction();
|
CalcFunction add = new AddFunction();
|
||||||
System.out.println("ADDITION: " + add.calc(2, 5, 6));
|
System.out.println("ADDITION: " + add.calc(2, 5, 6));
|
||||||
|
CalcFunction subtract = new SubtractFunction();
|
||||||
|
System.out.println("SUBTRACTION: " + subtract.calc(2, 5, 6));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.example.util;
|
||||||
|
|
||||||
|
public class SubtractFunction implements CalcFunction {
|
||||||
|
|
||||||
|
public double calc(double... input) {
|
||||||
|
if (input.length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
double result = input[0];
|
||||||
|
for (int i = 1; i < input.length; i++) {
|
||||||
|
result -= input[i];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.example.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class SubtractFunctionTest {
|
||||||
|
|
||||||
|
private final SubtractFunction subtractFunction = new SubtractFunction();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void subtractsMultipleValues() {
|
||||||
|
assertEquals(1.0, subtractFunction.calc(6.0, 2.0, 3.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void returnsSingleValue() {
|
||||||
|
assertEquals(5.0, subtractFunction.calc(5.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void returnsZeroForNoInput() {
|
||||||
|
assertEquals(0.0, subtractFunction.calc());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void subtractsNegativeValues() {
|
||||||
|
assertEquals(3.0, subtractFunction.calc(1.0, -2.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user