Skip to content

Commit 91dbd70

Browse files
committed
BAEL-4503
1 parent 785d52c commit 91dbd70

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.constantspatterns;
2+
3+
public class Calculator {
4+
public static final double PI = 3.14159265359;
5+
private static final double UPPER_LIMIT = 0x1.fffffffffffffP+1023;
6+
7+
public enum Operation {
8+
ADD, SUBTRACT, DIVIDE, MULTIPLY
9+
}
10+
11+
public double operateOnTwoNumbers(double numberOne, double numberTwo, Operation operation) {
12+
if (numberOne > UPPER_LIMIT) {
13+
throw new IllegalArgumentException("'numberOne' is too large");
14+
}
15+
if (numberTwo > UPPER_LIMIT) {
16+
throw new IllegalArgumentException("'numberTwo' is too large");
17+
}
18+
double answer = 0;
19+
20+
switch (operation) {
21+
case ADD:
22+
answer = numberOne + numberTwo;
23+
break;
24+
case SUBTRACT:
25+
answer = numberOne - numberTwo;
26+
break;
27+
case DIVIDE:
28+
answer = numberOne / numberTwo;
29+
break;
30+
case MULTIPLY:
31+
answer = numberOne * numberTwo;
32+
break;
33+
}
34+
35+
return answer;
36+
}
37+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.constantspatterns;
2+
3+
public interface CalculatorConstants {
4+
double PI = 3.14159265359;
5+
double UPPER_LIMIT = 0x1.fffffffffffffP+1023;
6+
7+
enum Operation {
8+
ADD, SUBTRACT, MULTIPLY, DIVIDE
9+
};
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.constantspatterns;
2+
3+
public class GeometryCalculator implements CalculatorConstants {
4+
public static final double UPPER_LIMIT = 100000000000000000000.0;
5+
6+
public double operateOnTwoNumbers(double numberOne, double numberTwo, Operation operation) {
7+
if (numberOne > UPPER_LIMIT) {
8+
throw new IllegalArgumentException("'numberOne' is too large");
9+
}
10+
if (numberTwo > UPPER_LIMIT) {
11+
throw new IllegalArgumentException("'numberTwo' is too large");
12+
}
13+
double answer = 0;
14+
15+
switch (operation) {
16+
case ADD:
17+
answer = numberOne + numberTwo;
18+
break;
19+
case SUBTRACT:
20+
answer = numberOne - numberTwo;
21+
break;
22+
case DIVIDE:
23+
answer = numberOne / numberTwo;
24+
break;
25+
case MULTIPLY:
26+
answer = numberOne * numberTwo;
27+
break;
28+
}
29+
30+
return answer;
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.constantspatterns.calculations;
2+
3+
public final class MathConstants {
4+
public static final double PI = 3.14159265359;
5+
public static final double GOLDEN_RATIO = 1.6180;
6+
public static final double GRAVITATIONAL_ACCELERATION = 9.8;
7+
public static final double EULERS_NUMBER = 2.7182818284590452353602874713527;
8+
9+
public enum Operation {
10+
ADD, SUBTRACT, DIVIDE, MULTIPLY
11+
}
12+
13+
private MathConstants() {
14+
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.constantspatterns;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class ConstantPatternUnitTest {
8+
@Test
9+
public void givenTwoNumbersAndAdd_whenCallingCalculatorOperatOneTwoNumbers_correctAnswerReturned() {
10+
Calculator calculator = new Calculator();
11+
double expected = 4;
12+
double answer = calculator.operateOnTwoNumbers(2, 2, Calculator.Operation.ADD);
13+
assertEquals(expected, answer);
14+
}
15+
16+
@Test
17+
public void givenTwoNumbersAndAdd_whenCallingGeometryCalculatorOperatOneTwoNumbers_correctAnswerReturned() {
18+
GeometryCalculator calculator = new GeometryCalculator();
19+
double expected = 4;
20+
double answer = calculator.operateOnTwoNumbers(2, 2, GeometryCalculator.Operation.ADD);
21+
assertEquals(expected, answer);
22+
}
23+
}

0 commit comments

Comments
 (0)