Commit 5
This commit is contained in:
parent
4f0c103bec
commit
14c6e4dc12
@ -24,8 +24,15 @@
|
||||
<button type="button" class="btn btn-primary" id="buttonMulti">*</button>
|
||||
<button type="button" class="btn btn-primary" id="buttonDiv">/</button>
|
||||
</div>
|
||||
Выберите тип данных
|
||||
<div>
|
||||
<select id="type" class="form-select" >
|
||||
<option value="int">Число</option>
|
||||
<option value="string">Строка</option>
|
||||
</select>
|
||||
</div>
|
||||
Результат
|
||||
<input id="res"></input>
|
||||
<input id="res" type="text"></input>
|
||||
</div>
|
||||
</form>
|
||||
<script src="script.js"></script>
|
||||
|
@ -6,32 +6,37 @@ let buttonDiv = document.getElementById("buttonDiv");
|
||||
let numberOneInput = document.getElementById("first");
|
||||
let numberTwoInput = document.getElementById("second");
|
||||
let resultInput = document.getElementById("res");
|
||||
let typeInput = document.getElementById("type");
|
||||
|
||||
buttonPlus.onclick = function(event) {
|
||||
let num_1 = numberOneInput.value;
|
||||
let num_2 = numberTwoInput.value;
|
||||
fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}`)
|
||||
let type = typeInput.value;
|
||||
fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}&type=${type}`)
|
||||
.then(response => response.text())
|
||||
.then(res => resultInput.value = res);
|
||||
}
|
||||
buttonMinus.onclick = function(event) {
|
||||
let num_1 = numberOneInput.value;
|
||||
let num_2 = numberTwoInput.value;
|
||||
fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}`)
|
||||
let type = typeInput.value;
|
||||
fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}&type=${type}`)
|
||||
.then(response => response.text())
|
||||
.then(res => resultInput.value = res);
|
||||
}
|
||||
buttonMulti.onclick = function(event) {
|
||||
let num_1 = numberOneInput.value;
|
||||
let num_2 = numberTwoInput.value;
|
||||
fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}`)
|
||||
let type = typeInput.value;
|
||||
fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}&type=${type}`)
|
||||
.then(response => response.text())
|
||||
.then(response => resultInput.value = response);
|
||||
}
|
||||
buttonDiv.onclick = function(event) {
|
||||
let num_1 = numberOneInput.value;
|
||||
let num_2 = numberTwoInput.value;
|
||||
fetch(`http://localhost:8080/div?first=${num_1}&second=${num_2}`)
|
||||
let type = typeInput.value;
|
||||
fetch(`http://localhost:8080/div?first=${num_1}&second=${num_2}&type=${type}`)
|
||||
.then(response => response.text())
|
||||
.then(res => resultInput.value = res);
|
||||
}
|
@ -12,35 +12,5 @@ public class DemoApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
@GetMapping("/hello")
|
||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||
return String.format("Hello %s!", name);
|
||||
}
|
||||
@GetMapping("/sum")
|
||||
public String Sum(@RequestParam(defaultValue = "0") double first,
|
||||
@RequestParam(defaultValue = "0") double second) {
|
||||
return Double.toString(first + second);
|
||||
}
|
||||
|
||||
@GetMapping("/minus")
|
||||
public String Ras(@RequestParam(defaultValue = "0") double first,
|
||||
@RequestParam(defaultValue = "0") double second) {
|
||||
return Double.toString(first - second);
|
||||
}
|
||||
|
||||
@GetMapping("/multi")
|
||||
public String Pros(@RequestParam(defaultValue = "2") double first,
|
||||
@RequestParam(defaultValue = "2") double second) {
|
||||
return Double.toString(first * second);
|
||||
}
|
||||
|
||||
@GetMapping("/div")
|
||||
public String Del(@RequestParam(defaultValue = "1") double first,
|
||||
@RequestParam(defaultValue = "1") double second) {
|
||||
if (second == 0) {
|
||||
return null;
|
||||
}
|
||||
return Double.toString(first / second);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.example.demo.speaker.controller;
|
||||
|
||||
import com.example.demo.speaker.service.MethodService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MethodController {
|
||||
private final MethodService speakerService;
|
||||
|
||||
public MethodController(MethodService speakerService) {
|
||||
this.speakerService = speakerService;
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public String Sum(@RequestParam(value = "first", defaultValue = "1") Object first,
|
||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
||||
return speakerService.Sum(first,second,type);
|
||||
}
|
||||
|
||||
@GetMapping("/minus")
|
||||
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 speakerService.Ras(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 speakerService.Pros(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 speakerService.Del(first,second,type);
|
||||
}
|
||||
}
|
11
src/main/java/com/example/demo/speaker/domain/IMethod.java
Normal file
11
src/main/java/com/example/demo/speaker/domain/IMethod.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public interface IMethod<T> {
|
||||
T Sum(T first, T second);
|
||||
|
||||
T Multiply(T first, Integer second);
|
||||
|
||||
T Minus(T first, Integer second);
|
||||
|
||||
T Div(T first, T second);
|
||||
}
|
27
src/main/java/com/example/demo/speaker/domain/MethodInt.java
Normal file
27
src/main/java/com/example/demo/speaker/domain/MethodInt.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="int")
|
||||
public class MethodInt implements IMethod<Integer>{
|
||||
public Integer Sum(Integer first, Integer second) {
|
||||
return Integer.parseInt(first.toString()) + Integer.parseInt(second.toString());
|
||||
}
|
||||
|
||||
public Integer Multiply(Integer first, Integer second) {
|
||||
return Integer.parseInt(first.toString()) * Integer.parseInt(second.toString());
|
||||
}
|
||||
|
||||
public Integer Minus(Integer first, Integer second) {
|
||||
return Integer.parseInt(first.toString()) - Integer.parseInt(second.toString());
|
||||
}
|
||||
|
||||
public Integer Div(Integer first, Integer second) {
|
||||
int num = Integer.parseInt(second.toString());
|
||||
if (num == 0){
|
||||
return null;
|
||||
}else{
|
||||
return Integer.parseInt(first.toString()) / num;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="string")
|
||||
public class MethodString implements IMethod<String>{
|
||||
@Override
|
||||
public String Sum(String first, String second) {
|
||||
return first.concat(second);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Multiply(String first, Integer second) {
|
||||
if (second != 0){
|
||||
String temp = "";
|
||||
for (int i = 0; i < second; i++){
|
||||
temp = temp.concat(first);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
else{
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Minus(String first, Integer second) {
|
||||
String temp = first;
|
||||
if(temp.length() >= second){
|
||||
return temp.substring(0, first.length() - second);
|
||||
}else{
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Div(String first, String second) {
|
||||
if (first.contains(second)){
|
||||
return "true";
|
||||
}else{
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import com.example.demo.speaker.domain.IMethod;
|
||||
import com.example.demo.speaker.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 speaker = (IMethod) applicationContext.getBean(type);
|
||||
if (speaker instanceof MethodString){
|
||||
return String.format("%s", speaker.Sum(first,second));
|
||||
}else{
|
||||
return String.format("%s", speaker.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
}
|
||||
|
||||
public String Ras(Object first, Object second, String type) {
|
||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
||||
if (speaker instanceof MethodString){
|
||||
return String.format("%s", speaker.Minus(first,Integer.parseInt(second.toString())));
|
||||
}else{
|
||||
return String.format("%s", speaker.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
}
|
||||
|
||||
public String Pros(Object first, Object second, String type) {
|
||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
||||
if (speaker instanceof MethodString){
|
||||
return String.format("%s", speaker.Multiply(first,Integer.parseInt(second.toString())));
|
||||
}else{
|
||||
return String.format("%s", speaker.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
}
|
||||
|
||||
public String Del(Object first, Object second, String type) {
|
||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
||||
if (speaker instanceof MethodString){
|
||||
return String.format("%s", speaker.Div(first,second));
|
||||
}else {
|
||||
return String.format("%s", speaker.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user