From eada9f3c88a0c1578afd5b3b64955ee7dc48c3e3 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Tue, 7 Mar 2023 00:21:23 +0400 Subject: [PATCH] =?UTF-8?q?LabWork02:=20=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/index.html | 66 ++++++++++++++----- .../ulstu/is/labwork/LabworkApplication.java | 23 +++++-- .../CalculatorConfiguration.java | 20 ++++++ .../controller/CalculatorController.java | 46 +++++++++++++ .../labwork/calculator/domain/Calculator.java | 11 ++++ .../calculator/domain/CalculatorInt.java | 36 ++++++++++ .../calculator/domain/CalculatorString.java | 23 +++++++ .../calculator/service/CalculatorService.java | 56 ++++++++++++++++ .../is/labwork/LabworkApplicationTests.java | 58 +++++++++++++++- 9 files changed, 316 insertions(+), 23 deletions(-) create mode 100644 src/main/java/ru/ulstu/is/labwork/calculator/configuration/CalculatorConfiguration.java create mode 100644 src/main/java/ru/ulstu/is/labwork/calculator/controller/CalculatorController.java create mode 100644 src/main/java/ru/ulstu/is/labwork/calculator/domain/Calculator.java create mode 100644 src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorInt.java create mode 100644 src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorString.java create mode 100644 src/main/java/ru/ulstu/is/labwork/calculator/service/CalculatorService.java diff --git a/frontend/index.html b/frontend/index.html index 066f24c..52c7d04 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -3,27 +3,61 @@ Calc - + - - - - - -

+
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ + +
diff --git a/src/main/java/ru/ulstu/is/labwork/LabworkApplication.java b/src/main/java/ru/ulstu/is/labwork/LabworkApplication.java index 65fff25..737caf9 100644 --- a/src/main/java/ru/ulstu/is/labwork/LabworkApplication.java +++ b/src/main/java/ru/ulstu/is/labwork/LabworkApplication.java @@ -3,21 +3,32 @@ package ru.ulstu.is.labwork; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController +//@RequestMapping("/lab1") public class LabworkApplication { + //инимум 4 операции разные интерфейс с дженерик использ сервис + //2 3 реализации с разными данными + //писать контроллеры и делать интерфейс + //озвращ свой тип данных обджект + // написать набор тестов для кажд метода 12 тестов + + //создать интерфейс 4 метода реализ какие то операц над данными + //написать 2-3 реализации + //каждая соответственно делает то что мы хотим с данными (реализ строка число) + //потом пишем сервис (можно из примера) из поиска берем нужную реализацию и делаем действия + //пишем констролер чтоб извне... + //пишем интерфес на жабе + //и написать тесты для каждого метода чтобы все проверить public static void main(String[] args) { SpringApplication.run(LabworkApplication.class, args); } - @GetMapping("/good_morning") - public String good_morning(@RequestParam(value = "name", defaultValue = "Sir") String name) { - return String.format("Good morning, %s!", name); - } - @GetMapping("/sum") + /*@GetMapping("/sum") public int sum( @RequestParam(value = "num1", defaultValue = "0") int num1, @RequestParam(value = "num2", defaultValue = "0") int num2) { @@ -40,5 +51,5 @@ public class LabworkApplication { @RequestParam(value = "num2", defaultValue = "0") int num2) { return num1 / num2; - } + }*/ } diff --git a/src/main/java/ru/ulstu/is/labwork/calculator/configuration/CalculatorConfiguration.java b/src/main/java/ru/ulstu/is/labwork/calculator/configuration/CalculatorConfiguration.java new file mode 100644 index 0000000..13356a2 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/calculator/configuration/CalculatorConfiguration.java @@ -0,0 +1,20 @@ +package ru.ulstu.is.labwork.calculator.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import ru.ulstu.is.labwork.calculator.domain.Calculator; +import ru.ulstu.is.labwork.calculator.domain.CalculatorInt; +import ru.ulstu.is.labwork.calculator.domain.CalculatorString; + +@Configuration +public class CalculatorConfiguration { + @Bean(value = "inttype") + public CalculatorInt createIntCalculator(){ + return new CalculatorInt(); + } + + @Bean(value = "stringtype") + public CalculatorString createStringCalculator(){ + return new CalculatorString(); + } +} diff --git a/src/main/java/ru/ulstu/is/labwork/calculator/controller/CalculatorController.java b/src/main/java/ru/ulstu/is/labwork/calculator/controller/CalculatorController.java new file mode 100644 index 0000000..89b7903 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/calculator/controller/CalculatorController.java @@ -0,0 +1,46 @@ +package ru.ulstu.is.labwork.calculator.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.is.labwork.calculator.service.CalculatorService; + +@RestController +//@RequestMapping("/lab2") +public class CalculatorController { + private final CalculatorService calculatorService; + + public CalculatorController(CalculatorService calculatorService){ + this.calculatorService = calculatorService; + } + + @GetMapping("/sum") + public String sum( @RequestParam(value = "val1", defaultValue = "0") Object val1, + @RequestParam(value = "val2", defaultValue = "0") Object val2, + @RequestParam(value = "type", defaultValue = "inttype") String type) + { + return calculatorService.sum(val1, val2, type).toString(); + } + @GetMapping("/sub") + public String sub( @RequestParam(value = "val1", defaultValue = "0") Object val1, + @RequestParam(value = "val2", defaultValue = "0") Object val2, + @RequestParam(value = "type", defaultValue = "inttype") String type) + { + return calculatorService.sub(val1, val2, type).toString(); + } + @GetMapping("/mul") + public String mul( @RequestParam(value = "val1", defaultValue = "0") Object val1, + @RequestParam(value = "val2", defaultValue = "0") Object val2, + @RequestParam(value = "type", defaultValue = "inttype") String type) + { + return calculatorService.mul(val1, val2, type).toString(); + } + @GetMapping("/div") + public String div( @RequestParam(value = "val1", defaultValue = "0") Object val1, + @RequestParam(value = "val2", defaultValue = "0") Object val2, + @RequestParam(value = "type", defaultValue = "inttype") String type) + { + return calculatorService.div(val1, val2, type).toString(); + } +} diff --git a/src/main/java/ru/ulstu/is/labwork/calculator/domain/Calculator.java b/src/main/java/ru/ulstu/is/labwork/calculator/domain/Calculator.java new file mode 100644 index 0000000..ab6c950 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/calculator/domain/Calculator.java @@ -0,0 +1,11 @@ +package ru.ulstu.is.labwork.calculator.domain; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +public interface Calculator { + Object sum(Object val1, Object val2); + Object sub(Object val1, Object val2); + Object mul(Object val1, Object val2); + Object div(Object val1, Object val2); +} diff --git a/src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorInt.java b/src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorInt.java new file mode 100644 index 0000000..ff1f151 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorInt.java @@ -0,0 +1,36 @@ +package ru.ulstu.is.labwork.calculator.domain; + +public class CalculatorInt implements Calculator{ + + @Override + public Object sum(Object val1, Object val2) { + if (val1 instanceof Integer && val2 instanceof Integer){ + return (Integer)val1 + (Integer)val2; + } + return 0; + } + + @Override + public Object sub(Object val1, Object val2) { + if (val1 instanceof Integer && val2 instanceof Integer){ + return (Integer)val1 - (Integer)val2; + } + return 0; + } + + @Override + public Object mul(Object val1, Object val2) { + if (val1 instanceof Integer && val2 instanceof Integer){ + return (Integer)val1 * (Integer)val2; + } + return 0; + } + + @Override + public Object div(Object val1, Object val2) { + if (val1 instanceof Integer && val2 instanceof Integer){ + return (Integer)val1 / (Integer)val2; + } + return 0; + } +} diff --git a/src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorString.java b/src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorString.java new file mode 100644 index 0000000..1e30e02 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/calculator/domain/CalculatorString.java @@ -0,0 +1,23 @@ +package ru.ulstu.is.labwork.calculator.domain; + +public class CalculatorString implements Calculator{ + @Override + public Object sum(Object val1, Object val2) { + return (String)val1 + '+' + (String)val2; + } + + @Override + public Object sub(Object val1, Object val2) { + return (String)val1 + '-' + (String)val2; + } + + @Override + public Object mul(Object val1, Object val2) { + return (String)val1 + '*' + (String)val2; + } + + @Override + public Object div(Object val1, Object val2) { + return (String)val1 + '/' + (String)val2; + } +} diff --git a/src/main/java/ru/ulstu/is/labwork/calculator/service/CalculatorService.java b/src/main/java/ru/ulstu/is/labwork/calculator/service/CalculatorService.java new file mode 100644 index 0000000..739322b --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/calculator/service/CalculatorService.java @@ -0,0 +1,56 @@ +package ru.ulstu.is.labwork.calculator.service; + +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; +import ru.ulstu.is.labwork.calculator.domain.Calculator; + +@Service +public class CalculatorService { + private final ApplicationContext applicationContext; + + public CalculatorService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public Object sum(Object val1, Object val2, String type){ + final Calculator calculator = (Calculator) applicationContext.getBean(type); + if (type.compareTo("inttype") == 0){ + return calculator.sum(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.compareTo("stringtype") == 0) { + return String.format("%s", calculator.sum(val1.toString(), val2.toString())); + } + return null; + } + + public Object sub(Object val1, Object val2, String type){ + final Calculator calculator = (Calculator) applicationContext.getBean(type); + if (type.compareTo("inttype") == 0){ + return calculator.sub(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.compareTo("stringtype") == 0) { + return String.format("%s", calculator.sub(val1.toString(), val2.toString())); + } + return null; + } + public Object mul(Object val1, Object val2, String type){ + final Calculator calculator = (Calculator) applicationContext.getBean(type); + if (type.compareTo("inttype") == 0){ + return calculator.mul(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.compareTo("stringtype") == 0) { + return String.format("%s", calculator.mul(val1.toString(), val2.toString())); + } + return null; + } + public Object div(Object val1, Object val2, String type){ + final Calculator calculator = (Calculator) applicationContext.getBean(type); + if (type.compareTo("inttype") == 0){ + return calculator.div(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString())); + } + else if (type.compareTo("stringtype") == 0) { + return String.format("%s", calculator.div(val1.toString(), val2.toString())); + } + return null; + } +} diff --git a/src/test/java/ru/ulstu/is/labwork/LabworkApplicationTests.java b/src/test/java/ru/ulstu/is/labwork/LabworkApplicationTests.java index e998452..c2a4ec7 100644 --- a/src/test/java/ru/ulstu/is/labwork/LabworkApplicationTests.java +++ b/src/test/java/ru/ulstu/is/labwork/LabworkApplicationTests.java @@ -1,13 +1,69 @@ package ru.ulstu.is.labwork; +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; +import ru.ulstu.is.labwork.calculator.service.CalculatorService; @SpringBootTest class LabworkApplicationTests { + @Autowired + CalculatorService calculatorService; + @Test - void contextLoads() { + void stringSum() { + Assertions.assertEquals("Hello+World!", calculatorService.sum("Hello", "World!", "stringtype")); + Assertions.assertEquals("+Hello, World!", calculatorService.sum("", "Hello, World!", "stringtype")); + } + + @Test + void stringSub() { + Assertions.assertEquals("Крокодил-Гена", calculatorService.sub("Крокодил", "Гена", "stringtype")); + Assertions.assertEquals("Атомное-сердце", calculatorService.sub("Атомное", "сердце", "stringtype")); + Assertions.assertEquals("Почему-Жак Фреско", calculatorService.sub("Почему", "Жак Фреско", "stringtype")); + } + + @Test + void stringMul() { + Assertions.assertEquals("Это*Звездочка", calculatorService.mul("Это", "Звездочка", "stringtype")); + Assertions.assertEquals("п*чему", calculatorService.mul("п", "чему", "stringtype")); + Assertions.assertEquals("Звездочкой * обозначают умножение", calculatorService.mul("Звездочкой ", " обозначают умножение", "stringtype")); + } + + @Test + void stringDiv() { + Assertions.assertEquals("б/п", calculatorService.div("б", "п", "stringtype")); + Assertions.assertEquals("/1", calculatorService.div("", "1", "stringtype")); + Assertions.assertEquals("-/-", calculatorService.div("-", "-", "stringtype")); + } + + @Test + void intSum() { + Assertions.assertEquals(22, calculatorService.sum(11, 11, "inttype")); + Assertions.assertEquals(-10, calculatorService.sum(15, -25, "inttype")); + } + + @Test + void intSub() { + Assertions.assertEquals(0, calculatorService.sub(10, 10, "inttype")); + Assertions.assertEquals(100, calculatorService.sub(100, 0, "inttype")); + Assertions.assertEquals(3, calculatorService.sub(-3, -6, "inttype")); + } + + @Test + void intMul() { + Assertions.assertEquals(0, calculatorService.mul(0, 10, "inttype")); + Assertions.assertEquals(18, calculatorService.mul(6, 3, "inttype")); + Assertions.assertEquals(-4, calculatorService.mul(-1, 4, "inttype")); + } + + @Test + void intDiv() { + Assertions.assertEquals(-1, calculatorService.div(-1, 1, "inttype")); + Assertions.assertEquals(2, calculatorService.div(100, 50, "inttype")); + Assertions.assertEquals(8, calculatorService.div(64, 8, "inttype")); } }