From 2712dcbfb0e78b61878b308c84550577f461b571 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Mon, 27 Mar 2023 21:58:26 +0400 Subject: [PATCH] 2nd labwork --- backend/lab/build.gradle | 1 + .../AllTypeCalc/configuration/CalcConfig.java | 30 ++++++ .../controller/CalcController.java | 43 ++++++++ .../AllTypeCalc/domain/CalculationArr.java | 97 +++++++++++++++++++ .../AllTypeCalc/domain/CalculationInt.java | 33 +++++++ .../AllTypeCalc/domain/CalculationStr.java | 54 +++++++++++ .../lab/AllTypeCalc/domain/ICalculation.java | 8 ++ .../lab/AllTypeCalc/service/CalcService.java | 79 +++++++++++++++ .../java/com/example/lab/LabApplication.java | 65 +------------ .../com/example/lab/WebConfiguration.java | 13 +++ .../com/example/lab/LabApplicationTests.java | 56 ++++++++++- front/index.html | 21 ++-- front/scripts.js | 24 +++-- 13 files changed, 444 insertions(+), 80 deletions(-) create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/configuration/CalcConfig.java create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/controller/CalcController.java create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationArr.java create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationInt.java create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationStr.java create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/ICalculation.java create mode 100644 backend/lab/src/main/java/com/example/lab/AllTypeCalc/service/CalcService.java create mode 100644 backend/lab/src/main/java/com/example/lab/WebConfiguration.java diff --git a/backend/lab/build.gradle b/backend/lab/build.gradle index bd92341..c5a0cf0 100644 --- a/backend/lab/build.gradle +++ b/backend/lab/build.gradle @@ -14,6 +14,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.testng:testng:7.1.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/configuration/CalcConfig.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/configuration/CalcConfig.java new file mode 100644 index 0000000..fde910b --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/configuration/CalcConfig.java @@ -0,0 +1,30 @@ +package com.example.lab.AllTypeCalc.configuration; + +import com.example.lab.AllTypeCalc.domain.CalculationInt; +import com.example.lab.AllTypeCalc.domain.CalculationStr; +import com.example.lab.AllTypeCalc.domain.CalculationArr; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; + +public class CalcConfig { + private final Logger log = LoggerFactory.getLogger(com.example.lab.AllTypeCalc.configuration.CalcConfig.class); + + @Bean(value = "int") + public CalculationInt createCalculationInt(){ + log.info("Call createCalculationInt()"); + return new CalculationInt(); + } + + @Bean(value = "str") + public CalculationStr createCalculationStr() { + log.info("Call createCalculationStr()"); + return new CalculationStr(); + } + + @Bean (value = "arr") + public CalculationArr createCalculationArr() { + log.info("Call createCalculationArr()"); + return new CalculationArr(); + } +} diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/controller/CalcController.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/controller/CalcController.java new file mode 100644 index 0000000..e45280d --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/controller/CalcController.java @@ -0,0 +1,43 @@ +package com.example.lab.AllTypeCalc.controller; + +import com.example.lab.AllTypeCalc.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("/GetSum") + public Object getSum(@RequestParam(value = "obj1", defaultValue = "null") Object obj1, + @RequestParam(value = "obj2", defaultValue = "null") Object obj2, + @RequestParam(value = "typeInp", defaultValue = "str") String typeInp) { + return calcService.GetSum(obj1, obj2, typeInp); + } + + @GetMapping("/GetMinus") + public Object getMinus(@RequestParam(value = "obj1", defaultValue = "null") Object obj1, + @RequestParam(value = "obj2", defaultValue = "null") Object obj2, + @RequestParam(value = "typeInp", defaultValue = "str") String typeInp) { + return calcService.GetMinus(obj1, obj2, typeInp); + } + + @GetMapping("/GetMult") + public Object getMult(@RequestParam(value = "obj1", defaultValue = "null") Object obj1, + @RequestParam(value = "obj2", defaultValue = "null") Object obj2, + @RequestParam(value = "typeInp", defaultValue = "str") String typeInp) { + return calcService.GetMult(obj1, obj2, typeInp); + } + + @GetMapping("/GetDiv") + public Object getDiv(@RequestParam(value = "obj1", defaultValue = "null") Object obj1, + @RequestParam(value = "obj2", defaultValue = "null") Object obj2, + @RequestParam(value = "typeInp", defaultValue = "str") String typeInp) { + return calcService.GetDiv(obj1, obj2, typeInp); + } +} diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationArr.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationArr.java new file mode 100644 index 0000000..77daa59 --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationArr.java @@ -0,0 +1,97 @@ +package com.example.lab.AllTypeCalc.domain; +import com.example.lab.AllTypeCalc.domain.ICalculation; +import org.springframework.stereotype.Component; + +import java.util.Arrays; + +@Component(value = "arr") +public class CalculationArr implements ICalculation { + + @Override + public String[] GetSum(String[] firstInp, String[] secondInp) { + String[] resArr; + String[] longArr; + String[] shortArr; + if (secondInp.length > firstInp.length) { + longArr = secondInp.clone(); + shortArr = firstInp.clone(); + } else { + longArr = firstInp.clone(); + shortArr = secondInp.clone(); + } + resArr = new String[longArr.length]; + for (int i = 0; i < shortArr.length; i++) { + resArr[i] = Integer.toString(Integer.parseInt(longArr[i]) + Integer.parseInt(shortArr[i])); + } + for (int i = shortArr.length; i < longArr.length; i++) { + resArr[i] = longArr[i]; + } + return resArr; + } + + @Override + public String[] GetMinus(String[] firstInp, String[] secondInp) { + String[] resArr; + String[] longArr; + String[] shortArr; + if (secondInp.length > firstInp.length) { + longArr = secondInp.clone(); + shortArr = firstInp.clone(); + } else { + longArr = firstInp.clone(); + shortArr = secondInp.clone(); + } + resArr = new String[longArr.length]; + for (int i = 0; i < shortArr.length; i++) { + resArr[i] = Integer.toString(Integer.parseInt(longArr[i]) - Integer.parseInt(shortArr[i])); + } + for (int i = shortArr.length; i < longArr.length; i++) { + resArr[i] = longArr[i]; + } + return resArr; + } + + @Override + public String[] GetMult(String firstInp[], String secondInp[]) { + String[] resArr; + String[] longArr; + String[] shortArr; + if (secondInp.length > firstInp.length) { + longArr = secondInp.clone(); + shortArr = firstInp.clone(); + } else { + longArr = firstInp.clone(); + shortArr = secondInp.clone(); + } + resArr = new String[longArr.length]; + for (int i = 0; i < shortArr.length; i++) { + resArr[i] = Integer.toString(Integer.parseInt(longArr[i]) * Integer.parseInt(shortArr[i])); + } + for (int i = shortArr.length; i < longArr.length; i++) { + resArr[i] = longArr[i]; + } + return resArr; + } + + @Override + public String[] GetDiv(String[] firstInp, String[] secondInp) { + String[] resArr; + String[] longArr; + String[] shortArr; + if (secondInp.length > firstInp.length) { + longArr = secondInp.clone(); + shortArr = firstInp.clone(); + } else { + longArr = firstInp.clone(); + shortArr = secondInp.clone(); + } + resArr = new String[longArr.length]; + for (int i = 0; i < shortArr.length; i++) { + resArr[i] =Integer.toString(Integer.parseInt(longArr[i]) / Integer.parseInt(shortArr[i])); + } + for (int i = shortArr.length; i < longArr.length; i++) { + resArr[i] = longArr[i]; + } + return resArr; + } +} diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationInt.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationInt.java new file mode 100644 index 0000000..7dedebd --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationInt.java @@ -0,0 +1,33 @@ +package com.example.lab.AllTypeCalc.domain; + +import com.example.lab.AllTypeCalc.domain.ICalculation; +import org.springframework.stereotype.Component; + +@Component(value = "int") + +public class CalculationInt implements ICalculation { + @Override + public Integer GetSum(Integer firstInp, Integer secondInp) { + return firstInp+secondInp; + } + + @Override + public Integer GetMinus(Integer firstInp, Integer secondInp) { + return firstInp - secondInp; + } + + @Override + public Integer GetMult(Integer firstInp, Integer secondInp) { + return firstInp*secondInp; + } + + @Override + public Integer GetDiv(Integer firstInp, Integer secondInp) { + if (secondInp == 0){ + return 0; + } + else{ + return (firstInp/secondInp); + } + } +} diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationStr.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationStr.java new file mode 100644 index 0000000..d33cff1 --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/CalculationStr.java @@ -0,0 +1,54 @@ +package com.example.lab.AllTypeCalc.domain; + +import com.example.lab.AllTypeCalc.domain.ICalculation; +import org.springframework.stereotype.Component; + +@Component(value = "str") + +public class CalculationStr implements ICalculation { + @Override + public String GetSum(String firstInp, String secondInp) { + return (firstInp+secondInp); + } + + @Override + public String GetMinus(String firstInp, String secondInp) { + StringBuilder bufStr = new StringBuilder(); + if (firstInp.equals(secondInp)){ + return "0"; + } + else { + for (int i = 0; i < firstInp.length(); i++) { + boolean check = true; + for (int k = 0; k < secondInp.length(); k++) { + if (firstInp.charAt(i) == secondInp.charAt(k)) { + check = false; + break; + } + } + if (check){ + bufStr.append(firstInp.charAt(i));} + } + } + return bufStr.toString(); + } + + @Override + public String GetMult(String firstInp, String secondInp) { + int multInt = secondInp.length(); + StringBuilder multResult = new StringBuilder(); + for (int i = 0; i < multInt; i++){ + multResult.append(firstInp); + } + return multResult.toString(); + } + + @Override + public String GetDiv(String firstInp, String secondInp) { + if (secondInp.length() !=0) { + double divRes = 1.0 * firstInp.length() / secondInp.length(); + return Double.toString(divRes); + } + else return "error"; + } +} diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/ICalculation.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/ICalculation.java new file mode 100644 index 0000000..cbb9eaa --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/domain/ICalculation.java @@ -0,0 +1,8 @@ +package com.example.lab.AllTypeCalc.domain; + +public interface ICalculation { + V GetSum(V firstInp, V secondInp); + V GetMinus (V firstInp, V secondInp); + V GetMult (V firstInp, V secondInp); + V GetDiv(V firstInp, V secondInp); +} diff --git a/backend/lab/src/main/java/com/example/lab/AllTypeCalc/service/CalcService.java b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/service/CalcService.java new file mode 100644 index 0000000..675d70f --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/AllTypeCalc/service/CalcService.java @@ -0,0 +1,79 @@ +package com.example.lab.AllTypeCalc.service; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; +import com.example.lab.AllTypeCalc.domain.CalculationInt; +import com.example.lab.AllTypeCalc.domain.ICalculation; + +import java.util.Arrays; + +@Service + +public class CalcService { + private final ApplicationContext applicationContext; + private final Logger log = LoggerFactory.getLogger(CalculationInt.class); + public CalcService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + + public Object GetSum(Object firstInp, Object secondInp, String type) { + final ICalculation calculator = (ICalculation) applicationContext.getBean(type); + if(type.equals("str")){ + return calculator.GetSum(firstInp.toString(),secondInp.toString()); + } + if(type.equals("int")) { + return calculator.GetSum(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString()) ); + } + if(type.equals("arr")){ + return calculator.GetSum(cleanToArr(firstInp) , cleanToArr(secondInp)); + } + return "error"; + } + public Object GetMinus(Object firstInp, Object secondInp, String type) { + final ICalculation calculator = (ICalculation) applicationContext.getBean(type); + if(type.equals("str")){ + return calculator.GetMinus(firstInp.toString(),secondInp.toString()); + } + if (type.equals("int")){ + return calculator.GetMinus(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString())); + } + if(type.equals("arr")){ + return calculator.GetMinus(cleanToArr(firstInp) , cleanToArr(secondInp)); + } + return "error"; + } + public Object GetMult(Object firstInp, Object secondInp, String type) { + final ICalculation calculator = (ICalculation) applicationContext.getBean(type); + if(type.equals("str")){ + return calculator.GetMult(firstInp.toString(), secondInp.toString()); + } + if (type.equals("int")){ + return calculator.GetMult(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString())); + } + if (type.equals("arr")){ + return calculator.GetMult(cleanToArr(firstInp) , cleanToArr(secondInp)); + } + return "error"; + } + public Object GetDiv(Object firstInp, Object secondInp, String type) { + final ICalculation calculator = (ICalculation) applicationContext.getBean(type); + if (type.equals("str")){ + return calculator.GetDiv(firstInp.toString(), secondInp.toString()); + } + if (type.equals("int")){ + return calculator.GetDiv(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString())); + } + if(type.equals("arr")){ + return calculator.GetDiv(cleanToArr(firstInp) , cleanToArr(secondInp)); + } + return "error"; + } + + public String[] cleanToArr(Object arr){ + return Arrays.stream(arr.toString().split("[\\(\\)\\,\\.\\[\\] \\!\\?\\:\\;]")).filter(e -> e.trim().length()>0).toArray(String[]::new); + } +} diff --git a/backend/lab/src/main/java/com/example/lab/LabApplication.java b/backend/lab/src/main/java/com/example/lab/LabApplication.java index 86c4b5a..91cd72d 100644 --- a/backend/lab/src/main/java/com/example/lab/LabApplication.java +++ b/backend/lab/src/main/java/com/example/lab/LabApplication.java @@ -1,77 +1,16 @@ package com.example.lab; +import com.example.lab.AllTypeCalc.controller.CalcController; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication -@RestController public class LabApplication { - public static void main(String[] args) { SpringApplication.run(LabApplication.class, args); } - - @GetMapping("/calculateSquare") - public String calculateSquare(@RequestParam int num, - @RequestParam int step) { - double res; - res = Math.pow(num,step); - return ("Результат: " + Double.toString(res)); - } - - @GetMapping("/calculateFactorial") - public String calculateFactorial(@RequestParam int num) { - long res = 1; - for (int i = 1; i <= num; i++) { - res = res * i; - } - return ("Результат: " + Long.toString(res)); - } - - public int getFibonacciValue(int n) { - if (n <= 1) { - return 0; - } else if (n == 2) { - return 1; - } else { - return getFibonacciValue(n - 1) + getFibonacciValue(n - 2); - } - } - - @GetMapping("/calculateFibbN") - public String calculateFibbN(@RequestParam int num) { - int result = 0; - result = getFibonacciValue(num); - return ("Результат: " + Integer.toString(result)); - } - - public static boolean checkSimple(int i){ - if (i<=1) - return false; - else if (i <=3) - return true; - else if (i%2==0 || i %3 ==0) - return false; - int n = 5; - while (n*n <=i){ - if (i % n ==0 || i % (n+2) == 0) - return false; - n=n+6; - } - return true; - } - - @GetMapping("/calculateLastSimple") - public String calculateSimple(@RequestParam int num){ - int result = 0; - for (int i=2;i<=num;i++){ - if(checkSimple(i)) - result = i; - } - return ("Результат: " + Integer.toString(result)); - } - } diff --git a/backend/lab/src/main/java/com/example/lab/WebConfiguration.java b/backend/lab/src/main/java/com/example/lab/WebConfiguration.java new file mode 100644 index 0000000..a557547 --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/WebConfiguration.java @@ -0,0 +1,13 @@ +package com.example.lab; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfiguration implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**").allowedMethods("*"); + } +} \ No newline at end of file diff --git a/backend/lab/src/test/java/com/example/lab/LabApplicationTests.java b/backend/lab/src/test/java/com/example/lab/LabApplicationTests.java index 74eb19f..e4b1d42 100644 --- a/backend/lab/src/test/java/com/example/lab/LabApplicationTests.java +++ b/backend/lab/src/test/java/com/example/lab/LabApplicationTests.java @@ -1,13 +1,67 @@ package com.example.lab; +import com.example.lab.AllTypeCalc.service.CalcService; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class LabApplicationTests { + @Autowired + CalcService calcService; @Test - void contextLoads() { + void testIntSum() { + final Object result = calcService.GetSum(1, 2, "int"); + Assertions.assertEquals("3", result.toString()); } + @Test + void testStringSum() { + final Object result = calcService.GetSum("ab", "bc", "str"); + Assertions.assertEquals("abbc", result.toString()); + } + + @Test + void testIntMinus() { + final Object result = calcService.GetMinus(5, 4, "int"); + Assertions.assertEquals("1", result.toString()); + } + + @Test + void testStringMinus() { + final Object result = calcService.GetMinus("abc", "bc", "str"); + Assertions.assertEquals("a", result.toString()); + } + + @Test + void testIntMult() { + final Object result = calcService.GetMult(4, 4, "int"); + Assertions.assertEquals("16", result.toString()); + } + + @Test + void testStringMult() { + final Object result = calcService.GetMult("ab", "bc", "str"); + Assertions.assertEquals("abab", result.toString()); + } + + @Test + void testIntDiv() { + final Object result = calcService.GetDiv(12, 6, "int"); + Assertions.assertEquals("2", result.toString()); + } + + @Test + void testStringDiv() { + final Object result = calcService.GetDiv("ab", "bc", "str"); + Assertions.assertEquals("1.0", result.toString()); + } + + @Test + void testCalculatorErrorWired() { + Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calcService.GetSum("1", "1", "integer")); + } } diff --git a/front/index.html b/front/index.html index adf93a7..b1f0ba6 100644 --- a/front/index.html +++ b/front/index.html @@ -11,16 +11,21 @@

Вычисления

- - - - + + + + +
- - - - + + + +
diff --git a/front/scripts.js b/front/scripts.js index c41ad6f..f94e57a 100644 --- a/front/scripts.js +++ b/front/scripts.js @@ -1,26 +1,34 @@ function Okclick(clicked) { - var num = document.getElementById("textFieldNum").value - var num2 = document.getElementById("textFieldNum2").value + var obj1 = document.getElementById("obj1Input").value + var obj2 = document.getElementById("obj2Input").value + var typeInp = document.getElementById("typeSelect").value var id = clicked; var argument = ""; + console.log(typeInp); + if (document.getElementById("typeSelect").value == "arr"){ + console.log("aaa"); + obj1= encodeURIComponent(obj1); + obj2 = encodeURIComponent(obj2); + console.log(obj1); + console.log(obj2); + } if(id == "1"){ - argument = ("calculateSquare?num=" + num +"&step=" + num2); + argument = ("GetSum?obj1=" + obj1 +"&obj2=" + obj2 + "&typeInp=" + typeInp); } else if (id == "2"){ - argument = ("calculateFactorial?num=" + num); + argument = ("GetMinus?obj1=" + obj1 + "&obj2=" + obj2 + "&typeInp=" + typeInp); } else if( id == "3"){ - argument = ("calculateFibbN?num=" + num); + argument = ("GetMult?obj1=" + obj1 + "&obj2=" + obj2 + "&typeInp=" + typeInp); } else if (id == "4"){ - argument = ("calculateLastSimple?num=" + num); + argument = ("GetDiv?obj1=" + obj1 + "&obj2=" + obj2 + "&typeInp=" + typeInp); } - fetch ("http://127.0.0.1:8080/" + argument) .then(response => response.text()) .then((response) => { console.log(response) - document.getElementById("res").innerHTML = response + document.getElementById("res").innerHTML = "Результат = " + response }) }