From 6ffff5b55bd6bd7023367b48531fdb5c7f593df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=A0=D0=BE=D0=B3=D0=B0=D1=88=D0=BE=D0=B2=D0=B0?= Date: Tue, 14 Mar 2023 10:00:36 +0400 Subject: [PATCH] Lab1 --- Front/index.html | 71 +++++++++++++++++++ Front/index.js | 48 +++++++++++++ .../java/ru/ulstu/is/sbapp/Controllers.java | 41 +++++++++++ .../is/sbapp/Interfaces/ITypeInterface.java | 8 +++ .../is/sbapp/Interfaces/IntegerType.java | 23 ++++++ .../ulstu/is/sbapp/Interfaces/StringType.java | 24 +++++++ .../java/ru/ulstu/is/sbapp/MethodService.java | 52 ++++++++++++++ .../ru/ulstu/is/sbapp/SbappApplication.java | 23 +++++- .../ru/ulstu/is/sbapp/WebConfiguration.java | 14 ++++ 9 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 Front/index.html create mode 100644 Front/index.js create mode 100644 src/main/java/ru/ulstu/is/sbapp/Controllers.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/Interfaces/ITypeInterface.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/Interfaces/IntegerType.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/Interfaces/StringType.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/MethodService.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java diff --git a/Front/index.html b/Front/index.html new file mode 100644 index 0000000..7700e95 --- /dev/null +++ b/Front/index.html @@ -0,0 +1,71 @@ + + + + + + App + + + + +
+ Выберите тип данных +
+ +
+
+

To Upper

+
+
+ + + +
+ +
+
+ +
+

To Lower

+
+
+ + + +
+ +
+
+ +
+

Sum

+
+
+ + + + +
+ +
+
+ +
+

Sum /2

+
+
+ + + + +
+ +
+
+
+ + + diff --git a/Front/index.js b/Front/index.js new file mode 100644 index 0000000..fb7ca04 --- /dev/null +++ b/Front/index.js @@ -0,0 +1,48 @@ +'use strict' + +let num1 = document.getElementById("input1") +let num2 = document.getElementById("input2") +let num3 = document.getElementById("input3") +let num4 = document.getElementById("input4") +let num5 = document.getElementById("input5") +let num6 = document.getElementById("input6") +let tucrez = document.getElementById("tuc_result") +let tlcrez = document.getElementById("tlc_result") +let chAtrez = document.getElementById("chAt_result") +let subrez = document.getElementById("sub_result") +let typeInput = document.getElementById("type"); + +function TUC(){ + let val1 = num1.value + let type = typeInput.value; + fetch(`http://localhost:8080/toUpperCase/${val1}`) + .then(response => response.text()) + .then(data =>{tucrez.value=data}); +} + +function TLC(){ + let val1 = num2.value + let type = typeInput.value; + fetch(`http://localhost:8080/toLowerCase/${val1}`) + .then(response => response.text()) + .then(data =>{tlcrez.value=data}); +} + +function CHAR(){ + let val1 = num3.value + let val2 = num4.value + let type = typeInput.value; + fetch(`http://localhost:8080/charAt/${val1}/${val2}`) + .then(response => response.text()) + .then(data =>{chAtrez.value=data}); +} + +function SUB(){ + let val1 = num5.value + let val2 = num6.value + let type = typeInput.value; + fetch(`http://localhost:8080/substring/${val1}/${val2}`) + .then(response => response.text()) + .then(data =>{subrez.value=data}); +} + diff --git a/src/main/java/ru/ulstu/is/sbapp/Controllers.java b/src/main/java/ru/ulstu/is/sbapp/Controllers.java new file mode 100644 index 0000000..b8febc6 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/Controllers.java @@ -0,0 +1,41 @@ +package ru.ulstu.is.sbapp; + +import ru.ulstu.is.sbapp.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 Controllers { + private final MethodService speakerService; + + public Controllers(MethodService speakerService) { + this.speakerService = speakerService; + } + + @GetMapping("/toUpper") + public String toUpper(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.toUpper(first, type); + } + + @GetMapping("/toLower") + public String toLower(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.toLower(first, type); + } + + @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("/sum1") + public String sum1(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.sum1(first, second, type); + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/Interfaces/ITypeInterface.java b/src/main/java/ru/ulstu/is/sbapp/Interfaces/ITypeInterface.java new file mode 100644 index 0000000..9e2c0c6 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/Interfaces/ITypeInterface.java @@ -0,0 +1,8 @@ +package ru.ulstu.is.sbapp.Interfaces; + +public interface ITypeInterface { + T Method1(T value1); + T Method2(T value1); + T Method3(T value1, T value2); + T Method4(T value1, T value2); +} diff --git a/src/main/java/ru/ulstu/is/sbapp/Interfaces/IntegerType.java b/src/main/java/ru/ulstu/is/sbapp/Interfaces/IntegerType.java new file mode 100644 index 0000000..24ca88f --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/Interfaces/IntegerType.java @@ -0,0 +1,23 @@ +package ru.ulstu.is.sbapp.Interfaces; + +public class IntegerType implements ITypeInterface { + @Override + public Integer Method1(Integer value1) { + return value1 + value1; + } + + @Override + public Integer Method2(Integer value1) { + return value1 - value1/2; + } + + @Override + public Integer Method3(Integer value1, Integer value2) { + return value1 + value2; + } + + @Override + public Integer Method4(Integer value1, Integer value2) { + return value1/2 + value2/2; + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/Interfaces/StringType.java b/src/main/java/ru/ulstu/is/sbapp/Interfaces/StringType.java new file mode 100644 index 0000000..a241d3b --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/Interfaces/StringType.java @@ -0,0 +1,24 @@ +package ru.ulstu.is.sbapp.Interfaces; + +public class StringType implements ITypeInterface { + + @Override + public String Method1(String value1) { + return value1.toUpperCase(); + } + + @Override + public String Method2(String value1) { + return value1.toLowerCase(); + } + + @Override + public String Method3(String value1, String value2) { + return value1.toLowerCase() + value2.toLowerCase(); + } + + @Override + public String Method4(String value1, String value2) { + return value1.toUpperCase() + value2.toUpperCase(); + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/MethodService.java b/src/main/java/ru/ulstu/is/sbapp/MethodService.java new file mode 100644 index 0000000..5f04b4c --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/MethodService.java @@ -0,0 +1,52 @@ +package ru.ulstu.is.sbapp; + +import ru.ulstu.is.sbapp.Interfaces.ITypeInterface; +import ru.ulstu.is.sbapp.Interfaces.StringType; +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 toUpper(Object first, String type) { + final ITypeInterface speaker = (ITypeInterface) applicationContext.getBean(type); + if (speaker instanceof StringType){ + return String.format("%s", speaker.Method1(first)); + }else{ + return String.format("%s", speaker.Method1(Integer.parseInt(first.toString()))); + } + } + + public String toLower(Object first, String type) { + final ITypeInterface speaker = (ITypeInterface) applicationContext.getBean(type); + if (speaker instanceof StringType){ + return String.format("%s", speaker.Method2(first)); + }else{ + return String.format("%s", speaker.Method2(Integer.parseInt(first.toString()))); + } + } + + public String sum(Object first, Object second, String type) { + final ITypeInterface speaker = (ITypeInterface) applicationContext.getBean(type); + if (speaker instanceof StringType){ + return String.format("%s", speaker.Method3(first,second)); + }else{ + return String.format("%s", speaker.Method3(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String sum1(Object first, Object second, String type) { + final ITypeInterface speaker = (ITypeInterface) applicationContext.getBean(type); + if (speaker instanceof StringType){ + return String.format("%s", speaker.Method4(first,second)); + }else { + return String.format("%s", speaker.Method4(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } +} + diff --git a/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java b/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java index 2c6e3b4..741467d 100644 --- a/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java +++ b/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java @@ -2,12 +2,33 @@ package ru.ulstu.is.sbapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.*; @SpringBootApplication +@RestController +@CrossOrigin public class SbappApplication { - public static void main(String[] args) { SpringApplication.run(SbappApplication.class, args); } + @GetMapping("/toUpperCase/{value}") + public @ResponseBody String toUpperCase(@PathVariable String value) { + return value.toUpperCase(); + } + + @GetMapping("/toLowerCase/{value}") + public @ResponseBody String toLowerCase(@PathVariable String value) { + return value.toLowerCase(); + } + @GetMapping("/charAt/{v1}/{v2}") + public Character charAt(@PathVariable String v1, @PathVariable Integer v2) { + Character c = v1.charAt(v2-1); + return c; + } + @GetMapping("/substring/{v1}/{v2}") + public String substring(@PathVariable String v1, @PathVariable Integer v2) { + String str = v1.substring(v2); + return str; + } } diff --git a/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java b/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java new file mode 100644 index 0000000..08f744f --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java @@ -0,0 +1,14 @@ +package ru.ulstu.is.sbapp; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfiguration implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry){ + registry.addMapping("/**").allowedMethods("*"); + } +}