diff --git a/MainPage/LogForMainPage.js b/MainPage/LogForMainPage.js index d387d23..5f586a4 100644 --- a/MainPage/LogForMainPage.js +++ b/MainPage/LogForMainPage.js @@ -1,40 +1,52 @@ const operateButton = document.getElementById("operate"); +const operateStringButton = document.getElementById("operateString"); const val1 = document.getElementById("val1"); const val2 = document.getElementById("val2"); const select = document.getElementById("operation"); const finish = document.getElementById("finish"); +const operateFButton = document.getElementById("operateF"); +const operateFStringButton = document.getElementById("operateFString"); +const float1 = document.getElementById("float1"); +const float2 = document.getElementById("float2"); +const selectF = document.getElementById("operationF"); +const finishF = document.getElementById("finishF"); operateButton.onclick=function(){ - operate(); + operate("number"); }; -function operate(){ +operateStringButton.onclick=function(){ + operate("string"); +} + +operateFButton.onclick=function(){ + operate("numberF"); +}; + +function operate(param){ switch(parseInt(select.value)){ case 1: - doOperation("calc1") + doOperation("calc1", param) break; case 2: - doOperation("calc2") + doOperation("calc2", param) break; case 3: - doOperation("calc3") + doOperation("calc3", param) break; case 4: - doOperation("calc4") + doOperation("calc4", param) break; }; } + function checkNumber(res){ - if(res.indexOf(".") != -1){ - return parseInt(res); - } - else{ - return res; - } + return res; } -async function doOperation(address) { - const response = await fetch(`http://localhost:8080/${address}?val1=${val1.value}&val2=${val2.value}`) - let res = await response.text() - finish.innerHTML = checkNumber(res); +function doOperation(address, type){ + console.log("Тип переменных: " + type) + fetch(`http://localhost:8080/${address}?val1=${val1.value}&val2=${val2.value}&type=${type}`) + .then(response => response.text()) + .then(res=>finish.innerHTML=checkNumber(res)); } diff --git a/MainPage/MainPage.html b/MainPage/MainPage.html index b1209e5..921dbf6 100644 --- a/MainPage/MainPage.html +++ b/MainPage/MainPage.html @@ -14,29 +14,33 @@
Введите число - +
Выберите действие
Введите второе число - +
+
+ +
+
+ +
Результат:

-
- - + \ No newline at end of file diff --git a/src/main/java/com/example/demo/Calculator/Controller/CalculatorController.java b/src/main/java/com/example/demo/Calculator/Controller/CalculatorController.java new file mode 100644 index 0000000..7640eec --- /dev/null +++ b/src/main/java/com/example/demo/Calculator/Controller/CalculatorController.java @@ -0,0 +1,40 @@ +package com.example.demo.Calculator.Controller; + +import com.example.demo.Calculator.Service.CalculatorService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CalculatorController { + private final CalculatorService calculatorService; + + public CalculatorController(CalculatorService calculatorService){ + this.calculatorService=calculatorService; + } + + @GetMapping("/calc1") + public Object calc1(@RequestParam(value = "val1", defaultValue = "null") Object val1, + @RequestParam(value = "val2", defaultValue = "null") Object val2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calculatorService.calc1(val1, val2, type); + } + @GetMapping("/calc2") + public Object calc2(@RequestParam(value = "val1", defaultValue = "null") Object val1, + @RequestParam(value = "val2", defaultValue = "null") Object val2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calculatorService.calc2(val1, val2, type); + } + @GetMapping("/calc3") + public Object calc3(@RequestParam(value = "val1", defaultValue = "null") Object val1, + @RequestParam(value = "val2", defaultValue = "null") Object val2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calculatorService.calc3(val1, val2, type); + } + @GetMapping("/calc4") + public Object calc4(@RequestParam(value = "val1", defaultValue = "null") Object val1, + @RequestParam(value = "val2", defaultValue = "null") Object val2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calculatorService.calc4(val1, val2, type); + } +} diff --git a/src/main/java/com/example/demo/Calculator/Domain/CalculatorDoubleArray.java b/src/main/java/com/example/demo/Calculator/Domain/CalculatorDoubleArray.java new file mode 100644 index 0000000..e1007e1 --- /dev/null +++ b/src/main/java/com/example/demo/Calculator/Domain/CalculatorDoubleArray.java @@ -0,0 +1,57 @@ +package com.example.demo.Calculator.Domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.stream.Stream; + +@Component(value="numberF") +public class CalculatorDoubleArray implements ICalculator, InitializingBean, DisposableBean { + private final Logger log= LoggerFactory.getLogger(CalculatorDoubleArray.class); + + @Override + public void afterPropertiesSet(){ + log.info("calling CalculatorDoubleArray.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("calling CalculatorDoubleArray.destroy()"); + } + + @Override + public String[] calc1(String[] val1, String[] val2) { + String[] resArr = Stream.concat(Arrays.stream(val1), + Arrays.stream(val2)) + .toArray(String[]::new);; + return resArr; + } + + @Override + public String[] calc2(String[] val1, String[] val2) { + String[] resArr = Stream.concat(Arrays.stream(val1), + Arrays.stream(val2)) + .toArray(String[]::new);; + return resArr; + } + + @Override + public String[] calc3(String[] val1, String[] val2) { + String[] resArr = Stream.concat(Arrays.stream(val1), + Arrays.stream(val2)) + .toArray(String[]::new);; + return resArr; + } + + @Override + public String[] calc4(String[] val1, String[] val2) { + String[] resArr = Stream.concat(Arrays.stream(val1), + Arrays.stream(val2)) + .toArray(String[]::new);; + return resArr; + } +} diff --git a/src/main/java/com/example/demo/Calculator/Domain/CalculatorNumber.java b/src/main/java/com/example/demo/Calculator/Domain/CalculatorNumber.java new file mode 100644 index 0000000..fa274ee --- /dev/null +++ b/src/main/java/com/example/demo/Calculator/Domain/CalculatorNumber.java @@ -0,0 +1,42 @@ +package com.example.demo.Calculator.Domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +@Component(value="number") +public class CalculatorNumber implements ICalculator, InitializingBean, DisposableBean{ + private final Logger log=LoggerFactory.getLogger(CalculatorNumber.class); + + @Override + public void afterPropertiesSet(){ + log.info("calling CalculatorNumber.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("calling CalculatorNumber.destroy()"); + } + + @Override + public Integer calc1(Integer val1, Integer val2) { + return val1 * val2; + } + + @Override + public Integer calc2(Integer val1, Integer val2) { + return val1 + val2; + } + + @Override + public Integer calc3(Integer val1, Integer val2) { + return val1 - val2; + } + + @Override + public Integer calc4(Integer val1, Integer val2) { + return val1 / val2; + } +} diff --git a/src/main/java/com/example/demo/Calculator/Domain/CalculatorString.java b/src/main/java/com/example/demo/Calculator/Domain/CalculatorString.java new file mode 100644 index 0000000..6456511 --- /dev/null +++ b/src/main/java/com/example/demo/Calculator/Domain/CalculatorString.java @@ -0,0 +1,46 @@ +package com.example.demo.Calculator.Domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +@Component(value="string") +public class CalculatorString implements ICalculator, InitializingBean, DisposableBean { + private final Logger log= LoggerFactory.getLogger(CalculatorString.class); + + @Override + public void afterPropertiesSet() { + log.info("calling CalculatorString.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("CalculatorString.destroy()"); + } + + @Override + public String calc1(String val1, String val2) { + String temp = val1; + for (int i = 0; i < Integer.parseInt(val2) - 1; i++) { + temp = calc2(temp, val1); + } + return temp; + } + + @Override + public String calc2(String val1, String val2) { + return val1.concat(val2); + } + + @Override + public String calc3(String val1, String val2) { + return val1.replaceFirst(val2, ""); + } + + @Override + public String calc4(String val1, String val2) { + return val1.replaceAll(val2, ""); + } +} diff --git a/src/main/java/com/example/demo/Calculator/Domain/ICalculator.java b/src/main/java/com/example/demo/Calculator/Domain/ICalculator.java new file mode 100644 index 0000000..14cbfdd --- /dev/null +++ b/src/main/java/com/example/demo/Calculator/Domain/ICalculator.java @@ -0,0 +1,11 @@ +package com.example.demo.Calculator.Domain; + +public interface ICalculator { + T calc1(T val1, T val2); + + T calc2(T val1, T val2); + + T calc3(T val1, T val2); + + T calc4(T val1, T val2); +} diff --git a/src/main/java/com/example/demo/Calculator/Service/CalculatorService.java b/src/main/java/com/example/demo/Calculator/Service/CalculatorService.java new file mode 100644 index 0000000..3ca4c3a --- /dev/null +++ b/src/main/java/com/example/demo/Calculator/Service/CalculatorService.java @@ -0,0 +1,81 @@ +package com.example.demo.Calculator.Service; + +import com.example.demo.Calculator.Domain.ICalculator; +import com.example.demo.Calculator.Domain.CalculatorNumber; +import com.example.demo.Calculator.Domain.CalculatorDoubleArray; +import com.example.demo.Calculator.Domain.CalculatorString; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; +import java.io.*; +import java.util.*; +import java.util.Arrays; + + +@Service +public class CalculatorService { + private final ApplicationContext applicationContext; + + public CalculatorService(ApplicationContext applicationContext){ + this.applicationContext=applicationContext; + } + + public Object calc2(Object val1, Object val2, String type){ + final ICalculator temp=(ICalculator) applicationContext.getBean(type); + if (type.equals("string")) { + return temp.calc2(val1.toString(), val2.toString()); + } + else if (type.equals("number")) { + return temp.calc2(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.equals("numberF")) { + return temp.calc2(convToArr(val1) , convToArr(val2)); + } + else return null; + } + + public Object calc1(Object val1, Object val2, String type){ + final ICalculator temp=(ICalculator) applicationContext.getBean(type); + if (type.equals("string")) { + return temp.calc1(val1.toString(), val2.toString()); + } + else if (type.equals("number")) { + return temp.calc1(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.equals("numberF")) { + return temp.calc1(convToArr(val1) , convToArr(val2)); + } + else return null; + } + + public Object calc3(Object val1, Object val2, String type){ + final ICalculator temp=(ICalculator) applicationContext.getBean(type); + if (type.equals("string")) { + return temp.calc3(val1.toString(), val2.toString()); + } + else if (type.equals("number")) { + return temp.calc3(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.equals("numberF")) { + return temp.calc3(convToArr(val1) , convToArr(val2)); + } + else return null; + } + + public Object calc4(Object val1, Object val2, String type){ + final ICalculator temp=(ICalculator) applicationContext.getBean(type); + if (type.equals("string")) { + return temp.calc4(val1.toString(), val2.toString()); + } + else if (type.equals("number")) { + return temp.calc4(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.equals("numberF")) { + return temp.calc4(convToArr(val1) , convToArr(val2)); + } + else return null; + } + + public String[] convToArr(Object arr){ + return Arrays.stream(arr.toString().split("[\\(\\)\\,\\[\\] \\!\\?\\:\\;]")).filter(e -> e.trim().length()>0).toArray(String[]::new); + } +} diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 650c5a9..7a9c7af 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -15,7 +15,7 @@ public class DemoApplication { } - @GetMapping("/hello") + /*@GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); } @@ -60,5 +60,5 @@ public class DemoApplication { } else { return calc4(val1 - 1) + calc4(val1 - 2); } - } + }*/ } diff --git a/src/test/java/com/example/demo/DemoApplicationTests.java b/src/test/java/com/example/demo/DemoApplicationTests.java index 2778a6a..aae638e 100644 --- a/src/test/java/com/example/demo/DemoApplicationTests.java +++ b/src/test/java/com/example/demo/DemoApplicationTests.java @@ -1,13 +1,109 @@ package com.example.demo; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import com.example.demo.Calculator.Service.CalculatorService; @SpringBootTest class DemoApplicationTests { + @Autowired + CalculatorService calculatorService; @Test - void contextLoads() { + void testCalculatorNumberCalc2() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc2(val1, val2, "number"); + Assertions.assertEquals("110", result); } -} + @Test + void testCalculatorNumberCalc3() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc3(val1, val2, "number"); + Assertions.assertEquals("90", result); + } + + @Test + void testCalculatorNumberCalc1() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc1(val1, val2, "number"); + Assertions.assertEquals("1000", result); + } + + @Test + void testCalculatorNumberCalc4() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc4(val1, val2, "number"); + Assertions.assertEquals("10", result); + } + + @Test + void testCalculatorStringCalc2() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc2(val1.toString(), val2.toString(), "string"); + Assertions.assertEquals("10010", result); + } + + @Test + void testCalculatorStringCalc3() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc3(val1.toString(), val2.toString(), "string"); + Assertions.assertEquals("0", result); + } + + @Test + void testCalculatorStringCalc1() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc1(val1.toString(), val2.toString(), "string"); + Assertions.assertEquals("100100100100100100100100100100", result); + } + + @Test + void testCalculatorStringCalc4() { + Object val1 = 100; + Object val2 = 10; + final Object result = calculatorService.calc4(val1.toString(), val2.toString(), "string"); + Assertions.assertEquals("0", result); + } + + @Test + void testCalculatorFloatNumberCalc2() { + Object val1 = 100.10; + Object val2 = 10.10; + final Object result = calculatorService.calc2(val1, val2, "numberF"); + Assertions.assertEquals("110.20", result); + } + + @Test + void testCalculatorFloatNumberCalc3() { + Object val1 = 100.10; + Object val2 = 10.10; + final Object result = calculatorService.calc3(val1, val2, "numberF"); + Assertions.assertEquals("90", result); + } + + @Test + void testCalculatorFloatNumberCalc1() { + Object val1 = 100.10; + Object val2 = 10.10; + final Object result = calculatorService.calc1(val1, val2, "numberF"); + Assertions.assertEquals("1011.01", result); + } + + @Test + void testCalculatorFloatNumberCalc4() { + Object val1 = 10.5; + Object val2 = 2; + final Object result = calculatorService.calc4(val1, val2, "numberF"); + Assertions.assertEquals("5.25", result); + } +} \ No newline at end of file