diff --git a/src/main/java/ru/ip/labworks/labworks/service/CalculatorService.java b/src/main/java/ru/ip/labworks/labworks/service/CalculatorService.java index 671bba6..403dec9 100644 --- a/src/main/java/ru/ip/labworks/labworks/service/CalculatorService.java +++ b/src/main/java/ru/ip/labworks/labworks/service/CalculatorService.java @@ -18,54 +18,49 @@ public class CalculatorService { this.applicationContext = applicationContext; } - private void ValidateParams(Object value1, Object value2, String type){ + private boolean ValidateParams(Object value1, Object value2, String type){ typeCalculator = (ITypeCalculator)applicationContext.getBean(type); - switch (type) { - case "int" -> { - try { + try { + switch (type) { + case "int" -> { arg1 = Integer.valueOf(value1.toString()); arg2 = Integer.valueOf(value2.toString()); - }catch (Exception ex){ - arg1 = 0; - arg2 = 0; } - } - case "array" -> { - try { + case "array" -> { arg1 = new ArrayList<>(Arrays.asList(value1.toString().split(","))); arg2 = new ArrayList<>(Arrays.asList(value2.toString().split(","))); - }catch (Exception ex){ - arg1 = new ArrayList<>(); - arg2 = new ArrayList<>(); + } + case "string" -> { + arg1 = value1.toString(); + arg2 = value2.toString(); } } - case "string" -> { - arg1 = value1.toString(); - arg2 = value2.toString(); - } } - + catch (Exception ex){ + return false; + } + return true; } public Object Plus(String type, Object arg1, Object arg2){ - ValidateParams(arg1,arg2,type); + if(!ValidateParams(arg1,arg2,type)) return String.format("Неверные данные"); return String.format("%s", typeCalculator.Plus(this.arg1, this.arg2)); } public Object Minus(String type, Object arg1, Object arg2){ - ValidateParams(arg1,arg2,type); + if(!ValidateParams(arg1,arg2,type)) return String.format("Неверные данные"); return String.format("%s", typeCalculator.Minus(this.arg1, this.arg2)); } public Object Multi(String type, Object arg1, Object arg2){ - ValidateParams(arg1,arg2,type); + if(!ValidateParams(arg1,arg2,type)) return String.format("Неверные данные"); return String.format("%s", typeCalculator.Multi(this.arg1, this.arg2)); } public Object Div(String type, Object arg1, Object arg2){ - ValidateParams(arg1,arg2,type); + if(!ValidateParams(arg1,arg2,type)) return String.format("Неверные данные"); return String.format("%s", typeCalculator.Div(this.arg1, this.arg2)); } }