diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..8bd7227
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..f88c0a7
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 07a289c..35eb1dd 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,7 +1,6 @@
-
-
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..0f8333f
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,85 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {
+ "customColor": "",
+ "associatedIndex": 6
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1785351688906
+
+
+ 1785351688906
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index a130341..7b01e3e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,5 +23,11 @@
+
+ org.testng
+ testng
+ 7.10.2
+ test
+
\ No newline at end of file
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
index 9b56d81..6f564d6 100644
--- a/src/main/java/Main.java
+++ b/src/main/java/Main.java
@@ -10,6 +10,11 @@ public class Main {
private static String[] readParams() {
// todo: считать из консоли данные, затем вернуть их в виде массива строк
- return null;
+ Scanner in = new Scanner(System.in);
+
+ String operator = in.nextLine();
+ String value1 = in.nextLine();
+ String value2 = in.nextLine();
+ return new String[]{operator, value1, value2};
}
}
diff --git a/src/main/java/model/Calculator.java b/src/main/java/model/Calculator.java
index a7050eb..18517d0 100644
--- a/src/main/java/model/Calculator.java
+++ b/src/main/java/model/Calculator.java
@@ -7,8 +7,10 @@ public class Calculator {
double value1;
double value2;
// todo: доработать код, чтобы все негативные тесты проходили успешно
- value1 = Double.parseDouble(params[1]);
- value2 = Double.parseDouble(params[2]);
+ //value1 = Double.parseDouble(params[1]);
+ //value2 = Double.parseDouble(params[2]);
+ value1 = validDouble(params[1]);
+ value2 = validDouble(params[2]);
double result = calculate(operator, value1, value2);
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
throw new CalculatorException("Превышен порог значений");
@@ -17,6 +19,9 @@ public class Calculator {
}
private static double calculate(String operator, double a, double b) {
+ if (operator == null) {
+ throw new CalculatorException("Оператор не может быть null");
+ }
switch (operator) {
case "+":
return add(a, b);
@@ -48,4 +53,16 @@ public class Calculator {
private static double mult(double a, double b) {
return a * b;
}
+
+ private static double validDouble (String number) throws CalculatorException{
+ if (number == null) {
+ throw new CalculatorException("Число не может быть null");
+ }
+ try{
+ return Double.parseDouble(number);
+ }
+ catch (NumberFormatException e){
+ throw new CalculatorException("Некорректное число: " + number, e);
+ }
+ }
}
diff --git a/src/test/java/NegativeCalculatorTest.java b/src/test/java/NegativeCalculatorTest.java
index 9be4aec..db8220a 100644
--- a/src/test/java/NegativeCalculatorTest.java
+++ b/src/test/java/NegativeCalculatorTest.java
@@ -1,4 +1,47 @@
+import model.Calculator;
+import model.CalculatorException;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
public class NegativeCalculatorTest {
// todo: @DataProvider for negativeData
// todo: negative test
+ @DataProvider
+ public Object[][] negativeData(){
+ return new Object [][]{
+ {"+", String.valueOf(Integer.MAX_VALUE), "1"},
+ {"+", String.valueOf(Integer.MIN_VALUE), "-1"},
+ {"*", String.valueOf(Integer.MAX_VALUE), "2"},
+
+ {"/", "10", "0"},
+
+ {"+", "", "5"},
+ {"-", "3", ""},
+ {"*", "", ""},
+
+ {"+", "abc", "5"},
+ {"-", "3", "xyz"},
+ {"*", "12.5a", "3"},
+ {"/", "4", "2.3.5"},
+
+ {"+", "@#$", "5"},
+ {"-", "3", "!@#"},
+
+ {"+", " ", "5"},
+ {"-", "3", " "},
+
+ {"", "10", "5"},
+ {"=", "5", "3"},
+
+ {null, "5", "3"},
+ {"+", null, "3"},
+ };
+ }
+
+ @Test (dataProvider = "negativeData", expectedExceptions = CalculatorException.class)
+ public void negativeTest(String operator, String a, String b){
+ String[] params = {operator, a, b};
+ Calculator.execute(params);
+ }
}
diff --git a/src/test/java/PositiveCalculatorTest.java b/src/test/java/PositiveCalculatorTest.java
index ce88925..b7f0f6e 100644
--- a/src/test/java/PositiveCalculatorTest.java
+++ b/src/test/java/PositiveCalculatorTest.java
@@ -1,4 +1,42 @@
+import model.Calculator;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
public class PositiveCalculatorTest {
// todo: @DataProvider for positiveData
// todo: positive test
+ @DataProvider
+ public Object[][] positiveData(){
+ return new Object [][]{
+ {"+", "3", "4", 7.0},
+ {"+", "10.5", "2.3", 12.8},
+ {"+", "-5.5", "3.2", -2.3},
+
+ {"-", "10", "5", 5.0},
+ {"-", "15.7", "3.2", 12.5},
+ {"-", "-2.5", "-1.5", -1.0},
+
+ {"*", "3", "0", 0.0},
+ {"*", "2.5", "4.0", 10.0},
+ {"*", "-3.0", "2.5", -7.5},
+
+ {"/", "10", "2", 5.0},
+ {"/", "15.0", "4.0", 3.75},
+ {"/", "-10.0", "4.0", -2.5},
+
+
+ {"+", String.valueOf(Integer.MAX_VALUE - 1), "1", (double)Integer.MAX_VALUE},
+ {"+", String.valueOf(Integer.MIN_VALUE + 1), "-1", (double)Integer.MIN_VALUE},
+ {"*", String.valueOf(Integer.MAX_VALUE), "1", (double)Integer.MAX_VALUE},
+ };
+ }
+
+ @Test(dataProvider = "positiveData")
+ public void positiveTest(String operator, String a, String b, double expected){
+ String[] params = {operator, a, b};
+ double result = Double.parseDouble(Calculator.execute(params));
+ Assert.assertEquals(result, expected, 0.001);
+ }
+
}
\ No newline at end of file
diff --git a/target/classes/Main.class b/target/classes/Main.class
new file mode 100644
index 0000000..33a2be2
Binary files /dev/null and b/target/classes/Main.class differ
diff --git a/target/classes/model/Calculator.class b/target/classes/model/Calculator.class
new file mode 100644
index 0000000..9fa2e9a
Binary files /dev/null and b/target/classes/model/Calculator.class differ
diff --git a/target/classes/model/CalculatorException.class b/target/classes/model/CalculatorException.class
new file mode 100644
index 0000000..2a16de7
Binary files /dev/null and b/target/classes/model/CalculatorException.class differ
diff --git a/target/test-classes/NegativeCalculatorTest.class b/target/test-classes/NegativeCalculatorTest.class
new file mode 100644
index 0000000..2eadf5d
Binary files /dev/null and b/target/test-classes/NegativeCalculatorTest.class differ
diff --git a/target/test-classes/PositiveCalculatorTest.class b/target/test-classes/PositiveCalculatorTest.class
new file mode 100644
index 0000000..9a479f8
Binary files /dev/null and b/target/test-classes/PositiveCalculatorTest.class differ