commit
d6d7661771
core-java-modules/core-java-lang-3/src
main/java/com/baeldung/constantspatterns
test/java/com/baeldung/constantspatterns
37
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/Calculator.java
Normal file
37
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/Calculator.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.constantspatterns;
|
||||||
|
|
||||||
|
public class Calculator {
|
||||||
|
public static final double PI = 3.14159265359;
|
||||||
|
private static final double UPPER_LIMIT = 0x1.fffffffffffffP+1023;
|
||||||
|
|
||||||
|
public enum Operation {
|
||||||
|
ADD, SUBTRACT, DIVIDE, MULTIPLY
|
||||||
|
}
|
||||||
|
|
||||||
|
public double operateOnTwoNumbers(double numberOne, double numberTwo, Operation operation) {
|
||||||
|
if (numberOne > UPPER_LIMIT) {
|
||||||
|
throw new IllegalArgumentException("'numberOne' is too large");
|
||||||
|
}
|
||||||
|
if (numberTwo > UPPER_LIMIT) {
|
||||||
|
throw new IllegalArgumentException("'numberTwo' is too large");
|
||||||
|
}
|
||||||
|
double answer = 0;
|
||||||
|
|
||||||
|
switch (operation) {
|
||||||
|
case ADD:
|
||||||
|
answer = numberOne + numberTwo;
|
||||||
|
break;
|
||||||
|
case SUBTRACT:
|
||||||
|
answer = numberOne - numberTwo;
|
||||||
|
break;
|
||||||
|
case DIVIDE:
|
||||||
|
answer = numberOne / numberTwo;
|
||||||
|
break;
|
||||||
|
case MULTIPLY:
|
||||||
|
answer = numberOne * numberTwo;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
}
|
10
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/CalculatorConstants.java
Normal file
10
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/CalculatorConstants.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.constantspatterns;
|
||||||
|
|
||||||
|
public interface CalculatorConstants {
|
||||||
|
double PI = 3.14159265359;
|
||||||
|
double UPPER_LIMIT = 0x1.fffffffffffffP+1023;
|
||||||
|
|
||||||
|
enum Operation {
|
||||||
|
ADD, SUBTRACT, MULTIPLY, DIVIDE
|
||||||
|
};
|
||||||
|
}
|
32
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/GeometryCalculator.java
Normal file
32
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/GeometryCalculator.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.constantspatterns;
|
||||||
|
|
||||||
|
public class GeometryCalculator implements CalculatorConstants {
|
||||||
|
public static final double UPPER_LIMIT = 100000000000000000000.0;
|
||||||
|
|
||||||
|
public double operateOnTwoNumbers(double numberOne, double numberTwo, Operation operation) {
|
||||||
|
if (numberOne > UPPER_LIMIT) {
|
||||||
|
throw new IllegalArgumentException("'numberOne' is too large");
|
||||||
|
}
|
||||||
|
if (numberTwo > UPPER_LIMIT) {
|
||||||
|
throw new IllegalArgumentException("'numberTwo' is too large");
|
||||||
|
}
|
||||||
|
double answer = 0;
|
||||||
|
|
||||||
|
switch (operation) {
|
||||||
|
case ADD:
|
||||||
|
answer = numberOne + numberTwo;
|
||||||
|
break;
|
||||||
|
case SUBTRACT:
|
||||||
|
answer = numberOne - numberTwo;
|
||||||
|
break;
|
||||||
|
case DIVIDE:
|
||||||
|
answer = numberOne / numberTwo;
|
||||||
|
break;
|
||||||
|
case MULTIPLY:
|
||||||
|
answer = numberOne * numberTwo;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
}
|
16
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/calculations/MathConstants.java
Normal file
16
core-java-modules/core-java-lang-3/src/main/java/com/baeldung/constantspatterns/calculations/MathConstants.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.constantspatterns.calculations;
|
||||||
|
|
||||||
|
public final class MathConstants {
|
||||||
|
public static final double PI = 3.14159265359;
|
||||||
|
static final double GOLDEN_RATIO = 1.6180;
|
||||||
|
static final double GRAVITATIONAL_ACCELERATION = 9.8;
|
||||||
|
static final double EULERS_NUMBER = 2.7182818284590452353602874713527;
|
||||||
|
|
||||||
|
public enum Operation {
|
||||||
|
ADD, SUBTRACT, DIVIDE, MULTIPLY
|
||||||
|
}
|
||||||
|
|
||||||
|
private MathConstants() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
23
core-java-modules/core-java-lang-3/src/test/java/com/baeldung/constantspatterns/ConstantPatternUnitTest.java
Normal file
23
core-java-modules/core-java-lang-3/src/test/java/com/baeldung/constantspatterns/ConstantPatternUnitTest.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.constantspatterns;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ConstantPatternUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenTwoNumbersAndAdd_whenCallingCalculatorOperatOneTwoNumbers_thenCorrectAnswerReturned() {
|
||||||
|
Calculator calculator = new Calculator();
|
||||||
|
double expected = 4;
|
||||||
|
double answer = calculator.operateOnTwoNumbers(2, 2, Calculator.Operation.ADD);
|
||||||
|
assertEquals(expected, answer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoNumbersAndAdd_whenCallingGeometryCalculatorOperatOneTwoNumbers_thenCorrectAnswerReturned() {
|
||||||
|
GeometryCalculator calculator = new GeometryCalculator();
|
||||||
|
double expected = 4;
|
||||||
|
double answer = calculator.operateOnTwoNumbers(2, 2, GeometryCalculator.Operation.ADD);
|
||||||
|
assertEquals(expected, answer);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user