second labWork
This commit is contained in:
parent
d67019aac6
commit
60dfe21df8
@ -1,40 +1,52 @@
|
||||
const operateButton = document.getElementById("operate");
|
||||
const operateStringButton = document.getElementById("operateString");
|
||||
const val1 = document.getElementById("val1");
|
||||
const val2 = document.getElementById("val2");
|
||||
const select = document.getElementById("operation");
|
||||
const finish = document.getElementById("finish");
|
||||
const operateFButton = document.getElementById("operateF");
|
||||
const operateFStringButton = document.getElementById("operateFString");
|
||||
const float1 = document.getElementById("float1");
|
||||
const float2 = document.getElementById("float2");
|
||||
const selectF = document.getElementById("operationF");
|
||||
const finishF = document.getElementById("finishF");
|
||||
|
||||
operateButton.onclick=function(){
|
||||
operate();
|
||||
operate("number");
|
||||
};
|
||||
|
||||
function operate(){
|
||||
operateStringButton.onclick=function(){
|
||||
operate("string");
|
||||
}
|
||||
|
||||
operateFButton.onclick=function(){
|
||||
operate("numberF");
|
||||
};
|
||||
|
||||
function operate(param){
|
||||
switch(parseInt(select.value)){
|
||||
case 1:
|
||||
doOperation("calc1")
|
||||
doOperation("calc1", param)
|
||||
break;
|
||||
case 2:
|
||||
doOperation("calc2")
|
||||
doOperation("calc2", param)
|
||||
break;
|
||||
case 3:
|
||||
doOperation("calc3")
|
||||
doOperation("calc3", param)
|
||||
break;
|
||||
case 4:
|
||||
doOperation("calc4")
|
||||
doOperation("calc4", param)
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
function checkNumber(res){
|
||||
if(res.indexOf(".") != -1){
|
||||
return parseInt(res);
|
||||
}
|
||||
else{
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
async function doOperation(address) {
|
||||
const response = await fetch(`http://localhost:8080/${address}?val1=${val1.value}&val2=${val2.value}`)
|
||||
let res = await response.text()
|
||||
finish.innerHTML = checkNumber(res);
|
||||
function doOperation(address, type){
|
||||
console.log("Тип переменных: " + type)
|
||||
fetch(`http://localhost:8080/${address}?val1=${val1.value}&val2=${val2.value}&type=${type}`)
|
||||
.then(response => response.text())
|
||||
.then(res=>finish.innerHTML=checkNumber(res));
|
||||
}
|
||||
|
@ -14,29 +14,33 @@
|
||||
<form class=" d-flex flex-column align-items-center text-center">
|
||||
<div class="">
|
||||
Введите число
|
||||
<input id="val1" class="form-control" type="number" value="0" />
|
||||
<input id="val1" class="form-control" type="numberF" value="0" />
|
||||
</div>
|
||||
<div>
|
||||
Выберите действие
|
||||
<select class="form-select" id="operation">
|
||||
<option value="1">умножить</option>
|
||||
<option value="2">возвести в степень</option>
|
||||
<option value="3">факториал</option>
|
||||
<option value="4">число Фибоначчи</option>
|
||||
<option value="2">сложить</option>
|
||||
<option value="3">вычесть</option>
|
||||
<option value="4">делить</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
Введите второе число
|
||||
<input id="val2" class="form-control" type="number" value="0" />
|
||||
<input id="val2" class="form-control" type="numberF" value="0" />
|
||||
</div>
|
||||
<div>
|
||||
<button type="button" class="btn btn-primary" id="operate">Вычислить</button>
|
||||
</div>
|
||||
<div>
|
||||
<button type="button" class="btn btn-primary" id="operateF">Вычислить вещественное</button>
|
||||
</div>
|
||||
<div>
|
||||
<button type="button" class="btn btn-primary" id="operateString">Строки</button>
|
||||
</div>
|
||||
<div>
|
||||
Результат:
|
||||
<p id="finish"></p>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script src="../../../../demo/demo/MainPage/LogForMainPage.js"></script>
|
||||
<script src="LogForMainPage.js"></script>
|
||||
</body>
|
@ -0,0 +1,40 @@
|
||||
package com.example.demo.Calculator.Controller;
|
||||
|
||||
import com.example.demo.Calculator.Service.CalculatorService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class CalculatorController {
|
||||
private final CalculatorService calculatorService;
|
||||
|
||||
public CalculatorController(CalculatorService calculatorService){
|
||||
this.calculatorService=calculatorService;
|
||||
}
|
||||
|
||||
@GetMapping("/calc1")
|
||||
public Object calc1(@RequestParam(value = "val1", defaultValue = "null") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "null") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||
return calculatorService.calc1(val1, val2, type);
|
||||
}
|
||||
@GetMapping("/calc2")
|
||||
public Object calc2(@RequestParam(value = "val1", defaultValue = "null") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "null") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||
return calculatorService.calc2(val1, val2, type);
|
||||
}
|
||||
@GetMapping("/calc3")
|
||||
public Object calc3(@RequestParam(value = "val1", defaultValue = "null") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "null") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||
return calculatorService.calc3(val1, val2, type);
|
||||
}
|
||||
@GetMapping("/calc4")
|
||||
public Object calc4(@RequestParam(value = "val1", defaultValue = "null") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "null") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||
return calculatorService.calc4(val1, val2, type);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.example.demo.Calculator.Domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Component(value="numberF")
|
||||
public class CalculatorDoubleArray implements ICalculator<String[]>, InitializingBean, DisposableBean {
|
||||
private final Logger log= LoggerFactory.getLogger(CalculatorDoubleArray.class);
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet(){
|
||||
log.info("calling CalculatorDoubleArray.afterPropertiesSet()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
log.info("calling CalculatorDoubleArray.destroy()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] calc1(String[] val1, String[] val2) {
|
||||
String[] resArr = Stream.concat(Arrays.stream(val1),
|
||||
Arrays.stream(val2))
|
||||
.toArray(String[]::new);;
|
||||
return resArr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] calc2(String[] val1, String[] val2) {
|
||||
String[] resArr = Stream.concat(Arrays.stream(val1),
|
||||
Arrays.stream(val2))
|
||||
.toArray(String[]::new);;
|
||||
return resArr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] calc3(String[] val1, String[] val2) {
|
||||
String[] resArr = Stream.concat(Arrays.stream(val1),
|
||||
Arrays.stream(val2))
|
||||
.toArray(String[]::new);;
|
||||
return resArr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] calc4(String[] val1, String[] val2) {
|
||||
String[] resArr = Stream.concat(Arrays.stream(val1),
|
||||
Arrays.stream(val2))
|
||||
.toArray(String[]::new);;
|
||||
return resArr;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.example.demo.Calculator.Domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="number")
|
||||
public class CalculatorNumber implements ICalculator<Integer>, InitializingBean, DisposableBean{
|
||||
private final Logger log=LoggerFactory.getLogger(CalculatorNumber.class);
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet(){
|
||||
log.info("calling CalculatorNumber.afterPropertiesSet()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
log.info("calling CalculatorNumber.destroy()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calc1(Integer val1, Integer val2) {
|
||||
return val1 * val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calc2(Integer val1, Integer val2) {
|
||||
return val1 + val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calc3(Integer val1, Integer val2) {
|
||||
return val1 - val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calc4(Integer val1, Integer val2) {
|
||||
return val1 / val2;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.example.demo.Calculator.Domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="string")
|
||||
public class CalculatorString implements ICalculator<String>, InitializingBean, DisposableBean {
|
||||
private final Logger log= LoggerFactory.getLogger(CalculatorString.class);
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
log.info("calling CalculatorString.afterPropertiesSet()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
log.info("CalculatorString.destroy()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String calc1(String val1, String val2) {
|
||||
String temp = val1;
|
||||
for (int i = 0; i < Integer.parseInt(val2) - 1; i++) {
|
||||
temp = calc2(temp, val1);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String calc2(String val1, String val2) {
|
||||
return val1.concat(val2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String calc3(String val1, String val2) {
|
||||
return val1.replaceFirst(val2, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String calc4(String val1, String val2) {
|
||||
return val1.replaceAll(val2, "");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.demo.Calculator.Domain;
|
||||
|
||||
public interface ICalculator<T> {
|
||||
T calc1(T val1, T val2);
|
||||
|
||||
T calc2(T val1, T val2);
|
||||
|
||||
T calc3(T val1, T val2);
|
||||
|
||||
T calc4(T val1, T val2);
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.example.demo.Calculator.Service;
|
||||
|
||||
import com.example.demo.Calculator.Domain.ICalculator;
|
||||
import com.example.demo.Calculator.Domain.CalculatorNumber;
|
||||
import com.example.demo.Calculator.Domain.CalculatorDoubleArray;
|
||||
import com.example.demo.Calculator.Domain.CalculatorString;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
@Service
|
||||
public class CalculatorService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public CalculatorService(ApplicationContext applicationContext){
|
||||
this.applicationContext=applicationContext;
|
||||
}
|
||||
|
||||
public Object calc2(Object val1, Object val2, String type){
|
||||
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
|
||||
if (type.equals("string")) {
|
||||
return temp.calc2(val1.toString(), val2.toString());
|
||||
}
|
||||
else if (type.equals("number")) {
|
||||
return temp.calc2(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.equals("numberF")) {
|
||||
return temp.calc2(convToArr(val1) , convToArr(val2));
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public Object calc1(Object val1, Object val2, String type){
|
||||
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
|
||||
if (type.equals("string")) {
|
||||
return temp.calc1(val1.toString(), val2.toString());
|
||||
}
|
||||
else if (type.equals("number")) {
|
||||
return temp.calc1(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.equals("numberF")) {
|
||||
return temp.calc1(convToArr(val1) , convToArr(val2));
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public Object calc3(Object val1, Object val2, String type){
|
||||
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
|
||||
if (type.equals("string")) {
|
||||
return temp.calc3(val1.toString(), val2.toString());
|
||||
}
|
||||
else if (type.equals("number")) {
|
||||
return temp.calc3(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.equals("numberF")) {
|
||||
return temp.calc3(convToArr(val1) , convToArr(val2));
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public Object calc4(Object val1, Object val2, String type){
|
||||
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
|
||||
if (type.equals("string")) {
|
||||
return temp.calc4(val1.toString(), val2.toString());
|
||||
}
|
||||
else if (type.equals("number")) {
|
||||
return temp.calc4(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.equals("numberF")) {
|
||||
return temp.calc4(convToArr(val1) , convToArr(val2));
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public String[] convToArr(Object arr){
|
||||
return Arrays.stream(arr.toString().split("[\\(\\)\\,\\[\\] \\!\\?\\:\\;]")).filter(e -> e.trim().length()>0).toArray(String[]::new);
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ public class DemoApplication {
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/hello")
|
||||
/*@GetMapping("/hello")
|
||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||
return String.format("Hello %s!", name);
|
||||
}
|
||||
@ -60,5 +60,5 @@ public class DemoApplication {
|
||||
} else {
|
||||
return calc4(val1 - 1) + calc4(val1 - 2);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -1,13 +1,109 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import com.example.demo.Calculator.Service.CalculatorService;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoApplicationTests {
|
||||
@Autowired
|
||||
CalculatorService calculatorService;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
void testCalculatorNumberCalc2() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc2(val1, val2, "number");
|
||||
Assertions.assertEquals("110", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorNumberCalc3() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc3(val1, val2, "number");
|
||||
Assertions.assertEquals("90", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorNumberCalc1() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc1(val1, val2, "number");
|
||||
Assertions.assertEquals("1000", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorNumberCalc4() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc4(val1, val2, "number");
|
||||
Assertions.assertEquals("10", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorStringCalc2() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc2(val1.toString(), val2.toString(), "string");
|
||||
Assertions.assertEquals("10010", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorStringCalc3() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc3(val1.toString(), val2.toString(), "string");
|
||||
Assertions.assertEquals("0", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorStringCalc1() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc1(val1.toString(), val2.toString(), "string");
|
||||
Assertions.assertEquals("100100100100100100100100100100", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorStringCalc4() {
|
||||
Object val1 = 100;
|
||||
Object val2 = 10;
|
||||
final Object result = calculatorService.calc4(val1.toString(), val2.toString(), "string");
|
||||
Assertions.assertEquals("0", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorFloatNumberCalc2() {
|
||||
Object val1 = 100.10;
|
||||
Object val2 = 10.10;
|
||||
final Object result = calculatorService.calc2(val1, val2, "numberF");
|
||||
Assertions.assertEquals("110.20", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorFloatNumberCalc3() {
|
||||
Object val1 = 100.10;
|
||||
Object val2 = 10.10;
|
||||
final Object result = calculatorService.calc3(val1, val2, "numberF");
|
||||
Assertions.assertEquals("90", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorFloatNumberCalc1() {
|
||||
Object val1 = 100.10;
|
||||
Object val2 = 10.10;
|
||||
final Object result = calculatorService.calc1(val1, val2, "numberF");
|
||||
Assertions.assertEquals("1011.01", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculatorFloatNumberCalc4() {
|
||||
Object val1 = 10.5;
|
||||
Object val2 = 2;
|
||||
final Object result = calculatorService.calc4(val1, val2, "numberF");
|
||||
Assertions.assertEquals("5.25", result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user