Lab2 done

This commit is contained in:
ker73rus 2023-03-06 23:14:24 +04:00
parent bff8534421
commit a8c8ec1bf8
8 changed files with 234 additions and 3 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"/>
</div>
</form>
<script src="/script.js"></script>

View File

@ -4,7 +4,6 @@ let numberTwoInput = document.getElementById("second");
let resultInput = document.getElementById("res");
let typeInput = document.getElementById("type");
function f(operation, event){
event.preventDefault();
let num_1 = numberOneInput.value;

View File

@ -0,0 +1,43 @@
package method.controller;
import 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 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 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 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 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 com.example.demo;
import 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"));
}
}