From 3b4ef07c6b575c6269b4a1027b3a8a6857fbd533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sat, 4 Mar 2023 22:02:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/index.html | 14 ++++- site/script.js | 16 ++++- .../example/demo/SpringAppApplication.java | 46 --------------- .../PerformerConfiguration.java} | 8 ++- .../controller/PerformerController.java | 57 ++++++++++++++++++ .../demo/performer/domain/IPerformer.java | 8 +++ .../performer/domain/PerformerInteger.java | 43 ++++++++++++++ .../performer/domain/PerformerString.java | 44 ++++++++++++++ .../performer/service/PerformerService.java | 58 +++++++++++++++++++ 9 files changed, 240 insertions(+), 54 deletions(-) rename src/main/java/com/example/demo/{WebConfiguration.java => performer/configuration/PerformerConfiguration.java} (57%) create mode 100644 src/main/java/com/example/demo/performer/controller/PerformerController.java create mode 100644 src/main/java/com/example/demo/performer/domain/IPerformer.java create mode 100644 src/main/java/com/example/demo/performer/domain/PerformerInteger.java create mode 100644 src/main/java/com/example/demo/performer/domain/PerformerString.java create mode 100644 src/main/java/com/example/demo/performer/service/PerformerService.java diff --git a/site/index.html b/site/index.html index f0edcaa..d29093a 100644 --- a/site/index.html +++ b/site/index.html @@ -42,7 +42,7 @@
- +
@@ -55,11 +55,19 @@
- +
+
- + +
+
+
+
diff --git a/site/script.js b/site/script.js index 1d32287..f80d321 100644 --- a/site/script.js +++ b/site/script.js @@ -2,6 +2,7 @@ let calculateButton = document.getElementById("calculate-button"); let helloButton = document.getElementById("hello-button"); let doButton = document.getElementById("do-button"); let yournamee = document.getElementById("yourname"); +let typedata = document.getElementById("typedata"); let numberOneInput = document.getElementById("number-1"); let numberTwoInput = document.getElementById("number-2"); let select = document.getElementById("operation"); @@ -43,6 +44,15 @@ function hello() { } function calculate() { + let type; + switch (parseInt(typedata.value)) { + case 1: + type = "double"; + break; + case 2: + type = "str"; + break; + } let address; switch (parseInt(select.value)) { case 1: @@ -58,13 +68,13 @@ function calculate() { address = "div"; break; } - executeRequest(address); + executeRequest(type, address); } -function executeRequest(address) { +function executeRequest(type, address) { let num_1 = numberOneInput.value; let num_2 = numberTwoInput.value; - fetch(`http://localhost:8080/${address}?num1=${num_1}&num2=${num_2}`) + fetch(`http://localhost:8080/${address}?num1=${num_1}&num2=${num_2}&type=${type}`) .then(response => response.text()) .then(res => result.textContent ="Ответ: " + res); } \ No newline at end of file diff --git a/src/main/java/com/example/demo/SpringAppApplication.java b/src/main/java/com/example/demo/SpringAppApplication.java index 2a9ff97..3d01fa5 100644 --- a/src/main/java/com/example/demo/SpringAppApplication.java +++ b/src/main/java/com/example/demo/SpringAppApplication.java @@ -2,56 +2,10 @@ package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -@RestController @SpringBootApplication public class SpringAppApplication { - public static void main(String[] args) { SpringApplication.run(SpringAppApplication.class, args); } - - @GetMapping("/") - public String root(@RequestParam(value = "name", defaultValue = "друг") String name) { - return "Привет, " + name+ "!"; - } - - @GetMapping("/sum") - public int sum(@RequestParam(value = "num1") int num1, - @RequestParam(value = "num2") int num2) { - return num1 + num2; - } - - @GetMapping("/dif") - public int dif(@RequestParam(value = "num1") int num1, - @RequestParam(value = "num2") int num2) { - return num1 - num2; - } - - @GetMapping("/mul") - public int mul(@RequestParam(value = "num1") int num1, - @RequestParam(value = "num2") int num2) { - return num1 * num2; - } - - @GetMapping("/div") - public double div(@RequestParam(value = "num1") double num1, - @RequestParam(value = "num2") double num2) { - if (num2 == 0) { - return 0; - } - return num1 / num2; - } - - @GetMapping("/array") - public int[] array(@RequestParam(value = "array") int[] array, - @RequestParam(value = "num") int num) { - for(int i=0;i { + T Sum(T num1, T num2); + T Dif(T num1, T num2); + T Mul(T num1, T num2); + String Div(T num1, T num2); +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/performer/domain/PerformerInteger.java b/src/main/java/com/example/demo/performer/domain/PerformerInteger.java new file mode 100644 index 0000000..44711e0 --- /dev/null +++ b/src/main/java/com/example/demo/performer/domain/PerformerInteger.java @@ -0,0 +1,43 @@ +package com.example.demo.performer.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 = "double") +public class PerformerInteger implements IPerformer, InitializingBean, DisposableBean { + private final Logger log = LoggerFactory.getLogger(PerformerInteger.class); + + @Override + public Double Sum(Double num1, Double num2) { + return num1 + num2; + } + + @Override + public Double Dif(Double num1, Double num2) { + return num1 - num2; + } + + @Override + public Double Mul(Double num1, Double num2) { + return num1 * num2; + } + + @Override + public String Div(Double num1, Double num2) { + if (num2==0) return "нельзя делить на ноль!"; + else return Double.toString(num1/num2); + } + + @Override + public void afterPropertiesSet() { + log.info("PerformerInteger.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("PerformerInteger.destroy()"); + } +} diff --git a/src/main/java/com/example/demo/performer/domain/PerformerString.java b/src/main/java/com/example/demo/performer/domain/PerformerString.java new file mode 100644 index 0000000..5f63f0f --- /dev/null +++ b/src/main/java/com/example/demo/performer/domain/PerformerString.java @@ -0,0 +1,44 @@ +package com.example.demo.performer.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 = "str") +public class PerformerString implements IPerformer, InitializingBean, DisposableBean { + private final Logger log = LoggerFactory.getLogger(PerformerInteger.class); + + @Override + public String Sum(String num1, String num2) { + return num1 + num2; + } + + @Override + public String Dif(String num1, String num2) { + return num1.replace(num2, ""); + } + + @Override + public String Mul(String num1, String num2) { + Integer number2 = Integer.parseInt(num2); + return num1.repeat(number2); + } + + @Override + public String Div(String num1, String num2) { + if(num1.contains(num2)) return "содержит"; + else return "не содержит"; + } + + @Override + public void afterPropertiesSet() { + log.info("PerformerString.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("PerformerString.destroy()"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/performer/service/PerformerService.java b/src/main/java/com/example/demo/performer/service/PerformerService.java new file mode 100644 index 0000000..17bb5cc --- /dev/null +++ b/src/main/java/com/example/demo/performer/service/PerformerService.java @@ -0,0 +1,58 @@ +package com.example.demo.performer.service; + +import com.example.demo.performer.domain.IPerformer; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +@Service +public class PerformerService { + private final ApplicationContext applicationContext; + + public PerformerService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public String Sum(Object num1, Object num2, String type) { + final IPerformer performer = (IPerformer) applicationContext.getBean(type); + if (type.equals("double")) { + Integer number1 = Integer.parseInt(String.valueOf(num1)); + Integer number2 = Integer.parseInt(String.valueOf(num2)); + return String.format("%s", performer.Sum(number1, number2)); + } else { + return String.format("%s", performer.Sum(num1, num2)); + } + } + + public String Dif(Object num1, Object num2, String type) { + final IPerformer performer = (IPerformer) applicationContext.getBean(type); + if (type.equals("double")) { + Integer number1 = Integer.parseInt(String.valueOf(num1)); + Integer number2 = Integer.parseInt(String.valueOf(num2)); + return String.format("%s", performer.Dif(number1, number2)); + } else { + return String.format("%s", performer.Dif(num1, num2)); + } + } + + public String Mul(Object num1, Object num2, String type) { + final IPerformer performer = (IPerformer) applicationContext.getBean(type); + if (type.equals("double")) { + Integer number1 = Integer.parseInt(String.valueOf(num1)); + Integer number2 = Integer.parseInt(String.valueOf(num2)); + return String.format("%s", performer.Mul(number1, number2)); + } else { + return String.format("%s", performer.Mul(num1, num2)); + } + } + + public String Div(Object num1, Object num2, String type) { + final IPerformer performer = (IPerformer) applicationContext.getBean(type); + if (type.equals("double")) { + Double number1 = Double.parseDouble(String.valueOf(num1)); + Double number2 = Double.parseDouble(String.valueOf(num2)); + return String.format("%s", performer.Div(number1, number2)); + } else { + return String.format("%s", performer.Div(num1, num2)); + } + } +}