diff --git a/frontend/index.html b/frontend/index.html index cae03df..eac3082 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -2,40 +2,63 @@ - Калькулятор - + Calculator + - -
-
- -
-
- -
-
- -
-
- -
-
-

-
+
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
diff --git a/frontend/index.js b/frontend/index.js index 69dc105..2f8f8a8 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -3,7 +3,7 @@ const http = require('http') const requestListener = async function (req, res) { res.writeHead(200); - fs.readFile('index.html', (err, data) => { + fs.readFile('index.html', 'utf8', (err, data) => { res.end(data); }); }; diff --git a/src/main/java/np/something/controllers/MathController.java b/src/main/java/np/something/controllers/MathController.java index 2e28fec..0663f33 100644 --- a/src/main/java/np/something/controllers/MathController.java +++ b/src/main/java/np/something/controllers/MathController.java @@ -1,41 +1,50 @@ package np.something.controllers; +import np.something.services.OperationsService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class MathController { - @GetMapping("/sum") - public int sum( - @RequestParam(value = "num1", defaultValue = "0") int num1, - @RequestParam(value = "num2", defaultValue = "0") int num2 - ) { + private final OperationsService operationsService; - return num1 + num2; + public MathController(OperationsService operationsService) { + this.operationsService = operationsService; + } + + @GetMapping("/sum") + public Object sum( + @RequestParam(value = "value1", defaultValue = "0") String value1, + @RequestParam(value = "value2", defaultValue = "0") String value2, + @RequestParam(value = "ops", defaultValue = "intops") String ops + ) { + return operationsService.add(value1, value2, ops); } @GetMapping("/sub") - public int sub( - @RequestParam(value = "num1", defaultValue = "0") int num1, - @RequestParam(value = "num2", defaultValue = "0") int num2 + public Object sub( + @RequestParam(value = "value1", defaultValue = "0") String value1, + @RequestParam(value = "value2", defaultValue = "0") String value2, + @RequestParam(value = "ops", defaultValue = "intops") String ops ) { - return num1 - num2; + return operationsService.sub(value1, value2, ops); } @GetMapping("/mul") - public int mul( - @RequestParam(value = "num1", defaultValue = "0") int num1, - @RequestParam(value = "num2", defaultValue = "0") int num2 + public Object mul( + @RequestParam(value = "value", defaultValue = "0") String value, + @RequestParam(value = "count", defaultValue = "0") int count, + @RequestParam(value = "ops", defaultValue = "intops") String ops ) { - return num1 * num2; + return operationsService.mul(value, count, ops); } - @GetMapping("/div") - public int div( - @RequestParam(value = "num1", defaultValue = "0") int num1, - @RequestParam(value = "num2", defaultValue = "0") int num2 + @GetMapping("/invert") + public Object div( + @RequestParam(value = "value", defaultValue = "0") String value, + @RequestParam(value = "ops", defaultValue = "intops") String ops ) { - return num1 / num2; + return operationsService.invert(value, ops); } } diff --git a/src/main/java/np/something/model/IntegerOperations.java b/src/main/java/np/something/model/IntegerOperations.java new file mode 100644 index 0000000..e91b099 --- /dev/null +++ b/src/main/java/np/something/model/IntegerOperations.java @@ -0,0 +1,25 @@ +package np.something.model; +import org.springframework.stereotype.Component; + +@Component(value = "intops") +public class IntegerOperations implements Operations { + @Override + public Integer add(Integer first, Integer second) { + return first + second; + } + + @Override + public Integer sub(Integer first, Integer second) { + return first - second; + } + + @Override + public Integer mul(Integer value, int count) { + return value * count; + } + + @Override + public Integer invert(Integer value) { + return ~value; + } +} diff --git a/src/main/java/np/something/model/Operations.java b/src/main/java/np/something/model/Operations.java new file mode 100644 index 0000000..dd2841b --- /dev/null +++ b/src/main/java/np/something/model/Operations.java @@ -0,0 +1,8 @@ +package np.something.model; + +public interface Operations { + T add(T first, T second); + T sub(T first, T second); + T mul(T value, int count); + T invert(T value); +} diff --git a/src/main/java/np/something/model/StringOperations.java b/src/main/java/np/something/model/StringOperations.java new file mode 100644 index 0000000..77b2b0e --- /dev/null +++ b/src/main/java/np/something/model/StringOperations.java @@ -0,0 +1,30 @@ +package np.something.model; +import org.springframework.stereotype.Component; + +@Component(value = "stringops") +public class StringOperations implements Operations{ + + @Override + public String add(String first, String second) { + return first + second; + } + + @Override + public String sub(String first, String second) { + return first.replace(second, ""); + } + + @Override + public String mul(String value, int count) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < count; i++) { + result.append(value); + } + return result.toString(); + } + + @Override + public String invert(String value) { + return new StringBuilder(value).reverse().toString(); + } +} diff --git a/src/main/java/np/something/services/OperationsService.java b/src/main/java/np/something/services/OperationsService.java new file mode 100644 index 0000000..e33d2d2 --- /dev/null +++ b/src/main/java/np/something/services/OperationsService.java @@ -0,0 +1,45 @@ +package np.something.services; +import np.something.model.Operations; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +@Service +public class OperationsService { + private final ApplicationContext applicationContext; + + public OperationsService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public Object add(Object first, Object second, String ops) { + Operations operations = (Operations) applicationContext.getBean(ops); + if (ops.startsWith("int")) { + return operations.add(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())); + } + return operations.add(first, second); + } + + public Object sub(Object first, Object second, String ops) { + Operations operations = (Operations) applicationContext.getBean(ops); + if (ops.startsWith("int")) { + return operations.sub(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())); + } + return operations.sub(first, second); + } + + public Object mul(Object value, int count, String ops) { + Operations operations = (Operations) applicationContext.getBean(ops); + if (ops.startsWith("int")) { + return operations.mul(Integer.parseInt(value.toString()), count); + } + return operations.mul(value, count); + } + + public Object invert(Object value, String ops) { + Operations operations = (Operations) applicationContext.getBean(ops); + if (ops.startsWith("int")) { + return operations.invert(Integer.parseInt(value.toString())); + } + return operations.invert(value); + } +} diff --git a/src/test/java/np/something/OperationsServiceTests.java b/src/test/java/np/something/OperationsServiceTests.java new file mode 100644 index 0000000..57855a8 --- /dev/null +++ b/src/test/java/np/something/OperationsServiceTests.java @@ -0,0 +1,67 @@ +package np.something; + +import np.something.services.OperationsService; +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 OperationsServiceTests { + @Autowired + OperationsService operationsService; + + @Test + void stringSum() { + Assertions.assertEquals("Hello, World!", operationsService.add("Hello,", " World!", "stringops")); + Assertions.assertEquals("Hello, World!", operationsService.add("", "Hello, World!", "stringops")); + } + + @Test + void stringSub() { + Assertions.assertEquals("Яблк", operationsService.sub("Яблоко", "о", "stringops")); + Assertions.assertEquals("Тлвизор", operationsService.sub("Телевизор", "е", "stringops")); + Assertions.assertEquals("Rewte", operationsService.sub("Rewrite", "ri", "stringops")); + } + + @Test + void stringMul() { + Assertions.assertEquals("ДеревоДерево", operationsService.mul("Дерево", 2, "stringops")); + Assertions.assertEquals("ЛоЛоЛо", operationsService.mul("Ло", 3, "stringops")); + Assertions.assertEquals("", operationsService.mul("Слово", 0, "stringops")); + } + + @Test + void stringInvert() { + Assertions.assertEquals("оволС", operationsService.invert("Слово", "stringops")); + Assertions.assertEquals("droW", operationsService.invert("Word", "stringops")); + Assertions.assertEquals("", operationsService.invert("", "stringops")); + } + + @Test + void intSum() { + Assertions.assertEquals(22, operationsService.add(11, 11, "intops")); + Assertions.assertEquals(-10, operationsService.add(15, -25, "intops")); + } + + @Test + void intSub() { + Assertions.assertEquals(0, operationsService.sub(10, 10, "intops")); + Assertions.assertEquals(100, operationsService.sub(100, 0, "intops")); + Assertions.assertEquals(3, operationsService.sub(-3, -6, "intops")); + } + + @Test + void intMul() { + Assertions.assertEquals(0, operationsService.mul(0, 10, "intops")); + Assertions.assertEquals(18, operationsService.mul(6, 3, "intops")); + Assertions.assertEquals(-4, operationsService.mul(-1, 4, "intops")); + } + + @Test + void intInvert() { + Assertions.assertEquals(0, operationsService.invert(-1, "intops")); + Assertions.assertEquals(-2, operationsService.invert(1, "intops")); + Assertions.assertEquals(-3, operationsService.invert(2, "intops")); + } +} diff --git a/src/test/java/np/something/SomethingApplicationTests.java b/src/test/java/np/something/SomethingApplicationTests.java deleted file mode 100644 index dd6d373..0000000 --- a/src/test/java/np/something/SomethingApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package np.something; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SomethingApplicationTests { - - @Test - void contextLoads() { - } - -}