diff --git a/Front/index.html b/Front/index.html index afc89a3..4b0fb0c 100644 --- a/Front/index.html +++ b/Front/index.html @@ -24,8 +24,15 @@ + Выберите тип данных +
+ +
Результат - + diff --git a/Front/script.js b/Front/script.js index c244e07..f808fd1 100644 --- a/Front/script.js +++ b/Front/script.js @@ -4,7 +4,6 @@ let numberTwoInput = document.getElementById("second"); let resultInput = document.getElementById("res"); let typeInput = document.getElementById("type"); - function f(operation, event){ event.preventDefault(); let num_1 = numberOneInput.value; diff --git a/src/main/java/method/controller/MethodController.java b/src/main/java/method/controller/MethodController.java new file mode 100644 index 0000000..ef81ee0 --- /dev/null +++ b/src/main/java/method/controller/MethodController.java @@ -0,0 +1,43 @@ +package method.controller; + +import method.service.MethodService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MethodController { + private final MethodService speakerService; + + public MethodController(MethodService speakerService) { + this.speakerService = speakerService; + } + + @GetMapping("/sum") + public String Sum(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Sum(first,second,type); + } + + @GetMapping("/minus") + public String Ras(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Ras(first,second,type); + } + + @GetMapping("/multi") + public String Pros(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Pros(first,second,type); + } + + @GetMapping("/div") + public String Del(@RequestParam(value = "first", defaultValue = "1") Object first, + @RequestParam(value = "second", defaultValue = "1") Object second, + @RequestParam(value = "type", defaultValue = "int") String type) { + return speakerService.Del(first,second,type); + } +} diff --git a/src/main/java/method/domain/IMethod.java b/src/main/java/method/domain/IMethod.java new file mode 100644 index 0000000..01bd0bd --- /dev/null +++ b/src/main/java/method/domain/IMethod.java @@ -0,0 +1,11 @@ +package method.domain; + +public interface IMethod { + T Sum(T first, T second); + + T Multiply(T first, Integer second); + + T Minus(T first, Integer second); + + T Div(T first, T second); +} diff --git a/src/main/java/method/domain/MethodInt.java b/src/main/java/method/domain/MethodInt.java new file mode 100644 index 0000000..c63e6c4 --- /dev/null +++ b/src/main/java/method/domain/MethodInt.java @@ -0,0 +1,27 @@ +package method.domain; + +import org.springframework.stereotype.Component; + +@Component(value="int") +public class MethodInt implements IMethod{ + public Integer Sum(Integer first, Integer second) { + return Integer.parseInt(first.toString()) + Integer.parseInt(second.toString()); + } + + public Integer Multiply(Integer first, Integer second) { + return Integer.parseInt(first.toString()) * Integer.parseInt(second.toString()); + } + + public Integer Minus(Integer first, Integer second) { + return Integer.parseInt(first.toString()) - Integer.parseInt(second.toString()); + } + + public Integer Div(Integer first, Integer second) { + int num = Integer.parseInt(second.toString()); + if (num == 0){ + return null; + }else{ + return Integer.parseInt(first.toString()) / num; + } + } +} diff --git a/src/main/java/method/domain/MethodString.java b/src/main/java/method/domain/MethodString.java new file mode 100644 index 0000000..13a4b06 --- /dev/null +++ b/src/main/java/method/domain/MethodString.java @@ -0,0 +1,44 @@ +package method.domain; + +import org.springframework.stereotype.Component; + +@Component(value="string") +public class MethodString implements IMethod{ + @Override + public String Sum(String first, String second) { + return first.concat(second); + } + + @Override + public String Multiply(String first, Integer second) { + if (second != 0){ + String temp = ""; + for (int i = 0; i < second; i++){ + temp = temp.concat(first); + } + return temp; + } + else{ + return first; + } + } + + @Override + public String Minus(String first, Integer second) { + String temp = first; + if(temp.length() >= second){ + return temp.substring(0, first.length() - second); + }else{ + return first; + } + } + + @Override + public String Div(String first, String second) { + if (first.contains(second)){ + return "true"; + }else{ + return "false"; + } + } +} \ No newline at end of file diff --git a/src/main/java/method/service/MethodService.java b/src/main/java/method/service/MethodService.java new file mode 100644 index 0000000..cca6567 --- /dev/null +++ b/src/main/java/method/service/MethodService.java @@ -0,0 +1,51 @@ +package method.service; + +import ip.labwork.method.domain.IMethod; +import ip.labwork.method.domain.MethodString; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +@Service +public class MethodService { + private final ApplicationContext applicationContext; + + public MethodService(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public String Sum(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Sum(first,second)); + }else{ + return String.format("%s", speaker.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Ras(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Minus(first,Integer.parseInt(second.toString()))); + }else{ + return String.format("%s", speaker.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Pros(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Multiply(first,Integer.parseInt(second.toString()))); + }else{ + return String.format("%s", speaker.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString()))); + } + } + + public String Del(Object first, Object second, String type) { + final IMethod speaker = (IMethod) applicationContext.getBean(type); + if (speaker instanceof MethodString){ + return String.format("%s", speaker.Div(first,second)); + }else { + return String.format("%s", speaker.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()))); + } + } +} diff --git a/src/test/java/com/example/demo/LabworkApplicationTests.java b/src/test/java/com/example/demo/LabworkApplicationTests.java index 2630db0..3619947 100644 --- a/src/test/java/com/example/demo/LabworkApplicationTests.java +++ b/src/test/java/com/example/demo/LabworkApplicationTests.java @@ -1,13 +1,62 @@ package com.example.demo; +import method.service.MethodService; +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 LabworkApplicationTests { + @Autowired + MethodService speakerService; @Test - void contextLoads() { + void testIntSum() { + final String res = speakerService.Sum(20,10,"int"); + Assertions.assertEquals(30, Integer.parseInt(res)); + } + + @Test + void testIntMinus() { + final String res = speakerService.Ras(20,10,"int"); + Assertions.assertEquals(10, Integer.parseInt(res)); + } + @Test + void testIntMulti() { + final String res = speakerService.Pros(20,10,"int"); + Assertions.assertEquals(200, Integer.parseInt(res)); + } + @Test + void testIntDiv() { + final String res = speakerService.Del(20,10,"int"); + Assertions.assertEquals(2, Integer.parseInt(res)); + } + @Test + void testStringSum() { + final String res = speakerService.Sum("20","10","string"); + Assertions.assertEquals("2010", res); + } + + @Test + void testStringMinus() { + final String res = speakerService.Ras("300",1,"string"); + Assertions.assertEquals("30", res); + } + @Test + void testStringMulti() { + final String res = speakerService.Pros("20",2,"string"); + Assertions.assertEquals("2020", res); + } + @Test + void testStringDiv() { + final String res = speakerService.Del("20","2","string"); + Assertions.assertEquals("true", res); + } + @Test + void testSpeakerErrorWired() { + Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.Sum("10", "20", "double")); } }