Сделанная вторая лаба
This commit is contained in:
parent
a9e7a9fc6b
commit
ca392cb92b
@ -16,7 +16,7 @@
|
|||||||
Введите первое число
|
Введите первое число
|
||||||
<input id="first"></input>
|
<input id="first"></input>
|
||||||
Введите второе число
|
Введите второе число
|
||||||
<input id="second"></input>
|
<input id="second" ></input>
|
||||||
Выберите операцию
|
Выберите операцию
|
||||||
<div>
|
<div>
|
||||||
<button type="button" class="btn btn-primary" id="buttonPlus">+</button>
|
<button type="button" class="btn btn-primary" id="buttonPlus">+</button>
|
||||||
@ -24,8 +24,15 @@
|
|||||||
<button type="button" class="btn btn-primary" id="buttonMulti">*</button>
|
<button type="button" class="btn btn-primary" id="buttonMulti">*</button>
|
||||||
<button type="button" class="btn btn-primary" id="buttonDiv">/</button>
|
<button type="button" class="btn btn-primary" id="buttonDiv">/</button>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<script src="/script.js"></script>
|
<script src="/script.js"></script>
|
||||||
|
@ -2,50 +2,50 @@ let calculateButton = document.getElementById("calculate");
|
|||||||
let numberOneInput = document.getElementById("first");
|
let numberOneInput = document.getElementById("first");
|
||||||
let numberTwoInput = document.getElementById("second");
|
let numberTwoInput = document.getElementById("second");
|
||||||
let resultInput = document.getElementById("res");
|
let resultInput = document.getElementById("res");
|
||||||
|
let typeInput = document.getElementById("type");
|
||||||
|
|
||||||
buttonPlus.onclick = function(event) {
|
buttonPlus.onclick = function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let num_1 = numberOneInput.value;
|
let num_1 = numberOneInput.value;
|
||||||
let num_2 = numberTwoInput.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(response => response.text())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
const ru = new Intl.NumberFormat("ru").format(res);
|
resultInput.value = res;
|
||||||
resultInput.value = ru;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
buttonMinus.onclick = function(event) {
|
buttonMinus.onclick = function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let num_1 = numberOneInput.value;
|
let num_1 = numberOneInput.value;
|
||||||
let num_2 = numberTwoInput.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(response => response.text())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
const ru = new Intl.NumberFormat("ru").format(res);
|
resultInput.value = res;
|
||||||
resultInput.value = ru;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
buttonMulti.onclick = function(event) {
|
buttonMulti.onclick = function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let num_1 = numberOneInput.value;
|
let num_1 = numberOneInput.value;
|
||||||
let num_2 = numberTwoInput.value;
|
let num_2 = numberTwoInput.value;
|
||||||
|
let type = typeInput.value;
|
||||||
fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}`)
|
fetch(`http://localhost:8080/multi?first=${num_1}&second=${num_2}&type=${type}`)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
const ru = new Intl.NumberFormat("ru").format(res);
|
resultInput.value = res;
|
||||||
resultInput.value = ru;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
buttonDiv.onclick = function(event) {
|
buttonDiv.onclick = function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let num_1 = numberOneInput.value;
|
let num_1 = numberOneInput.value;
|
||||||
let num_2 = numberTwoInput.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(response => response.text())
|
||||||
.then(res =>
|
.then(res =>
|
||||||
{
|
{
|
||||||
const ru = new Intl.NumberFormat("ru").format(res);
|
resultInput.value = res;
|
||||||
resultInput.value = ru;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -12,7 +12,7 @@ public class LabworkApplication {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(LabworkApplication.class, args);
|
SpringApplication.run(LabworkApplication.class, args);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||||
return String.format("Hello %s!", name);
|
return String.format("Hello %s!", name);
|
||||||
@ -43,5 +43,5 @@ public class LabworkApplication {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return Double.toString(first / second);
|
return Double.toString(first / second);
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
package ip.labwork.method.controller;
|
||||||
|
|
||||||
|
import ip.labwork.method.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/ip/labwork/method/domain/IMethod.java
Normal file
11
src/main/java/ip/labwork/method/domain/IMethod.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package ip.labwork.method.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/ip/labwork/method/domain/MethodInt.java
Normal file
27
src/main/java/ip/labwork/method/domain/MethodInt.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package ip.labwork.method.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
src/main/java/ip/labwork/method/domain/MethodString.java
Normal file
44
src/main/java/ip/labwork/method/domain/MethodString.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package ip.labwork.method.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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
src/main/java/ip/labwork/method/service/MethodService.java
Normal file
51
src/main/java/ip/labwork/method/service/MethodService.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package ip.labwork.method.service;
|
||||||
|
|
||||||
|
import ip.labwork.method.domain.IMethod;
|
||||||
|
import ip.labwork.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 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())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,62 @@
|
|||||||
package ip.labwork;
|
package ip.labwork;
|
||||||
|
|
||||||
|
import ip.labwork.method.service.MethodService;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
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 org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class LabworkApplicationTests {
|
class LabworkApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MethodService speakerService;
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void testIntSum() {
|
||||||
|
final String res = speakerService.Sum(20,10,"int");
|
||||||
|
Assertions.assertEquals(30, Integer.parseInt(res));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIntMinus() {
|
||||||
|
final String res = speakerService.Ras(20,10,"int");
|
||||||
|
Assertions.assertEquals(10, Integer.parseInt(res));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testIntMulti() {
|
||||||
|
final String res = speakerService.Pros(20,10,"int");
|
||||||
|
Assertions.assertEquals(200, Integer.parseInt(res));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testIntDiv() {
|
||||||
|
final String res = speakerService.Del(20,10,"int");
|
||||||
|
Assertions.assertEquals(2, Integer.parseInt(res));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testStringSum() {
|
||||||
|
final String res = speakerService.Sum("20","10","string");
|
||||||
|
Assertions.assertEquals("2010", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStringMinus() {
|
||||||
|
final String res = speakerService.Ras("300",1,"string");
|
||||||
|
Assertions.assertEquals("30", res);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testStringMulti() {
|
||||||
|
final String res = speakerService.Pros("20",2,"string");
|
||||||
|
Assertions.assertEquals("2020", res);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testStringDiv() {
|
||||||
|
final String res = speakerService.Del("20","2","string");
|
||||||
|
Assertions.assertEquals("true", res);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testSpeakerErrorWired() {
|
||||||
|
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.Sum("10", "20", "double"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user