lab2: fixes

This commit is contained in:
Katerina881 2023-02-21 12:03:06 +04:00
parent 5d6c6afeba
commit 3bea45850b
3 changed files with 30 additions and 55 deletions

View File

@ -14,23 +14,27 @@ public class OperationController {
}
@GetMapping("/sum")
public Object sum(@RequestParam String val1, @RequestParam String val2) {
return (Object) operationService.sum(val1, val2);
public Object sum(@RequestParam String val1, @RequestParam String val2, @RequestParam String type) {
return (Object) operationService.sum(val1, val2, type);
}
@GetMapping("/mul")
public Object mul(@RequestParam String val1, @RequestParam Integer val2) {
return (Object) operationService.mul(val1, val2);
public Object mul(@RequestParam String val1, @RequestParam Integer val2, @RequestParam String type) {
return (Object) operationService.mul(val1, val2, type);
}
@GetMapping("/length")
public Object length(@RequestParam String val1) {
return (Object) operationService.length(val1);
public Object length(@RequestParam String val1, @RequestParam String type) {
return (Object) operationService.length(val1, type);
}
@GetMapping("/startsWith")
public Object startsWith(@RequestParam String val1, @RequestParam String val2) {
return (Object) operationService.startsWith(val1, val2);
public Object startsWith(@RequestParam String val1, @RequestParam String val2, @RequestParam String type) {
return (Object) operationService.startsWith(val1, val2, type);
}

View File

@ -13,64 +13,35 @@ public class OperationService {
this.applicationContext = applicationContext;
}
public Object sum(String o, String o2){
String type = "";
try {
Integer.parseInt(o);
Integer.parseInt(o2);
type = "num";
} catch (Exception ex) {
type = "str";
}
public Object sum(Object o, Object o2, String type){
OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
if (type.equals("num"))
return operationInterface.sum(Integer.parseInt(o),Integer.parseInt(o2));
return operationInterface.sum(Integer.parseInt(o.toString()),Integer.parseInt(o2.toString()));
return operationInterface.sum(o, o2);
}
public Object mul(String o, Integer o2){
String type = "";
public Object mul(Object o, Object o2, String type){
try {
Integer.parseInt(o);
type = "num";
} catch (Exception ex) {
type = "str";
}
OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
if (type.equals("num"))
return operationInterface.mul(Integer.parseInt(o), o2);
return operationInterface.mul(o, o2);
return operationInterface.mul(Integer.parseInt(o.toString()), Integer.parseInt(o2.toString()));
return operationInterface.mul(o, Integer.parseInt(o2.toString()));
}
public Object length(String o){
String type = "";
public Object length(Object o, String type){
try {
Integer.parseInt(o);
type = "num";
} catch (Exception ex) {
type = "str";
}
OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
if (type.equals("num"))
return operationInterface.length(Integer.parseInt(o));
return operationInterface.length(Integer.parseInt(o.toString()));
return operationInterface.length(o);
}
public Object startsWith(String o, String o2){
String type = "";
public Object startsWith(Object o, Object o2, String type){
try {
Integer.parseInt(o);
Integer.parseInt(o2);
type = "num";
} catch (Exception ex) {
type = "str";
}
OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
if (type.equals("num"))
return operationInterface.startsWith(Integer.parseInt(o), Integer.parseInt(o2));
return operationInterface.startsWith(Integer.parseInt(o.toString()), Integer.parseInt(o2.toString()));
return operationInterface.startsWith(o,o2);
}

View File

@ -13,48 +13,48 @@ class SpringipApplicationTests {
@Test
public void testSumString() {
final String res = (String) operationService.sum("Привет ", "Мир!");
final String res = (String) operationService.sum("Привет ", "Мир!", "str");
Assertions.assertEquals("Привет Мир!", res);
}
@Test
public void testSumNum() {
final Integer res = (Integer) operationService.sum("1", "2");
final Integer res = (Integer) operationService.sum(1, 2, "num");
Assertions.assertEquals(3, res);
}
@Test
public void testMulString() {
final String res = (String) operationService.mul("A", 4);
final String res = (String) operationService.mul("A", 4, "str");
Assertions.assertEquals("AAAA", res);
}
@Test
public void testMulNum() {
final Integer res = (Integer) operationService.mul("2", 4);
final Integer res = (Integer) operationService.mul(2, 4, "num");
Assertions.assertEquals(8, res);
}
@Test
public void testLengthString() {
final Integer res = (Integer) operationService.length("Hello");
final Integer res = (Integer) operationService.length("Hello", "str");
Assertions.assertEquals(5, res);
}
@Test
public void testLengthNum() {
final Integer res = (Integer) operationService.length("777");
final Integer res = (Integer) operationService.length(777,"num");
Assertions.assertEquals(3, res);
}
@Test
public void testStWString() {
final boolean res = (boolean) operationService.startsWith("Orange","Or");
final boolean res = (boolean) operationService.startsWith("Orange","Or", "str");
Assertions.assertEquals(true, res);
}
@Test
public void testStWNum() {
final boolean res = (boolean) operationService.startsWith("777","77");
final boolean res = (boolean) operationService.startsWith(777, 77, "num");
Assertions.assertEquals(true, res);
}