parent
41a931d293
commit
44a1ec0195
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
public class Addition implements CalculatorOperation {
|
||||
private double number1;
|
||||
private double number2;
|
||||
private double result = 0.0;
|
||||
|
||||
public Addition(double number1, double number2) {
|
||||
this.number1 = number1;
|
||||
this.number2 = number2;
|
||||
}
|
||||
|
||||
public double getNumber1() {
|
||||
return number1;
|
||||
}
|
||||
|
||||
public void setNumber1(double number1) {
|
||||
this.number1 = number1;
|
||||
}
|
||||
|
||||
public double getNumber2() {
|
||||
return number2;
|
||||
}
|
||||
|
||||
public void setNumber2(double number2) {
|
||||
this.number2 = number2;
|
||||
}
|
||||
|
||||
public double getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(double result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform() {
|
||||
result = number1 + number2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
public class Calculator {
|
||||
|
||||
public void calculate(CalculatorOperation operation) {
|
||||
if (operation == null) {
|
||||
throw new InvalidParameterException("Can not perform operation");
|
||||
}
|
||||
|
||||
operation.perform();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
public interface CalculatorOperation {
|
||||
|
||||
void perform();
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
public class Division implements CalculatorOperation {
|
||||
private double number1;
|
||||
private double number2;
|
||||
private double result = 0.0;
|
||||
|
||||
public Division(double number1, double number2) {
|
||||
this.number1 = number1;
|
||||
this.number2 = number2;
|
||||
}
|
||||
|
||||
public double getNumber1() {
|
||||
return number1;
|
||||
}
|
||||
|
||||
public void setNumber1(double number1) {
|
||||
this.number1 = number1;
|
||||
}
|
||||
|
||||
public double getNumber2() {
|
||||
return number2;
|
||||
}
|
||||
|
||||
public void setNumber2(double number2) {
|
||||
this.number2 = number2;
|
||||
}
|
||||
|
||||
public double getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(double result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform() {
|
||||
if (number2 != 0) {
|
||||
result = number1 / number2;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
public class Subtraction implements CalculatorOperation {
|
||||
private double number1;
|
||||
private double number2;
|
||||
private double result = 0.0;
|
||||
|
||||
public Subtraction(double number1, double number2) {
|
||||
this.number1 = number1;
|
||||
this.number2 = number2;
|
||||
}
|
||||
|
||||
public double getNumber1() {
|
||||
return number1;
|
||||
}
|
||||
|
||||
public void setNumber1(double number1) {
|
||||
this.number1 = number1;
|
||||
}
|
||||
|
||||
public double getNumber2() {
|
||||
return number2;
|
||||
}
|
||||
|
||||
public void setNumber2(double number2) {
|
||||
this.number2 = number2;
|
||||
}
|
||||
|
||||
public double getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(double result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform() {
|
||||
result = number1 - number2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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 SUM = 30.0;
|
||||
private static final double SUBTRACTION_RESULT = 10.0;
|
||||
private static final double DIVISION_RESULT = 2.0;
|
||||
|
||||
private Calculator calculator;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
calculator = new Calculator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddTwoNumber_returnSum() {
|
||||
Addition addition = new Addition(NUMBER_1, NUMBER_2);
|
||||
calculator.calculate(addition);
|
||||
assertEquals(SUM, addition.getResult(), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSutractTwoNumber_returnCorrectResult() {
|
||||
Subtraction subtraction = new Subtraction(NUMBER_2, NUMBER_1);
|
||||
calculator.calculate(subtraction);
|
||||
assertEquals(SUBTRACTION_RESULT, subtraction.getResult(), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoNumber_returnCorrectResult() {
|
||||
Division division = new Division(NUMBER_2, NUMBER_1);
|
||||
calculator.calculate(division);
|
||||
assertEquals(DIVISION_RESULT, division.getResult(), 0.0);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue