This commit is contained in:
shadowik 2023-03-07 13:01:10 +04:00
parent 70d597ce77
commit dd9d2b9bcd
8 changed files with 80 additions and 52 deletions

View File

@ -18,18 +18,15 @@ public class MethodController {
@GetMapping("/sum")
@ResponseBody
public String sum(@RequestParam(value = "first", defaultValue = "0") String first,
@RequestParam(value = "second", defaultValue = "0") String second,
public Object sum(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
Object a = first;
Object b = second;
System.out.println(a.getClass());
return methodService.Sum(a, b, type);
return methodService.Sum(first, second, type);
}
@GetMapping("/mul")
@ResponseBody
public String mul(@RequestParam(value = "first", defaultValue = "0") Object first,
public Object mul(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Multiply(first, second, type);
@ -37,7 +34,7 @@ public class MethodController {
@GetMapping("/minus")
@ResponseBody
public String minus(@RequestParam(value = "first", defaultValue = "0") Object first,
public Object minus(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Minus(first, second, type);
@ -45,7 +42,7 @@ public class MethodController {
@GetMapping("/cont")
@ResponseBody
public String cont(@RequestParam(value = "first", defaultValue = "0") Object first,
public Object cont(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Contains(first, second, type).toString();

View File

@ -1,12 +1,12 @@
package com.example.demo.speaker.domain;
public interface IMethods<T> {
T Sum(Object first, Object second);
T Sum(T first, T second);
T Multiply(Object first, Object second);
T Multiply(T first, T second);
T Minus(Object first, Object second);
T Minus(T first, T second);
Boolean Contains(Object first, Object second);
Boolean Contains(T first, T second);
}

View File

@ -2,21 +2,21 @@ package com.example.demo.speaker.domain;
public class MethodInteger implements IMethods<Integer> {
@Override
public Integer Sum(Object first, Object second) {
return Integer.parseInt((String) first) + Integer.parseInt((String) second);
public Integer Sum(Integer first, Integer second) {
return first + second;
}
@Override
public Integer Multiply(Object first, Object second) {
return Integer.parseInt((String) first) * Integer.parseInt((String) second);
public Integer Multiply(Integer first, Integer second) {
return first * second;
}
@Override
public Integer Minus(Object first, Object second) {
return Integer.parseInt((String) first) - Integer.parseInt((String) second);
public Integer Minus(Integer first, Integer second) {
return first - second;
}
@Override
public Boolean Contains(Object first, Object second) {
return ((String)first).contains((String)second);
public Boolean Contains(Integer first, Integer second) {
return Integer.toString(first).contains(Integer.toString(second));
}
}

View File

@ -2,29 +2,29 @@ package com.example.demo.speaker.domain;
public class MethodString implements IMethods<String>{
@Override
public String Sum(Object first, Object second) {
return ((String)first).concat(((String)second));
public String Sum(String first, String second) {
return first.concat(second);
}
@Override
public String Multiply(Object first, Object second) {
String res = ((String)first);
for (int i = 0; i < Integer.parseInt((String) second) - 1; i++) {
public String Multiply(String first, String second) {
String res = first;
for (int i = 0; i < Integer.parseInt(second) - 1; i++) {
res = Sum(res, first);
}
return res;
}
@Override
public String Minus(Object first, Object second) {
public String Minus(String first, String second) {
if (Contains(first, second)) {
return ((String)first).replace((String)second, "");
return first.replace(second, "");
}
return ((String)first);
return first;
}
@Override
public Boolean Contains(Object first, Object second) {
return ((String)first).contains((String)second);
public Boolean Contains(String first, String second) {
return first.contains(second);
}
}

View File

@ -4,6 +4,8 @@ import com.example.demo.speaker.domain.IMethods;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.util.function.BiFunction;
@Service
public class MethodService {
private final ApplicationContext applicationContext;
@ -12,23 +14,34 @@ public class MethodService {
this.applicationContext = applicationContext;
}
public String Sum(Object first, Object second, String type) {
public Object Sum(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Sum(first, second));
return calculate((x, y) -> method.Sum(x, y), type, first, second);
}
public String Multiply(Object first, Object second, String type) {
public Object Multiply(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Multiply(first, second));
return calculate((x, y) -> method.Multiply(x, y), type, first, second);
}
public String Minus(Object first, Object second, String type) {
public Object Minus(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Minus(first, second));
return calculate((x, y) -> method.Minus(x, y), type, first, second);
}
public String Contains(Object first, Object second, String type) {
public Object Contains(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Contains(first, second));
return calculate((x, y) -> method.Contains(x, y), type, first, second);
}
private Object calculate(BiFunction<Object, Object, Object> methodCalc, String type, Object a, Object b) {
switch (type) {
case "int":
return methodCalc.apply(Integer.parseInt(a.toString()), Integer.parseInt(b.toString()));
case "string":
return methodCalc.apply(a.toString(), b.toString());
default:
throw new IllegalArgumentException("Type not found");
}
}
}

View File

@ -14,49 +14,61 @@ class Demo1ApplicationTests {
@Test
void testMethodSumInt() {
final String res = methodService.Sum("1", "2", "int");
Assertions.assertEquals("3", res);
final Object res = methodService.Sum(1, 2, "int");
Assertions.assertEquals(3, res);
}
@Test
void testMethodSumString() {
final String res = methodService.Sum("1", "2", "string");
final Object res = methodService.Sum("1", "2", "string");
Assertions.assertEquals("12", res);
}
@Test
void testMethodMinusInt() {
final String res = methodService.Minus("1", "2", "int");
Assertions.assertEquals("-1", res);
final Object res = methodService.Minus(1, 2, "int");
Assertions.assertEquals(-1, res);
}
@Test
void testMethodMinusString() {
final String res = methodService.Minus("214324", "4", "string");
final Object res = methodService.Minus("214324", "4", "string");
Assertions.assertEquals("2132", res);
}
@Test
void testMethodMultInt() {
final String res = methodService.Multiply("1", "2", "int");
Assertions.assertEquals("2", res);
final Object res = methodService.Multiply(1, 2, "int");
Assertions.assertEquals(2, res);
}
@Test
void testMethodMultString() {
final String res = methodService.Multiply("1", "2", "string");
final Object res = methodService.Multiply("1", "2", "string");
Assertions.assertEquals("11", res);
}
@Test
void testMethodContainsInt() {
final String res = methodService.Contains("123", "2", "int");
Assertions.assertEquals("true", res);
final Object res = methodService.Contains(123, 2, "int");
Assertions.assertEquals(true, res);
}
@Test
void testMethodContainsString() {
final String res = methodService.Contains("1", "2", "string");
Assertions.assertEquals("false", res);
final Object res = methodService.Contains("1", "2", "string");
Assertions.assertEquals(false, res);
}
@Test()
void testErrorWrongType() {
try {
final Object res = methodService.Sum(1, "ds", "int");
Assertions.fail();
}
catch (Exception e) {
}
}
}

View File

@ -16,6 +16,9 @@
"devDependencies": {
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.0.0"
},
"engines": {
"node": "^14.18.0 || >=16.0.0"
}
},
"node_modules/@babel/parser": {

View File

@ -16,5 +16,8 @@
"devDependencies": {
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.0.0"
},
"engines": {
"node": "^14.18.0 || >=16.0.0"
}
}