diff --git a/frontend/index.html b/frontend/index.html index 6b89df2..e40b773 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -13,7 +13,7 @@
- +
@@ -23,7 +23,12 @@ - + +
diff --git a/frontend/js/script.js b/frontend/js/script.js index 633182d..3424c3e 100644 --- a/frontend/js/script.js +++ b/frontend/js/script.js @@ -9,31 +9,51 @@ form.onsubmit = async (e) => { if(form.num2.value === "") return; let index = form.selected.selectedIndex; + let indexType = form.type.selectedIndex; let op = form.selected.options[index].textContent; + let type = form.type.options[indexType].textContent; let res = ""; - switch(op) { - case "+": - res = await fetch(`http://localhost:8080/sum?num1=${form.num1.value}&num2=${form.num2.value}`) - res = await res.text(); - break; - case "-": - res = await fetch(`http://localhost:8080/diff?num1=${form.num1.value}&num2=${form.num2.value}`) - res = await res.text(); - break; + function calc() { + switch(op) { + case "+": + return "sum"; + break; - case "*": - res = await fetch(`http://localhost:8080/multiply?num1=${form.num1.value}&num2=${form.num2.value}`) - res = await res.text(); - break; + case "-": + return "diff"; + break; - case "/": - if(form.num2.value == 0) return; - res = await fetch(`http://localhost:8080/divide?num1=${form.num1.value}&num2=${form.num2.value}`) - res = await res.text(); - break; + case "*": + return "mul"; + break; + + case "/": + return "div"; + break; + } } + function typeOfCalc() { + switch(type) { + case "Int": + return "int"; + break; + + case "String": + return "string"; + break; + + case "FloatArray": + return "floatArray"; + break; + } + } + + if (form.num2.value == 0 && calc(op) == "divide") return; + res = await fetch(`http://localhost:8080/${calc()}?num1=${form.num1.value}&num2=${form.num2.value}&operationType=${typeOfCalc()}`) + res = await res.text(); + info.textContent = res; } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java b/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java index 236ecb0..90ba03e 100644 --- a/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java +++ b/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java @@ -13,41 +13,4 @@ public class SbappApplication { public static void main(String[] args) { SpringApplication.run(SbappApplication.class, args); } - - @GetMapping("/sum") - public Integer sum(@RequestParam Integer num1, - @RequestParam Integer num2) { - return num1 + num2; - } - - @GetMapping("/diff") - public Integer diff(@RequestParam Integer num1, - @RequestParam Integer num2) { - return num1 - num2; - } - - @GetMapping("/multiply") - public Integer multiply(@RequestParam Integer num1, - @RequestParam Integer num2) { - return num1 * num2; - } - - @GetMapping("/divide") - public Integer divide(@RequestParam Integer num1, - @RequestParam Integer num2) { - return num1 / num2; - } - - @GetMapping("/divider") - public String IsInt(@RequestParam int num1, - @RequestParam(defaultValue = "1") int num2) { - if (num1 % num2 == 0) - { - return String.format("Делится на цело, результат: %s", num1 / num2); - } - else - { - return String.format("Неделится на цело, результат: %s", num1 / num2 + " Остаток: " + num1 % num2); - } - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/configuration/TypeConfiguration.java b/src/main/java/ru/ulstu/is/sbapp/calc/configuration/TypeConfiguration.java new file mode 100644 index 0000000..dbda42b --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/configuration/TypeConfiguration.java @@ -0,0 +1,21 @@ +package ru.ulstu.is.sbapp.calc.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import ru.ulstu.is.sbapp.calc.domain.FloatArrayCalculator; +import ru.ulstu.is.sbapp.calc.domain.IntCalculator; +import ru.ulstu.is.sbapp.calc.domain.StringCalculator; + +@Configuration +public class TypeConfiguration { + @Bean(value = "int") + public IntCalculator createIntType() { + return new IntCalculator(); + } + + @Bean(value = "string") + public StringCalculator createStringCalculator() { return new StringCalculator(); } + + @Bean(value = "floatArray") + public FloatArrayCalculator createfloatArrayCalculator() { return new FloatArrayCalculator(); } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/controller/CalculatorController.java b/src/main/java/ru/ulstu/is/sbapp/calc/controller/CalculatorController.java new file mode 100644 index 0000000..05125ef --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/controller/CalculatorController.java @@ -0,0 +1,33 @@ +package ru.ulstu.is.sbapp.calc.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.is.sbapp.calc.service.CalculatorService; + +@RestController +public class CalculatorController { + private final CalculatorService calculatorService; + + public CalculatorController(CalculatorService calculatorService) { this.calculatorService = calculatorService; } + + @GetMapping("/sum") + public Object sum(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) { + return calculatorService.getSum(num1, num2, operationType); + } + + @GetMapping("/diff") + public Object diff(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) { + return calculatorService.getDiff(num1, num2, operationType); + } + + @GetMapping("/mul") + public Object mul(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) { + return calculatorService.getMul(num1, num2, operationType); + } + + @GetMapping("/div") + public Object div(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) { + return calculatorService.getDiv(num1, num2, operationType); + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/domain/Calculator.java b/src/main/java/ru/ulstu/is/sbapp/calc/domain/Calculator.java new file mode 100644 index 0000000..5b32beb --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/domain/Calculator.java @@ -0,0 +1,8 @@ +package ru.ulstu.is.sbapp.calc.domain; + +public interface Calculator { + public T sum(T num1, int num2); + public T diff(T num1, int num2); + public T multiply(T num1, int num2); + public T divide(T num1, int num2); +} diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/domain/FloatArrayCalculator.java b/src/main/java/ru/ulstu/is/sbapp/calc/domain/FloatArrayCalculator.java new file mode 100644 index 0000000..f9c96a8 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/domain/FloatArrayCalculator.java @@ -0,0 +1,44 @@ +package ru.ulstu.is.sbapp.calc.domain; + +import org.springframework.stereotype.Component; + +import java.util.ArrayList; + +public class FloatArrayCalculator implements Calculator>{ + + @Override + public ArrayList sum(ArrayList num1, int num2) { + ArrayList res = new ArrayList<>(); + for (int i = 0; i < num1.size(); ++i) { + res.add(num1.get(i) + Float.parseFloat(Integer.toString(num2))); + } + return res; + } + + @Override + public ArrayList diff(ArrayList num1, int num2) { + ArrayList res = new ArrayList<>(); + for (int i = 0; i < num1.size(); ++i) { + res.add(num1.get(i) - Float.parseFloat(Integer.toString(num2))); + } + return res; + } + + @Override + public ArrayList multiply(ArrayList num1, int num2) { + ArrayList res = new ArrayList<>(); + for (int i = 0; i < num1.size(); ++i) { + res.add(num1.get(i) * Float.parseFloat(Integer.toString(num2))); + } + return res; + } + + @Override + public ArrayList divide(ArrayList num1, int num2) { + ArrayList res = new ArrayList<>(); + for (int i = 0; i < num1.size(); ++i) { + res.add(num1.get(i) / Float.parseFloat(Integer.toString(num2))); + } + return res; + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/domain/IntCalculator.java b/src/main/java/ru/ulstu/is/sbapp/calc/domain/IntCalculator.java new file mode 100644 index 0000000..eaddf84 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/domain/IntCalculator.java @@ -0,0 +1,27 @@ +package ru.ulstu.is.sbapp.calc.domain; + +import org.springframework.stereotype.Component; + + +public class IntCalculator implements Calculator{ + + @Override + public Integer sum(Integer num1, int num2) { + return num1 + num2; + } + + @Override + public Integer diff(Integer num1, int num2) { + return num1 - num2; + } + + @Override + public Integer multiply(Integer num1, int num2) { + return num1 * num2; + } + + @Override + public Integer divide(Integer num1, int num2) { + return num1 / num2; + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/domain/StringCalculator.java b/src/main/java/ru/ulstu/is/sbapp/calc/domain/StringCalculator.java new file mode 100644 index 0000000..90f1ea1 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/domain/StringCalculator.java @@ -0,0 +1,44 @@ +package ru.ulstu.is.sbapp.calc.domain; + +import org.springframework.stereotype.Component; + +public class StringCalculator implements Calculator{ + @Override + public String sum(String num1, int num2) { + return num1.concat(String.format(String.valueOf(num2))); + } + + @Override + public String diff(String num1, int num2) { + String res = ""; + if (num1.length() <= num2) { + return "Длинна первого аргумента меньше значения второго."; + } + for (int i = 0; i < num1.length() - num2; ++i) { + res += num1.charAt(i); + } + return res; + } + + @Override + public String multiply(String num1, int num2) { + String res = ""; + for (int i = 0; i < num2; ++i) { + res += num1; + } + return res; + } + + @Override + public String divide(String num1, int num2) { + String res = ""; + int len = num1.length() / num2; + if (len == 0) { + return "Длинна первого аргумента слишком маленькая относительно значения второго."; + } + for (int i = 0; i < len; ++i) { + res += num1.charAt(i); + } + return res; + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/calc/service/CalculatorService.java b/src/main/java/ru/ulstu/is/sbapp/calc/service/CalculatorService.java new file mode 100644 index 0000000..b589691 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/calc/service/CalculatorService.java @@ -0,0 +1,69 @@ +package ru.ulstu.is.sbapp.calc.service; + +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; +import ru.ulstu.is.sbapp.calc.domain.Calculator; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.stream.Collectors; + +@Service +public class CalculatorService { + private final ApplicationContext applicationContext; + + public CalculatorService(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } + + private Calculator _calculator; + Object arg1 = ""; + + private void Num1Type(Object num1, int num2, String operationType) { + _calculator = (Calculator) applicationContext.getBean(operationType); + switch (operationType) { + case "int" -> { + try { + arg1 = Integer.valueOf(num1.toString()); + } + catch (Exception ex) { + arg1 = 0; + } + } + + case "floatArray" -> { + try { + arg1 = Arrays.stream(num1.toString().split(";")) + .map(Float::parseFloat) + .collect(Collectors.toList()); + + } + catch (Exception ex) { + arg1 = num1.toString(); + } + } + + case "string" -> { + arg1 = num1; + } + } + } + + public Object getSum(Object num1, int num2, String operationType) { + Num1Type(num1, num2, operationType); + return _calculator.sum(arg1, num2); + } + + public Object getDiff(Object num1, int num2, String operationType) { + Num1Type(num1, num2, operationType); + return _calculator.diff(arg1, num2); + } + + public Object getMul(Object num1, int num2, String operationType) { + Num1Type(num1, num2, operationType); + return _calculator.multiply(arg1, num2); + } + + public Object getDiv(Object num1, int num2, String operationType) { + Num1Type(num1, num2, operationType); + return _calculator.divide(arg1, num2); + } +} diff --git a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java index bcde5ac..7862fee 100644 --- a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java +++ b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java @@ -1,13 +1,91 @@ package ru.ulstu.is.sbapp; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import ru.ulstu.is.sbapp.calc.service.CalculatorService; @SpringBootTest class SbappApplicationTests { + @Autowired + CalculatorService calculatorService; @Test - void contextLoads() { + void IntSum() { + final String res = calculatorService.getSum(3, 6, "int").toString(); + Assertions.assertEquals("9", res); } + @Test + void IntDiff() { + final String res = calculatorService.getDiff(15, 3, "int").toString(); + Assertions.assertEquals("12", res); + } + + @Test + void IntMul() { + final String res = calculatorService.getMul(12, 3, "int").toString(); + Assertions.assertEquals("36", res); + } + + @Test + void IntDiv() { + final String res = calculatorService.getDiv(12, 3, "int").toString(); + Assertions.assertEquals("4", res); + } + + @Test + void StringSum() { + final String res = calculatorService.getSum("abc", 6, "string").toString(); + Assertions.assertEquals("abc6", res); + } + + @Test + void StringDiff() { + final String res = calculatorService.getDiff("abc", 2, "string").toString(); + Assertions.assertEquals("a", res); + } + + @Test + void StringMul() { + final String res = calculatorService.getMul("abc", 3, "string").toString(); + Assertions.assertEquals("abcabcabc", res); + } + + @Test + void StringDiv() { + final String res = calculatorService.getDiv("abc", 3, "string").toString(); + Assertions.assertEquals("a", res); + } + + @Test + void FloatArraySum() { + final String res = calculatorService.getSum("3.2;5.6", 3, "floatArray").toString(); + Assertions.assertEquals("[6.2, 8.6]", res); + } + + @Test + void FloatArrayDiff() { + final String res = calculatorService.getDiff("3.2;5.6", 2, "floatArray").toString(); + Assertions.assertEquals("[1.2, 3.6]", res); + } + + @Test + void FloatArrayMul() { + final String res = calculatorService.getMul("3.2;5.6", 2, "floatArray").toString(); + Assertions.assertEquals("[6.4, 11.2]", res); + } + + @Test + void FloatArrayDiv() { + final String res = calculatorService.getDiv("3.2;5.6", 2, "floatArray").toString(); + Assertions.assertEquals("[1.6, 2.8]", res); + } + + @Test + void testErrorErrorWired() { + Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calculatorService.getSum(1, 2, "date")); + } }