Compare commits

..

1 Commits

Author SHA1 Message Date
7fa4777711 врварвава 2023-03-04 15:27:44 +03:00
10 changed files with 60 additions and 340 deletions

View File

@ -3,7 +3,6 @@ function setResult(result) {
document.getElementById("result").innerHTML = lbl;
}
function add(){
executeRequest("add");
}
@ -27,11 +26,10 @@ function enterArray(){
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}&type=${type}`)
fetch(`http://localhost:8080/${address}?a=${num1}&b=${num2}`)
.then(response => {
return response.text();
return response.json();
})
.then(result => {
setResult(result);
@ -40,7 +38,7 @@ function executeRequest(address) {
function executeRequestArray(address) {
let array = document.getElementById("enterArray").value;
fetch(`http://localhost:8080/${address}?InputNumbers=${array}&type=${type}`)
fetch(`http://localhost:8080/${address}?InputNumbers=${array}`)
.then(response => {
return response.json();
})

View File

@ -9,27 +9,24 @@
<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="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>
<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%;">
<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>
<input type="text" class="form-control" id="enterArray" placeholder="" value="0" required="" style="width: 10%;">
<button class="btn btn-success" onclick="enterArray()">Ввести</button>
<div id="result">Ответ:</div>
<br>
<br>
</div>
</main>
<script src="MyScript.js"></script>

View File

@ -15,9 +15,57 @@ 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){
if(b!=0) {
return a / b;
}
return null;
}
@GetMapping("/mu")
public String len(@RequestParam(value = "word", defaultValue = "") String name){
return String.format("Длина слова " + name + " " + len(name));
}
@CrossOrigin
@GetMapping("/array")
public Integer[] massive(@RequestParam(value = "InputNumbers") Integer[] inputNumbers){
String str = "";
for(int i = 0; i < inputNumbers.length; i++){
inputNumbers[i] = (inputNumbers[i] + 1);
}
return inputNumbers;
}
@GetMapping("/de")
public String root(){
return new Date().toString();
}

View File

@ -1,53 +0,0 @@
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();
}
}

View File

@ -1,28 +0,0 @@
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;
}
}

View File

@ -1,9 +0,0 @@
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);
}

View File

@ -1,30 +0,0 @@
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;
}
}

View File

@ -1,49 +0,0 @@
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;
}
}

View File

@ -1,76 +0,0 @@
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;
}
}

View File

@ -1,91 +1,13 @@
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);
}
}