diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java new file mode 100644 index 0000000000..5aa0de7adc --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java @@ -0,0 +1,19 @@ +package com.baeldung.reducingIfElse; + +public class AddCommand implements Command { + + private int a; + private int b; + + @Override + public Integer execute() { + return a + b; + } + + @Override + public Command takeInput(Integer a, Integer b) { + this.a = a; + this.b = b; + return this; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java new file mode 100644 index 0000000000..871ff1f2d1 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java @@ -0,0 +1,21 @@ +package com.baeldung.reducingIfElse; + +public class AddRule implements Rule { + + private int result; + + @Override + public boolean evaluate(Expression expression) { + boolean evalResult = false; + if (expression.getOperator() == Operator.ADD) { + this.result = expression.getX() + expression.getY(); + evalResult = true; + } + return evalResult; + } + + @Override + public int getResult() { + return result; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Addition.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Addition.java new file mode 100644 index 0000000000..3174ea558c --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Addition.java @@ -0,0 +1,8 @@ +package com.baeldung.reducingIfElse; + +public class Addition implements Operation { + @Override + public int apply(int a, int b) { + return a + b; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java new file mode 100644 index 0000000000..9b8cce130f --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java @@ -0,0 +1,84 @@ +package com.baeldung.reducingIfElse; + +public class Calculator { + + public int calculate(int a, int b, String operator) { + int result = Integer.MIN_VALUE; + + if ("add".equals(operator)) { + result = a + b; + } else if ("multiply".equals(operator)) { + result = a * b; + } else if ("divide".equals(operator)) { + result = a / b; + } else if ("subtract".equals(operator)) { + result = a - b; + } else if ("modulo".equals(operator)) { + result = a % b; + } + return result; + } + + public int calculateUsingSwitch(int a, int b, String operator) { + int result = 0; + switch (operator) { + case "add": + result = a + b; + break; + case "multiply": + result = a * b; + break; + case "divide": + result = a / b; + break; + case "subtract": + result = a - b; + break; + case "modulo": + result = a % b; + break; + default: + result = Integer.MIN_VALUE; + } + return result; + } + + public int calculateUsingSwitch(int a, int b, Operator operator) { + int result = 0; + switch (operator) { + case ADD: + result = a + b; + break; + case MULTIPLY: + result = a * b; + break; + case DIVIDE: + result = a / b; + break; + case SUBTRACT: + result = a - b; + break; + case MODULO: + result = a % b; + break; + default: + result = Integer.MIN_VALUE; + } + return result; + } + + public int calculate(int a, int b, Operator operator) { + return operator.apply(a, b); + } + + public int calculateUsingFactory(int a, int b, String operation) { + Operation targetOperation = OperatorFactory.getOperation(operation) + .orElseThrow(() -> new IllegalArgumentException("Invalid Operator")); + return targetOperation.apply(a, b); + } + + public int calculate(int a, int b, Command command) { + return command.takeInput(a, b) + .execute(); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java new file mode 100644 index 0000000000..d9f00e31b4 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java @@ -0,0 +1,7 @@ +package com.baeldung.reducingIfElse; + +public interface Command { + R execute(); + + Command takeInput(A a, B b); +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Division.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Division.java new file mode 100644 index 0000000000..75b1297655 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Division.java @@ -0,0 +1,7 @@ +package com.baeldung.reducingIfElse; + +public class Division implements Operation { + @Override public int apply(int a, int b) { + return a / b; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Expression.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Expression.java new file mode 100644 index 0000000000..4d3fe1b824 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Expression.java @@ -0,0 +1,26 @@ +package com.baeldung.reducingIfElse; + +public class Expression { + + private Integer x; + private Integer y; + private Operator operator; + + public Expression(Integer x, Integer y, Operator operator) { + this.x = x; + this.y = y; + this.operator = operator; + } + + public Integer getX() { + return x; + } + + public Integer getY() { + return y; + } + + public Operator getOperator() { + return operator; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Modulo.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Modulo.java new file mode 100644 index 0000000000..a7a081704c --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Modulo.java @@ -0,0 +1,7 @@ +package com.baeldung.reducingIfElse; + +public class Modulo implements Operation { + @Override public int apply(int a, int b) { + return a % b; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Multiplication.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Multiplication.java new file mode 100644 index 0000000000..e1a39b33c4 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Multiplication.java @@ -0,0 +1,7 @@ +package com.baeldung.reducingIfElse; + +public class Multiplication implements Operation { + @Override public int apply(int a, int b) { + return 0; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Operation.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Operation.java new file mode 100644 index 0000000000..41241fa810 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Operation.java @@ -0,0 +1,5 @@ +package com.baeldung.reducingIfElse; + +public interface Operation { + int apply(int a, int b); +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Operator.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Operator.java new file mode 100644 index 0000000000..831b8fa146 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Operator.java @@ -0,0 +1,41 @@ +package com.baeldung.reducingIfElse; + +public enum Operator { + + ADD { + @Override + public int apply(int a, int b) { + return a + b; + } + }, + + MULTIPLY { + @Override + public int apply(int a, int b) { + return a * b; + } + }, + + SUBTRACT { + @Override + public int apply(int a, int b) { + return a - b; + } + }, + + DIVIDE { + @Override + public int apply(int a, int b) { + return a / b; + } + }, + + MODULO { + @Override + public int apply(int a, int b) { + return a % b; + } + }; + + public abstract int apply(int a, int b); +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/OperatorFactory.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/OperatorFactory.java new file mode 100644 index 0000000000..18ed63adbd --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/OperatorFactory.java @@ -0,0 +1,21 @@ +package com.baeldung.reducingIfElse; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class OperatorFactory { + + static Map operationMap = new HashMap<>(); + static { + operationMap.put("add", new Addition()); + operationMap.put("divide", new Division()); + operationMap.put("multiply", new Multiplication()); + operationMap.put("subtract", new Subtraction()); + operationMap.put("modulo", new Modulo()); + } + + public static Optional getOperation(String operation) { + return Optional.ofNullable(operationMap.get(operation)); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java new file mode 100644 index 0000000000..202072dd66 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java @@ -0,0 +1,8 @@ +package com.baeldung.reducingIfElse; + +public interface Rule { + + boolean evaluate(Expression expression); + + int getResult(); +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java new file mode 100644 index 0000000000..3af67aff11 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java @@ -0,0 +1,20 @@ +package com.baeldung.reducingIfElse; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class RuleEngine { + + private static List rules = new ArrayList<>(); + + static { + rules.add(new AddRule()); + } + + public List process(Expression expression) { + return rules.stream() + .filter(r -> r.evaluate(expression)) + .collect(Collectors.toList()); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Subtraction.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Subtraction.java new file mode 100644 index 0000000000..948998810e --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Subtraction.java @@ -0,0 +1,7 @@ +package com.baeldung.reducingIfElse; + +public class Subtraction implements Operation { + @Override public int apply(int a, int b) { + return a - b; + } +} diff --git a/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java b/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java new file mode 100644 index 0000000000..227dd12f0d --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.reduceIfelse; + +import com.baeldung.reducingIfElse.Expression; +import com.baeldung.reducingIfElse.Operator; +import com.baeldung.reducingIfElse.Rule; +import com.baeldung.reducingIfElse.RuleEngine; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class RuleEngineUnitTest { + + @Test + public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() { + Expression expression = new Expression(5, 5, Operator.ADD); + RuleEngine engine = new RuleEngine(); + List rules = engine.process(expression); + + assertNotNull(rules); + assertEquals(1, rules.size()); + assertEquals(10, rules.get(0) + .getResult()); + } +}