Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions src/main/java/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.NoSuchElementException;

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 NoSuchElementException("Incorrect operation");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe IllegalArgumentException would be better here?
image


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We don't need whitespace here

}
}

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.

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

import java.util.NoSuchElementException;
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 Calculate calculate = new Calculate();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's rename this constant, because when we write calculate.calculate it's confusing. And also constants should be with only uppercase chars and _.


@Test
void additionWithTwoPositiveOperands_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.

Let's name our methods based on the pattern <methodUnderTest>_<state>_<expectedBehavior>

double actual = calculate.calculate(6, 8, Calculate.PLUS);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's create fields answer and actual in class and in tests we only initialize them

double answer = 14;
assertEquals(answer, actual);
}

@Test
void additionWithTwoNegativeOperands_Ok() {
double actual = calculate.calculate(-6, -8, Calculate.PLUS);
double answer = -14;
assertEquals(answer, actual);
}

@Test
void additionWithPositiveAndNegativeOperands_Ok() {
double actual = calculate.calculate(-6, 8, Calculate.PLUS);
double answer = 2;
assertEquals(answer, actual);
}

@Test
void additionWithZeroInDifferentPlaces_Ok() {
double actual = calculate.calculate(0, 8, Calculate.PLUS);
double answer = 8;
assertEquals(answer,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

actual = calculate.calculate(6, 0, Calculate.PLUS);
answer = 6;
assertEquals(answer,actual);

actual = calculate.calculate(0, 0, Calculate.PLUS);
answer = 0;
assertEquals(answer,actual);
}

@Test
void subtractionWithTwoPositiveOperands_Ok() {
double actual = calculate.calculate(6, 8, Calculate.MINUS);
double answer = -2;
assertEquals(answer, actual);
}

@Test
void subtractionWithTwoNegativeOperands_Ok() {
double actual = calculate.calculate(-6, -8, Calculate.MINUS);
double answer = 2;
assertEquals(answer, actual);
}

@Test
void subtractionWithPositiveAndNegativeOperands_Ok() {
double actual = calculate.calculate(-6, 8, Calculate.MINUS);
double answer = -14;
assertEquals(answer, actual);
}

@Test
void subtractionWithZeroInDifferentPlaces_Ok() {
double actual = calculate.calculate(0, 8, Calculate.MINUS);
double answer = -8;
assertEquals(answer,actual);

actual = calculate.calculate(6, 0, Calculate.MINUS);
answer = 6;
assertEquals(answer,actual);

actual = calculate.calculate(0, 0, Calculate.MINUS);
answer = 0;
assertEquals(answer,actual);
}

@Test
void divisionWithTwoPositiveOperands_Ok() {
double actual = calculate.calculate(6, 2, Calculate.DIVISION);
double answer = 3;
assertEquals(answer, actual);
}

@Test
void divisionWithTwoNegativeOperands_Ok() {
double actual = calculate.calculate(-6, -2, Calculate.DIVISION);
double answer = 3;
assertEquals(answer, actual);
}

@Test
void divisionWithPositiveAndNegativeOperands_Ok() {
double actual = calculate.calculate(-6, 2, Calculate.DIVISION);
double answer = -3;
assertEquals(answer, actual);
}

@Test
void divisionWithZeroInStart_Ok() {
double actual = calculate.calculate(0, 8, Calculate.DIVISION);
double answer = 0;
assertEquals(answer, actual);
}

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

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

@Test
void multiplicationWithTwoPositiveOperands_Ok() {
double actual = calculate.calculate(6, 8, Calculate.MULTIPLICATION);
double answer = 48;
assertEquals(answer, actual);
}

@Test
void multiplicationWithTwoNegativeOperands_Ok() {
double actual = calculate.calculate(-6, -8, Calculate.MULTIPLICATION);
double answer = 48;
assertEquals(answer, actual);
}

@Test
void multiplicationWithPositiveAndNegativeOperands_Ok() {
double actual = calculate.calculate(-6, 8, Calculate.MULTIPLICATION);
double answer = -48;
assertEquals(answer, actual);
}

@Test
void multiplicationWithZeroInDifferentPlaces_Ok() {
double actual = calculate.calculate(0, 8, Calculate.MULTIPLICATION);
double answer = 0;
assertEquals(answer,actual);

actual = calculate.calculate(6, 0, Calculate.MULTIPLICATION);
answer = 0;
assertEquals(answer,actual);

actual = calculate.calculate(0, 0, Calculate.MULTIPLICATION);
answer = 0;
assertEquals(answer,actual);
}

@Test
void raisingPositiveValueToThePositivePower_Ok() {
double actual = calculate.calculate(5, 2, Calculate.EXPONENTIATION);
double answer = 25;
assertEquals(answer,actual);
}

@Test
void raisingNegativeValueToThePositivePower_Ok() {
double actual = calculate.calculate(-5, 2, Calculate.EXPONENTIATION);
double answer = 25;
assertEquals(answer,actual);
}

@Test
void raisingPositiveValueToTheNegativePower_Ok() {
double actual = calculate.calculate(10, -3, Calculate.EXPONENTIATION);
double answer = 0.001;
assertEquals(answer,actual);
}

@Test
void raisingNegativeValueToTheNegativePower_Ok() {
double actual = calculate.calculate(-10, -3, Calculate.EXPONENTIATION);
double answer = -0.001;
assertEquals(answer,actual);
}

@Test
void raisingPositiveValueToZeroPower_Ok() {
double actual = calculate.calculate(5, 0, Calculate.EXPONENTIATION);
double answer = 1;
assertEquals(answer,actual);
}

@Test
void raisingNegativeValueToZeroPower_Ok() {
double actual = calculate.calculate(-5, 0, Calculate.EXPONENTIATION);
double answer = 1;
assertEquals(answer,actual);
}

@Test
void raisingZeroToPower_Ok() {
double actual = calculate.calculate(0, 0, Calculate.EXPONENTIATION);
double answer = 1;
assertEquals(answer,actual);

actual = calculate.calculate(0, 5, Calculate.EXPONENTIATION);
answer = 0;
assertEquals(answer,actual);
}

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

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

This file was deleted.