From 70d597ce771a0a0bc6d6bcd2806b6043f4ddb3da Mon Sep 17 00:00:00 2001 From: shadowik Date: Tue, 21 Feb 2023 16:06:34 +0400 Subject: [PATCH] First Version --- .../com/example/demo/Demo1Application.java | 62 +++++++++---------- .../configuration/MethodConfiguration.java | 20 ++++++ .../speaker/controller/MethodController.java | 54 ++++++++++++++++ .../example/demo/speaker/domain/IMethods.java | 12 ++++ .../demo/speaker/domain/MethodInteger.java | 22 +++++++ .../demo/speaker/domain/MethodString.java | 30 +++++++++ .../demo/speaker/service/MethodService.java | 34 ++++++++++ .../example/demo/Demo1ApplicationTests.java | 51 ++++++++++++++- untitled1/src/components/Main.vue | 44 ++++++++----- 9 files changed, 280 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/example/demo/speaker/configuration/MethodConfiguration.java create mode 100644 src/main/java/com/example/demo/speaker/controller/MethodController.java create mode 100644 src/main/java/com/example/demo/speaker/domain/IMethods.java create mode 100644 src/main/java/com/example/demo/speaker/domain/MethodInteger.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/src/main/java/com/example/demo/Demo1Application.java b/src/main/java/com/example/demo/Demo1Application.java index 5454899..68f3dff 100644 --- a/src/main/java/com/example/demo/Demo1Application.java +++ b/src/main/java/com/example/demo/Demo1Application.java @@ -12,35 +12,35 @@ public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } - @GetMapping("/hello") - public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s!", name); - } - - @GetMapping("/div") - @ResponseBody - public double Division(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) { - if (b == 0) return 0; - return a / b; - } - - @GetMapping("/mul") - @ResponseBody - public double Multiply(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) { - return a * b; - } - - @GetMapping("/pow") - @ResponseBody - public double Pow(@RequestParam(defaultValue = "1") double a, - @RequestParam(defaultValue = "1") double b) { - return Math.pow(a, b); - } - - @GetMapping("/plus") - @ResponseBody - public double Plus(@RequestParam(defaultValue = "1") double a, - @RequestParam(defaultValue = "1") double b) { - return a + b; - } +// @GetMapping("/hello") +// public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { +// return String.format("Hello %s!", name); +// } +// +// @GetMapping("/div") +// @ResponseBody +// public double Division(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) { +// if (b == 0) return 0; +// return a / b; +// } +// +// @GetMapping("/mul") +// @ResponseBody +// public double Multiply(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) { +// return a * b; +// } +// +// @GetMapping("/pow") +// @ResponseBody +// public double Pow(@RequestParam(defaultValue = "1") double a, +// @RequestParam(defaultValue = "1") double b) { +// return Math.pow(a, b); +// } +// +// @GetMapping("/plus") +// @ResponseBody +// public double Plus(@RequestParam(defaultValue = "1") double a, +// @RequestParam(defaultValue = "1") double b) { +// return a + b; +// } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/speaker/configuration/MethodConfiguration.java b/src/main/java/com/example/demo/speaker/configuration/MethodConfiguration.java new file mode 100644 index 0000000..f4928c4 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/configuration/MethodConfiguration.java @@ -0,0 +1,20 @@ +package com.example.demo.speaker.configuration; + +import com.example.demo.speaker.domain.IMethods; +import com.example.demo.speaker.domain.MethodInteger; +import com.example.demo.speaker.domain.MethodString; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MethodConfiguration { + @Bean(value = "int") + public MethodInteger createIntegerMethods() { + return new MethodInteger(); + } + + @Bean(value = "string") + public MethodString createStringMethods() { + return new MethodString(); + } +} 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..3ef95ef --- /dev/null +++ b/src/main/java/com/example/demo/speaker/controller/MethodController.java @@ -0,0 +1,54 @@ +package com.example.demo.speaker.controller; + +import com.example.demo.speaker.service.MethodService; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.GetMapping; + +import java.io.Console; + +@RestController +public class MethodController { + private final MethodService methodService; + + public MethodController(MethodService methodService) { + this.methodService = methodService; + } + + @GetMapping("/sum") + @ResponseBody + public String sum(@RequestParam(value = "first", defaultValue = "0") String first, + @RequestParam(value = "second", defaultValue = "0") String second, + @RequestParam(value = "type", defaultValue = "int") String type) { + Object a = first; + Object b = second; + System.out.println(a.getClass()); + return methodService.Sum(a, b, type); + } + + @GetMapping("/mul") + @ResponseBody + public String mul(@RequestParam(value = "first", defaultValue = "0") Object first, + @RequestParam(value = "second", defaultValue = "0") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return methodService.Multiply(first, second, type); + } + + @GetMapping("/minus") + @ResponseBody + public String minus(@RequestParam(value = "first", defaultValue = "0") Object first, + @RequestParam(value = "second", defaultValue = "0") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return methodService.Minus(first, second, type); + } + + @GetMapping("/cont") + @ResponseBody + public String cont(@RequestParam(value = "first", defaultValue = "0") Object first, + @RequestParam(value = "second", defaultValue = "0") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return methodService.Contains(first, second, type).toString(); + } + +} diff --git a/src/main/java/com/example/demo/speaker/domain/IMethods.java b/src/main/java/com/example/demo/speaker/domain/IMethods.java new file mode 100644 index 0000000..37df6f7 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/domain/IMethods.java @@ -0,0 +1,12 @@ +package com.example.demo.speaker.domain; + +public interface IMethods { + T Sum(Object first, Object second); + + T Multiply(Object first, Object second); + + T Minus(Object first, Object second); + + Boolean Contains(Object first, Object second); + +} diff --git a/src/main/java/com/example/demo/speaker/domain/MethodInteger.java b/src/main/java/com/example/demo/speaker/domain/MethodInteger.java new file mode 100644 index 0000000..7fa3595 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/domain/MethodInteger.java @@ -0,0 +1,22 @@ +package com.example.demo.speaker.domain; + +public class MethodInteger implements IMethods { + @Override + public Integer Sum(Object first, Object second) { + return Integer.parseInt((String) first) + Integer.parseInt((String) second); + } + @Override + public Integer Multiply(Object first, Object second) { + return Integer.parseInt((String) first) * Integer.parseInt((String) second); + } + + @Override + public Integer Minus(Object first, Object second) { + return Integer.parseInt((String) first) - Integer.parseInt((String) second); + } + + @Override + public Boolean Contains(Object first, Object second) { + return ((String)first).contains((String)second); + } +} 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..0087c48 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/domain/MethodString.java @@ -0,0 +1,30 @@ +package com.example.demo.speaker.domain; + +public class MethodString implements IMethods{ + @Override + public String Sum(Object first, Object second) { + return ((String)first).concat(((String)second)); + } + + @Override + public String Multiply(Object first, Object second) { + String res = ((String)first); + for (int i = 0; i < Integer.parseInt((String) second) - 1; i++) { + res = Sum(res, first); + } + return res; + } + + @Override + public String Minus(Object first, Object second) { + if (Contains(first, second)) { + return ((String)first).replace((String)second, ""); + } + return ((String)first); + } + + @Override + public Boolean Contains(Object first, Object second) { + return ((String)first).contains((String)second); + } +} 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..8654e54 --- /dev/null +++ b/src/main/java/com/example/demo/speaker/service/MethodService.java @@ -0,0 +1,34 @@ +package com.example.demo.speaker.service; + +import com.example.demo.speaker.domain.IMethods; +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 IMethods method = (IMethods) applicationContext.getBean(type); + return String.format("%s", method.Sum(first, second)); + } + + public String Multiply(Object first, Object second, String type) { + final IMethods method = (IMethods) applicationContext.getBean(type); + return String.format("%s", method.Multiply(first, second)); + } + + public String Minus(Object first, Object second, String type) { + final IMethods method = (IMethods) applicationContext.getBean(type); + return String.format("%s", method.Minus(first, second)); + } + + public String Contains(Object first, Object second, String type) { + final IMethods method = (IMethods) applicationContext.getBean(type); + return String.format("%s", method.Contains(first, second)); + } +} diff --git a/src/test/java/com/example/demo/Demo1ApplicationTests.java b/src/test/java/com/example/demo/Demo1ApplicationTests.java index 22afcdb..a98b311 100644 --- a/src/test/java/com/example/demo/Demo1ApplicationTests.java +++ b/src/test/java/com/example/demo/Demo1ApplicationTests.java @@ -1,13 +1,62 @@ package com.example.demo; +import com.example.demo.speaker.service.MethodService; +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; @SpringBootTest class Demo1ApplicationTests { + @Autowired + MethodService methodService; + @Test - void contextLoads() { + void testMethodSumInt() { + final String res = methodService.Sum("1", "2", "int"); + Assertions.assertEquals("3", res); } + @Test + void testMethodSumString() { + final String res = methodService.Sum("1", "2", "string"); + Assertions.assertEquals("12", res); + } + + @Test + void testMethodMinusInt() { + final String res = methodService.Minus("1", "2", "int"); + Assertions.assertEquals("-1", res); + } + + @Test + void testMethodMinusString() { + final String res = methodService.Minus("214324", "4", "string"); + Assertions.assertEquals("2132", res); + } + + @Test + void testMethodMultInt() { + final String res = methodService.Multiply("1", "2", "int"); + Assertions.assertEquals("2", res); + } + + @Test + void testMethodMultString() { + final String res = methodService.Multiply("1", "2", "string"); + Assertions.assertEquals("11", res); + } + + @Test + void testMethodContainsInt() { + final String res = methodService.Contains("123", "2", "int"); + Assertions.assertEquals("true", res); + } + + @Test + void testMethodContainsString() { + final String res = methodService.Contains("1", "2", "string"); + Assertions.assertEquals("false", res); + } } diff --git a/untitled1/src/components/Main.vue b/untitled1/src/components/Main.vue index 5d1b35d..5f47782 100644 --- a/untitled1/src/components/Main.vue +++ b/untitled1/src/components/Main.vue @@ -2,17 +2,23 @@
- +
- +
+
+ +
- - + + - +
@@ -29,18 +35,20 @@ export default { name: "Main", data() { return { - val1: 0, - val2: 0, + val1: "", + val2: "", + type: "int", res: "" } }, methods: { sum() { - let url = "http://localhost:8080/plus?a=" + this.val1.toString() + "&b=" + this.val2.toString(); + let url = "http://localhost:8080/sum?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type; let vm = this; + console.log(url); fetch(url) .then(function (response) { - return response.json(); + return response.text(); }) .then(function (data) { console.info('Loaded'); @@ -52,12 +60,12 @@ export default { throw "Can't load items"; }); }, - pow() { - let url = "http://localhost:8080/pow?a=" + this.val1.toString() + "&b=" + this.val2.toString(); + Contains() { + let url = "http://localhost:8080/cont?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type; let vm = this; fetch(url) .then(function (response) { - return response.json(); + return response.text(); }) .then(function (data) { console.info('Loaded'); @@ -70,11 +78,12 @@ export default { }); }, mul() { - let url = "http://localhost:8080/mul?a=" + this.val1.toString() + "&b=" + this.val2.toString(); + let url = "http://localhost:8080/mul?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type; let vm = this; + console.log(url); fetch(url) .then(function (response) { - return response.json(); + return response.text(); }) .then(function (data) { console.info('Loaded'); @@ -86,12 +95,13 @@ export default { throw "Can't load items"; }); }, - div() { - let url = "http://localhost:8080/div?a=" + this.val1.toString() + "&b=" + this.val2.toString(); + minus() { + let url = "http://localhost:8080/minus?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type; let vm = this; + console.log(url); fetch(url) .then(function (response) { - return response.json(); + return response.text(); }) .then(function (data) { console.info('Loaded');