diff --git a/src/main/java/ru/ip/labworks/labworks/domain/ArrayCalculator.java b/src/main/java/ru/ip/labworks/labworks/domain/ArrayCalculator.java index 728d2bd..1822bc5 100644 --- a/src/main/java/ru/ip/labworks/labworks/domain/ArrayCalculator.java +++ b/src/main/java/ru/ip/labworks/labworks/domain/ArrayCalculator.java @@ -2,41 +2,47 @@ package ru.ip.labworks.labworks.domain; import org.springframework.stereotype.Component; +import java.util.ArrayList; + @Component(value = "array") -public class ArrayCalculator implements ITypeCalculator{ +public class ArrayCalculator implements ITypeCalculator> { @Override - public Object Plus(Object arg1, Object arg2) { - String[] result = arg1.toString().split(","); - for (int i = 0; i < result.length; ++i){ - result[i] = Integer.toString(Integer.parseInt(result[i]) + Integer.parseInt(arg2.toString())); + public ArrayList Plus(ArrayList arg1, ArrayList arg2) { + ArrayList result = new ArrayList<>(); + for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){ + result.add(arg1.get(i) + arg2.get(i)); } - return String.join(",", result); + return result; } @Override - public Object Minus(Object arg1, Object arg2) { - String[] result = arg1.toString().split(","); - for (int i = 0; i < result.length; ++i){ - result[i] = Integer.toString(Integer.parseInt(result[i]) - Integer.parseInt(arg2.toString())); + public ArrayList Minus(ArrayList arg1, ArrayList arg2) { + ArrayList result = new ArrayList<>(); + for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){ + result.add(arg1.get(i).replace(arg2.get(i), "")); } - return String.join(",", result); + return result; } @Override - public Object Multi(Object arg1, Object arg2) { - String[] result = arg1.toString().split(","); - for (int i = 0; i < result.length; ++i){ - result[i] = Integer.toString(Integer.parseInt(result[i]) * Integer.parseInt(arg2.toString())); + public ArrayList Multi(ArrayList arg1, ArrayList arg2) { + ArrayList result = new ArrayList<>(); + for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){ + String temp = ""; + for (int j = 0; j < Integer.parseInt(arg2.get(i)); ++j){ + temp += arg1.get(i); + } + result.add(temp); } - return String.join(",", result); + return result; } @Override - public Object Div(Object arg1, Object arg2) { - String[] result = arg1.toString().split(","); - for (int i = 0; i < result.length; ++i){ - result[i] = Integer.toString(Integer.parseInt(result[i]) / Integer.parseInt(arg2.toString())); + public ArrayList Div(ArrayList arg1, ArrayList arg2) { + ArrayList result = new ArrayList<>(); + for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){ + result.add(arg1.get(i).substring(0, Integer.parseInt(arg2.get(i)))); } - return String.join(",", result); + return result; } } diff --git a/src/main/java/ru/ip/labworks/labworks/domain/ITypeCalculator.java b/src/main/java/ru/ip/labworks/labworks/domain/ITypeCalculator.java index 081fedd..afaa406 100644 --- a/src/main/java/ru/ip/labworks/labworks/domain/ITypeCalculator.java +++ b/src/main/java/ru/ip/labworks/labworks/domain/ITypeCalculator.java @@ -1,9 +1,9 @@ package ru.ip.labworks.labworks.domain; -public interface ITypeCalculator { +public interface ITypeCalculator { - Object Plus(Object arg1, Object arg2); - Object Minus(Object arg1, Object arg2); - Object Multi(Object arg1, Object arg2); - Object Div(Object arg1, Object arg2); + T Plus(T arg1, T arg2); + T Minus(T arg1, T arg2); + T Multi(T arg1, T arg2); + T Div(T arg1, T arg2); } diff --git a/src/main/java/ru/ip/labworks/labworks/domain/IntCalculator.java b/src/main/java/ru/ip/labworks/labworks/domain/IntCalculator.java index 8156b41..43fc34f 100644 --- a/src/main/java/ru/ip/labworks/labworks/domain/IntCalculator.java +++ b/src/main/java/ru/ip/labworks/labworks/domain/IntCalculator.java @@ -3,28 +3,24 @@ package ru.ip.labworks.labworks.domain; import org.springframework.stereotype.Component; @Component(value = "int") -public class IntCalculator implements ITypeCalculator{ +public class IntCalculator implements ITypeCalculator{ @Override - public Object Plus(Object arg1, Object arg2) { - int result = Integer.parseInt(arg1.toString()) + Integer.parseInt(arg2.toString()); - return Integer.toString(result); + public Integer Plus(Integer arg1, Integer arg2) { + return arg1 + arg2; } @Override - public Object Minus(Object arg1, Object arg2) { - int result = Integer.parseInt(arg1.toString()) - Integer.parseInt(arg2.toString()); - return Integer.toString(result); + public Integer Minus(Integer arg1, Integer arg2) { + return arg1 - arg2; } @Override - public Object Multi(Object arg1, Object arg2) { - int result = Integer.parseInt(arg1.toString()) * Integer.parseInt(arg2.toString()); - return Integer.toString(result); + public Integer Multi(Integer arg1, Integer arg2) { + return arg1 * arg2; } @Override - public Object Div(Object arg1, Object arg2) { - int result = Integer.parseInt(arg1.toString()) / Integer.parseInt(arg2.toString()); - return Integer.toString(result); + public Integer Div(Integer arg1, Integer arg2) { + return arg1 / arg2; } } diff --git a/src/main/java/ru/ip/labworks/labworks/domain/StringCalculator.java b/src/main/java/ru/ip/labworks/labworks/domain/StringCalculator.java index bb70756..074afa9 100644 --- a/src/main/java/ru/ip/labworks/labworks/domain/StringCalculator.java +++ b/src/main/java/ru/ip/labworks/labworks/domain/StringCalculator.java @@ -3,29 +3,29 @@ package ru.ip.labworks.labworks.domain; import org.springframework.stereotype.Component; @Component(value = "string") -public class StringCalculator implements ITypeCalculator{ +public class StringCalculator implements ITypeCalculator{ @Override - public Object Plus(Object arg1, Object arg2) { - return arg1.toString().concat(arg2.toString()); + public String Plus(String arg1, String arg2) { + return arg1 + arg2; } @Override - public Object Minus(Object arg1, Object arg2) { - return arg1.toString().replaceAll(arg2.toString(), ""); + public String Minus(String arg1, String arg2) { + return arg1.replaceAll(arg2, ""); } @Override - public Object Multi(Object arg1, Object arg2) { + public String Multi(String arg1, String arg2) { String result = ""; - for (int i = 0; i < Integer.parseInt(arg2.toString()); ++i){ - result += arg1.toString(); + for (int i = 0; i < Integer.parseInt(arg2); ++i){ + result += arg1; } return result; } @Override - public Object Div(Object arg1, Object arg2) { - String result = arg1.toString().substring(0, arg1.toString().length() / Integer.parseInt(arg2.toString())); + public String Div(String arg1, String arg2) { + String result = arg1.substring(0, arg1.length() / Integer.parseInt(arg2)); return result; } } 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 3aa551a..671bba6 100644 --- a/src/main/java/ru/ip/labworks/labworks/service/CalculatorService.java +++ b/src/main/java/ru/ip/labworks/labworks/service/CalculatorService.java @@ -4,31 +4,68 @@ import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import ru.ip.labworks.labworks.domain.ITypeCalculator; +import java.util.ArrayList; +import java.util.Arrays; + @Service public class CalculatorService { private final ApplicationContext applicationContext; + private ITypeCalculator typeCalculator; + private Object arg1; + private Object arg2; public CalculatorService(ApplicationContext applicationContext){ this.applicationContext = applicationContext; } + private void ValidateParams(Object value1, Object value2, String type){ + typeCalculator = (ITypeCalculator)applicationContext.getBean(type); + + switch (type) { + case "int" -> { + try { + arg1 = Integer.valueOf(value1.toString()); + arg2 = Integer.valueOf(value2.toString()); + }catch (Exception ex){ + arg1 = 0; + arg2 = 0; + } + } + case "array" -> { + try { + 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(); + } + } + + } + + public Object Plus(String type, Object arg1, Object arg2){ - final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); - return calculator.Plus(arg1, arg2); + ValidateParams(arg1,arg2,type); + return String.format("%s", typeCalculator.Plus(this.arg1, this.arg2)); } public Object Minus(String type, Object arg1, Object arg2){ - final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); - return calculator.Minus(arg1, arg2); + ValidateParams(arg1,arg2,type); + return String.format("%s", typeCalculator.Minus(this.arg1, this.arg2)); } public Object Multi(String type, Object arg1, Object arg2){ - final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); - return calculator.Multi(arg1, arg2); + ValidateParams(arg1,arg2,type); + return String.format("%s", typeCalculator.Multi(this.arg1, this.arg2)); } public Object Div(String type, Object arg1, Object arg2){ - final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); - return calculator.Div(arg1, arg2); + ValidateParams(arg1,arg2,type); + return String.format("%s", typeCalculator.Div(this.arg1, this.arg2)); } } diff --git a/src/main/resources/frontend/spa-vue/src/pages/Index.vue b/src/main/resources/frontend/spa-vue/src/pages/Index.vue index 15a0b69..081887e 100644 --- a/src/main/resources/frontend/spa-vue/src/pages/Index.vue +++ b/src/main/resources/frontend/spa-vue/src/pages/Index.vue @@ -36,11 +36,20 @@ export default{
Действие: - +
Тип данных: - +
Аргумент№1: