diff --git a/Front/index.html b/Front/index.html
index afc89a3..35c52bd 100644
--- a/Front/index.html
+++ b/Front/index.html
@@ -16,7 +16,7 @@
Введите первое число
Введите второе число
-
+
Выберите операцию
+
@@ -24,8 +24,15 @@
*
/
+ Выберите тип данных
+
+
+ Число
+ Строка
+
+
Результат
-
+
diff --git a/Front/script.js b/Front/script.js
index 521a1c2..036f53a 100644
--- a/Front/script.js
+++ b/Front/script.js
@@ -2,50 +2,50 @@ let calculateButton = document.getElementById("calculate");
let numberOneInput = document.getElementById("first");
let numberTwoInput = document.getElementById("second");
let resultInput = document.getElementById("res");
+let typeInput = document.getElementById("type");
buttonPlus.onclick = function(event) {
event.preventDefault();
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
- fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}`)
+ let type = typeInput.value;
+ fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => {
- const ru = new Intl.NumberFormat("ru").format(res);
- resultInput.value = ru;
+ resultInput.value = res;
});
}
buttonMinus.onclick = function(event) {
event.preventDefault();
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
- fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}`)
+ let type = typeInput.value;
+ fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => {
- const ru = new Intl.NumberFormat("ru").format(res);
- resultInput.value = ru;
+ resultInput.value = res;
});
}
buttonMulti.onclick = function(event) {
event.preventDefault();
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
-
- fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}`)
+ let type = typeInput.value;
+ fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => {
- const ru = new Intl.NumberFormat("ru").format(res);
- resultInput.value = ru;
+ resultInput.value = res;
});
}
buttonDiv.onclick = function(event) {
event.preventDefault();
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
- fetch(`http://localhost:8080/div?first=${num_1}&second=${num_2}`)
+ let type = typeInput.value;
+ fetch(`http://localhost:8080/div?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res =>
{
- const ru = new Intl.NumberFormat("ru").format(res);
- resultInput.value = ru;
+ resultInput.value = res;
});
}
\ No newline at end of file
diff --git a/src/main/java/ip/labwork/LabworkApplication.java b/src/main/java/ip/labwork/LabworkApplication.java
index 67b6872..582c1f4 100644
--- a/src/main/java/ip/labwork/LabworkApplication.java
+++ b/src/main/java/ip/labwork/LabworkApplication.java
@@ -12,7 +12,7 @@ public class LabworkApplication {
public static void main(String[] args) {
SpringApplication.run(LabworkApplication.class, args);
}
-
+/*
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
@@ -43,5 +43,5 @@ public class LabworkApplication {
return null;
}
return Double.toString(first / second);
- }
+ }*/
}
diff --git a/src/main/java/ip/labwork/method/controller/MethodController.java b/src/main/java/ip/labwork/method/controller/MethodController.java
new file mode 100644
index 0000000..63add70
--- /dev/null
+++ b/src/main/java/ip/labwork/method/controller/MethodController.java
@@ -0,0 +1,43 @@
+package ip.labwork.method.controller;
+
+import ip.labwork.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/ip/labwork/method/domain/IMethod.java b/src/main/java/ip/labwork/method/domain/IMethod.java
new file mode 100644
index 0000000..073c6be
--- /dev/null
+++ b/src/main/java/ip/labwork/method/domain/IMethod.java
@@ -0,0 +1,11 @@
+package ip.labwork.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/ip/labwork/method/domain/MethodInt.java b/src/main/java/ip/labwork/method/domain/MethodInt.java
new file mode 100644
index 0000000..78db736
--- /dev/null
+++ b/src/main/java/ip/labwork/method/domain/MethodInt.java
@@ -0,0 +1,27 @@
+package ip.labwork.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/ip/labwork/method/domain/MethodString.java b/src/main/java/ip/labwork/method/domain/MethodString.java
new file mode 100644
index 0000000..40c2c20
--- /dev/null
+++ b/src/main/java/ip/labwork/method/domain/MethodString.java
@@ -0,0 +1,44 @@
+package ip.labwork.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/ip/labwork/method/service/MethodService.java b/src/main/java/ip/labwork/method/service/MethodService.java
new file mode 100644
index 0000000..fa14f6c
--- /dev/null
+++ b/src/main/java/ip/labwork/method/service/MethodService.java
@@ -0,0 +1,51 @@
+package ip.labwork.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/ip/labwork/LabworkApplicationTests.java b/src/test/java/ip/labwork/LabworkApplicationTests.java
index 556b574..36d35ab 100644
--- a/src/test/java/ip/labwork/LabworkApplicationTests.java
+++ b/src/test/java/ip/labwork/LabworkApplicationTests.java
@@ -1,13 +1,62 @@
package ip.labwork;
+import ip.labwork.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"));
}
}