From 14c6e4dc12ce56e3b59c28e0e0aa525d0dfe20a3 Mon Sep 17 00:00:00 2001 From: Arklightning Date: Mon, 27 Feb 2023 18:12:01 +0400 Subject: [PATCH] Commit 5 --- front/index.html | 9 +++- front/script.js | 13 +++-- .../com/example/demo/DemoApplication.java | 30 ----------- .../speaker/controller/MethodController.java | 43 ++++++++++++++++ .../example/demo/speaker/domain/IMethod.java | 11 ++++ .../demo/speaker/domain/MethodInt.java | 27 ++++++++++ .../demo/speaker/domain/MethodString.java | 44 ++++++++++++++++ .../demo/speaker/service/MethodService.java | 51 +++++++++++++++++++ 8 files changed, 193 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/example/demo/speaker/controller/MethodController.java create mode 100644 src/main/java/com/example/demo/speaker/domain/IMethod.java create mode 100644 src/main/java/com/example/demo/speaker/domain/MethodInt.java create mode 100644 src/main/java/com/example/demo/speaker/domain/MethodString.java create mode 100644 src/main/java/com/example/demo/speaker/service/MethodService.java diff --git a/front/index.html b/front/index.html index 09ff26e..3e02e3a 100644 --- a/front/index.html +++ b/front/index.html @@ -24,8 +24,15 @@ + Выберите тип данных +
+ +
Результат - + diff --git a/front/script.js b/front/script.js index 6f2169d..e55baae 100644 --- a/front/script.js +++ b/front/script.js @@ -6,32 +6,37 @@ let buttonDiv = document.getElementById("buttonDiv"); let numberOneInput = document.getElementById("first"); let numberTwoInput = document.getElementById("second"); let resultInput = document.getElementById("res"); +let typeInput = document.getElementById("type"); buttonPlus.onclick = function(event) { let num_1 = numberOneInput.value; let num_2 = numberTwoInput.value; - fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}`) + let type = typeInput.value; + fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}&type=${type}`) .then(response => response.text()) .then(res => resultInput.value = res); } buttonMinus.onclick = function(event) { let num_1 = numberOneInput.value; let num_2 = numberTwoInput.value; - fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}`) + let type = typeInput.value; + fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}&type=${type}`) .then(response => response.text()) .then(res => resultInput.value = res); } buttonMulti.onclick = function(event) { let num_1 = numberOneInput.value; let num_2 = numberTwoInput.value; - fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}`) + let type = typeInput.value; + fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}&type=${type}`) .then(response => response.text()) .then(response => resultInput.value = response); } buttonDiv.onclick = function(event) { let num_1 = numberOneInput.value; let num_2 = numberTwoInput.value; - fetch(`http://localhost:8080/div?first=${num_1}&second=${num_2}`) + let type = typeInput.value; + fetch(`http://localhost:8080/div?first=${num_1}&second=${num_2}&type=${type}`) .then(response => response.text()) .then(res => resultInput.value = res); } \ No newline at end of file diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index caaa6e5..217cd73 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -12,35 +12,5 @@ public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } - @GetMapping("/hello") - public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s!", name); - } - @GetMapping("/sum") - public String Sum(@RequestParam(defaultValue = "0") double first, - @RequestParam(defaultValue = "0") double second) { - return Double.toString(first + second); - } - - @GetMapping("/minus") - public String Ras(@RequestParam(defaultValue = "0") double first, - @RequestParam(defaultValue = "0") double second) { - return Double.toString(first - second); - } - - @GetMapping("/multi") - public String Pros(@RequestParam(defaultValue = "2") double first, - @RequestParam(defaultValue = "2") double second) { - return Double.toString(first * second); - } - - @GetMapping("/div") - public String Del(@RequestParam(defaultValue = "1") double first, - @RequestParam(defaultValue = "1") double second) { - if (second == 0) { - return null; - } - return Double.toString(first / second); - } } diff --git a/src/main/java/com/example/demo/speaker/controller/MethodController.java b/src/main/java/com/example/demo/speaker/controller/MethodController.java new file mode 100644 index 0000000..647da48 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/controller/MethodController.java @@ -0,0 +1,43 @@ +package com.example.demo.speaker.controller; + +import com.example.demo.speaker.service.MethodService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MethodController { + private final MethodService speakerService; + + public MethodController(MethodService speakerService) { + this.speakerService = speakerService; + } + + @GetMapping("/sum") + public String Sum(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Sum(first,second,type); + } + + @GetMapping("/minus") + public String Ras(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Ras(first,second,type); + } + + @GetMapping("/multi") + public String Pros(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Pros(first,second,type); + } + + @GetMapping("/div") + public String Del(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Del(first,second,type); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/speaker/domain/IMethod.java b/src/main/java/com/example/demo/speaker/domain/IMethod.java new file mode 100644 index 0000000..310c34a --- /dev/null +++ b/src/main/java/com/example/demo/speaker/domain/IMethod.java @@ -0,0 +1,11 @@ +package com.example.demo.speaker.domain; + +public interface IMethod { + T Sum(T first, T second); + + T Multiply(T first, Integer second); + + T Minus(T first, Integer second); + + T Div(T first, T second); +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/speaker/domain/MethodInt.java b/src/main/java/com/example/demo/speaker/domain/MethodInt.java new file mode 100644 index 0000000..5c5f9be --- /dev/null +++ b/src/main/java/com/example/demo/speaker/domain/MethodInt.java @@ -0,0 +1,27 @@ +package com.example.demo.speaker.domain; + +import org.springframework.stereotype.Component; + +@Component(value="int") +public class MethodInt implements IMethod{ + public Integer Sum(Integer first, Integer second) { + return Integer.parseInt(first.toString()) + Integer.parseInt(second.toString()); + } + + public Integer Multiply(Integer first, Integer second) { + return Integer.parseInt(first.toString()) * Integer.parseInt(second.toString()); + } + + public Integer Minus(Integer first, Integer second) { + return Integer.parseInt(first.toString()) - Integer.parseInt(second.toString()); + } + + public Integer Div(Integer first, Integer second) { + int num = Integer.parseInt(second.toString()); + if (num == 0){ + return null; + }else{ + return Integer.parseInt(first.toString()) / num; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/speaker/domain/MethodString.java b/src/main/java/com/example/demo/speaker/domain/MethodString.java new file mode 100644 index 0000000..fb4cb55 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/domain/MethodString.java @@ -0,0 +1,44 @@ +package com.example.demo.speaker.domain; + +import org.springframework.stereotype.Component; + +@Component(value="string") +public class MethodString implements IMethod{ + @Override + public String Sum(String first, String second) { + return first.concat(second); + } + + @Override + public String Multiply(String first, Integer second) { + if (second != 0){ + String temp = ""; + for (int i = 0; i < second; i++){ + temp = temp.concat(first); + } + return temp; + } + else{ + return first; + } + } + + @Override + public String Minus(String first, Integer second) { + String temp = first; + if(temp.length() >= second){ + return temp.substring(0, first.length() - second); + }else{ + return first; + } + } + + @Override + public String Div(String first, String second) { + if (first.contains(second)){ + return "true"; + }else{ + return "false"; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/speaker/service/MethodService.java b/src/main/java/com/example/demo/speaker/service/MethodService.java new file mode 100644 index 0000000..3704e1e --- /dev/null +++ b/src/main/java/com/example/demo/speaker/service/MethodService.java @@ -0,0 +1,51 @@ +package com.example.demo.speaker.service; + +import com.example.demo.speaker.domain.IMethod; +import com.example.demo.speaker.domain.MethodString; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +@Service +public class MethodService { + private final ApplicationContext applicationContext; + + public MethodService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public String Sum(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Sum(first,second)); + }else{ + return String.format("%s", speaker.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Ras(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Minus(first,Integer.parseInt(second.toString()))); + }else{ + return String.format("%s", speaker.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Pros(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Multiply(first,Integer.parseInt(second.toString()))); + }else{ + return String.format("%s", speaker.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Del(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Div(first,second)); + }else { + return String.format("%s", speaker.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } +} \ No newline at end of file