diff --git a/front/index.html b/front/index.html index 6e8a456..e0209e6 100644 --- a/front/index.html +++ b/front/index.html @@ -8,10 +8,10 @@ - Volt's Site + Calculate Site -
+
Введите первое число diff --git a/src/main/java/com/example/demo/speaker/service/MethodService.java b/src/main/java/com/example/demo/speaker/service/MethodService.java index 2c0c17e..821d7f2 100644 --- a/src/main/java/com/example/demo/speaker/service/MethodService.java +++ b/src/main/java/com/example/demo/speaker/service/MethodService.java @@ -17,34 +17,70 @@ public class MethodService { public String Sum(Object first, Object second, String type) { final IMethod operation = (IMethod) applicationContext.getBean(type); if (operation instanceof MethodString){ - return String.format("%s", operation.Sum(first,second)); + if(first instanceof String && second instanceof String) + { + return String.format("%s", operation.Sum(first,second)); + } + return "Uncorrect type"; }else if (operation instanceof MethodBoolean){ - return operation.Sum(Boolean.parseBoolean(first.toString()),Boolean.parseBoolean(second.toString())).toString(); + if(first instanceof Boolean && second instanceof Boolean) + { + return operation.Sum(Boolean.parseBoolean(first.toString()),Boolean.parseBoolean(second.toString())).toString(); + } + else return "Uncorrect type"; } else{ - return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + if(first instanceof Integer && second instanceof Integer) + { + return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + else return "Uncorrect type"; } } public String Ras(Object first, Object second, String type) { final IMethod operation = (IMethod) applicationContext.getBean(type); if (operation instanceof MethodString){ - return String.format("%s", operation.Minus(first,Integer.parseInt(second.toString()))); + if(first instanceof String && second instanceof Integer) + { + return String.format("%s", operation.Minus(first,Integer.parseInt(second.toString()))); + } + return "Uncorrect type"; }else if (operation instanceof MethodBoolean){ - return operation.Minus(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString(); + if(first instanceof Boolean && second instanceof Integer) + { + return operation.Minus(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString(); + } + else return "Uncorrect type"; }else{ - return String.format("%s", operation.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + if(first instanceof Integer && second instanceof Integer) + { + return String.format("%s", operation.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + else return "Uncorrect type"; } } public String Pros(Object first, Object second, String type) { final IMethod operation = (IMethod) applicationContext.getBean(type); if (operation instanceof MethodString){ - return String.format("%s", operation.Multiply(first,Integer.parseInt(second.toString()))); + if(first instanceof String && second instanceof Integer) + { + return String.format("%s", operation.Multiply(first,Integer.parseInt(second.toString()))); + } + return "Uncorrect type"; }else if (operation instanceof MethodBoolean){ - return operation.Multiply(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString(); + if(first instanceof Boolean && second instanceof Integer) + { + return operation.Multiply(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString(); + } + else return "Uncorrect type"; }else{ - return String.format("%s", operation.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + if(first instanceof Integer && second instanceof Integer) + { + return String.format("%s", operation.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + else return "Uncorrect type"; } } @@ -53,6 +89,10 @@ public class MethodService { if (operation instanceof MethodString){ return String.format("%s", operation.Div(first,second)); }else if (operation instanceof MethodBoolean){ + if(first.toString()!="true" && second.toString() != "false") + { + throw new ClassCastException("Uncorrect type"); + } return String.format("%s", operation.Div(Boolean.parseBoolean(first.toString()), Boolean.parseBoolean(second.toString())).toString()); }else { return String.format("%s", operation.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); diff --git a/src/test/java/com/example/demo/DemoApplicationTests.java b/src/test/java/com/example/demo/DemoApplicationTests.java index 798ae30..3afd397 100644 --- a/src/test/java/com/example/demo/DemoApplicationTests.java +++ b/src/test/java/com/example/demo/DemoApplicationTests.java @@ -11,6 +11,22 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException; class DemoApplicationTests { @Autowired private MethodService methodService; + @Test + void testIntType() { + String res = methodService.Sum(2, "2", "int"); + Assertions.assertEquals("Uncorrect type", res); + } + @Test + void testStringType() { + String res = methodService.Sum(2, 2, "string"); + Assertions.assertEquals("Uncorrect type", res); + } + @Test + void testBooleanType() { + String res = methodService.Sum("3", "2", "boolean"); + Assertions.assertEquals("Uncorrect type", res); + } + @Test void testIntSum() { Integer res = Integer.valueOf(methodService.Sum(2, 2, "int")); @@ -18,7 +34,7 @@ class DemoApplicationTests { } @Test void testStringSum() { - String res = methodService.Sum("hello, ", 2, "string"); + String res = methodService.Sum("hello, ", "2", "string"); Assertions.assertEquals("hello, 2", res); } @Test @@ -39,8 +55,8 @@ class DemoApplicationTests { } @Test void testBooleanDiff() { - String res = methodService.Ras(1, 2, "boolean"); - Assertions.assertEquals(true, res); + String res = methodService.Ras(true, 2, "boolean"); + Assertions.assertEquals("false", res); } @Test @@ -66,7 +82,7 @@ class DemoApplicationTests { } @Test void testStringDivide() { - String res = methodService.Del("test", 2, "string"); + String res = methodService.Del("test", "2", "string"); Assertions.assertEquals("false", res); } @Test @@ -80,4 +96,19 @@ class DemoApplicationTests { Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> methodService.Sum(1, 2, "date")); } + @Test + void testErrorTypeString() { + Assertions.assertThrows(ClassCastException.class, () -> methodService.Del(1, 2, "string")); + } + + @Test + void testErrorTypeBoolean() { + Assertions.assertThrows(ClassCastException.class, () -> methodService.Del("jjj15", 3, "boolean")); + } + + @Test + void testErrorTypeInt() { + Assertions.assertThrows(NumberFormatException.class, () -> methodService.Del(true, 2, "int")); + } + }