From d80f64a86e62282c0966db044033a9f05b26db0f Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Sat, 11 Mar 2023 20:38:01 +0400 Subject: [PATCH] LabWork_02 done --- frontend/index.html | 32 +++++--- frontend/script.js | 30 +++----- .../java/ru/IP_LabWorks/IP/IpApplication.java | 3 - .../IP/configuration/TypeConfiguration.java | 25 ++++++ .../IP/controllers/MainController.java | 42 ++++++---- .../IP_LabWorks/IP/domain/ITypeInterface.java | 8 ++ .../ru/IP_LabWorks/IP/domain/TypeDouble.java | 24 ++++++ .../ru/IP_LabWorks/IP/domain/TypeInt.java | 24 ++++++ .../ru/IP_LabWorks/IP/domain/TypeString.java | 24 ++++++ .../IP_LabWorks/IP/service/TypeService.java | 68 +++++++++++++++++ .../ru/IP_LabWorks/IP/IpApplicationTests.java | 76 +++++++++++++++++++ 11 files changed, 309 insertions(+), 47 deletions(-) create mode 100644 src/main/java/ru/IP_LabWorks/IP/configuration/TypeConfiguration.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/domain/ITypeInterface.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/domain/TypeDouble.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/domain/TypeInt.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/domain/TypeString.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/service/TypeService.java diff --git a/frontend/index.html b/frontend/index.html index d7e5dcf..93614f3 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -14,31 +14,41 @@
-

Calculator

+

LabWork2

- - + +
- - + +
- + +
+
+
+
+ +
@@ -47,7 +57,7 @@
- +
diff --git a/frontend/script.js b/frontend/script.js index ce82cf6..1cc88ef 100644 --- a/frontend/script.js +++ b/frontend/script.js @@ -1,26 +1,16 @@ 'use strict' -let num1 = document.getElementById("input1") -let num2 = document.getElementById("input2") -let operator = document.getElementById("operator") -let result = document.getElementById("result") +async function calculate(){ + let num1 = document.getElementById("input1").value + let num2 = document.getElementById("input2").value + let operator = document.getElementById("operator").value + let result = document.getElementById("result") + let type = document.getElementById("Type").value -function FindOperator(operat){ - let OperatorVal = operat - switch(OperatorVal){ - case "+": return "add" - case "-": return "subtract" - case "/": return "divide" - case "*": return "multiply" - } + let response = await fetch(`http://localhost:8080/${operator}?Type=${type}&value1=${num1}&value2=${num2}`) + let res = await response.text() + result.value = res } -function calculate(){ - let number1 = num1.value - let number2 = num2.value - let oper = FindOperator(operator.value) - fetch(`http://localhost:8080/${oper}/${number1}/${number2}`) - .then(response => response.text()) - .then(data => {result.value = data}) -} \ No newline at end of file + diff --git a/src/main/java/ru/IP_LabWorks/IP/IpApplication.java b/src/main/java/ru/IP_LabWorks/IP/IpApplication.java index ed2eea2..6943810 100644 --- a/src/main/java/ru/IP_LabWorks/IP/IpApplication.java +++ b/src/main/java/ru/IP_LabWorks/IP/IpApplication.java @@ -13,7 +13,4 @@ public class IpApplication { public static void main(String[] args) { SpringApplication.run(IpApplication.class, args); } - - - } diff --git a/src/main/java/ru/IP_LabWorks/IP/configuration/TypeConfiguration.java b/src/main/java/ru/IP_LabWorks/IP/configuration/TypeConfiguration.java new file mode 100644 index 0000000..d35aaea --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/configuration/TypeConfiguration.java @@ -0,0 +1,25 @@ +package ru.IP_LabWorks.IP.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import ru.IP_LabWorks.IP.domain.TypeDouble; +import ru.IP_LabWorks.IP.domain.TypeInt; +import ru.IP_LabWorks.IP.domain.TypeString; + +@Configuration +public class TypeConfiguration { + @Bean(value = "int") + public TypeInt createIntType(){ + return new TypeInt(); + } + + @Bean(value = "str") + public TypeString createStrType(){ + return new TypeString(); + } + + @Bean(value = "double") + public TypeDouble createDoubleType(){ + return new TypeDouble(); + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/controllers/MainController.java b/src/main/java/ru/IP_LabWorks/IP/controllers/MainController.java index 0474872..4b94c54 100644 --- a/src/main/java/ru/IP_LabWorks/IP/controllers/MainController.java +++ b/src/main/java/ru/IP_LabWorks/IP/controllers/MainController.java @@ -1,29 +1,45 @@ package ru.IP_LabWorks.IP.controllers; +import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import ru.IP_LabWorks.IP.domain.ITypeInterface; +import ru.IP_LabWorks.IP.service.TypeService; @RestController public class MainController { - @GetMapping("/add/{num1}/{num2}") - public int add(@PathVariable int num1, @PathVariable int num2) { - return num1 + num2; + private final TypeService Service; + + public MainController(TypeService service) { + Service = service; } - @GetMapping("/subtract/{num1}/{num2}") - public int subtract(@PathVariable int num1, @PathVariable int num2) { - return num1 - num2; + @GetMapping("/Method1") + public Object Method1(@RequestParam(value = "Type") String Type, + @RequestParam(value = "value1") Object value1, + @RequestParam(value = "value2") Object value2){ + return Service.Method1(value1,value2,Type); } - @GetMapping("/multiply/{num1}/{num2}") - public int multiply(@PathVariable int num1, @PathVariable int num2) { - return num1 * num2; + @GetMapping("/Method2") + public Object Method2(@RequestParam(value = "Type") String Type, + @RequestParam(value = "value1") Object value1, + @RequestParam(value = "value2") Object value2){ + return Service.Method2(value1,value2,Type); } - @GetMapping("/divide/{num1}/{num2}") - public int divide(@PathVariable int num1, @PathVariable int num2) { - return num1 / num2; + @GetMapping("/Method3") + public Object Method3(@RequestParam(value = "Type") String Type, + @RequestParam(value = "value1") Object value1, + @RequestParam(value = "value2") Object value2){ + return Service.Method3(value1,value2,Type); + } + + @GetMapping("/Method4") + public Object Method4(@RequestParam(value = "Type") String Type, + @RequestParam(value = "value1") Object value1, + @RequestParam(value = "value2") Object value2){ + return Service.Method4(value1,value2,Type); } } diff --git a/src/main/java/ru/IP_LabWorks/IP/domain/ITypeInterface.java b/src/main/java/ru/IP_LabWorks/IP/domain/ITypeInterface.java new file mode 100644 index 0000000..a85610a --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/domain/ITypeInterface.java @@ -0,0 +1,8 @@ +package ru.IP_LabWorks.IP.domain; + +public interface ITypeInterface { + T Method1(T value1, T value2); + T Method2(T value1, T value2); + T Method3(T value1, T value2); + T Method4(T value1, T value2); +} diff --git a/src/main/java/ru/IP_LabWorks/IP/domain/TypeDouble.java b/src/main/java/ru/IP_LabWorks/IP/domain/TypeDouble.java new file mode 100644 index 0000000..ab0e64b --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/domain/TypeDouble.java @@ -0,0 +1,24 @@ +package ru.IP_LabWorks.IP.domain; + +public class TypeDouble implements ITypeInterface { + + @Override + public Double Method1(Double value1, Double value2) { + return value1 + value2; + } + + @Override + public Double Method2(Double value1, Double value2) { + return value1 - value2; + } + + @Override + public Double Method3(Double value1, Double value2) { + return value1 * value2; + } + + @Override + public Double Method4(Double value1, Double value2) { + return value1 / value2; + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/domain/TypeInt.java b/src/main/java/ru/IP_LabWorks/IP/domain/TypeInt.java new file mode 100644 index 0000000..18b5891 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/domain/TypeInt.java @@ -0,0 +1,24 @@ +package ru.IP_LabWorks.IP.domain; + + +public class TypeInt implements ITypeInterface { + @Override + public Integer Method1(Integer value1, Integer value2) { + return value1 + value2; + } + + @Override + public Integer Method2(Integer value1, Integer value2) { + return value1 - value2; + } + + @Override + public Integer Method3(Integer value1, Integer value2) { + return value1 * value2; + } + + @Override + public Integer Method4(Integer value1, Integer value2) { + return value1 / value2; + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/domain/TypeString.java b/src/main/java/ru/IP_LabWorks/IP/domain/TypeString.java new file mode 100644 index 0000000..5ff82d1 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/domain/TypeString.java @@ -0,0 +1,24 @@ +package ru.IP_LabWorks.IP.domain; + +public class TypeString implements ITypeInterface { + + @Override + public String Method1(String value1, String value2) { + return value1 + value2; + } + + @Override + public String Method2(String value1, String value2) { + return value1.toUpperCase() + value2.toUpperCase(); + } + + @Override + public String Method3(String value1, String value2) { + return value1.toLowerCase() + value2.toLowerCase(); + } + + @Override + public String Method4(String value1, String value2) { + return value1.toLowerCase() + value2.toUpperCase(); + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/service/TypeService.java b/src/main/java/ru/IP_LabWorks/IP/service/TypeService.java new file mode 100644 index 0000000..2094e71 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/service/TypeService.java @@ -0,0 +1,68 @@ +package ru.IP_LabWorks.IP.service; + +import ru.IP_LabWorks.IP.domain.ITypeInterface; +import org.springframework.stereotype.Service; +import org.springframework.context.ApplicationContext; + +@Service +public class TypeService { + + private final ApplicationContext applicationContext; + private ITypeInterface _type; + private Object _value1; + private Object _value2; + + public TypeService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + private void ValidateParams(Object value1, Object value2, String type){ + _type = (ITypeInterface)applicationContext.getBean(type); + + switch (type) { + case "int" -> { + try { + _value1 = Integer.valueOf(value1.toString()); + _value2 = Integer.valueOf(value2.toString()); + }catch (Exception ex){ + _value1 = 0; + _value2 = 0; + } + } + case "double" -> { + try { + _value1 = Double.valueOf(value1.toString()); + _value2 = Double.valueOf(value2.toString()); + }catch (Exception ex){ + _value1 = 0.0; + _value2 = 0.0; + } + } + case "str" -> { + _value1 = value1; + _value2 = value2; + } + } + + } + + public Object Method1(Object value1, Object value2, String type){ + ValidateParams(value1,value2,type); + return String.format("%s", _type.Method1(_value1,_value2)); + } + + public Object Method2(Object value1, Object value2, String type){ + ValidateParams(value1,value2,type); + return String.format("%s", _type.Method2(_value1,_value2)); + } + + public Object Method3(Object value1, Object value2, String type){ + ValidateParams(value1,value2,type); + return String.format("%s", _type.Method3(_value1,_value2)); + } + + public Object Method4(Object value1, Object value2, String type){ + ValidateParams(value1,value2,type); + return String.format("%s", _type.Method4(_value1,_value2)); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/IpApplicationTests.java b/src/test/java/ru/IP_LabWorks/IP/IpApplicationTests.java index afcbb67..9d9a77b 100644 --- a/src/test/java/ru/IP_LabWorks/IP/IpApplicationTests.java +++ b/src/test/java/ru/IP_LabWorks/IP/IpApplicationTests.java @@ -1,13 +1,89 @@ package ru.IP_LabWorks.IP; +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.IP_LabWorks.IP.service.TypeService; @SpringBootTest class IpApplicationTests { + @Autowired + TypeService Service; @Test void contextLoads() { } + @Test + void testIntPlus() { + final String res = (String) Service.Method1( "100", "10", "int"); + Assertions.assertEquals("110", res); + } + + @Test + void testIntMinus() { + final String res = (String)Service.Method2( "100", "10", "int"); + Assertions.assertEquals("90", res); + } + + @Test + void testIntMultiply() { + final String res = (String)Service.Method3( "100", "10", "int"); + Assertions.assertEquals("1000", res); + } + + @Test + void testIntDivision() { + final String res = (String)Service.Method4( "100", "10", "int"); + Assertions.assertEquals("10", res); + } + + @Test + void testStringPlus() { + final String res = (String)Service.Method1( "abc", "dfe", "str"); + Assertions.assertEquals("abcdfe", res); + } + + @Test + void testStringtoUpper() { + final String res = (String)Service.Method2( "abc", "dfe", "str"); + Assertions.assertEquals("ABCDFE", res); + } + + @Test + void testStringtoLower() { + final String res = (String)Service.Method3( "abc", "dfe", "str"); + Assertions.assertEquals("abcdfe", res); + } + + @Test + void testStringStrange() { + final String res = (String)Service.Method4( "abc", "dfe", "str"); + Assertions.assertEquals("abcDFE", res); + } + + @Test + void testDoublePlus() { + final String res = (String)Service.Method1( "1.01", "2.76", "double"); + Assertions.assertEquals("3.7699999999999996", res); + } + + @Test + void testDoubleMinus() { + final String res = (String)Service.Method2( "1.01", "2.76", "double"); + Assertions.assertEquals("-1.7499999999999998", res); + } + + @Test + void testDoubleMyltiply() { + final String res = (String)Service.Method3( "1.01", "2.76", "double"); + Assertions.assertEquals("2.7876", res); + } + + @Test + void testDoubleDivision() { + final String res = (String)Service.Method4( "1.01", "2.76", "double"); + Assertions.assertEquals("0.36594202898550726", res); + } }