From dd9d2b9bcd2804e5fab6fa1f6672c2f1aa30ff5d Mon Sep 17 00:00:00 2001 From: shadowik Date: Tue, 7 Mar 2023 13:01:10 +0400 Subject: [PATCH] finish --- .../speaker/controller/MethodController.java | 15 +++----- .../example/demo/speaker/domain/IMethods.java | 8 ++-- .../demo/speaker/domain/MethodInteger.java | 16 ++++---- .../demo/speaker/domain/MethodString.java | 20 +++++----- .../demo/speaker/service/MethodService.java | 29 ++++++++++---- .../example/demo/Demo1ApplicationTests.java | 38 ++++++++++++------- untitled1/package-lock.json | 3 ++ untitled1/package.json | 3 ++ 8 files changed, 80 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/example/demo/speaker/controller/MethodController.java b/src/main/java/com/example/demo/speaker/controller/MethodController.java index 3ef95ef..598790a 100644 --- a/src/main/java/com/example/demo/speaker/controller/MethodController.java +++ b/src/main/java/com/example/demo/speaker/controller/MethodController.java @@ -18,18 +18,15 @@ public class MethodController { @GetMapping("/sum") @ResponseBody - public String sum(@RequestParam(value = "first", defaultValue = "0") String first, - @RequestParam(value = "second", defaultValue = "0") String second, + public Object sum(@RequestParam(value = "first", defaultValue = "0") Object first, + @RequestParam(value = "second", defaultValue = "0") Object 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); + return methodService.Sum(first, second, type); } @GetMapping("/mul") @ResponseBody - public String mul(@RequestParam(value = "first", defaultValue = "0") Object first, + public Object 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); @@ -37,7 +34,7 @@ public class MethodController { @GetMapping("/minus") @ResponseBody - public String minus(@RequestParam(value = "first", defaultValue = "0") Object first, + public Object 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); @@ -45,7 +42,7 @@ public class MethodController { @GetMapping("/cont") @ResponseBody - public String cont(@RequestParam(value = "first", defaultValue = "0") Object first, + public Object 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 index 37df6f7..32fed65 100644 --- a/src/main/java/com/example/demo/speaker/domain/IMethods.java +++ b/src/main/java/com/example/demo/speaker/domain/IMethods.java @@ -1,12 +1,12 @@ package com.example.demo.speaker.domain; public interface IMethods { - T Sum(Object first, Object second); + T Sum(T first, T second); - T Multiply(Object first, Object second); + T Multiply(T first, T second); - T Minus(Object first, Object second); + T Minus(T first, T second); - Boolean Contains(Object first, Object second); + Boolean Contains(T first, T 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 index 7fa3595..89bf00e 100644 --- a/src/main/java/com/example/demo/speaker/domain/MethodInteger.java +++ b/src/main/java/com/example/demo/speaker/domain/MethodInteger.java @@ -2,21 +2,21 @@ 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); + public Integer Sum(Integer first, Integer second) { + return first + second; } @Override - public Integer Multiply(Object first, Object second) { - return Integer.parseInt((String) first) * Integer.parseInt((String) second); + public Integer Multiply(Integer first, Integer second) { + return first * second; } @Override - public Integer Minus(Object first, Object second) { - return Integer.parseInt((String) first) - Integer.parseInt((String) second); + public Integer Minus(Integer first, Integer second) { + return first - second; } @Override - public Boolean Contains(Object first, Object second) { - return ((String)first).contains((String)second); + public Boolean Contains(Integer first, Integer second) { + return Integer.toString(first).contains(Integer.toString(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 index 0087c48..227c360 100644 --- a/src/main/java/com/example/demo/speaker/domain/MethodString.java +++ b/src/main/java/com/example/demo/speaker/domain/MethodString.java @@ -2,29 +2,29 @@ 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)); + public String Sum(String first, String second) { + return first.concat(second); } @Override - public String Multiply(Object first, Object second) { - String res = ((String)first); - for (int i = 0; i < Integer.parseInt((String) second) - 1; i++) { + public String Multiply(String first, String second) { + String res = first; + for (int i = 0; i < Integer.parseInt(second) - 1; i++) { res = Sum(res, first); } return res; } @Override - public String Minus(Object first, Object second) { + public String Minus(String first, String second) { if (Contains(first, second)) { - return ((String)first).replace((String)second, ""); + return first.replace(second, ""); } - return ((String)first); + return first; } @Override - public Boolean Contains(Object first, Object second) { - return ((String)first).contains((String)second); + public Boolean Contains(String first, String second) { + return first.contains(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 index 8654e54..8ab3aca 100644 --- a/src/main/java/com/example/demo/speaker/service/MethodService.java +++ b/src/main/java/com/example/demo/speaker/service/MethodService.java @@ -4,6 +4,8 @@ import com.example.demo.speaker.domain.IMethods; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; +import java.util.function.BiFunction; + @Service public class MethodService { private final ApplicationContext applicationContext; @@ -12,23 +14,34 @@ public class MethodService { this.applicationContext = applicationContext; } - public String Sum(Object first, Object second, String type) { + public Object Sum(Object first, Object second, String type) { final IMethods method = (IMethods) applicationContext.getBean(type); - return String.format("%s", method.Sum(first, second)); + return calculate((x, y) -> method.Sum(x, y), type, first, second); } - public String Multiply(Object first, Object second, String type) { + public Object Multiply(Object first, Object second, String type) { final IMethods method = (IMethods) applicationContext.getBean(type); - return String.format("%s", method.Multiply(first, second)); + return calculate((x, y) -> method.Multiply(x, y), type, first, second); } - public String Minus(Object first, Object second, String type) { + public Object Minus(Object first, Object second, String type) { final IMethods method = (IMethods) applicationContext.getBean(type); - return String.format("%s", method.Minus(first, second)); + return calculate((x, y) -> method.Minus(x, y), type, first, second); } - public String Contains(Object first, Object second, String type) { + public Object Contains(Object first, Object second, String type) { final IMethods method = (IMethods) applicationContext.getBean(type); - return String.format("%s", method.Contains(first, second)); + return calculate((x, y) -> method.Contains(x, y), type, first, second); + } + + private Object calculate(BiFunction methodCalc, String type, Object a, Object b) { + switch (type) { + case "int": + return methodCalc.apply(Integer.parseInt(a.toString()), Integer.parseInt(b.toString())); + case "string": + return methodCalc.apply(a.toString(), b.toString()); + default: + throw new IllegalArgumentException("Type not found"); + } } } diff --git a/src/test/java/com/example/demo/Demo1ApplicationTests.java b/src/test/java/com/example/demo/Demo1ApplicationTests.java index a98b311..a450d68 100644 --- a/src/test/java/com/example/demo/Demo1ApplicationTests.java +++ b/src/test/java/com/example/demo/Demo1ApplicationTests.java @@ -14,49 +14,61 @@ class Demo1ApplicationTests { @Test void testMethodSumInt() { - final String res = methodService.Sum("1", "2", "int"); - Assertions.assertEquals("3", res); + final Object res = methodService.Sum(1, 2, "int"); + Assertions.assertEquals(3, res); } @Test void testMethodSumString() { - final String res = methodService.Sum("1", "2", "string"); + final Object 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); + final Object res = methodService.Minus(1, 2, "int"); + Assertions.assertEquals(-1, res); } @Test void testMethodMinusString() { - final String res = methodService.Minus("214324", "4", "string"); + final Object 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); + final Object res = methodService.Multiply(1, 2, "int"); + Assertions.assertEquals(2, res); } @Test void testMethodMultString() { - final String res = methodService.Multiply("1", "2", "string"); + final Object 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); + final Object 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); + final Object res = methodService.Contains("1", "2", "string"); + Assertions.assertEquals(false, res); + } + + @Test() + void testErrorWrongType() { + try { + final Object res = methodService.Sum(1, "ds", "int"); + Assertions.fail(); + } + catch (Exception e) { + + } + } } diff --git a/untitled1/package-lock.json b/untitled1/package-lock.json index 429576b..b994d18 100644 --- a/untitled1/package-lock.json +++ b/untitled1/package-lock.json @@ -16,6 +16,9 @@ "devDependencies": { "@vitejs/plugin-vue": "^4.0.0", "vite": "^4.0.0" + }, + "engines": { + "node": "^14.18.0 || >=16.0.0" } }, "node_modules/@babel/parser": { diff --git a/untitled1/package.json b/untitled1/package.json index b62ce5b..1272e62 100644 --- a/untitled1/package.json +++ b/untitled1/package.json @@ -16,5 +16,8 @@ "devDependencies": { "@vitejs/plugin-vue": "^4.0.0", "vite": "^4.0.0" + }, + "engines": { + "node": "^14.18.0 || >=16.0.0" } }