Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Calculate {
double calculate(double a, double b, char c);
}
25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax;

public class Calculator implements Calculate {

@Override
public double calculate(double firstParameter, double secondParameter, char operation) {
switch (operation) {
case '+':
return firstParameter + secondParameter;
case '-':
return firstParameter - secondParameter;
case '*':
return firstParameter * secondParameter;
case '^':
return Math.pow(firstParameter, secondParameter);
Comment on lines +14 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а что если первый операнд 0, а второй -2?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

добавлена проверка Double.POSITIVE_INFINITY

case '/':
if (secondParameter == 0) {
throw new IllegalArgumentException("Division by zero");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new ArithmeticException("Division by zero is not possible!");

}
return firstParameter / secondParameter;
default:
throw new IllegalArgumentException("Invalid operation");
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

137 changes: 137 additions & 0 deletions src/test/java/core/basesyntax/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package core.basesyntax;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class CalculatorTest {
private static final double DELTA = 0.0001;
private Calculate calculator = new Calculator();

@Test
void additionWithTwoPositiveOperands_Ok() {
double expected = 100.0;
double actual = calculator.calculate(90.0, 10.0, '+');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

создай 1 раз expected and actual и используй их в каждом методе

assertEquals(expected, actual);
}

@Test
void additionWithTwoNegativeOperands_Ok() {
double expected = - 150.0;
double actual = calculator.calculate(-100.0, -50.0, '+');
assertEquals(expected, actual);
}

@Test
void additionWithPositiveAndNegativeOperands_Ok() {
double expected = - 150.0;
double actual = calculator.calculate(-200.0, 50.0, '+');
assertEquals(expected, actual);
}

@Test
void additionWithFirstOperandIsZero_Ok() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

измени все названия методов по примеру calculate_additionPositiveAndNegativeOperand_ok

double expected = 50.0;
double actual = calculator.calculate(0.0, 50.0, '+');
assertEquals(expected, actual);
}

@Test
void additionWithSecondOperandIsZero_Ok() {
double expected = 60.0;
double actual = calculator.calculate(60.0, 0.0, '+');
assertEquals(expected, actual);
}

@Test
void additionWithMinAndMaxDoubleValues_Ok() {
double expected = Double.MAX_VALUE + Double.MIN_VALUE;
double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+');
assertEquals(expected, actual, DELTA);
}

@Test
void subtractionWithMinAndMaxDoubleValues_Ok() {
double expected = Double.MAX_VALUE - Double.MIN_VALUE;
double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-');
assertEquals(expected, actual, DELTA);
}

@Test
void multiplicationWithMinAndMaxDoubleValues_Ok() {
double expected = Double.MAX_VALUE * Double.MIN_VALUE;
double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*');
assertEquals(expected, actual, DELTA);
}

@Test
void divisionWithMinAndMaxDoubleValues_Ok() {
double expected = Double.MAX_VALUE / Double.MIN_VALUE;
double actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '/');
assertEquals(expected, actual, DELTA);
}

@Test
void divisionByZero_NotOk() {
assertThrows(IllegalArgumentException.class, () -> {
calculator.calculate(2.0, 0.0, '/');
});
}

@Test
void raisingPositiveValueToThePositivePower_Ok() {
double expected = 64.0;
double actual = calculator.calculate(8.0, 2.0, '^');
assertEquals(expected, actual);
}

@Test
void raisingNegativeValueToThePositivePower_Ok() {
double expected = 64.0;
double actual = calculator.calculate(-8.0, 2.0, '^');
assertEquals(expected, actual);
}

@Test
void raisingPositiveValueToTheNegativePower_Ok() {
double expected = 0.015625;
double actual = calculator.calculate(8.0, -2.0, '^');
assertEquals(expected, actual, DELTA);
}

@Test
void raisingNegativeValueToTheNegativePower_Ok() {
double expected = 0.015625;
double actual = calculator.calculate(-8.0, -2.0, '^');
assertEquals(expected, actual, DELTA);
}

@Test
void raisingPositiveValueToZeroPower_Ok() {
double expected = 1.0;
double actual = calculator.calculate(8.0, 0.0, '^');
assertEquals(expected, actual);
}

@Test
void raisingNegativeValueToZeroPower_Ok() {
double expected = 1.0;
double actual = calculator.calculate(-8.0, 0.0, '^');
assertEquals(expected, actual);
}

@Test
void raisingZeroToPower_Ok() {
double expected = 0.0;
double actual = calculator.calculate(0.0, 10.0, '^');
assertEquals(expected, actual);
}

@Test
void illegalOperation_NotOk() {
assertThrows(IllegalArgumentException.class, () -> {
calculator.calculate(2.0, 3.0, '#');
});
}
}
8 changes: 0 additions & 8 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.