Compare commits

..

1 Commits
LW02 ... master

Author SHA1 Message Date
27ec055c62 отчеты 2023-06-15 13:35:28 +04:00
15 changed files with 12 additions and 262 deletions

View File

@ -30,10 +30,7 @@
<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 my-2" id="calculate">Числа</button> <button type="button" class="btn btn-primary" id="calculate">Тык</button>
</div>
<div>
<button type="button" class="btn btn-primary mb-2" id="calculateStr">Строки</button>
</div> </div>
<div> <div>
Результат: Результат:

View File

@ -1,30 +1,26 @@
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("num"); calculate();
};
calculateStrButton.onclick = function() {
calculate("str");
}; };
function calculate(parametr) { function calculate() {
switch (parseInt(select.value)) { switch (parseInt(select.value)) {
case 1: case 1:
doSmth("plus", parametr) doSmth("plus")
break; break;
case 2: case 2:
doSmth("minus", parametr) doSmth("minus")
break; break;
case 3: case 3:
doSmth("mult", parametr) doSmth("mult")
break; break;
case 4: case 4:
doSmth("div", parametr) doSmth("div")
break; break;
}; };
} }
@ -36,9 +32,8 @@ function checkNum(res) {
return res return res
} }
function doSmth(address, type) { function doSmth(address) {
console.log("Тип " + type) fetch(`http://localhost:8080/${address}?first=${first.value}&second=${second.value}`)
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

@ -1,44 +0,0 @@
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

@ -1,46 +0,0 @@
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

@ -1,46 +0,0 @@
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

@ -1,11 +0,0 @@
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

@ -1,52 +0,0 @@
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,56 +1,13 @@
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 testPlusNum() { void contextLoads() {
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);
} }
} }