From 84c206fb613b2967f2c42bb4eb389f140e47c8a4 Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Tue, 28 Feb 2023 10:16:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A02=20=D0=92=D1=81=D0=B5=20=D0=BA?= =?UTF-8?q?=D1=80=D0=BE=D0=BC=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frontend/index.html | 24 +++++---- Frontend/script.js | 49 +++++++++--------- src/main/java/ru/ulstu/is/lab1/Lab1.java | 28 ---------- .../speaker/controller/MethodController.java | 43 ++++++++++++++++ .../ulstu/is/lab1/speaker/domain/IMethod.java | 11 ++++ .../is/lab1/speaker/domain/MethodInt.java | 28 ++++++++++ .../is/lab1/speaker/domain/MethodString.java | 41 +++++++++++++++ .../lab1/speaker/service/MethodService.java | 51 +++++++++++++++++++ 8 files changed, 214 insertions(+), 61 deletions(-) create mode 100644 src/main/java/ru/ulstu/is/lab1/speaker/controller/MethodController.java create mode 100644 src/main/java/ru/ulstu/is/lab1/speaker/domain/IMethod.java create mode 100644 src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodInt.java create mode 100644 src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodString.java create mode 100644 src/main/java/ru/ulstu/is/lab1/speaker/service/MethodService.java diff --git a/Frontend/index.html b/Frontend/index.html index a4b7c9d..6851fc3 100644 --- a/Frontend/index.html +++ b/Frontend/index.html @@ -13,20 +13,26 @@
- Введите строку - - укажите разделитель - + Выберите тип данных +
+ +
+ Введите первое значение + + Введите второе значение + Выберите операцию
- - + + - - +
Результат - +
diff --git a/Frontend/script.js b/Frontend/script.js index 09a7f36..1aab2c5 100644 --- a/Frontend/script.js +++ b/Frontend/script.js @@ -1,40 +1,41 @@ -let buttonUpper = document.getElementById("buttonUpper"); -let buttonLower = document.getElementById("buttonLower"); +let buttonSum = document.getElementById("buttonSum"); +let buttonMinus = document.getElementById("buttonMinus"); let buttonReverse = document.getElementById("buttonReverse"); -let buttonCount = document.getElementById("buttonCount"); -let buttonDel = document.getElementById("buttonDel"); -let stringInput = document.getElementById("string"); -let placeInput = document.getElementById("place"); +let buttonComparison = document.getElementById("buttonComparison"); +let numberOneInput = document.getElementById("first"); +let numberTwoInput = document.getElementById("second"); +let typeInput = document.getElementById("type"); let resultInput = document.getElementById("res"); -buttonUpper.onclick = function(event) { - let str = stringInput.value; - fetch(`http://localhost:8080/toUpperCase?string=${str}`) +buttonSum.onclick = function(event) { + let num_1 = numberOneInput.value; + let num_2 = numberTwoInput.value; + 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); } -buttonLower.onclick = function(event) { - let str = stringInput.value; - fetch(`http://localhost:8080/toLowerCase?string=${str}`) +buttonMinus.onclick = function(event) { + let num_1 = numberOneInput.value; + let num_2 = numberTwoInput.value; + 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); } buttonReverse.onclick = function(event) { - let str = stringInput.value; - fetch(`http://localhost:8080/reverse?string=${str}`) + let num_1 = numberOneInput.value; + let num_2 = numberTwoInput.value; + let type = typeInput.value; + fetch(`http://localhost:8080/reverse?first=${num_1}&second=${num_2}&type=${type}`) .then(response => response.text()) .then(res => resultInput.value = res); } -buttonCount.onclick = function(event) { - let str = stringInput.value; - fetch(`http://localhost:8080/count?string=${str}`) - .then(response => response.text()) - .then(res => resultInput.value = res); -} -buttonDel.onclick = function(event) { - let str = stringInput.value; - let plc = placeInput.value; - fetch(`http://localhost:8080/del?string=${str}&place=${plc}`) +buttonComparison.onclick = function(event) { + let num_1 = numberOneInput.value; + let num_2 = numberTwoInput.value; + let type = typeInput.value; + fetch(`http://localhost:8080/comparison?first=${num_1}&second=${num_2}&type=${type}`) .then(response => response.text()) .then(res => resultInput.value = res); } diff --git a/src/main/java/ru/ulstu/is/lab1/Lab1.java b/src/main/java/ru/ulstu/is/lab1/Lab1.java index 5d856db..fb8823a 100644 --- a/src/main/java/ru/ulstu/is/lab1/Lab1.java +++ b/src/main/java/ru/ulstu/is/lab1/Lab1.java @@ -13,32 +13,4 @@ public class Lab1 { public static void main(String[] args) { SpringApplication.run(Lab1.class, args); } - @GetMapping("/hello") - public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s!", name); - } - @GetMapping("/toUpperCase") - public String toUpperCase(@RequestParam(defaultValue = "word") String string){ - return string.toUpperCase(); - } - @GetMapping("/toLowerCase") - public String toLowerCase(@RequestParam(defaultValue = "word") String string){ - return string.toLowerCase(); - } - @GetMapping("/reverse") - public String reverse(@RequestParam(defaultValue = "word") String string){ - String newStr=""; - for(int i = string.length() - 1; i >= 0; i--){ - newStr += string.charAt(i); - } - return newStr; - } - @GetMapping("/count") - public String count(@RequestParam(defaultValue = "word") String string){ - return Integer.toString(string.length()); - } - @GetMapping("/del") - public String del(@RequestParam(defaultValue = "word") String string, String place){ - return String.join(" ", string.split(place)); - } } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/lab1/speaker/controller/MethodController.java b/src/main/java/ru/ulstu/is/lab1/speaker/controller/MethodController.java new file mode 100644 index 0000000..3078f46 --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/speaker/controller/MethodController.java @@ -0,0 +1,43 @@ +package ru.ulstu.is.lab1.speaker.controller; + +import ru.ulstu.is.lab1.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("/reverse") + 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.Rev(first, second, type); + } + + @GetMapping("/comparison") + 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.Com(first, second, type); + } +} diff --git a/src/main/java/ru/ulstu/is/lab1/speaker/domain/IMethod.java b/src/main/java/ru/ulstu/is/lab1/speaker/domain/IMethod.java new file mode 100644 index 0000000..da3db8e --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/speaker/domain/IMethod.java @@ -0,0 +1,11 @@ +package ru.ulstu.is.lab1.speaker.domain; + +public interface IMethod { + T Sum(T first, T second); + + T Minus(T first, T second); + + T Reverse(T first, T second); + + T Comparison(T first, T second); +} diff --git a/src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodInt.java b/src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodInt.java new file mode 100644 index 0000000..6034879 --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodInt.java @@ -0,0 +1,28 @@ +package ru.ulstu.is.lab1.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 Minus(Integer first, Integer second) { + return Integer.parseInt(first.toString()) - Integer.parseInt(second.toString()); + } + + public Integer Reverse(Integer first, Integer second) { + return (Integer.parseInt(first.toString()) + Integer.parseInt(second.toString())) * (-1); + } + + public Integer Comparison(Integer first, Integer second) { + int num1 = Integer.parseInt(first.toString()); + int num2 = Integer.parseInt(second.toString()); + if (num1 >= num2){ + return num1; + }else{ + return num2; + } + } +} diff --git a/src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodString.java b/src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodString.java new file mode 100644 index 0000000..a12c991 --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/speaker/domain/MethodString.java @@ -0,0 +1,41 @@ +package ru.ulstu.is.lab1.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 Minus(String first, String second) { + String arr[] = first.split(""); + for(int i = 0; i < first.length(); i++){ + if (second.indexOf(arr[i]) >= 0){ + arr[i] = ""; + } + } + return String.join("", arr); + } + + @Override + public String Reverse(String first, String second) { + String ourStr = first.concat(second); + String newStr = ""; + for(int i = ourStr.length() - 1; i >= 0; i--){ + newStr += ourStr.charAt(i); + } + return newStr; + } + + @Override + public String Comparison(String first, String second) { + if (first.length() >= second.length()){ + return first; + }else{ + return second; + } + } +} diff --git a/src/main/java/ru/ulstu/is/lab1/speaker/service/MethodService.java b/src/main/java/ru/ulstu/is/lab1/speaker/service/MethodService.java new file mode 100644 index 0000000..49a728b --- /dev/null +++ b/src/main/java/ru/ulstu/is/lab1/speaker/service/MethodService.java @@ -0,0 +1,51 @@ +package ru.ulstu.is.lab1.speaker.service; + +import ru.ulstu.is.lab1.speaker.domain.IMethod; +import ru.ulstu.is.lab1.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,second)); + }else{ + return String.format("%s", speaker.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Rev(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Reverse(first,second)); + }else{ + return String.format("%s", speaker.Reverse(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Com(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Comparison(first,second)); + }else { + return String.format("%s", speaker.Comparison(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } +}