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

View File

@ -1,12 +1,12 @@
package com.example.demo.speaker.domain; package com.example.demo.speaker.domain;
public interface IMethods<T> { 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> { public class MethodInteger implements IMethods<Integer> {
@Override @Override
public Integer Sum(Object first, Object second) { public Integer Sum(Integer first, Integer second) {
return Integer.parseInt((String) first) + Integer.parseInt((String) second); return first + second;
} }
@Override @Override
public Integer Multiply(Object first, Object second) { public Integer Multiply(Integer first, Integer second) {
return Integer.parseInt((String) first) * Integer.parseInt((String) second); return first * second;
} }
@Override @Override
public Integer Minus(Object first, Object second) { public Integer Minus(Integer first, Integer second) {
return Integer.parseInt((String) first) - Integer.parseInt((String) second); return first - second;
} }
@Override @Override
public Boolean Contains(Object first, Object second) { public Boolean Contains(Integer first, Integer second) {
return ((String)first).contains((String)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>{ public class MethodString implements IMethods<String>{
@Override @Override
public String Sum(Object first, Object second) { public String Sum(String first, String second) {
return ((String)first).concat(((String)second)); return first.concat(second);
} }
@Override @Override
public String Multiply(Object first, Object second) { public String Multiply(String first, String second) {
String res = ((String)first); String res = first;
for (int i = 0; i < Integer.parseInt((String) second) - 1; i++) { for (int i = 0; i < Integer.parseInt(second) - 1; i++) {
res = Sum(res, first); res = Sum(res, first);
} }
return res; return res;
} }
@Override @Override
public String Minus(Object first, Object second) { public String Minus(String first, String second) {
if (Contains(first, second)) { if (Contains(first, second)) {
return ((String)first).replace((String)second, ""); return first.replace(second, "");
} }
return ((String)first); return first;
} }
@Override @Override
public Boolean Contains(Object first, Object second) { public Boolean Contains(String first, String second) {
return ((String)first).contains((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.context.ApplicationContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.function.BiFunction;
@Service @Service
public class MethodService { public class MethodService {
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
@ -12,23 +14,34 @@ public class MethodService {
this.applicationContext = applicationContext; 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); 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); 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); 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); 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 @Test
void testMethodSumInt() { void testMethodSumInt() {
final String res = methodService.Sum("1", "2", "int"); final Object res = methodService.Sum(1, 2, "int");
Assertions.assertEquals("3", res); Assertions.assertEquals(3, res);
} }
@Test @Test
void testMethodSumString() { void testMethodSumString() {
final String res = methodService.Sum("1", "2", "string"); final Object res = methodService.Sum("1", "2", "string");
Assertions.assertEquals("12", res); Assertions.assertEquals("12", res);
} }
@Test @Test
void testMethodMinusInt() { void testMethodMinusInt() {
final String res = methodService.Minus("1", "2", "int"); final Object res = methodService.Minus(1, 2, "int");
Assertions.assertEquals("-1", res); Assertions.assertEquals(-1, res);
} }
@Test @Test
void testMethodMinusString() { void testMethodMinusString() {
final String res = methodService.Minus("214324", "4", "string"); final Object res = methodService.Minus("214324", "4", "string");
Assertions.assertEquals("2132", res); Assertions.assertEquals("2132", res);
} }
@Test @Test
void testMethodMultInt() { void testMethodMultInt() {
final String res = methodService.Multiply("1", "2", "int"); final Object res = methodService.Multiply(1, 2, "int");
Assertions.assertEquals("2", res); Assertions.assertEquals(2, res);
} }
@Test @Test
void testMethodMultString() { void testMethodMultString() {
final String res = methodService.Multiply("1", "2", "string"); final Object res = methodService.Multiply("1", "2", "string");
Assertions.assertEquals("11", res); Assertions.assertEquals("11", res);
} }
@Test @Test
void testMethodContainsInt() { void testMethodContainsInt() {
final String res = methodService.Contains("123", "2", "int"); final Object res = methodService.Contains(123, 2, "int");
Assertions.assertEquals("true", res); Assertions.assertEquals(true, res);
} }
@Test @Test
void testMethodContainsString() { void testMethodContainsString() {
final String res = methodService.Contains("1", "2", "string"); final Object res = methodService.Contains("1", "2", "string");
Assertions.assertEquals("false", res); 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": { "devDependencies": {
"@vitejs/plugin-vue": "^4.0.0", "@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.0.0" "vite": "^4.0.0"
},
"engines": {
"node": "^14.18.0 || >=16.0.0"
} }
}, },
"node_modules/@babel/parser": { "node_modules/@babel/parser": {

View File

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