big-ass changes

This commit is contained in:
maxnes3 2023-03-18 22:56:06 +04:00
parent f3f31c1a03
commit 309e915e7b
6 changed files with 107 additions and 59 deletions

View File

@ -2,41 +2,47 @@ package ru.ip.labworks.labworks.domain;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList;
@Component(value = "array") @Component(value = "array")
public class ArrayCalculator implements ITypeCalculator{ public class ArrayCalculator implements ITypeCalculator<ArrayList<String>> {
@Override @Override
public Object Plus(Object arg1, Object arg2) { public ArrayList<String> Plus(ArrayList<String> arg1, ArrayList<String> arg2) {
String[] result = arg1.toString().split(","); ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < result.length; ++i){ for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) + Integer.parseInt(arg2.toString())); result.add(arg1.get(i) + arg2.get(i));
} }
return String.join(",", result); return result;
} }
@Override @Override
public Object Minus(Object arg1, Object arg2) { public ArrayList<String> Minus(ArrayList<String> arg1, ArrayList<String> arg2) {
String[] result = arg1.toString().split(","); ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < result.length; ++i){ for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) - Integer.parseInt(arg2.toString())); result.add(arg1.get(i).replace(arg2.get(i), ""));
} }
return String.join(",", result); return result;
} }
@Override @Override
public Object Multi(Object arg1, Object arg2) { public ArrayList<String> Multi(ArrayList<String> arg1, ArrayList<String> arg2) {
String[] result = arg1.toString().split(","); ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < result.length; ++i){ for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) * Integer.parseInt(arg2.toString())); String temp = "";
for (int j = 0; j < Integer.parseInt(arg2.get(i)); ++j){
temp += arg1.get(i);
} }
return String.join(",", result); result.add(temp);
}
return result;
} }
@Override @Override
public Object Div(Object arg1, Object arg2) { public ArrayList<String> Div(ArrayList<String> arg1, ArrayList<String> arg2) {
String[] result = arg1.toString().split(","); ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < result.length; ++i){ for (int i = 0; i < Math.min(arg1.size(), arg2.size()); ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) / Integer.parseInt(arg2.toString())); result.add(arg1.get(i).substring(0, Integer.parseInt(arg2.get(i))));
} }
return String.join(",", result); return result;
} }
} }

View File

@ -1,9 +1,9 @@
package ru.ip.labworks.labworks.domain; package ru.ip.labworks.labworks.domain;
public interface ITypeCalculator { public interface ITypeCalculator<T> {
Object Plus(Object arg1, Object arg2); T Plus(T arg1, T arg2);
Object Minus(Object arg1, Object arg2); T Minus(T arg1, T arg2);
Object Multi(Object arg1, Object arg2); T Multi(T arg1, T arg2);
Object Div(Object arg1, Object arg2); T Div(T arg1, T arg2);
} }

View File

@ -3,28 +3,24 @@ package ru.ip.labworks.labworks.domain;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component(value = "int") @Component(value = "int")
public class IntCalculator implements ITypeCalculator{ public class IntCalculator implements ITypeCalculator<Integer>{
@Override @Override
public Object Plus(Object arg1, Object arg2) { public Integer Plus(Integer arg1, Integer arg2) {
int result = Integer.parseInt(arg1.toString()) + Integer.parseInt(arg2.toString()); return arg1 + arg2;
return Integer.toString(result);
} }
@Override @Override
public Object Minus(Object arg1, Object arg2) { public Integer Minus(Integer arg1, Integer arg2) {
int result = Integer.parseInt(arg1.toString()) - Integer.parseInt(arg2.toString()); return arg1 - arg2;
return Integer.toString(result);
} }
@Override @Override
public Object Multi(Object arg1, Object arg2) { public Integer Multi(Integer arg1, Integer arg2) {
int result = Integer.parseInt(arg1.toString()) * Integer.parseInt(arg2.toString()); return arg1 * arg2;
return Integer.toString(result);
} }
@Override @Override
public Object Div(Object arg1, Object arg2) { public Integer Div(Integer arg1, Integer arg2) {
int result = Integer.parseInt(arg1.toString()) / Integer.parseInt(arg2.toString()); return arg1 / arg2;
return Integer.toString(result);
} }
} }

View File

@ -3,29 +3,29 @@ package ru.ip.labworks.labworks.domain;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component(value = "string") @Component(value = "string")
public class StringCalculator implements ITypeCalculator{ public class StringCalculator implements ITypeCalculator<String>{
@Override @Override
public Object Plus(Object arg1, Object arg2) { public String Plus(String arg1, String arg2) {
return arg1.toString().concat(arg2.toString()); return arg1 + arg2;
} }
@Override @Override
public Object Minus(Object arg1, Object arg2) { public String Minus(String arg1, String arg2) {
return arg1.toString().replaceAll(arg2.toString(), ""); return arg1.replaceAll(arg2, "");
} }
@Override @Override
public Object Multi(Object arg1, Object arg2) { public String Multi(String arg1, String arg2) {
String result = ""; String result = "";
for (int i = 0; i < Integer.parseInt(arg2.toString()); ++i){ for (int i = 0; i < Integer.parseInt(arg2); ++i){
result += arg1.toString(); result += arg1;
} }
return result; return result;
} }
@Override @Override
public Object Div(Object arg1, Object arg2) { public String Div(String arg1, String arg2) {
String result = arg1.toString().substring(0, arg1.toString().length() / Integer.parseInt(arg2.toString())); String result = arg1.substring(0, arg1.length() / Integer.parseInt(arg2));
return result; return result;
} }
} }

View File

@ -4,31 +4,68 @@ import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ip.labworks.labworks.domain.ITypeCalculator; import ru.ip.labworks.labworks.domain.ITypeCalculator;
import java.util.ArrayList;
import java.util.Arrays;
@Service @Service
public class CalculatorService { public class CalculatorService {
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
private ITypeCalculator typeCalculator;
private Object arg1;
private Object arg2;
public CalculatorService(ApplicationContext applicationContext){ public CalculatorService(ApplicationContext applicationContext){
this.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){ public Object Plus(String type, Object arg1, Object arg2){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); ValidateParams(arg1,arg2,type);
return calculator.Plus(arg1, 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){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); ValidateParams(arg1,arg2,type);
return calculator.Minus(arg1, 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){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); ValidateParams(arg1,arg2,type);
return calculator.Multi(arg1, 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){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type); ValidateParams(arg1,arg2,type);
return calculator.Div(arg1, arg2); return String.format("%s", typeCalculator.Div(this.arg1, this.arg2));
} }
} }

View File

@ -36,11 +36,20 @@ export default{
<main class="container" style="margin-top: 50pt;"> <main class="container" style="margin-top: 50pt;">
<div class="input-group mb-2 my-5"> <div class="input-group mb-2 my-5">
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Действие:</strong></span> <span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Действие:</strong></span>
<input type="text" class="form-control" placeholder="Выберите действие" v-model="action"> <select class="form-select" aria-label="Default select example" v-model="action">
<option value="+">Plus(+)</option>
<option value="-">Minus(-)</option>
<option value="*">Multi(*)</option>
<option value=":">Div(\)</option>
</select>
</div> </div>
<div class="input-group mb-2"> <div class="input-group mb-2">
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Тип данных:</strong></span> <span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Тип данных:</strong></span>
<input type="text" class="form-control" placeholder="Введите тип данных" v-model="type"> <select class="form-select" aria-label="Default select example" v-model="type">
<option value="int">Integer{}</option>
<option value="string">String"_"</option>
<option value="array">Array[]</option>
</select>
</div> </div>
<div class="input-group mb-2"> <div class="input-group mb-2">
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Аргумент1:</strong></span> <span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Аргумент1:</strong></span>