Initial commit for unit-master from Calculator template
This commit is contained in:
commit
bfca1525ac
7
.idea/vcs.xml
Normal file
7
.idea/vcs.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
<mapping directory="$PROJECT_DIR$/temp_calculator" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
27
pom.xml
Normal file
27
pom.xml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>unit-test</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- todo: добавить необходимые зависимости -->
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
15
src/main/java/Main.java
Normal file
15
src/main/java/Main.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import model.Calculator;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String[] params = readParams();
|
||||||
|
System.out.println(Calculator.execute(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] readParams() {
|
||||||
|
// todo: считать из консоли данные, затем вернуть их в виде массива строк
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/main/java/model/Calculator.java
Normal file
51
src/main/java/model/Calculator.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
public class Calculator {
|
||||||
|
|
||||||
|
public static String execute(String[] params) {
|
||||||
|
String operator = params[0];
|
||||||
|
double value1;
|
||||||
|
double value2;
|
||||||
|
// todo: доработать код, чтобы все негативные тесты проходили успешно
|
||||||
|
value1 = Double.parseDouble(params[1]);
|
||||||
|
value2 = Double.parseDouble(params[2]);
|
||||||
|
double result = calculate(operator, value1, value2);
|
||||||
|
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
|
||||||
|
throw new CalculatorException("Превышен порог значений");
|
||||||
|
}
|
||||||
|
return String.valueOf(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double calculate(String operator, double a, double b) {
|
||||||
|
switch (operator) {
|
||||||
|
case "+":
|
||||||
|
return add(a, b);
|
||||||
|
case "-":
|
||||||
|
return subst(a, b);
|
||||||
|
case "*":
|
||||||
|
return mult(a, b);
|
||||||
|
case "/":
|
||||||
|
return div(a, b);
|
||||||
|
}
|
||||||
|
throw new CalculatorException("Неизвестный оператор");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double add(double a, double b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double subst(double a, double b) {
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double div(double a, double b) {
|
||||||
|
if (b == 0) {
|
||||||
|
throw new CalculatorException();
|
||||||
|
}
|
||||||
|
return a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double mult(double a, double b) {
|
||||||
|
return a * b;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/main/java/model/CalculatorException.java
Normal file
23
src/main/java/model/CalculatorException.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
public class CalculatorException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -1135355618665392597L;
|
||||||
|
private static final String MESSAGE = "Ошибка вычисления!";
|
||||||
|
|
||||||
|
public CalculatorException() {
|
||||||
|
super(MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CalculatorException(String str) {
|
||||||
|
super(MESSAGE + " " + str);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CalculatorException(Throwable e) {
|
||||||
|
super(MESSAGE, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CalculatorException(String str, Throwable e) {
|
||||||
|
super(MESSAGE + " " + str, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/test/java/NegativeCalculatorTest.java
Normal file
4
src/test/java/NegativeCalculatorTest.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
public class NegativeCalculatorTest {
|
||||||
|
// todo: @DataProvider for negativeData
|
||||||
|
// todo: negative test
|
||||||
|
}
|
||||||
4
src/test/java/PositiveCalculatorTest.java
Normal file
4
src/test/java/PositiveCalculatorTest.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
public class PositiveCalculatorTest {
|
||||||
|
// todo: @DataProvider for positiveData
|
||||||
|
// todo: positive test
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user