OCP review comments. (#9310)
* OCP change * calculator unit test * renamed variable and abstract class addition
This commit is contained in:
parent
c66c60bcff
commit
515bcf55ea
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.o;
|
||||||
|
|
||||||
|
public abstract class AbstractCalculatorOperation {
|
||||||
|
|
||||||
|
abstract void perform();
|
||||||
|
|
||||||
|
}
|
|
@ -1,29 +1,29 @@
|
||||||
package com.baeldung.o;
|
package com.baeldung.o;
|
||||||
|
|
||||||
public class Addition implements CalculatorOperation {
|
public class Addition implements CalculatorOperation {
|
||||||
private double number1;
|
private double left;
|
||||||
private double number2;
|
private double right;
|
||||||
private double result = 0.0;
|
private double result = 0.0;
|
||||||
|
|
||||||
public Addition(double number1, double number2) {
|
public Addition(double left, double right) {
|
||||||
this.number1 = number1;
|
this.left = left;
|
||||||
this.number2 = number2;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNumber1() {
|
public double getLeft() {
|
||||||
return number1;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber1(double number1) {
|
public void setLeft(double left) {
|
||||||
this.number1 = number1;
|
this.left = left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNumber2() {
|
public double getRight() {
|
||||||
return number2;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber2(double number2) {
|
public void setRight(double right) {
|
||||||
this.number2 = number2;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getResult() {
|
public double getResult() {
|
||||||
|
@ -36,6 +36,6 @@ public class Addition implements CalculatorOperation {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void perform() {
|
public void perform() {
|
||||||
result = number1 + number2;
|
result = left + right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +1,43 @@
|
||||||
package com.baeldung.o;
|
package com.baeldung.o;
|
||||||
|
|
||||||
public class Division implements CalculatorOperation {
|
public class Division implements CalculatorOperation {
|
||||||
private double number1;
|
private double left;
|
||||||
private double number2;
|
private double right;
|
||||||
private double result = 0.0;
|
private double result = 0.0;
|
||||||
|
|
||||||
public Division(double number1, double number2) {
|
public Division(double left, double right) {
|
||||||
this.number1 = number1;
|
this.left = left;
|
||||||
this.number2 = number2;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNumber1() {
|
public double getLeft() {
|
||||||
return number1;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber1(double number1) {
|
public void setLeft(double left) {
|
||||||
this.number1 = number1;
|
this.left = left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNumber2() {
|
public double getRight() {
|
||||||
return number2;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber2(double number2) {
|
public void setRight(double right) {
|
||||||
this.number2 = number2;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getResult() {
|
public double getResult() {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setResult(double result) {
|
public void setResult(double result) {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void perform() {
|
public void perform() {
|
||||||
if (number2 != 0) {
|
if (right != 0) {
|
||||||
result = number1 / number2;
|
result = left / right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
package com.baeldung.o;
|
package com.baeldung.o;
|
||||||
|
|
||||||
public class Subtraction implements CalculatorOperation {
|
public class Subtraction implements CalculatorOperation {
|
||||||
private double number1;
|
private double left;
|
||||||
private double number2;
|
private double right;
|
||||||
private double result = 0.0;
|
private double result = 0.0;
|
||||||
|
|
||||||
public Subtraction(double number1, double number2) {
|
public Subtraction(double left, double right) {
|
||||||
this.number1 = number1;
|
this.left = left;
|
||||||
this.number2 = number2;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNumber1() {
|
public double getLeft() {
|
||||||
return number1;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber1(double number1) {
|
public void setLeft(double left) {
|
||||||
this.number1 = number1;
|
this.left = left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNumber2() {
|
public double getRight() {
|
||||||
return number2;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNumber2(double number2) {
|
public void setRight(double right) {
|
||||||
this.number2 = number2;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getResult() {
|
public double getResult() {
|
||||||
|
@ -36,6 +36,6 @@ public class Subtraction implements CalculatorOperation {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void perform() {
|
public void perform() {
|
||||||
result = number1 - number2;
|
result = left - right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class CalculatorUnitTest {
|
public class CalculatorUnitTest {
|
||||||
|
|
||||||
private static final double NUMBER_1 = 10.0;
|
private static final double RIGHT = 10.0;
|
||||||
private static final double NUMBER_2 = 20.0;
|
private static final double LEFT = 20.0;
|
||||||
private static final double SUM = 30.0;
|
private static final double SUM = 30.0;
|
||||||
private static final double SUBTRACTION_RESULT = 10.0;
|
private static final double SUBTRACTION_RESULT = 10.0;
|
||||||
private static final double DIVISION_RESULT = 2.0;
|
private static final double DIVISION_RESULT = 2.0;
|
||||||
|
@ -22,21 +22,21 @@ public class CalculatorUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAddTwoNumber_returnSum() {
|
public void whenAddTwoNumber_returnSum() {
|
||||||
Addition addition = new Addition(NUMBER_1, NUMBER_2);
|
Addition addition = new Addition(RIGHT, LEFT);
|
||||||
calculator.calculate(addition);
|
calculator.calculate(addition);
|
||||||
assertEquals(SUM, addition.getResult(), 0.0);
|
assertEquals(SUM, addition.getResult(), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSutractTwoNumber_returnCorrectResult() {
|
public void whenSutractTwoNumber_returnCorrectResult() {
|
||||||
Subtraction subtraction = new Subtraction(NUMBER_2, NUMBER_1);
|
Subtraction subtraction = new Subtraction(LEFT, RIGHT);
|
||||||
calculator.calculate(subtraction);
|
calculator.calculate(subtraction);
|
||||||
assertEquals(SUBTRACTION_RESULT, subtraction.getResult(), 0.0);
|
assertEquals(SUBTRACTION_RESULT, subtraction.getResult(), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDivideTwoNumber_returnCorrectResult() {
|
public void whenDivideTwoNumber_returnCorrectResult() {
|
||||||
Division division = new Division(NUMBER_2, NUMBER_1);
|
Division division = new Division(LEFT, RIGHT);
|
||||||
calculator.calculate(division);
|
calculator.calculate(division);
|
||||||
assertEquals(DIVISION_RESULT, division.getResult(), 0.0);
|
assertEquals(DIVISION_RESULT, division.getResult(), 0.0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue