Готовая лабораторная работа №2.

This commit is contained in:
Павел Сорокин 2023-02-27 17:41:33 +04:00
parent 23bb2f4187
commit 41fff9ad53
9 changed files with 234 additions and 36 deletions

View File

@ -24,6 +24,12 @@
<button id="com" type="button" class="btn btn-dark">*</button>
<button id="div" type="button" class="btn btn-dark">/</button>
</div>
<div>
<select id="SelectId">
<option value="int">int</option>
<option value="string">str</option>
</select>
</div>
<p id="Conclusion">Вывод:</p>
</div>
</article>

View File

@ -5,6 +5,7 @@ let button_dif=document.getElementById("dif");
let button_com=document.getElementById("com");
let button_div=document.getElementById("div");
let Conclusion=document.getElementById("Conclusion");
let Select=document.getElementById("SelectId")
button_sum.onclick=function(event){
executeRequest("sum");
@ -22,7 +23,9 @@ button_div.onclick=function(event){
function executeRequest(operationAddress) {
let numOne = number1_input.value;
let numTwo = number2_input.value;
fetch(`http://localhost:8080/${operationAddress}?firstNum=${numOne}&secondNum=${numTwo}`)
let selecttype=Select.value;
console.log(`http://localhost:8080/${operationAddress}?firstObj=${numOne}&secondObj=${numTwo}&type=${selecttype}`)
fetch(`http://localhost:8080/${operationAddress}?firstObj=${numOne}&secondObj=${numTwo}&type=${selecttype}`)
.then((response) => {
return response.text()
})

View File

@ -13,37 +13,6 @@ public class SbappApplication {
public static void main(String[] args) {
SpringApplication.run(SbappApplication.class, args);
}
@GetMapping("/String")
public String Vvod(@RequestParam(value = "str", defaultValue = "none") String str) {
return String.format("Вы ввели %s!", str);
}
@GetMapping("/sum")
public String sum(@RequestParam(required = false,defaultValue = "0") float firstNum,
@RequestParam(required = false,defaultValue = "0") float secondNum)
{
return Float.toString(firstNum+secondNum);
}
@GetMapping("/dif")
public String dif(@RequestParam(required = false,defaultValue = "0") float firstNum,
@RequestParam(required = false,defaultValue = "0") float secondNum)
{
return Float.toString(firstNum-secondNum);
}
@GetMapping("/com")
public String com(@RequestParam(required = false,defaultValue = "0") float firstNum,
@RequestParam(required = false,defaultValue = "0") float secondNum)
{
return Float.toString(firstNum*secondNum);
}
@GetMapping("/div")
public String div(@RequestParam(required = false,defaultValue = "0") float firstNum,
@RequestParam(required = false,defaultValue = "0") float secondNum)
{
if(secondNum==0)
{
return "Error";
}
return Float.toString(firstNum/secondNum);
}
}

View File

@ -0,0 +1,52 @@
package ru.ulstu.is.sbapp.speaker.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.is.sbapp.speaker.service.MethodService;
@RestController
public class UserController {
private final MethodService methodService;
public UserController(MethodService methodService)
{
this.methodService=methodService;
}
@GetMapping("/sum")
public String sum(@RequestParam(value = "firstObj",defaultValue = "0") String firstObj,
@RequestParam(value = "secondObj",defaultValue = "0") String secondObj,
@RequestParam(value = "type",defaultValue = "int")String type)
{
Object a = firstObj;
Object b = secondObj;
return methodService.Sum(a,b,type);
}
@GetMapping("/dif")
public String dif(@RequestParam(value = "firstObj",defaultValue = "0") String firstObj,
@RequestParam(value = "secondObj",defaultValue = "0") String secondObj,
@RequestParam(value = "type",defaultValue = "int")String type)
{
Object a = firstObj;
Object b = secondObj;
return methodService.Dif(a,b,type);
}
@GetMapping("/com")
public String com(@RequestParam(value = "firstObj",defaultValue = "0") String firstObj,
@RequestParam(value = "secondObj",defaultValue = "0") String secondObj,
@RequestParam(value = "type",defaultValue = "int")String type)
{
Object a = firstObj;
Object b = secondObj;
return methodService.Com(a,b,type);
}
@GetMapping("/div")
public String div(@RequestParam(value = "firstObj",defaultValue = "0") String firstObj,
@RequestParam(value = "secondObj",defaultValue = "0") String secondObj,
@RequestParam(value = "type",defaultValue = "int")String type)
{
Object a = firstObj;
Object b = secondObj;
return methodService.Div(a,b,type);
}
}

View File

@ -0,0 +1,8 @@
package ru.ulstu.is.sbapp.speaker.domain;
public interface IMethods<T> {
T Sum(T firstObj,T secondObj);
T Dif (T firstObj, T secondObj);
T Com(T firstObj,T secondObj);
T Div (T firstObj, T secondObj);
}

View File

@ -0,0 +1,29 @@
package ru.ulstu.is.sbapp.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value = "int")
public class MethodInteger implements IMethods<Integer> {
@Override
public Integer Sum(Integer firstObj, Integer secondObj) {
return firstObj + secondObj;
}
@Override
public Integer Dif(Integer firstObj, Integer secondObj) {
return firstObj -secondObj;
}
@Override
public Integer Com(Integer firstObj, Integer secondObj) {
return firstObj * secondObj;
}
@Override
public Integer Div(Integer firstObj, Integer secondObj) {
int num = secondObj;
if (num == 0){
return null;
}else{
return firstObj / num;
}
}
}

View File

@ -0,0 +1,39 @@
package ru.ulstu.is.sbapp.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value = "string")
public class MethodString implements IMethods<String> {
@Override
public String Sum(String firstObj,String secondObj)
{
return firstObj.concat(secondObj);
}
@Override
public String Dif(String firstObj,String secondObj)
{
if(firstObj.contains(secondObj))
{
return firstObj.replace(secondObj,"");
}
return (firstObj);
}
@Override
public String Com(String firstObj,String secondObj)
{
String res = (firstObj);
for (int i = 0; i < (secondObj).length(); i++) {
res = Sum(res, firstObj);
}
return res;
}
@Override
public String Div(String firstObj,String secondObj)
{
if (firstObj.contains(secondObj)){
return "true";
}else{
return "false";
}
}
}

View File

@ -0,0 +1,52 @@
package ru.ulstu.is.sbapp.speaker.service;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import ru.ulstu.is.sbapp.speaker.domain.IMethods;
import ru.ulstu.is.sbapp.speaker.domain.MethodInteger;
import ru.ulstu.is.sbapp.speaker.domain.MethodString;
@Service
public class MethodService {
private final ApplicationContext applicationContext;
public MethodService(ApplicationContext applicationContext)
{
this.applicationContext=applicationContext;
}
public String Sum(Object firstObj,Object secondObj,String type)
{
final IMethods method=(IMethods) applicationContext.getBean(type);
if (method instanceof MethodString){
return String.format("%s", method.Sum(firstObj,secondObj));
}else{
return String.format("%s", method.Sum(Integer.parseInt(firstObj.toString()),Integer.parseInt(secondObj.toString())));
}
}
public String Com(Object firstObj,Object secondObj,String type)
{
final IMethods method=(IMethods) applicationContext.getBean(type);
if (method instanceof MethodString){
return String.format("%s", method.Com(firstObj,secondObj));
}else{
return String.format("%s", method.Com(Integer.parseInt(firstObj.toString()),Integer.parseInt(secondObj.toString())));
}
}
public String Dif(Object firstObj,Object secondObj,String type)
{
final IMethods method=(IMethods) applicationContext.getBean(type);
if (method instanceof MethodString){
return String.format("%s", method.Dif(firstObj,secondObj));
}else{
return String.format("%s", method.Dif(Integer.parseInt(firstObj.toString()),Integer.parseInt(secondObj.toString())));
}
}
public String Div(Object firstObj,Object secondObj,String type)
{
final IMethods method=(IMethods) applicationContext.getBean(type);
if (method instanceof MethodString){
return String.format("%s", method.Div(firstObj,secondObj));
}else{
return String.format("%s", method.Div(Integer.parseInt(firstObj.toString()),Integer.parseInt(secondObj.toString())));
}
}
}

View File

@ -1,13 +1,53 @@
package ru.ulstu.is.sbapp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.speaker.service.MethodService;
@SpringBootTest
class SbappApplicationTests {
@Autowired
MethodService methodService;
@Test
void contextLoads() {
void testSumInt() {
final String res=methodService.Sum(1,1,"int");
Assertions.assertEquals(2,Integer.parseInt(res));
}
@Test
void testDifInt() {
final String res=methodService.Dif(1,1,"int");
Assertions.assertEquals(0,Integer.parseInt(res));
}
@Test
void testComInt() {
final String res=methodService.Com(3,2,"int");
Assertions.assertEquals(6,Integer.parseInt(res));
}
@Test
void testDivInt() {
final String res=methodService.Div(6,2,"int");
Assertions.assertEquals(3,Integer.parseInt(res));
}
@Test
void testSumStr() {
final String res=methodService.Sum("1","1","string");
Assertions.assertEquals("11",res);
}
@Test
void testDifStr() {
final String res=methodService.Dif("2133","2","string");
Assertions.assertEquals("133",res);
}
@Test
void testComStr() {
final String res=methodService.Com("abc","ba","string");
Assertions.assertEquals("abcabcabc",res);
}
@Test
void testDivStr() {
final String res=methodService.Div("1010","2","string");
Assertions.assertEquals("false",res);
}
}