diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java new file mode 100644 index 0000000..d0b8da2 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java @@ -0,0 +1,23 @@ +package com.example.ipLab.TypesCalc.Configuration; + +import com.example.ipLab.TypesCalc.domen.TypeCalcInteger; +import com.example.ipLab.TypesCalc.domen.TypeCalcString; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CalcConfiguration { + private final Logger log = LoggerFactory.getLogger(CalcConfiguration.class); + + @Bean(value="int") + public TypeCalcInteger createTypeCalcInteger(){ + return new TypeCalcInteger(); + } + + @Bean(value="string") + public TypeCalcString createTypeCalcString(){ + return new TypeCalcString(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java new file mode 100644 index 0000000..306cfc2 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java @@ -0,0 +1,33 @@ +package com.example.ipLab.TypesCalc.Service; + +import com.example.ipLab.TypesCalc.domen.TypeCalc; +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 Object Sum(Object obj1, Object obj2, String type){ + final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); + if (type.startsWith("int")) return typeCalculator.Sum(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); + return typeCalculator.Sum(obj1, obj2); + } + public Object Dif(Object obj1, Object obj2, String type){ + final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); + if (type.startsWith("int")) return typeCalculator.Dif(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); + return typeCalculator.Dif(obj1, obj2); + } + public Object Multiply(Object obj1, Object obj2, String type){ + final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); + if (type.startsWith("int")) return typeCalculator.Multiply(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); + return typeCalculator.Multiply(obj1, obj2); + } + public Object Div(Object obj1, Object obj2, String type){ + final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); + if (type.startsWith("int")) return typeCalculator.Div(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); + return typeCalculator.Div(obj1, obj2); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java new file mode 100644 index 0000000..55decec --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java @@ -0,0 +1,41 @@ +package com.example.ipLab.TypesCalc.controller; + +import com.example.ipLab.TypesCalc.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 TypeCalcController { + private final CalcService calcService; + + public TypeCalcController(CalcService calcService) {this.calcService = calcService;} + + @GetMapping("/CalcSum") + public Object calcSum(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, + @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calcService.Sum(obj1, obj2, type); + } + + @GetMapping("/CalcDif") + public Object calcDif(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, + @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calcService.Dif(obj1, obj2, type); + } + + @GetMapping("/CalcMultiply") + public Object calcMultiply(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, + @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calcService.Multiply(obj1, obj2, type); + } + + @GetMapping("/CalcDiv") + public Object calcDiv(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, + @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, + @RequestParam(value = "type", defaultValue = "string") String type){ + return calcService.Div(obj1, obj2, type); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java new file mode 100644 index 0000000..b5f4038 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java @@ -0,0 +1,8 @@ +package com.example.ipLab.TypesCalc.domen; + +public interface TypeCalc { + T Sum(T obj1, T obj2); + T Dif(T obj1, T obj2); + T Multiply(T obj1, T obj2); + T Div(T obj1, T obj2); +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java new file mode 100644 index 0000000..4fe348d --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java @@ -0,0 +1,30 @@ +package com.example.ipLab.TypesCalc.domen; + +import org.springframework.stereotype.Component; + +public class TypeCalcInteger implements TypeCalc{ + @Override + public Integer Sum(Integer num1, Integer num2) { + return num1 + num2; + } + + @Override + public Integer Dif(Integer num1, Integer num2) { + return num1 - num2; + } + + @Override + public Integer Multiply(Integer num1, Integer num2) { + return num1 * num2; + } + + @Override + public Integer Div(Integer num1, Integer num2) { + try { + return num1 / num2; + } + catch (ArithmeticException ex){ + return 0; + } + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java new file mode 100644 index 0000000..969cecb --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java @@ -0,0 +1,57 @@ +package com.example.ipLab.TypesCalc.domen; + +import org.springframework.stereotype.Component; + +public class TypeCalcString implements TypeCalc { + @Override + public String Sum(String s1, String s2) { + return s1 + s2; + } + + @Override + public String Dif(String s1, String s2) { + String res = ""; + for (char c : s1.toCharArray()){ + boolean foundInOther = false; + for (int i = 0; i < s2.length(); i++){ + if (c == s2.charAt(i)) { + foundInOther = true; + break; + } + } + if (!foundInOther) res += c; + } + return res; + } + + @Override + public String Multiply(String s1, String s2) { + String res = ""; + for (char c : s1.toCharArray()){ + boolean foundInOther = false; + for (int i = 0; i < s2.length(); i++){ + if (c == s2.charAt(i)) { + foundInOther = true; + break; + } + } + if (foundInOther) res += c; + } + return res; + } + + @Override + public String Div(String s1, String s2) { + StringBuilder res = new StringBuilder(); + int maxLength = Integer.max(s1.length(), s2.length()); + for (int i = 0; i < maxLength; i++){ + if (i < s1.length()){ + res.append(s1.charAt(i)); + } + if (i < s2.length()){ + res.append(s2.charAt(i)); + } + } + return res.toString(); + } +} diff --git a/backend/ipLab/src/test/java/com/example/ipLab/IpLabApplicationTests.java b/backend/ipLab/src/test/java/com/example/ipLab/IpLabApplicationTests.java index 0737935..f397096 100644 --- a/backend/ipLab/src/test/java/com/example/ipLab/IpLabApplicationTests.java +++ b/backend/ipLab/src/test/java/com/example/ipLab/IpLabApplicationTests.java @@ -1,13 +1,72 @@ package com.example.ipLab; +import com.example.ipLab.TypesCalc.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 IpLabApplicationTests { + @Autowired + CalcService calcService; + @Test - void contextLoads() { + void testIntegerCalcSum() { + final Object res = calcService.Sum(2, 2, "int"); + Assertions.assertEquals("4", res.toString()); + } + @Test + void testIntegerCalcDif() { + final Object res = calcService.Dif(2, 2, "int"); + Assertions.assertEquals("0", res.toString()); } + @Test + void testIntegerCalcMultiply() { + final Object res = calcService.Multiply(2, 3, "int"); + Assertions.assertEquals("6", res.toString()); + } + @Test + void testIntegerCalcDiv() { + final Object res = calcService.Div(4, 2, "int"); + Assertions.assertEquals("2", res.toString()); + } + + @Test + void testIntegerCalcDivBy0() { + final Object res = calcService.Div(4, 0, "int"); + Assertions.assertEquals("0", res.toString()); + } + + @Test + void testStringCalcSum(){ + final Object res = calcService.Sum("2", "2", "string"); + Assertions.assertEquals("22", res.toString()); + } + + @Test + void testStringCalcDif(){ + final Object res = calcService.Dif("524", "24", "string"); + Assertions.assertEquals("5", res.toString()); + } + + @Test + void testStringCalcMultiply(){ + final Object res = calcService.Multiply("523", "215", "string"); + Assertions.assertEquals("52", res.toString()); + } + + @Test + void testStringCalcDiv(){ + final Object res = calcService.Div("135", "24", "string"); + Assertions.assertEquals("12345", res.toString()); + } + + @Test + void testSpeakerErrorWiredInt() { + Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calcService.Sum("1", "1", "integer")); + } } diff --git a/frontend/index.html b/frontend/index.html index 5e6fdca..e7119ab 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -4,7 +4,7 @@ - + Calc @@ -12,13 +12,18 @@
-
- - - - - -
+
+ + + + + + + +
diff --git a/frontend/scripts/calc.js b/frontend/scripts/calc.js index 091f711..e632dbd 100644 --- a/frontend/scripts/calc.js +++ b/frontend/scripts/calc.js @@ -1,4 +1,4 @@ -function calcSecond(){ +function calcSum(){ var num = document.getElementById("numberInput").value; fetch("http://127.0.0.1:8080/second?num=" + num) .then(function(response) { @@ -13,7 +13,7 @@ function calcSecond(){ .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) } -function calcRoot(){ +function calcDif(){ var num = document.getElementById("numberInput").value; if (num < 0) { document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число"; @@ -33,7 +33,7 @@ function calcRoot(){ .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) } -function calcFact(){ +function calcMultiply(){ var num = document.getElementById("numberInput").value; if (num < 0) { document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число"; @@ -53,7 +53,7 @@ function calcFact(){ .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) } -function calcDigit(){ +function calcDiv(){ var num = document.getElementById("numberInput").value; fetch("http://127.0.0.1:8080/digit?num=" + num) .then((response) => { diff --git a/frontend/scripts/calcType.js b/frontend/scripts/calcType.js new file mode 100644 index 0000000..5f20589 --- /dev/null +++ b/frontend/scripts/calcType.js @@ -0,0 +1,36 @@ +function calcSum(){ + fetchServer("CalcSum"); +} + +function calcDif(){ + fetchServer("CalcDif"); +} + +function calcMultiply(){ + fetchServer("CalcMultiply"); +} + +function calcDiv(){ + fetchServer("CalcDiv"); +} + +function fetchServer(adress){ + var obj1 = document.getElementById("obj1Input").value; + var obj2 = document.getElementById("obj2Input").value; + var type = document.getElementById("typeSelect").value; + if (type == "int" && (isNaN(obj1) || isNaN(obj2))){ + document.getElementById("responseField").innerHTML = "Ошибка: введите число для операций с числами или измените тип данных!"; + return; + } + fetch("http://127.0.0.1:8080/" + adress + "?obj1=" + obj1 + "&obj2=" + obj2 + "&type=" + type) + .then(function(response) { + if (!response.ok){ + return response.text().then(text => {throw new Error(text)}); + } + return response.text(); + }) + .then((response) => { + document.getElementById("responseField").innerHTML = "Результат: " + response; + }) + .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) +} \ No newline at end of file