Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions src/main/java/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
public class Calculate {
public static final char PLUS = '+';
public static final char MINUS = '-';
public static final char DIVISION = '/';
public static final char MULTIPLICATION = '*';
public static final char EXPONENTIATION = '^';

public double calculate(double firstNumber, double secondNumber, char operation) {
switch (operation) {
case PLUS:
return getTheAmountResult(firstNumber, secondNumber);
case MINUS:
return getTheSubtractionResult(firstNumber, secondNumber);
case DIVISION:
return getTheDivisionResult(firstNumber, secondNumber);
case MULTIPLICATION:
return getTheMultiplicationResult(firstNumber, secondNumber);
case EXPONENTIATION:
return getTheExponentiationResult(firstNumber, secondNumber);
default:
throw new IllegalArgumentException("Incorrect operation");
}
}

private double getTheAmountResult(double firstNumber, double secondNumber) {
return firstNumber + secondNumber;
}

private double getTheSubtractionResult(double firstNumber, double secondNumber) {
return firstNumber - secondNumber;
}

private double getTheDivisionResult(double firstNumber, double secondNumber) {
if (secondNumber == 0) {
throw new ArithmeticException("We can't divide by zero");
}
return firstNumber / secondNumber;
}

private double getTheMultiplicationResult(double firstNumber, double secondNumber) {
return firstNumber * secondNumber;
}

private double getTheExponentiationResult(double firstNumber, double secondNumber) {
if (firstNumber == 0 && secondNumber < 0) {
throw new ArithmeticException("We can't divide by zero");
}
return Math.pow(firstNumber, secondNumber);
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

248 changes: 248 additions & 0 deletions src/test/java/CalculateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class CalculateTest {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what about addition for min and max double values;?

private static final double DELTA = 0.0001;
private static Calculate calculator;
private double actual;
private double expected;

@BeforeAll
static void beforeAll() {
calculator = new Calculate();
}

@Test
void calculate_additionWithTwoPositiveOperands_Ok() {
actual = calculator.calculate(6, 8, Calculate.PLUS);
expected = 14;
assertEquals(expected, actual);
}

@Test
void calculate_additionWithTwoNegativeOperands_Ok() {
actual = calculator.calculate(-6, -8, Calculate.PLUS);
expected = -14;
assertEquals(expected, actual);
}

@Test
void calculate_additionWithPositiveAndNegativeOperands_Ok() {
actual = calculator.calculate(-6, 8, Calculate.PLUS);
expected = 2;
assertEquals(expected, actual);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't think that it is good to use whitespaces between tests in one method. Maybe is not a mistake, and that is more readable, better to ask a mentor.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agree, lets remove redundant empty lines

@Test
void calculate_additionWithZeroInDifferentPlaces_Ok() {
actual = calculator.calculate(0, 8, Calculate.PLUS);
expected = 8;
assertEquals(expected,actual);
actual = calculator.calculate(6, 0, Calculate.PLUS);
expected = 6;
assertEquals(expected,actual);
actual = calculator.calculate(0, 0, Calculate.PLUS);
expected = 0;
assertEquals(expected,actual);
}

@Test
void calculate_additionWithMaxValues_OK() {
actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, Calculate.PLUS);
expected = Double.POSITIVE_INFINITY;
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_additionWithMinValue_OK() {
actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, Calculate.PLUS);
expected = 0;
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_subtractionWithTwoPositiveOperands_Ok() {
actual = calculator.calculate(6, 8, Calculate.MINUS);
expected = -2;
assertEquals(expected, actual);
}

@Test
void calculate_subtractionWithTwoNegativeOperands_Ok() {
actual = calculator.calculate(-6, -8, Calculate.MINUS);
expected = 2;
assertEquals(expected, actual);
}

@Test
void calculate_subtractionWithFirstPositiveAndSecondNegativeOperands_Ok() {
actual = calculator.calculate(-6, 8, Calculate.MINUS);
expected = -14;
assertEquals(expected, actual);
}

@Test
void calculate_subtractionWithFirstNegativeAndSecondPositiveOperands_Ok() {
actual = calculator.calculate(6, -8, Calculate.MINUS);
expected = 14;
assertEquals(expected, actual);
}

@Test
void calculate_subtractionWithZeroInDifferentPlaces_Ok() {
actual = calculator.calculate(0, 8, Calculate.MINUS);
expected = -8;
assertEquals(expected,actual);
actual = calculator.calculate(6, 0, Calculate.MINUS);
expected = 6;
assertEquals(expected,actual);
actual = calculator.calculate(0, 0, Calculate.MINUS);
expected = 0;
assertEquals(expected,actual);
}

@Test
void calculate_divisionWithTwoPositiveOperands_Ok() {
actual = calculator.calculate(6, 2, Calculate.DIVISION);
expected = 3;
assertEquals(expected, actual);
}

@Test
void calculate_divisionWithTwoNegativeOperands_Ok() {
actual = calculator.calculate(-6, -2, Calculate.DIVISION);
expected = 3;
assertEquals(expected, actual);
}

@Test
void calculate_divisionWithPositiveAndNegativeOperands_Ok() {
actual = calculator.calculate(-6, 2, Calculate.DIVISION);
expected = -3;
assertEquals(expected, actual);
}

@Test
void calculate_divisionWithZeroInStart_Ok() {
actual = calculator.calculate(0, 8, Calculate.DIVISION);
expected = 0;
assertEquals(expected, actual);
}

@Test
void calculate_divisionWithZeroInEnd_notOk() {
assertThrows(ArithmeticException.class, () -> {
calculator.calculate(6, 0, Calculate.DIVISION);
});
}

@Test
void calculate_divisionWithZeroInStartAndEnd_notOk() {
assertThrows(ArithmeticException.class, () -> {
calculator.calculate(0, 0, Calculate.DIVISION);
});
}

@Test
void calculate_multiplicationWithTwoPositiveOperands_Ok() {
actual = calculator.calculate(6, 8, Calculate.MULTIPLICATION);
expected = 48;
assertEquals(expected, actual);
}

@Test
void calculate_multiplicationWithTwoNegativeOperands_Ok() {
actual = calculator.calculate(-6, -8, Calculate.MULTIPLICATION);
expected = 48;
assertEquals(expected, actual);
}

@Test
void calculate_multiplicationWithPositiveAndNegativeOperands_Ok() {
actual = calculator.calculate(-6, 8, Calculate.MULTIPLICATION);
expected = -48;
assertEquals(expected, actual);
}

@Test
void calculate_multiplicationWithZeroInDifferentPlaces_Ok() {
actual = calculator.calculate(0, 8, Calculate.MULTIPLICATION);
expected = 0;
assertEquals(expected,actual);
actual = calculator.calculate(6, 0, Calculate.MULTIPLICATION);
expected = 0;
assertEquals(expected,actual);
actual = calculator.calculate(0, 0, Calculate.MULTIPLICATION);
expected = 0;
assertEquals(expected,actual);
}

@Test
void calculate_raisingPositiveValueToThePositivePower_Ok() {
actual = calculator.calculate(5, 2, Calculate.EXPONENTIATION);
expected = 25;
assertEquals(expected,actual);
}

@Test
void calculate_raisingNegativeValueToThePositivePower_Ok() {
actual = calculator.calculate(-5, 2, Calculate.EXPONENTIATION);
expected = 25;
assertEquals(expected,actual);
}

@Test
void calculate_raisingPositiveValueToTheNegativePower_Ok() {
actual = calculator.calculate(10, -3, Calculate.EXPONENTIATION);
expected = 0.001;
assertEquals(expected,actual);
}

@Test
void calculate_raisingNegativeValueToTheNegativePower_Ok() {
actual = calculator.calculate(-10, -3, Calculate.EXPONENTIATION);
expected = -0.001;
assertEquals(expected,actual);
}

@Test
void calculate_raisingPositiveValueToZeroPower_Ok() {
actual = calculator.calculate(5, 0, Calculate.EXPONENTIATION);
expected = 1;
assertEquals(expected,actual);
}

@Test
void calculate_raisingNegativeValueToZeroPower_Ok() {
actual = calculator.calculate(-5, 0, Calculate.EXPONENTIATION);
expected = 1;
assertEquals(expected,actual);
}

@Test
void calculate_raisingZeroToPower_Ok() {
actual = calculator.calculate(0, 0, Calculate.EXPONENTIATION);
expected = 1;
assertEquals(expected,actual);
actual = calculator.calculate(0, 5, Calculate.EXPONENTIATION);
expected = 0;
assertEquals(expected,actual);
}

@Test
void calculate_raisingZeroToNegativePower_notOk() {
assertThrows(ArithmeticException.class, () -> {
calculator.calculate(0, -5, Calculate.EXPONENTIATION);
});
}

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

This file was deleted.