OCP review comments. (#9310)

* OCP change

* calculator unit test

* renamed variable and abstract class addition
This commit is contained in:
ajay74984 2020-05-17 20:51:59 +02:00 committed by GitHub
parent c66c60bcff
commit 515bcf55ea
5 changed files with 71 additions and 64 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.o;
public abstract class AbstractCalculatorOperation {
abstract void perform();
}

View File

@ -1,29 +1,29 @@
package com.baeldung.o;
public class Addition implements CalculatorOperation {
private double number1;
private double number2;
private double left;
private double right;
private double result = 0.0;
public Addition(double number1, double number2) {
this.number1 = number1;
this.number2 = number2;
public Addition(double left, double right) {
this.left = left;
this.right = right;
}
public double getNumber1() {
return number1;
public double getLeft() {
return left;
}
public void setNumber1(double number1) {
this.number1 = number1;
public void setLeft(double left) {
this.left = left;
}
public double getNumber2() {
return number2;
public double getRight() {
return right;
}
public void setNumber2(double number2) {
this.number2 = number2;
public void setRight(double right) {
this.right = right;
}
public double getResult() {
@ -36,6 +36,6 @@ public class Addition implements CalculatorOperation {
@Override
public void perform() {
result = number1 + number2;
result = left + right;
}
}

View File

@ -1,43 +1,43 @@
package com.baeldung.o;
public class Division implements CalculatorOperation {
private double number1;
private double number2;
private double result = 0.0;
private double left;
private double right;
private double result = 0.0;
public Division(double number1, double number2) {
this.number1 = number1;
this.number2 = number2;
}
public Division(double left, double right) {
this.left = left;
this.right = right;
}
public double getNumber1() {
return number1;
}
public double getLeft() {
return left;
}
public void setNumber1(double number1) {
this.number1 = number1;
}
public void setLeft(double left) {
this.left = left;
}
public double getNumber2() {
return number2;
}
public double getRight() {
return right;
}
public void setNumber2(double number2) {
this.number2 = number2;
}
public void setRight(double right) {
this.right = right;
}
public double getResult() {
return result;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public void perform() {
if (number2 != 0) {
result = number1 / number2;
}
}
@Override
public void perform() {
if (right != 0) {
result = left / right;
}
}
}

View File

@ -1,29 +1,29 @@
package com.baeldung.o;
public class Subtraction implements CalculatorOperation {
private double number1;
private double number2;
private double left;
private double right;
private double result = 0.0;
public Subtraction(double number1, double number2) {
this.number1 = number1;
this.number2 = number2;
public Subtraction(double left, double right) {
this.left = left;
this.right = right;
}
public double getNumber1() {
return number1;
public double getLeft() {
return left;
}
public void setNumber1(double number1) {
this.number1 = number1;
public void setLeft(double left) {
this.left = left;
}
public double getNumber2() {
return number2;
public double getRight() {
return right;
}
public void setNumber2(double number2) {
this.number2 = number2;
public void setRight(double right) {
this.right = right;
}
public double getResult() {
@ -36,6 +36,6 @@ public class Subtraction implements CalculatorOperation {
@Override
public void perform() {
result = number1 - number2;
result = left - right;
}
}

View File

@ -7,8 +7,8 @@ import org.junit.jupiter.api.Test;
public class CalculatorUnitTest {
private static final double NUMBER_1 = 10.0;
private static final double NUMBER_2 = 20.0;
private static final double RIGHT = 10.0;
private static final double LEFT = 20.0;
private static final double SUM = 30.0;
private static final double SUBTRACTION_RESULT = 10.0;
private static final double DIVISION_RESULT = 2.0;
@ -22,21 +22,21 @@ public class CalculatorUnitTest {
@Test
public void whenAddTwoNumber_returnSum() {
Addition addition = new Addition(NUMBER_1, NUMBER_2);
Addition addition = new Addition(RIGHT, LEFT);
calculator.calculate(addition);
assertEquals(SUM, addition.getResult(), 0.0);
}
@Test
public void whenSutractTwoNumber_returnCorrectResult() {
Subtraction subtraction = new Subtraction(NUMBER_2, NUMBER_1);
Subtraction subtraction = new Subtraction(LEFT, RIGHT);
calculator.calculate(subtraction);
assertEquals(SUBTRACTION_RESULT, subtraction.getResult(), 0.0);
}
@Test
public void whenDivideTwoNumber_returnCorrectResult() {
Division division = new Division(NUMBER_2, NUMBER_1);
Division division = new Division(LEFT, RIGHT);
calculator.calculate(division);
assertEquals(DIVISION_RESULT, division.getResult(), 0.0);
}