This commit is contained in:
Катя Ихонкина 2023-03-04 22:02:12 +04:00
parent af1bc9135e
commit 3b4ef07c6b
9 changed files with 240 additions and 54 deletions

View File

@ -42,7 +42,7 @@
<div class="row" style="margin-top: 3%">
<div class="col">
<label for="number-1" class="form-label">Первое число</label>
<input type="number" class="form-control" value="0" id="number-1" />
<input class="form-control" value="0" id="number-1" />
</div>
<div class="col">
<label for="operation" class="form-label">Действие</label>
@ -55,11 +55,19 @@
</div>
<div class="col">
<label for="number-2" class="form-label">Второе число</label>
<input type="number" class="form-control" value="0" id="number-2" />
<input class="form-control" value="0" id="number-2" />
</div>
<div class="col">
<button type="button" class="btn btn-success" id="calculate-button">Посчитать</button>
<label for="operation" class="form-label">Тип данных</label>
<select class="form-select" id="typedata">
<option value="1">Число</option>
<option value="2">Строка</option>
</select>
</div>
</div>
<div class="row" style="margin-top: 3%">
<button type="button" class="btn btn-success" id="calculate-button">Посчитать</button>
</div>
<div class="row" style="margin-top: 3%">
<div class="input-group">

View File

@ -2,6 +2,7 @@ let calculateButton = document.getElementById("calculate-button");
let helloButton = document.getElementById("hello-button");
let doButton = document.getElementById("do-button");
let yournamee = document.getElementById("yourname");
let typedata = document.getElementById("typedata");
let numberOneInput = document.getElementById("number-1");
let numberTwoInput = document.getElementById("number-2");
let select = document.getElementById("operation");
@ -43,6 +44,15 @@ function hello() {
}
function calculate() {
let type;
switch (parseInt(typedata.value)) {
case 1:
type = "double";
break;
case 2:
type = "str";
break;
}
let address;
switch (parseInt(select.value)) {
case 1:
@ -58,13 +68,13 @@ function calculate() {
address = "div";
break;
}
executeRequest(address);
executeRequest(type, address);
}
function executeRequest(address) {
function executeRequest(type, address) {
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
fetch(`http://localhost:8080/${address}?num1=${num_1}&num2=${num_2}`)
fetch(`http://localhost:8080/${address}?num1=${num_1}&num2=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => result.textContent ="Ответ: " + res);
}

View File

@ -2,56 +2,10 @@ package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAppApplication.class, args);
}
@GetMapping("/")
public String root(@RequestParam(value = "name", defaultValue = "друг") String name) {
return "Привет, " + name+ "!";
}
@GetMapping("/sum")
public int sum(@RequestParam(value = "num1") int num1,
@RequestParam(value = "num2") int num2) {
return num1 + num2;
}
@GetMapping("/dif")
public int dif(@RequestParam(value = "num1") int num1,
@RequestParam(value = "num2") int num2) {
return num1 - num2;
}
@GetMapping("/mul")
public int mul(@RequestParam(value = "num1") int num1,
@RequestParam(value = "num2") int num2) {
return num1 * num2;
}
@GetMapping("/div")
public double div(@RequestParam(value = "num1") double num1,
@RequestParam(value = "num2") double num2) {
if (num2 == 0) {
return 0;
}
return num1 / num2;
}
@GetMapping("/array")
public int[] array(@RequestParam(value = "array") int[] array,
@RequestParam(value = "num") int num) {
for(int i=0;i<array.length;i++) {
array[i] = array[i] * num;
}
return array;
}
}

View File

@ -1,11 +1,15 @@
package com.example.demo;
package com.example.demo.performer.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
public class PerformerConfiguration implements WebMvcConfigurer {
private final Logger log = LoggerFactory.getLogger(PerformerConfiguration.class);
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");

View File

@ -0,0 +1,57 @@
package com.example.demo.performer.controller;
import com.example.demo.performer.service.PerformerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PerformerController {
private final PerformerService performerService;
public PerformerController(PerformerService performerService) {
this.performerService = performerService;
}
@GetMapping("/")
public String root(@RequestParam(value = "name", defaultValue = "друг") String name) {
return "Привет, " + name+ "!";
}
@GetMapping("/sum")
public String sum(@RequestParam(value = "num1") Object num1,
@RequestParam(value = "num2") Object num2,
@RequestParam(value = "type") String type) {
return performerService.Sum(num1, num2, type);
}
@GetMapping("/dif")
public String dif(@RequestParam(value = "num1") Object num1,
@RequestParam(value = "num2") Object num2,
@RequestParam(value = "type") String type) {
return performerService.Dif(num1, num2, type);
}
@GetMapping("/mul")
public String mul(@RequestParam(value = "num1") Object num1,
@RequestParam(value = "num2") Object num2,
@RequestParam(value = "type") String type) {
return performerService.Mul(num1, num2, type);
}
@GetMapping("/div")
public String div(@RequestParam(value = "num1") Object num1,
@RequestParam(value = "num2") Object num2,
@RequestParam(value = "type") String type) {
return performerService.Div(num1, num2, type);
}
@GetMapping("/array")
public int[] array(@RequestParam(value = "array") int[] array,
@RequestParam(value = "num") int num) {
for(int i=0;i<array.length;i++) {
array[i] = array[i] * num;
}
return array;
}
}

View File

@ -0,0 +1,8 @@
package com.example.demo.performer.domain;
public interface IPerformer<T> {
T Sum(T num1, T num2);
T Dif(T num1, T num2);
T Mul(T num1, T num2);
String Div(T num1, T num2);
}

View File

@ -0,0 +1,43 @@
package com.example.demo.performer.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 = "double")
public class PerformerInteger implements IPerformer<Double>, InitializingBean, DisposableBean {
private final Logger log = LoggerFactory.getLogger(PerformerInteger.class);
@Override
public Double Sum(Double num1, Double num2) {
return num1 + num2;
}
@Override
public Double Dif(Double num1, Double num2) {
return num1 - num2;
}
@Override
public Double Mul(Double num1, Double num2) {
return num1 * num2;
}
@Override
public String Div(Double num1, Double num2) {
if (num2==0) return "нельзя делить на ноль!";
else return Double.toString(num1/num2);
}
@Override
public void afterPropertiesSet() {
log.info("PerformerInteger.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("PerformerInteger.destroy()");
}
}

View File

@ -0,0 +1,44 @@
package com.example.demo.performer.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 = "str")
public class PerformerString implements IPerformer<String>, InitializingBean, DisposableBean {
private final Logger log = LoggerFactory.getLogger(PerformerInteger.class);
@Override
public String Sum(String num1, String num2) {
return num1 + num2;
}
@Override
public String Dif(String num1, String num2) {
return num1.replace(num2, "");
}
@Override
public String Mul(String num1, String num2) {
Integer number2 = Integer.parseInt(num2);
return num1.repeat(number2);
}
@Override
public String Div(String num1, String num2) {
if(num1.contains(num2)) return "содержит";
else return "не содержит";
}
@Override
public void afterPropertiesSet() {
log.info("PerformerString.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("PerformerString.destroy()");
}
}

View File

@ -0,0 +1,58 @@
package com.example.demo.performer.service;
import com.example.demo.performer.domain.IPerformer;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class PerformerService {
private final ApplicationContext applicationContext;
public PerformerService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String Sum(Object num1, Object num2, String type) {
final IPerformer performer = (IPerformer) applicationContext.getBean(type);
if (type.equals("double")) {
Integer number1 = Integer.parseInt(String.valueOf(num1));
Integer number2 = Integer.parseInt(String.valueOf(num2));
return String.format("%s", performer.Sum(number1, number2));
} else {
return String.format("%s", performer.Sum(num1, num2));
}
}
public String Dif(Object num1, Object num2, String type) {
final IPerformer performer = (IPerformer) applicationContext.getBean(type);
if (type.equals("double")) {
Integer number1 = Integer.parseInt(String.valueOf(num1));
Integer number2 = Integer.parseInt(String.valueOf(num2));
return String.format("%s", performer.Dif(number1, number2));
} else {
return String.format("%s", performer.Dif(num1, num2));
}
}
public String Mul(Object num1, Object num2, String type) {
final IPerformer performer = (IPerformer) applicationContext.getBean(type);
if (type.equals("double")) {
Integer number1 = Integer.parseInt(String.valueOf(num1));
Integer number2 = Integer.parseInt(String.valueOf(num2));
return String.format("%s", performer.Mul(number1, number2));
} else {
return String.format("%s", performer.Mul(num1, num2));
}
}
public String Div(Object num1, Object num2, String type) {
final IPerformer performer = (IPerformer) applicationContext.getBean(type);
if (type.equals("double")) {
Double number1 = Double.parseDouble(String.valueOf(num1));
Double number2 = Double.parseDouble(String.valueOf(num2));
return String.format("%s", performer.Div(number1, number2));
} else {
return String.format("%s", performer.Div(num1, num2));
}
}
}