Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
108665ee17 |
@ -2,40 +2,49 @@ function setResult(result) {
|
||||
let lbl = `<label class="bg-success">Ответ:${result}</label>`;
|
||||
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);
|
||||
})
|
||||
}
|
@ -9,20 +9,27 @@
|
||||
<body>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
|
||||
<main>
|
||||
<div class = "container" style="margin: 1%">
|
||||
<h4 class="mb-3">Команды</h4>
|
||||
<p>Сложение</p>
|
||||
<input type="number" class="form-control" id="addNum1" placeholder="" value="0" required="" style="width: 10%;">
|
||||
<input type="number" class="form-control" id="addNum2" placeholder="" value="0" required="" style="width: 10%;">
|
||||
|
||||
<input type="text" class="form-control" id="addNum1" placeholder="" value="0" required="" style="width: 10%;">
|
||||
<input type="text" class="form-control" id="addNum2" placeholder="" value="0" required="" style="width: 10%;">
|
||||
<select class="form-select form-select-lg mb-3" id= "type" aria-label=".form-select-lg example" style="width: 10%">
|
||||
<option selected>Тип</option>
|
||||
<option value="string">string</option>
|
||||
<option value="int">int</option>
|
||||
<option value="boolean">boolean</option>
|
||||
</select>
|
||||
<button class="btn btn-success" onclick="add()">Сложить</button>
|
||||
<button class="btn btn-success" onclick="sub()">Вычесть</button>
|
||||
<button class="btn btn-success" onclick="mul()">Умножить</button>
|
||||
<button class="btn btn-success" onclick="del()">Поделить</button>
|
||||
|
||||
<div id="result">Ответ:</div>
|
||||
|
||||
|
||||
<div id="result">Ответ:</div>
|
||||
|
||||
|
||||
</div>
|
||||
</main>
|
||||
<script src="MyScript.js"></script>
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
53
src/main/java/ru/ulstu/is/myapp/controller/Controller.java
Normal file
53
src/main/java/ru/ulstu/is/myapp/controller/Controller.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
28
src/main/java/ru/ulstu/is/myapp/domain/BoolMethod.java
Normal file
28
src/main/java/ru/ulstu/is/myapp/domain/BoolMethod.java
Normal file
@ -0,0 +1,28 @@
|
||||
package ru.ulstu.is.myapp.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="boolean")
|
||||
public class BoolMethod implements IMethods<Boolean>{
|
||||
@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;
|
||||
}
|
||||
}
|
9
src/main/java/ru/ulstu/is/myapp/domain/IMethods.java
Normal file
9
src/main/java/ru/ulstu/is/myapp/domain/IMethods.java
Normal file
@ -0,0 +1,9 @@
|
||||
package ru.ulstu.is.myapp.domain;
|
||||
|
||||
public interface IMethods<T> {
|
||||
T doSum(T a, T b);
|
||||
T doSub(T a, Integer b);
|
||||
T doMul(T a, Integer b);
|
||||
T doDel(T a, Integer b);
|
||||
|
||||
}
|
30
src/main/java/ru/ulstu/is/myapp/domain/IntMethod.java
Normal file
30
src/main/java/ru/ulstu/is/myapp/domain/IntMethod.java
Normal file
@ -0,0 +1,30 @@
|
||||
package ru.ulstu.is.myapp.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="int")
|
||||
public class IntMethod implements IMethods<Integer> {
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
49
src/main/java/ru/ulstu/is/myapp/domain/StrMethod.java
Normal file
49
src/main/java/ru/ulstu/is/myapp/domain/StrMethod.java
Normal file
@ -0,0 +1,49 @@
|
||||
package ru.ulstu.is.myapp.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="string")
|
||||
public class StrMethod implements IMethods<String> {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
||||
}
|
76
src/main/java/ru/ulstu/is/myapp/service/MethodService.java
Normal file
76
src/main/java/ru/ulstu/is/myapp/service/MethodService.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user