Сделанная вторая лаба

This commit is contained in:
Katerina881 2023-02-27 17:00:41 +04:00
parent a9e7a9fc6b
commit ca392cb92b
9 changed files with 250 additions and 18 deletions

View File

@ -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>

View File

@ -2,50 +2,50 @@ let calculateButton = document.getElementById("calculate");
let numberOneInput = document.getElementById("first");
let numberTwoInput = document.getElementById("second");
let resultInput = document.getElementById("res");
let typeInput = document.getElementById("type");
buttonPlus.onclick = function(event) {
event.preventDefault();
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 => {
const ru = new Intl.NumberFormat("ru").format(res);
resultInput.value = ru;
resultInput.value = res;
});
}
buttonMinus.onclick = function(event) {
event.preventDefault();
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 => {
const ru = new Intl.NumberFormat("ru").format(res);
resultInput.value = ru;
resultInput.value = res;
});
}
buttonMulti.onclick = function(event) {
event.preventDefault();
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(res => {
const ru = new Intl.NumberFormat("ru").format(res);
resultInput.value = ru;
resultInput.value = res;
});
}
buttonDiv.onclick = function(event) {
event.preventDefault();
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 =>
{
const ru = new Intl.NumberFormat("ru").format(res);
resultInput.value = ru;
resultInput.value = res;
});
}

View File

@ -12,7 +12,7 @@ public class LabworkApplication {
public static void main(String[] args) {
SpringApplication.run(LabworkApplication.class, args);
}
/*
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
@ -43,5 +43,5 @@ public class LabworkApplication {
return null;
}
return Double.toString(first / second);
}
}*/
}

View File

@ -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);
}
}

View 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);
}

View 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;
}
}
}

View 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";
}
}
}

View 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())));
}
}
}

View File

@ -1,13 +1,62 @@
package ip.labwork;
import ip.labwork.method.service.MethodService;
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;
@SpringBootTest
class LabworkApplicationTests {
@Autowired
MethodService speakerService;
@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"));
}
}