From 3bea45850ba3c8dde1143cb5b472e98683fe726f Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Tue, 21 Feb 2023 12:03:06 +0400 Subject: [PATCH] lab2: fixes --- .../controllers/OperationController.java | 20 +++++--- .../springip/service/OperationService.java | 49 ++++--------------- .../springip/SpringipApplicationTests.java | 16 +++--- 3 files changed, 30 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/example/springip/controllers/OperationController.java b/src/main/java/com/example/springip/controllers/OperationController.java index ffd6372..c50419d 100644 --- a/src/main/java/com/example/springip/controllers/OperationController.java +++ b/src/main/java/com/example/springip/controllers/OperationController.java @@ -14,23 +14,27 @@ public class OperationController { } @GetMapping("/sum") - public Object sum(@RequestParam String val1, @RequestParam String val2) { - return (Object) operationService.sum(val1, val2); + public Object sum(@RequestParam String val1, @RequestParam String val2, @RequestParam String type) { + + return (Object) operationService.sum(val1, val2, type); } @GetMapping("/mul") - public Object mul(@RequestParam String val1, @RequestParam Integer val2) { - return (Object) operationService.mul(val1, val2); + public Object mul(@RequestParam String val1, @RequestParam Integer val2, @RequestParam String type) { + + return (Object) operationService.mul(val1, val2, type); } @GetMapping("/length") - public Object length(@RequestParam String val1) { - return (Object) operationService.length(val1); + public Object length(@RequestParam String val1, @RequestParam String type) { + + return (Object) operationService.length(val1, type); } @GetMapping("/startsWith") - public Object startsWith(@RequestParam String val1, @RequestParam String val2) { - return (Object) operationService.startsWith(val1, val2); + public Object startsWith(@RequestParam String val1, @RequestParam String val2, @RequestParam String type) { + + return (Object) operationService.startsWith(val1, val2, type); } diff --git a/src/main/java/com/example/springip/service/OperationService.java b/src/main/java/com/example/springip/service/OperationService.java index 8510ca4..83d1f6f 100644 --- a/src/main/java/com/example/springip/service/OperationService.java +++ b/src/main/java/com/example/springip/service/OperationService.java @@ -13,64 +13,35 @@ public class OperationService { this.applicationContext = applicationContext; } - public Object sum(String o, String o2){ - String type = ""; - try { - Integer.parseInt(o); - Integer.parseInt(o2); - type = "num"; - } catch (Exception ex) { - type = "str"; - } + public Object sum(Object o, Object o2, String type){ + OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type); if (type.equals("num")) - return operationInterface.sum(Integer.parseInt(o),Integer.parseInt(o2)); + return operationInterface.sum(Integer.parseInt(o.toString()),Integer.parseInt(o2.toString())); return operationInterface.sum(o, o2); } - public Object mul(String o, Integer o2){ - String type = ""; + public Object mul(Object o, Object o2, String type){ - try { - Integer.parseInt(o); - type = "num"; - } catch (Exception ex) { - type = "str"; - } OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type); if (type.equals("num")) - return operationInterface.mul(Integer.parseInt(o), o2); - return operationInterface.mul(o, o2); + return operationInterface.mul(Integer.parseInt(o.toString()), Integer.parseInt(o2.toString())); + return operationInterface.mul(o, Integer.parseInt(o2.toString())); } - public Object length(String o){ - String type = ""; + public Object length(Object o, String type){ - try { - Integer.parseInt(o); - type = "num"; - } catch (Exception ex) { - type = "str"; - } OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type); if (type.equals("num")) - return operationInterface.length(Integer.parseInt(o)); + return operationInterface.length(Integer.parseInt(o.toString())); return operationInterface.length(o); } - public Object startsWith(String o, String o2){ - String type = ""; + public Object startsWith(Object o, Object o2, String type){ - try { - Integer.parseInt(o); - Integer.parseInt(o2); - type = "num"; - } catch (Exception ex) { - type = "str"; - } OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type); if (type.equals("num")) - return operationInterface.startsWith(Integer.parseInt(o), Integer.parseInt(o2)); + return operationInterface.startsWith(Integer.parseInt(o.toString()), Integer.parseInt(o2.toString())); return operationInterface.startsWith(o,o2); } diff --git a/src/test/java/com/example/springip/SpringipApplicationTests.java b/src/test/java/com/example/springip/SpringipApplicationTests.java index e63058c..f2f78d1 100644 --- a/src/test/java/com/example/springip/SpringipApplicationTests.java +++ b/src/test/java/com/example/springip/SpringipApplicationTests.java @@ -13,48 +13,48 @@ class SpringipApplicationTests { @Test public void testSumString() { - final String res = (String) operationService.sum("Привет ", "Мир!"); + final String res = (String) operationService.sum("Привет ", "Мир!", "str"); Assertions.assertEquals("Привет Мир!", res); } @Test public void testSumNum() { - final Integer res = (Integer) operationService.sum("1", "2"); + final Integer res = (Integer) operationService.sum(1, 2, "num"); Assertions.assertEquals(3, res); } @Test public void testMulString() { - final String res = (String) operationService.mul("A", 4); + final String res = (String) operationService.mul("A", 4, "str"); Assertions.assertEquals("AAAA", res); } @Test public void testMulNum() { - final Integer res = (Integer) operationService.mul("2", 4); + final Integer res = (Integer) operationService.mul(2, 4, "num"); Assertions.assertEquals(8, res); } @Test public void testLengthString() { - final Integer res = (Integer) operationService.length("Hello"); + final Integer res = (Integer) operationService.length("Hello", "str"); Assertions.assertEquals(5, res); } @Test public void testLengthNum() { - final Integer res = (Integer) operationService.length("777"); + final Integer res = (Integer) operationService.length(777,"num"); Assertions.assertEquals(3, res); } @Test public void testStWString() { - final boolean res = (boolean) operationService.startsWith("Orange","Or"); + final boolean res = (boolean) operationService.startsWith("Orange","Or", "str"); Assertions.assertEquals(true, res); } @Test public void testStWNum() { - final boolean res = (boolean) operationService.startsWith("777","77"); + final boolean res = (boolean) operationService.startsWith(777, 77, "num"); Assertions.assertEquals(true, res); }