ЛР2
This commit is contained in:
parent
810da185a0
commit
2138819860
@ -12,7 +12,7 @@ public class AppApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AppApplication.class, args);
|
||||
}
|
||||
@GetMapping("/hello")
|
||||
/*@GetMapping("/hello")
|
||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||
return String.format("Hello %s!", name);
|
||||
}
|
||||
@ -39,5 +39,5 @@ public class AppApplication {
|
||||
return null;
|
||||
}
|
||||
return Double.toString(first/second);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
46
src/main/java/com/labwork1/app/calc/domain/CalcNum.java
Normal file
46
src/main/java/com/labwork1/app/calc/domain/CalcNum.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
46
src/main/java/com/labwork1/app/calc/domain/CalcStr.java
Normal file
46
src/main/java/com/labwork1/app/calc/domain/CalcStr.java
Normal 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, "");
|
||||
}
|
||||
}
|
11
src/main/java/com/labwork1/app/calc/domain/ICalc.java
Normal file
11
src/main/java/com/labwork1/app/calc/domain/ICalc.java
Normal 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);
|
||||
}
|
52
src/main/java/com/labwork1/app/calc/service/CalcService.java
Normal file
52
src/main/java/com/labwork1/app/calc/service/CalcService.java
Normal 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())));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,56 @@
|
||||
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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class AppApplicationTests {
|
||||
|
||||
@Autowired
|
||||
CalcService calcService;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user