Compare commits

..

3 Commits
master ... LW02

Author SHA1 Message Date
cfcae77ad5 Merge remote-tracking branch 'origin/LW02' into LW02 2023-02-27 18:00:27 +04:00
5ff927f236 ЛР2 2023-02-27 17:59:04 +04:00
2138819860 ЛР2 2023-02-27 17:53:52 +04:00
15 changed files with 262 additions and 12 deletions

View File

@ -30,7 +30,10 @@
<input id="second" class="form-control" type='number' value='0' /> <input id="second" class="form-control" type='number' value='0' />
</div> </div>
<div> <div>
<button type="button" class="btn btn-primary" id="calculate">Тык</button> <button type="button" class="btn btn-primary my-2" id="calculate">Числа</button>
</div>
<div>
<button type="button" class="btn btn-primary mb-2" id="calculateStr">Строки</button>
</div> </div>
<div> <div>
Результат: Результат:

View File

@ -1,26 +1,30 @@
const calculateButton = document.getElementById("calculate"); const calculateButton = document.getElementById("calculate");
const calculateStrButton = document.getElementById("calculateStr");
const first = document.getElementById("first"); const first = document.getElementById("first");
const second = document.getElementById("second"); const second = document.getElementById("second");
const select = document.getElementById("operation"); const select = document.getElementById("operation");
const result = document.getElementById("result"); const result = document.getElementById("result");
calculateButton.onclick = function() { calculateButton.onclick = function() {
calculate(); calculate("num");
};
calculateStrButton.onclick = function() {
calculate("str");
}; };
function calculate() { function calculate(parametr) {
switch (parseInt(select.value)) { switch (parseInt(select.value)) {
case 1: case 1:
doSmth("plus") doSmth("plus", parametr)
break; break;
case 2: case 2:
doSmth("minus") doSmth("minus", parametr)
break; break;
case 3: case 3:
doSmth("mult") doSmth("mult", parametr)
break; break;
case 4: case 4:
doSmth("div") doSmth("div", parametr)
break; break;
}; };
} }
@ -32,8 +36,9 @@ function checkNum(res) {
return res return res
} }
function doSmth(address) { function doSmth(address, type) {
fetch(`http://localhost:8080/${address}?first=${first.value}&second=${second.value}`) console.log("Тип " + type)
fetch(`http://localhost:8080/${address}?first=${first.value}&second=${second.value}&type=${type}`)
.then(response => response.text()) .then(response => response.text())
.then(res => result.innerHTML = checkNum(res)); .then(res => result.innerHTML = checkNum(res));
} }

View File

@ -12,7 +12,7 @@ public class AppApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args); SpringApplication.run(AppApplication.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);
} }
@ -39,5 +39,5 @@ public class AppApplication {
return null; return null;
} }
return Double.toString(first/second); return Double.toString(first/second);
} }*/
} }

View File

@ -0,0 +1,44 @@
package com.labwork1.app.calc.controller;
import com.labwork1.app.calc.service.CalcService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalcController {
private final CalcService calcService;
public CalcController(CalcService calcService) {
this.calcService = calcService;
}
@GetMapping("/plus")
public String plus(@RequestParam(required = false, defaultValue = "0") Object first,
@RequestParam(required = false, defaultValue = "0") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
return calcService.plus((Object) first, (Object) second, type);
}
@GetMapping("/minus")
public String minus(@RequestParam(required = false, defaultValue = "0") Object first,
@RequestParam(required = false, defaultValue = "0") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
return calcService.minus((Object) first, (Object) second, type);
}
@GetMapping("/mult")
public String mult(@RequestParam(required = false, defaultValue = "1") Object first,
@RequestParam(required = false, defaultValue = "1") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
return calcService.mult((Object) first, (Object) second, type);
}
@GetMapping("/div")
public String div(@RequestParam(required = false, defaultValue = "1") Object first,
@RequestParam(required = false, defaultValue = "1") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
if(Integer.parseInt(second.toString()) == 0)
{
return null;
}
return calcService.div((Object) first, (Object) second, type);
}
}

View File

@ -0,0 +1,46 @@
package com.labwork1.app.calc.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component(value = "num")
public class CalcNum implements ICalc<Integer>, InitializingBean, DisposableBean {
private final Logger log = LoggerFactory.getLogger(CalcNum.class);
@Override
public void afterPropertiesSet() {
log.info("CalcNum.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("CalcNum.destroy()");
}
@Override
public Integer Plus(Integer first, Integer second) {
return first + second;
}
@Override
public Integer Mult(Integer first, Integer second) {
return first * second;
}
@Override
public Integer Minus(Integer first, Integer second) {
return first - second;
}
@Override
public Integer Div(Integer first, Integer second) {
if (second == 0){
return null;
}else{
return first / second;
}
}
}

View File

@ -0,0 +1,46 @@
package com.labwork1.app.calc.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component(value = "str")
public class CalcStr implements ICalc<String>, InitializingBean, DisposableBean {
private final Logger log = LoggerFactory.getLogger(CalcStr.class);
@Override
public void afterPropertiesSet() {
log.info("CalcStr.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("CalcStr.destroy()");
}
@Override
public String Plus(String first, String second) {
return first.concat(second);
}
@Override
public String Mult(String first, String second) {
String temp = first;
for (int i = 0; i < Integer.parseInt(second) - 1; i++) {
temp = Plus(temp, first);
}
return temp;
}
@Override
public String Minus(String first, String second) {
return first.replaceFirst(second, "");
}
@Override
public String Div(String first, String second) {
return first.replaceAll(second, "");
}
}

View File

@ -0,0 +1,11 @@
package com.labwork1.app.calc.domain;
public interface ICalc<T> {
T Plus(T first, T second);
T Mult(T first, T second);
T Minus(T first, T second);
T Div(T first, T second);
}

View File

@ -0,0 +1,52 @@
package com.labwork1.app.calc.service;
import com.labwork1.app.calc.domain.CalcNum;
import com.labwork1.app.calc.domain.CalcStr;
import com.labwork1.app.calc.domain.ICalc;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class CalcService {
private final ApplicationContext applicationContext;
public CalcService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String plus(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Plus(first, second));
}else{
return String.format("%s", temp.Plus(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
public String minus(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Minus(first, second));
}else{
return String.format("%s", temp.Minus(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
public String mult(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Mult(first, second));
}else{
return String.format("%s", temp.Mult(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
public String div(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Div(first, second));
}else{
return String.format("%s", temp.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
}

View File

@ -1,13 +1,56 @@
package com.labwork1.app; package com.labwork1.app;
import com.labwork1.app.calc.service.CalcService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class AppApplicationTests { class AppApplicationTests {
@Autowired
CalcService calcService;
@Test @Test
void contextLoads() { void testPlusNum() {
final String res = calcService.plus(10, 10, "num");
Assertions.assertEquals(20, Integer.parseInt(res));
}
@Test
void testMinusNum() {
final String res = calcService.minus(5, 2, "num");
Assertions.assertEquals(3, Integer.parseInt(res));
}
@Test
void testMultNum() {
final String res = calcService.mult(10, 10, "num");
Assertions.assertEquals(100, Integer.parseInt(res));
}
@Test
void testDivNum() {
final String res = calcService.div(20, 10, "num");
Assertions.assertEquals(2, Integer.parseInt(res));
}
@Test
void testPlusStr() {
final String res = calcService.plus("10", "10", "str");
Assertions.assertEquals("1010", res);
}
@Test
void testMinusStr() {
final String res = calcService.minus("5252", "2", "str");
Assertions.assertEquals("552", res);
}
@Test
void testMultStr() {
final String res = calcService.mult("5", "3", "str");
Assertions.assertEquals("555", res);
}
@Test
void testDivStr() {
final String res = calcService.div("5252", "2", "str");
Assertions.assertEquals("55", res);
} }
} }