From 108665ee17abbaa5f58377e516763f36095dec78 Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Tue, 7 Mar 2023 09:42:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=202?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/MyScript.js | 49 +++++++----- front/index.html | 17 ++-- .../ru/ulstu/is/myapp/MyappApplication.java | 34 +------- .../ulstu/is/myapp/controller/Controller.java | 53 ++++++++++++ .../ru/ulstu/is/myapp/domain/BoolMethod.java | 28 +++++++ .../ru/ulstu/is/myapp/domain/IMethods.java | 9 +++ .../ru/ulstu/is/myapp/domain/IntMethod.java | 30 +++++++ .../ru/ulstu/is/myapp/domain/StrMethod.java | 49 ++++++++++++ .../ulstu/is/myapp/service/MethodService.java | 76 ++++++++++++++++++ .../ulstu/is/myapp/MyappApplicationTests.java | 80 ++++++++++++++++++- 10 files changed, 366 insertions(+), 59 deletions(-) create mode 100644 src/main/java/ru/ulstu/is/myapp/controller/Controller.java create mode 100644 src/main/java/ru/ulstu/is/myapp/domain/BoolMethod.java create mode 100644 src/main/java/ru/ulstu/is/myapp/domain/IMethods.java create mode 100644 src/main/java/ru/ulstu/is/myapp/domain/IntMethod.java create mode 100644 src/main/java/ru/ulstu/is/myapp/domain/StrMethod.java create mode 100644 src/main/java/ru/ulstu/is/myapp/service/MethodService.java diff --git a/front/MyScript.js b/front/MyScript.js index 7907977..608cf7b 100644 --- a/front/MyScript.js +++ b/front/MyScript.js @@ -2,40 +2,49 @@ function setResult(result) { let lbl = ``; document.getElementById("result").innerHTML = lbl; } -let addButton = document.getElementById("getResultAdd"); -let a = document.getElementById("addNum1"); -let b = document.getElementById("addNum2"); -let Output = document.getElementById("addAnswer"); -let address = "hello" + + function add(){ -address = "add" -executeRequest(); + executeRequest("add"); } + function sub(){ -address = "sub" -executeRequest(); + executeRequest("sub"); } + function mul(){ -address = "mul" -executeRequest(); + executeRequest("mul"); } + function del(){ - -address = "del" -if(b.value != 0) executeRequest(); - + executeRequest("del"); } -function executeRequest() { +function enterArray(){ + executeRequestArray("array"); +} - let num1 = a.value; - let num2 = b.value; +function executeRequest(address) { + let num1 = document.getElementById("addNum1").value; + let num2 = document.getElementById("addNum2").value; + let type = document.getElementById("type").value; console.log("a" + num1 + "b" + num2) - fetch(`http://localhost:8080/${address}?a=${num1}&b=${num2}`) + fetch(`http://localhost:8080/${address}?a=${num1}&b=${num2}&type=${type}`) .then(response => { - return response.json(); + return response.text(); }) .then(result => { setResult(result); }) +} + +function executeRequestArray(address) { + let array = document.getElementById("enterArray").value; + fetch(`http://localhost:8080/${address}?InputNumbers=${array}&type=${type}`) + .then(response => { + return response.json(); + }) + .then(result => { + setResult(result); + }) } \ No newline at end of file diff --git a/front/index.html b/front/index.html index 5247738..7ee821c 100644 --- a/front/index.html +++ b/front/index.html @@ -9,20 +9,27 @@
+

Команды

Сложение

- - - + + + -
Ответ:
- + +
Ответ:
+
diff --git a/src/main/java/ru/ulstu/is/myapp/MyappApplication.java b/src/main/java/ru/ulstu/is/myapp/MyappApplication.java index 1b6c49a..a02efd5 100644 --- a/src/main/java/ru/ulstu/is/myapp/MyappApplication.java +++ b/src/main/java/ru/ulstu/is/myapp/MyappApplication.java @@ -15,39 +15,7 @@ public class MyappApplication { SpringApplication.run(MyappApplication.class, args); } - @GetMapping("/hello") - public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s!", name); - } - @CrossOrigin - @GetMapping(value = "/add") - public Integer doSum(@RequestParam(value = "a", defaultValue = "0") int a, - @RequestParam(value = "b", defaultValue = "0") int b){ - return a + b; - } - @GetMapping("/sub") - public Integer doSub(@RequestParam(value = "a", defaultValue = "0") int a, - @RequestParam(value = "b", defaultValue = "0") int b){ - return a - b; - } - @GetMapping("/mul") - public Integer doMul(@RequestParam(value = "a", defaultValue = "0") int a, - @RequestParam(value = "b", defaultValue = "0") int b){ - return a * b; - } - @GetMapping("/del") - public Integer doDel(@RequestParam(value = "a", defaultValue = "0") int a, - @RequestParam(value = "b", defaultValue = "1") int b){ - return a / b; - } - @GetMapping("/mu") - public String len(@RequestParam(value = "word", defaultValue = "") String name){ - return String.format("Длина слова " + name + " " + len(name)); - } - @GetMapping("/de") - public String root(){ - return new Date().toString(); - } + diff --git a/src/main/java/ru/ulstu/is/myapp/controller/Controller.java b/src/main/java/ru/ulstu/is/myapp/controller/Controller.java new file mode 100644 index 0000000..7f9b2e9 --- /dev/null +++ b/src/main/java/ru/ulstu/is/myapp/controller/Controller.java @@ -0,0 +1,53 @@ +package ru.ulstu.is.myapp.controller; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.is.myapp.service.MethodService; + +import java.util.Date; +@RestController +public class Controller { + private final MethodService methodService; + public Controller(MethodService methodService){ + this.methodService = methodService; + } + @GetMapping("/hello") + public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { + return String.format("Hello %s!", name); + } + @CrossOrigin + @GetMapping(value = "/add") + public String doSum(@RequestParam(value = "a", defaultValue = "0") Object a, + @RequestParam(value = "b", defaultValue = "0") Object b, + @RequestParam(value = "type", defaultValue = "int") String type){ + return methodService.doSum(a,b, type); + } + @GetMapping("/sub") + public String doSub(@RequestParam(value = "a", defaultValue = "0") Object a, + @RequestParam(value = "b", defaultValue = "0") Object b, + @RequestParam(value = "type", defaultValue = "int") String type){ + return methodService.doSub(a, b, type); + } + @GetMapping("/mul") + public String doMul(@RequestParam(value = "a", defaultValue = "0") Object a, + @RequestParam(value = "b", defaultValue = "0") Object b, + @RequestParam(value = "type", defaultValue = "int") String type){ + return methodService.doMul(a, b, type); + } + @GetMapping("/del") + public String doDel(@RequestParam(value = "a", defaultValue = "0") Object a, + @RequestParam(value = "b", defaultValue = "1") Object b, + @RequestParam(value = "type", defaultValue = "int") String type){ + return methodService.doDel(a, b, type); + } + @GetMapping("/mu") + public String len(@RequestParam(value = "word", defaultValue = "") String name){ + return String.format("Длина слова " + name + " " + len(name)); + } + @GetMapping("/de") + public String root(){ + return new Date().toString(); + } +} diff --git a/src/main/java/ru/ulstu/is/myapp/domain/BoolMethod.java b/src/main/java/ru/ulstu/is/myapp/domain/BoolMethod.java new file mode 100644 index 0000000..b524bf4 --- /dev/null +++ b/src/main/java/ru/ulstu/is/myapp/domain/BoolMethod.java @@ -0,0 +1,28 @@ +package ru.ulstu.is.myapp.domain; + +import org.springframework.stereotype.Component; + +@Component(value="boolean") +public class BoolMethod implements IMethods{ + @Override + public Boolean doSum(Boolean a, Boolean b) { + return a || b; + } + + @Override + public Boolean doSub(Boolean a, Integer b) { + return (!a && b%2==0) || (a && !(b%2==0)); + } + + @Override + public Boolean doMul(Boolean a, Integer b) { + return a && b%2 != 0; + } + + @Override + public Boolean doDel(Boolean a, Integer b) { + if (b%2==0) return false; + else if(!a) return false; + else return true; + } +} diff --git a/src/main/java/ru/ulstu/is/myapp/domain/IMethods.java b/src/main/java/ru/ulstu/is/myapp/domain/IMethods.java new file mode 100644 index 0000000..dd6002a --- /dev/null +++ b/src/main/java/ru/ulstu/is/myapp/domain/IMethods.java @@ -0,0 +1,9 @@ +package ru.ulstu.is.myapp.domain; + +public interface IMethods { + T doSum(T a, T b); + T doSub(T a, Integer b); + T doMul(T a, Integer b); + T doDel(T a, Integer b); + +} diff --git a/src/main/java/ru/ulstu/is/myapp/domain/IntMethod.java b/src/main/java/ru/ulstu/is/myapp/domain/IntMethod.java new file mode 100644 index 0000000..67843a6 --- /dev/null +++ b/src/main/java/ru/ulstu/is/myapp/domain/IntMethod.java @@ -0,0 +1,30 @@ +package ru.ulstu.is.myapp.domain; + +import org.springframework.stereotype.Component; + +@Component(value="int") +public class IntMethod implements IMethods { + + @Override + public Integer doSum(Integer a, Integer b) { + return a + b; + } + + @Override + public Integer doSub(Integer a, Integer b) { + return a - b; + } + + @Override + public Integer doMul(Integer a, Integer b) { + return a*b; + } + + @Override + public Integer doDel(Integer a, Integer b) { + if(b!=0){ + return a/b; + } + return null; + } +} diff --git a/src/main/java/ru/ulstu/is/myapp/domain/StrMethod.java b/src/main/java/ru/ulstu/is/myapp/domain/StrMethod.java new file mode 100644 index 0000000..75ef737 --- /dev/null +++ b/src/main/java/ru/ulstu/is/myapp/domain/StrMethod.java @@ -0,0 +1,49 @@ +package ru.ulstu.is.myapp.domain; + +import org.springframework.stereotype.Component; + +@Component(value="string") +public class StrMethod implements IMethods { + + @Override + public String doSum(String a, String b) { + return a.concat(b); + } + + @Override + public String doSub(String a, Integer b) { + String temp = a; + if(temp.length() >= b){ + return temp.substring(0, a.length() - b); + }else{ + return a; + } + } + + @Override + public String doMul(String a, Integer b) { + String str = " "; + if(b == 0){ + return str; + } + for(int i = 0; i < b; i++){ + str = str + a; + } + return str; + } + + @Override + public String doDel(String a, Integer b) { + if(b == 0){ + return "На ноль делить нельзя!"; + } + char[] chara = a.toCharArray(); + String result = ""; + for(int i = 0; i < chara.length/b; i++){ + result += chara[i]; + } + return result; + } + + +} diff --git a/src/main/java/ru/ulstu/is/myapp/service/MethodService.java b/src/main/java/ru/ulstu/is/myapp/service/MethodService.java new file mode 100644 index 0000000..4b257fc --- /dev/null +++ b/src/main/java/ru/ulstu/is/myapp/service/MethodService.java @@ -0,0 +1,76 @@ +package ru.ulstu.is.myapp.service; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Service; +import ru.ulstu.is.myapp.domain.BoolMethod; +import ru.ulstu.is.myapp.domain.IMethods; +import ru.ulstu.is.myapp.domain.IntMethod; +import ru.ulstu.is.myapp.domain.StrMethod; + +@Service +public class MethodService { + private final ApplicationContext applicationContext; + + public MethodService(ApplicationContext applicationContext){ + this.applicationContext = applicationContext; + + } + + public String doSum(Object a, Object b, String type){ + final IMethods method = (IMethods) applicationContext.getBean(type); + if(method instanceof StrMethod){ + System.out.print("str"); + return String.format("%s" , method.doSum(a, b)); + } + if(method instanceof IntMethod){ + System.out.print("int"); + return String.format("%s" , method.doSum(Integer.parseInt(a.toString()), Integer.parseInt(b.toString()))); + } + if(method instanceof BoolMethod){ + System.out.print("bool"); + return String.format("%s" , method.doSum(Boolean.parseBoolean(a.toString()), Boolean.parseBoolean(b.toString()))); + } + return null; + } + public String doSub(Object a, Object b, String type){ + final IMethods method = (IMethods) applicationContext.getBean(type); + if(method instanceof StrMethod){ + return String.format("%s" , method.doSub(a.toString(), Integer.parseInt(b.toString()))); + } + if(method instanceof IntMethod){ + return String.format("%s" , method.doSub(Integer.parseInt(a.toString()), Integer.parseInt(b.toString()))); + } + if(method instanceof BoolMethod){ + return String.format("%s" , method.doSub(Boolean.parseBoolean(a.toString()), Integer.parseInt(b.toString()))); + } + return null; + } + public String doMul(Object a, Object b, String type){ + final IMethods method = (IMethods) applicationContext.getBean(type); + if(method instanceof StrMethod){ + return String.format("%s" , method.doMul(a.toString(), Integer.parseInt(b.toString()))); + } + if(method instanceof IntMethod){ + return String.format("%s" , method.doMul(Integer.parseInt(a.toString()), Integer.parseInt(b.toString()))); + } + if(method instanceof BoolMethod){ + return String.format("%s" , method.doMul(Boolean.parseBoolean(a.toString()), Integer.parseInt(b.toString()))); + } + return null; + } + public String doDel(Object a, Object b, String type){ + final IMethods method = (IMethods) applicationContext.getBean(type); + if(method instanceof StrMethod){ + return String.format("%s" , method.doDel(a.toString(), Integer.parseInt(b.toString()))); + } + if(method instanceof IntMethod){ + return String.format("%s" , method.doDel(Integer.parseInt(a.toString()), Integer.parseInt(b.toString()))); + } + if(method instanceof BoolMethod){ + return String.format("%s" , method.doDel(Boolean.parseBoolean(a.toString()), Integer.parseInt(b.toString()))); + } + return null; + } + +} diff --git a/src/test/java/ru/ulstu/is/myapp/MyappApplicationTests.java b/src/test/java/ru/ulstu/is/myapp/MyappApplicationTests.java index e5d6918..80b60e8 100644 --- a/src/test/java/ru/ulstu/is/myapp/MyappApplicationTests.java +++ b/src/test/java/ru/ulstu/is/myapp/MyappApplicationTests.java @@ -1,13 +1,91 @@ package ru.ulstu.is.myapp; +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; +import ru.ulstu.is.myapp.service.MethodService; @SpringBootTest class MyappApplicationTests { - + @Autowired + private MethodService methodService; @Test void contextLoads() { } + //sum + @Test + void testSumStr(){ + String res = methodService.doSum("Hi, ", "Dima", "string"); + Assertions.assertEquals("Hi, Dima", res); + } + @Test + void testSumInt(){ + Integer res = Integer.valueOf(methodService.doSum(5, 5, "int")); + Assertions.assertEquals(10, res); + } + @Test + void testSumBool(){ + Boolean res = Boolean.valueOf(methodService.doSum(true, true, "boolean")); + Assertions.assertEquals(true, res); + } + + //Sub + @Test + void testSubStr(){ + String res = methodService.doSub("hello", 1, "string"); + Assertions.assertEquals("hell", res); + } + @Test + void testSubStr2(){ + Assertions.assertThrows(NumberFormatException.class, () -> methodService.doSub("hello", 2, "int")); + } + @Test + void testSubInt(){ + Integer res = Integer.valueOf(methodService.doSub(5, 2, "int")); + Assertions.assertEquals(3, res); + } + @Test + void testSubBool(){ + Boolean res = Boolean.valueOf(methodService.doSub(true, 0, "boolean")); + Assertions.assertEquals(false, res); + } + + //Mul + @Test + void testMulStr(){ + String res = methodService.doMul("hello", 2, "string"); + Assertions.assertEquals(" hellohello", res); + } + @Test + void testMulInt(){ + Integer res = Integer.valueOf(methodService.doMul(5, 2, "int")); + Assertions.assertEquals(10, res); + } + @Test + void testMulBool(){ + Boolean res = Boolean.valueOf(methodService.doMul(true, 0, "boolean")); + Assertions.assertEquals(false, res); + } + + //Del + @Test + void testDelStr(){ + String res = methodService.doDel("hello0", 2, "string"); + Assertions.assertEquals("hel", res); + } + @Test + void testDelInt(){ + Integer res = Integer.valueOf(methodService.doDel(6, 2, "int")); + Assertions.assertEquals(3, res); + } + @Test + void testDelBool(){ + Boolean res = Boolean.valueOf(methodService.doDel(true, 2, "boolean")); + Assertions.assertEquals(false, res); + } + + }