From 2138819860e1ff0774fabd9638e3f0e7481e70fa Mon Sep 17 00:00:00 2001 From: dasha Date: Mon, 27 Feb 2023 17:53:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/labwork1/app/AppApplication.java | 4 +- .../app/calc/controller/CalcController.java | 44 ++++++++++++++++ .../com/labwork1/app/calc/domain/CalcNum.java | 46 ++++++++++++++++ .../com/labwork1/app/calc/domain/CalcStr.java | 46 ++++++++++++++++ .../com/labwork1/app/calc/domain/ICalc.java | 11 ++++ .../app/calc/service/CalcService.java | 52 +++++++++++++++++++ .../com/labwork1/app/AppApplicationTests.java | 45 +++++++++++++++- 7 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/labwork1/app/calc/controller/CalcController.java create mode 100644 src/main/java/com/labwork1/app/calc/domain/CalcNum.java create mode 100644 src/main/java/com/labwork1/app/calc/domain/CalcStr.java create mode 100644 src/main/java/com/labwork1/app/calc/domain/ICalc.java create mode 100644 src/main/java/com/labwork1/app/calc/service/CalcService.java diff --git a/src/main/java/com/labwork1/app/AppApplication.java b/src/main/java/com/labwork1/app/AppApplication.java index 2203f45..d5d2762 100644 --- a/src/main/java/com/labwork1/app/AppApplication.java +++ b/src/main/java/com/labwork1/app/AppApplication.java @@ -12,7 +12,7 @@ public class AppApplication { public static void main(String[] args) { SpringApplication.run(AppApplication.class, args); } - @GetMapping("/hello") + /*@GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); } @@ -39,5 +39,5 @@ public class AppApplication { return null; } return Double.toString(first/second); - } + }*/ } diff --git a/src/main/java/com/labwork1/app/calc/controller/CalcController.java b/src/main/java/com/labwork1/app/calc/controller/CalcController.java new file mode 100644 index 0000000..0ad5ae4 --- /dev/null +++ b/src/main/java/com/labwork1/app/calc/controller/CalcController.java @@ -0,0 +1,44 @@ +package com.labwork1.app.calc.controller; + +import com.labwork1.app.calc.service.CalcService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CalcController { + private final CalcService calcService; + + public CalcController(CalcService calcService) { + this.calcService = calcService; + } + + @GetMapping("/plus") + public String plus(@RequestParam(required = false, defaultValue = "0") Object first, + @RequestParam(required = false, defaultValue = "0") Object second, + @RequestParam(required = false, defaultValue = "num") String type) { + return calcService.plus((Object) first, (Object) second, type); + } + @GetMapping("/minus") + public String minus(@RequestParam(required = false, defaultValue = "0") Object first, + @RequestParam(required = false, defaultValue = "0") Object second, + @RequestParam(required = false, defaultValue = "num") String type) { + return calcService.minus((Object) first, (Object) second, type); + } + @GetMapping("/mult") + public String mult(@RequestParam(required = false, defaultValue = "1") Object first, + @RequestParam(required = false, defaultValue = "1") Object second, + @RequestParam(required = false, defaultValue = "num") String type) { + return calcService.mult((Object) first, (Object) second, type); + } + @GetMapping("/div") + public String div(@RequestParam(required = false, defaultValue = "1") Object first, + @RequestParam(required = false, defaultValue = "1") Object second, + @RequestParam(required = false, defaultValue = "num") String type) { + if(Integer.parseInt(second.toString()) == 0) + { + return null; + } + return calcService.div((Object) first, (Object) second, type); + } +} diff --git a/src/main/java/com/labwork1/app/calc/domain/CalcNum.java b/src/main/java/com/labwork1/app/calc/domain/CalcNum.java new file mode 100644 index 0000000..b7b90ac --- /dev/null +++ b/src/main/java/com/labwork1/app/calc/domain/CalcNum.java @@ -0,0 +1,46 @@ +package com.labwork1.app.calc.domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +@Component(value = "num") +public class CalcNum implements ICalc, InitializingBean, DisposableBean { + private final Logger log = LoggerFactory.getLogger(CalcNum.class); + + @Override + public void afterPropertiesSet() { + log.info("CalcNum.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("CalcNum.destroy()"); + } + + @Override + public Integer Plus(Integer first, Integer second) { + return first + second; + } + + @Override + public Integer Mult(Integer first, Integer second) { + return first * second; + } + + @Override + public Integer Minus(Integer first, Integer second) { + return first - second; + } + + @Override + public Integer Div(Integer first, Integer second) { + if (second == 0){ + return null; + }else{ + return first / second; + } + } +} diff --git a/src/main/java/com/labwork1/app/calc/domain/CalcStr.java b/src/main/java/com/labwork1/app/calc/domain/CalcStr.java new file mode 100644 index 0000000..ac5aa4a --- /dev/null +++ b/src/main/java/com/labwork1/app/calc/domain/CalcStr.java @@ -0,0 +1,46 @@ +package com.labwork1.app.calc.domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +@Component(value = "str") +public class CalcStr implements ICalc, InitializingBean, DisposableBean { + private final Logger log = LoggerFactory.getLogger(CalcStr.class); + + @Override + public void afterPropertiesSet() { + log.info("CalcStr.afterPropertiesSet()"); + } + + @Override + public void destroy() { + log.info("CalcStr.destroy()"); + } + + @Override + public String Plus(String first, String second) { + return first.concat(second); + } + + @Override + public String Mult(String first, String second) { + String temp = first; + for (int i = 0; i < Integer.parseInt(second) - 1; i++) { + temp = Plus(temp, first); + } + return temp; + } + + @Override + public String Minus(String first, String second) { + return first.replaceFirst(second, ""); + } + + @Override + public String Div(String first, String second) { + return first.replaceAll(second, ""); + } +} diff --git a/src/main/java/com/labwork1/app/calc/domain/ICalc.java b/src/main/java/com/labwork1/app/calc/domain/ICalc.java new file mode 100644 index 0000000..a839a8d --- /dev/null +++ b/src/main/java/com/labwork1/app/calc/domain/ICalc.java @@ -0,0 +1,11 @@ +package com.labwork1.app.calc.domain; + +public interface ICalc { + T Plus(T first, T second); + + T Mult(T first, T second); + + T Minus(T first, T second); + + T Div(T first, T second); +} diff --git a/src/main/java/com/labwork1/app/calc/service/CalcService.java b/src/main/java/com/labwork1/app/calc/service/CalcService.java new file mode 100644 index 0000000..c20295b --- /dev/null +++ b/src/main/java/com/labwork1/app/calc/service/CalcService.java @@ -0,0 +1,52 @@ +package com.labwork1.app.calc.service; + +import com.labwork1.app.calc.domain.CalcNum; +import com.labwork1.app.calc.domain.CalcStr; +import com.labwork1.app.calc.domain.ICalc; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +@Service +public class CalcService { + private final ApplicationContext applicationContext; + + public CalcService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public String plus(Object first, Object second, String type) { + final ICalc temp = (ICalc) applicationContext.getBean(type); + if (temp instanceof CalcStr) { + return String.format("%s", temp.Plus(first, second)); + }else{ + return String.format("%s", temp.Plus(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } + + public String minus(Object first, Object second, String type) { + final ICalc temp = (ICalc) applicationContext.getBean(type); + if (temp instanceof CalcStr) { + return String.format("%s", temp.Minus(first, second)); + }else{ + return String.format("%s", temp.Minus(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } + + public String mult(Object first, Object second, String type) { + final ICalc temp = (ICalc) applicationContext.getBean(type); + if (temp instanceof CalcStr) { + return String.format("%s", temp.Mult(first, second)); + }else{ + return String.format("%s", temp.Mult(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } + + public String div(Object first, Object second, String type) { + final ICalc temp = (ICalc) applicationContext.getBean(type); + if (temp instanceof CalcStr) { + return String.format("%s", temp.Div(first, second)); + }else{ + return String.format("%s", temp.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } +} diff --git a/src/test/java/com/labwork1/app/AppApplicationTests.java b/src/test/java/com/labwork1/app/AppApplicationTests.java index 0d5c808..357b11b 100644 --- a/src/test/java/com/labwork1/app/AppApplicationTests.java +++ b/src/test/java/com/labwork1/app/AppApplicationTests.java @@ -1,13 +1,56 @@ package com.labwork1.app; +import com.labwork1.app.calc.service.CalcService; +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 AppApplicationTests { + @Autowired + CalcService calcService; + @Test - void contextLoads() { + void testPlusNum() { + final String res = calcService.plus(10, 10, "num"); + Assertions.assertEquals(20, Integer.parseInt(res)); + } + @Test + void testMinusNum() { + final String res = calcService.minus(5, 2, "num"); + Assertions.assertEquals(3, Integer.parseInt(res)); + } + @Test + void testMultNum() { + final String res = calcService.mult(10, 10, "num"); + Assertions.assertEquals(100, Integer.parseInt(res)); + } + @Test + void testDivNum() { + final String res = calcService.div(20, 10, "num"); + Assertions.assertEquals(2, Integer.parseInt(res)); + } + @Test + void testPlusStr() { + final String res = calcService.plus("10", "10", "str"); + Assertions.assertEquals("1010", res); + } + @Test + void testMinusStr() { + final String res = calcService.minus("5252", "2", "str"); + Assertions.assertEquals("552", res); + } + @Test + void testMultStr() { + final String res = calcService.mult("5", "3", "str"); + Assertions.assertEquals("555", res); + } + @Test + void testDivStr() { + final String res = calcService.div("5252", "2", "str"); + Assertions.assertEquals("55", res); } }