сдана 2 лабораторная
This commit is contained in:
parent
50b59892fb
commit
6c9446844e
@ -1,35 +0,0 @@
|
||||
package com.example.demo.Controllers;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
public class Controllers {
|
||||
@GetMapping("/")
|
||||
public @ResponseBody String hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
|
||||
return String.format("Hello %s!", name);
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public @ResponseBody Integer sum(@RequestParam(required = false, defaultValue = "0") int val1,
|
||||
@RequestParam(required = false, defaultValue = "0") int val2){
|
||||
return val1 + val2;
|
||||
}
|
||||
|
||||
@GetMapping("/toLowerCase")
|
||||
public @ResponseBody String toLowerCase(@RequestParam(defaultValue = "TO LOW CASE") String value) {
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
@GetMapping("/minus")
|
||||
public @ResponseBody Integer minus(@RequestParam(required = false, defaultValue = "0") int val1,
|
||||
@RequestParam(required = false, defaultValue = "0") int val2){
|
||||
return val1 - val2;
|
||||
}
|
||||
|
||||
@GetMapping("/join")
|
||||
public @ResponseBody String join(@RequestParam(required = false, defaultValue = "0") String str1,
|
||||
@RequestParam(required = false, defaultValue = "0") String str2){
|
||||
return String.join("", str1, str2);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.demo.method.controller;
|
||||
|
||||
import com.example.demo.method.service.MethodService;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
public class MethodController {
|
||||
private final MethodService methodService;
|
||||
|
||||
public MethodController(MethodService methodService) {
|
||||
this.methodService = methodService;
|
||||
}
|
||||
|
||||
@GetMapping("/summa")
|
||||
public String qw(@RequestParam(value = "first", defaultValue = "1") Object first,
|
||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
||||
return methodService.Sum(first,second,type);
|
||||
}
|
||||
|
||||
@GetMapping("/min")
|
||||
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 methodService.Min(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 methodService.Mult(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 methodService.Div(first,second,type);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.demo.method.domain;
|
||||
|
||||
public interface IMethod<T> {
|
||||
T Sum(T first, T second);
|
||||
|
||||
T Mult(T first, T second);
|
||||
|
||||
T Min(T first, T second);
|
||||
|
||||
T Div(T first, T second);
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.example.demo.method.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="int")
|
||||
public class MethodInt implements IMethod<Integer>{
|
||||
public Integer Sum(Integer first, Integer second) {
|
||||
return first + second;
|
||||
}
|
||||
|
||||
public Integer Mult(Integer first, Integer second) {
|
||||
return first * second;
|
||||
}
|
||||
|
||||
public Integer Min(Integer first, Integer second) {
|
||||
return first - second;
|
||||
}
|
||||
|
||||
public Integer Div(Integer first, Integer second) {
|
||||
if (second == 0) second = 1;
|
||||
return first/second;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.example.demo.method.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="str")
|
||||
public class MethodString implements IMethod<String>{
|
||||
@Override
|
||||
public String Sum(String first, String second) {
|
||||
return first + "+" + second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Mult(String first, String second) {
|
||||
return first + "*" + second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Min(String first, String second) {
|
||||
return first + "-" + second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Div(String first, String second) {
|
||||
return first + "/" + second;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.example.demo.method.service;
|
||||
|
||||
import com.example.demo.method.domain.IMethod;
|
||||
import com.example.demo.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 operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString)
|
||||
return String.format("%s", operation.Sum(first,second));
|
||||
else
|
||||
|
||||
return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
|
||||
public String Min(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString)
|
||||
return String.format("%s", operation.Min(first,second));
|
||||
else
|
||||
return String.format("%s", operation.Min(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
|
||||
public String Mult(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString)
|
||||
return String.format("%s", operation.Mult(first,second));
|
||||
else
|
||||
return String.format("%s", operation.Mult(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
|
||||
public String Div(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString)
|
||||
return String.format("%s", operation.Div(first,second));
|
||||
else
|
||||
return String.format("%s", operation.Div(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.example.demo.speaker.controller;
|
||||
import com.example.demo.speaker.service.SpeakerService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@ -17,4 +18,27 @@ public class SpeakerController {
|
||||
@RequestParam(defaultValue = "ru") String lang){
|
||||
return speakerService.say(name, lang);
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public @ResponseBody Integer sum(@RequestParam(required = false, defaultValue = "0") int val1,
|
||||
@RequestParam(required = false, defaultValue = "0") int val2){
|
||||
return val1 + val2;
|
||||
}
|
||||
|
||||
@GetMapping("/toLowerCase")
|
||||
public @ResponseBody String toLowerCase(@RequestParam(defaultValue = "TO LOW CASE") String value) {
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
@GetMapping("/minus")
|
||||
public @ResponseBody Integer minus(@RequestParam(required = false, defaultValue = "0") int val1,
|
||||
@RequestParam(required = false, defaultValue = "0") int val2){
|
||||
return val1 - val2;
|
||||
}
|
||||
|
||||
@GetMapping("/join")
|
||||
public @ResponseBody String join(@RequestParam(required = false, defaultValue = "0") String str1,
|
||||
@RequestParam(required = false, defaultValue = "0") String str2){
|
||||
return String.join("", str1, str2);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,71 @@
|
||||
package com.example.demo;
|
||||
|
||||
import com.example.demo.method.service.MethodService;
|
||||
import com.example.demo.speaker.service.SpeakerService;
|
||||
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 DemoApplicationTests {
|
||||
|
||||
@Autowired
|
||||
MethodService methodService;
|
||||
@Test
|
||||
void contextLoads() {
|
||||
void testSumInt() {
|
||||
final String res = methodService.Sum(12, 12, "int");
|
||||
Assertions.assertEquals(24, Integer.parseInt(res));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMinInt() {
|
||||
final String res = methodService.Min(12, 12, "int");
|
||||
Assertions.assertEquals(0, Integer.parseInt(res));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultInt() {
|
||||
final String res = methodService.Mult(2, 3, "int");
|
||||
Assertions.assertEquals(6, Integer.parseInt(res));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDivInt() {
|
||||
final String res = methodService.Div(12, 6, "int");
|
||||
Assertions.assertEquals(2, Integer.parseInt(res));
|
||||
}
|
||||
@Test
|
||||
void testSumStr() {
|
||||
final String res = methodService.Sum("12", "12", "str");
|
||||
Assertions.assertEquals("12+12", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMinStr() {
|
||||
final String res = methodService.Min("12", "12", "str");
|
||||
Assertions.assertEquals("12-12", res);
|
||||
}
|
||||
@Test
|
||||
void testMultStr() {
|
||||
final String res = methodService.Mult("12", "12", "str");
|
||||
Assertions.assertEquals("12*12", res);
|
||||
}
|
||||
@Test
|
||||
void testDivStr() {
|
||||
final String res = methodService.Div("12", "12", "str");
|
||||
Assertions.assertEquals("12/12", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoSuchBean() {
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> methodService.Min("12", "12", "string"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNumFormatError() {
|
||||
Assertions.assertThrows(NumberFormatException.class, () -> methodService.Min("12dd", 12, "int"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -7,58 +7,38 @@
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container-sm p-3">
|
||||
<form id="sum">
|
||||
<h2 class="text-center mb-3">Sum</h2>
|
||||
<div class="mb-3">
|
||||
<div class="input-group mb-1">
|
||||
<input id="sumVal1" type="number" class="form-control" placeholder="V1" aria-label="First Number" name="v1" autocomplete="off">
|
||||
<span class="input-group-text">+</span>
|
||||
<input id="sumVal2" type="number" class="form-control" placeholder="V2" aria-label="Second Number" name="v2" autocomplete="off">
|
||||
<span id="sumResult" class="input-group-text" id="add_result">0</span>
|
||||
<button id="sumSubmitButton" type="submit" class="btn btn-primary">Result</button>
|
||||
</div>
|
||||
<div class="container">
|
||||
<form class="mx-2 d-flex flex-column align-items-center text-center">
|
||||
<div>
|
||||
Первый объект
|
||||
<input id="first" class="form-control" type='number' value='0' />
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<form id="minus">
|
||||
<h2 class="text-center mb-3">Minus</h2>
|
||||
<div class="mb-3">
|
||||
<div class="input-group mb-1">
|
||||
<input id="minVal1" type="number" class="form-control" placeholder="V1" aria-label="First Number" name="v1" autocomplete="off">
|
||||
<span class="input-group-text">-</span>
|
||||
<input id="minVal2" type="number" class="form-control" placeholder="V2" aria-label="Second Number" name="v2" autocomplete="off">
|
||||
<span id="minResult" class="input-group-text">0</span>
|
||||
<button id="minSubmitButton" type="submit" class="btn btn-primary">Result</button>
|
||||
</div>
|
||||
<div>
|
||||
Операция
|
||||
<select class="form-select" id="operation">
|
||||
<option value="1">+</option>
|
||||
<option value="2">-</option>
|
||||
<option value="3">*</option>
|
||||
<option value="4">/</option>
|
||||
</select>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<form id="toLowerCase">
|
||||
<h2 class="text-center mb-3">To Lower Case</h2>
|
||||
<div class="mb-3">
|
||||
<div class="input-group mb-1">
|
||||
<input id="lowCaseInput" type="text" class="form-control" placeholder="String" aria-label="Your String" name="value" autocomplete="off">
|
||||
<span id="lowCaseResult" class="input-group-text"></span>
|
||||
<button id="lowCaseButton" type="submit" class="btn btn-primary">Submit</button>
|
||||
</div>
|
||||
<div>
|
||||
Второй объект
|
||||
<input id="second" class="form-control" type='number' value='0' />
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<form id="join">
|
||||
<h2 class="text-center mb-3">Join</h2>
|
||||
<div class="mb-3">
|
||||
<div class="input-group mb-1">
|
||||
<input id="joinInput1" type="text" class="form-control" placeholder="Number" aria-label="Your Number" name="value" autocomplete="off">
|
||||
<span class="input-group-text">join</span>
|
||||
<input id="joinInput2" type="text" class="form-control" placeholder="Number" aria-label="Your Number" name="value" autocomplete="off">
|
||||
<span id="joinResult" class="input-group-text" id="ntb_result"></span>
|
||||
<button id="joinButton" type="submit" class="btn btn-primary">Submit</button>
|
||||
</div>
|
||||
<div>
|
||||
<button type="button" class="btn btn-primary my-2" id="intButton">Integer</button>
|
||||
</div>
|
||||
</form>
|
||||
<div>
|
||||
<button type="button" class="btn btn-primary mb-2" id="strButton">String</button>
|
||||
</div>
|
||||
<div>
|
||||
Результат:
|
||||
<p id="result"> </p>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
<script type="module" src="./xhr.js"></script>
|
||||
<script type="module" src="./index.js"></script>
|
||||
</body>
|
||||
</html>
|
46
front/index.js
Normal file
46
front/index.js
Normal file
@ -0,0 +1,46 @@
|
||||
const intButton = document.getElementById("intButton");
|
||||
const strButton = document.getElementById("strButton");
|
||||
const first = document.getElementById("first");
|
||||
const second = document.getElementById("second");
|
||||
const operation = document.getElementById("operation");
|
||||
const result = document.getElementById("result");
|
||||
|
||||
|
||||
intButton.onclick = () => {
|
||||
method("int");
|
||||
};
|
||||
strButton.onclick = () => {
|
||||
method("str");
|
||||
};
|
||||
|
||||
function method(parametr) {
|
||||
switch (parseInt(operation.value)) {
|
||||
case 1:
|
||||
get("summa", parametr)
|
||||
break;
|
||||
case 2:
|
||||
get("min", parametr)
|
||||
break;
|
||||
case 3:
|
||||
get("multi", parametr)
|
||||
break;
|
||||
case 4:
|
||||
get("div", parametr)
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
function checkNum(res) {
|
||||
if (res.indexOf(".") != -1)
|
||||
return parseInt(res)
|
||||
else
|
||||
return res
|
||||
}
|
||||
|
||||
function get(address, type) {
|
||||
console.log("Тип " + type)
|
||||
|
||||
fetch(`http://localhost:8080/${address}?first=${first.value}&second=${second.value}&type=${type}`)
|
||||
.then(response => response.text())
|
||||
.then(res => result.innerHTML = res);
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>App</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="module" src="./xhr.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -10,10 +10,11 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"vite": "^4.1.0"
|
||||
},
|
||||
},
|
||||
"dependencies": {
|
||||
"ajax": "^0.0.4",
|
||||
"axios": "^1.3.2",
|
||||
"bootstrap": "^5.2.3"
|
||||
"bootstrap": "5.2.1",
|
||||
"@fortawesome/fontawesome-free": "6.2.0"
|
||||
}
|
||||
}
|
||||
|
69
front/xhr.js
69
front/xhr.js
@ -1,69 +0,0 @@
|
||||
const requestURL = 'http://localhost:8080/'
|
||||
const xhr = new XMLHttpRequest()
|
||||
|
||||
const sumButton = document.getElementById('sumSubmitButton')
|
||||
const minButton = document.getElementById('minSubmitButton')
|
||||
const lowCaseButton = document.getElementById('lowCaseButton')
|
||||
const joinButton = document.getElementById('joinButton')
|
||||
|
||||
const get = (url) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
xhr.open('GET', url)
|
||||
|
||||
xhr.onload = () => {
|
||||
if(xhr.status == 200) resolve(xhr.response)
|
||||
else reject(xhr.response)
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
reject(xhr.response)
|
||||
}
|
||||
xhr.send()
|
||||
})
|
||||
}
|
||||
|
||||
sumButton.addEventListener("click", (e) => {
|
||||
e.preventDefault()
|
||||
const value1 = (document.getElementById('sumVal1')).value
|
||||
const value2 = (document.getElementById('sumVal2')).value
|
||||
const res = document.getElementById('sumResult')
|
||||
get(`${requestURL}sum?val1=${value1}&val2=${value2}`)
|
||||
.then(data => {
|
||||
res.innerHTML = data
|
||||
})
|
||||
.catch(err => console.log(err))
|
||||
})
|
||||
|
||||
minButton.addEventListener("click", (e) => {
|
||||
e.preventDefault()
|
||||
const value1 = (document.getElementById('minVal1')).value
|
||||
const value2 = (document.getElementById('minVal2')).value
|
||||
const res = document.getElementById('minResult')
|
||||
get(`${requestURL}minus?val1=${value1}&val2=${value2}`)
|
||||
.then(data => {
|
||||
res.innerHTML = data
|
||||
})
|
||||
.catch(err => console.log(err))
|
||||
})
|
||||
|
||||
lowCaseButton.addEventListener("click", (e) => {
|
||||
e.preventDefault()
|
||||
const value = document.getElementById('lowCaseInput').value
|
||||
const res = document.getElementById('lowCaseResult')
|
||||
get(`${requestURL}toLowerCase?value=${value}`)
|
||||
.then(data => {
|
||||
res.innerHTML = data
|
||||
})
|
||||
.catch(err => console.log(err))
|
||||
})
|
||||
|
||||
joinButton.addEventListener("click", (e) => {
|
||||
e.preventDefault()
|
||||
const str1 = document.getElementById('joinInput1').value
|
||||
const str2 = document.getElementById('joinInput2').value
|
||||
const res = document.getElementById('joinResult')
|
||||
get(`${requestURL}join?str1=${str1}&str2=${str2}`)
|
||||
.then(data => {
|
||||
res.innerHTML = data
|
||||
})
|
||||
.catch(err => console.log(err))
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user