улучшил

This commit is contained in:
maxnes3 2023-03-20 13:38:43 +04:00
parent 2855793c7b
commit 402b25c5cc

View File

@ -18,54 +18,49 @@ public class CalculatorService {
this.applicationContext = applicationContext; 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); typeCalculator = (ITypeCalculator)applicationContext.getBean(type);
switch (type) { try {
case "int" -> { switch (type) {
try { case "int" -> {
arg1 = Integer.valueOf(value1.toString()); arg1 = Integer.valueOf(value1.toString());
arg2 = Integer.valueOf(value2.toString()); arg2 = Integer.valueOf(value2.toString());
}catch (Exception ex){
arg1 = 0;
arg2 = 0;
} }
} case "array" -> {
case "array" -> {
try {
arg1 = new ArrayList<>(Arrays.asList(value1.toString().split(","))); arg1 = new ArrayList<>(Arrays.asList(value1.toString().split(",")));
arg2 = new ArrayList<>(Arrays.asList(value2.toString().split(","))); arg2 = new ArrayList<>(Arrays.asList(value2.toString().split(",")));
}catch (Exception ex){ }
arg1 = new ArrayList<>(); case "string" -> {
arg2 = new ArrayList<>(); 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){ 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)); return String.format("%s", typeCalculator.Plus(this.arg1, this.arg2));
} }
public Object Minus(String type, Object arg1, Object 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)); return String.format("%s", typeCalculator.Minus(this.arg1, this.arg2));
} }
public Object Multi(String type, Object arg1, Object 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)); return String.format("%s", typeCalculator.Multi(this.arg1, this.arg2));
} }
public Object Div(String type, Object arg1, Object 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)); return String.format("%s", typeCalculator.Div(this.arg1, this.arg2));
} }
} }