lab2: interface, impls, controller and service
This commit is contained in:
parent
a40ef56620
commit
32cb0492c3
@ -0,0 +1,38 @@
|
||||
package com.example.springip.controllers;
|
||||
|
||||
import com.example.springip.service.OperationService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OperationController {
|
||||
private final OperationService operationService;
|
||||
|
||||
public OperationController( OperationService operationService) {
|
||||
this.operationService = operationService;
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public Object sum(@RequestParam String val1, @RequestParam String val2) {
|
||||
return (Object) operationService.sum(val1, val2);
|
||||
}
|
||||
|
||||
@GetMapping("/mul")
|
||||
public Object mul(@RequestParam String val1, @RequestParam Integer val2) {
|
||||
return (Object) operationService.mul(val1, val2);
|
||||
}
|
||||
|
||||
@GetMapping("/length")
|
||||
public Object length(@RequestParam String val1) {
|
||||
return (Object) operationService.length(val1);
|
||||
}
|
||||
|
||||
@GetMapping("/startsWith")
|
||||
public Object startsWith(@RequestParam String val1, @RequestParam String val2) {
|
||||
return (Object) operationService.startsWith(val1, val2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.springip.domains;
|
||||
|
||||
public interface OperationInterface<T> {
|
||||
T sum(T o,T o2);
|
||||
T mul(T o, int num);
|
||||
int length(T o);
|
||||
boolean startsWith(T o, T symb);
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.example.springip.domains;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "num")
|
||||
public class OperationNumImpl implements OperationInterface<Integer>{
|
||||
@Override
|
||||
public Integer sum(Integer o, Integer o2) {
|
||||
return o + o2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer mul(Integer o, int num) {
|
||||
return o * num;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length(Integer o) {
|
||||
int n = o;
|
||||
int k = 0;
|
||||
while (n % 10 != 0) {
|
||||
n /= 10;
|
||||
k++;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startsWith(Integer o, Integer symb) {
|
||||
return o % (Math.pow(10,length(symb))) == symb;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.example.springip.domains;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "str")
|
||||
public class OperationStringImpl implements OperationInterface<String>{
|
||||
@Override
|
||||
public String sum(String o, String o2) {
|
||||
return o+o2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String mul(String o, int num) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (int i = 0; i < num; i++) {
|
||||
res.append(o);
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length(String o) {
|
||||
return o.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startsWith(String o, String symb) {
|
||||
return o.startsWith(symb);
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.example.springip.service;
|
||||
|
||||
import com.example.springip.domains.OperationInterface;
|
||||
import com.example.springip.domains.OperationNumImpl;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OperationService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public OperationService(ApplicationContext applicationContext) {
|
||||
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";
|
||||
}
|
||||
OperationInterface operationInterface = (OperationInterface) applicationContext.getBean(type);
|
||||
if (type.equals("num"))
|
||||
return operationInterface.sum(Integer.parseInt(o),Integer.parseInt(o2));
|
||||
return operationInterface.sum(o, o2);
|
||||
}
|
||||
|
||||
public Object mul(String o, Integer 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);
|
||||
}
|
||||
|
||||
public Object length(String 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(o);
|
||||
}
|
||||
|
||||
public Object startsWith(String o, String 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(o,o2);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user