This commit is contained in:
Татьяна Артамонова 2023-03-14 11:27:13 +04:00
parent e8358ded65
commit 6bd3b5c5be
7 changed files with 178 additions and 21 deletions

View File

@ -2,33 +2,12 @@ package ru.ulstu.is.sbapp;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @SpringBootApplication
@RestController
public class SbappApplication { public class SbappApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SbappApplication.class, args); SpringApplication.run(SbappApplication.class, args);
} }
@GetMapping("/Sum/{val1}/{val2}")
public String Sum(@PathVariable String val1, @PathVariable String val2) {
return val1 + val2;
}
@GetMapping("/ToUpper/{str3}")
public String ToUpper(@PathVariable String str3) {
return str3.toUpperCase();
}
@GetMapping("/Split/{str1}/{str2}")
public String Split(@PathVariable String str1, @PathVariable String str2) {
String[] array = str1.split(str2);
return String.join(" ", array);
}
@GetMapping("/Replace/{str}/{oldChar}/{newChar}")
public String Replace(@PathVariable String str, @PathVariable char oldChar, @PathVariable char newChar) {
return str.replace(oldChar, newChar);
}
} }

View File

@ -0,0 +1,20 @@
package ru.ulstu.is.sbapp.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.ulstu.is.sbapp.interfaces.DoubleType;
import ru.ulstu.is.sbapp.interfaces.StringType;
@Configuration
public class TypeConfiguration {
@Bean(value = "str")
public StringType createStrType(){
return new StringType();
}
@Bean(value = "double")
public DoubleType createDoubleType(){
return new DoubleType();
}
}

View File

@ -0,0 +1,43 @@
package ru.ulstu.is.sbapp.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.is.sbapp.service.ServiceType;
@RestController
public class MainController {
private final ServiceType Service;
public MainController(ServiceType service) {
Service = service;
}
@GetMapping("/Func1")
public Object Func1(@RequestParam(value = "Type") String Type,
@RequestParam(value = "value1") Object value1,
@RequestParam(value = "value2") Object value2){
return Service.Func1(value1,value2,Type);
}
@GetMapping("/Func2")
public Object Func2(@RequestParam(value = "Type") String Type,
@RequestParam(value = "value1") Object value1,
@RequestParam(value = "value2") Object value2){
return Service.Func2(value1, value2, Type);
}
@GetMapping("/Func3")
public Object Func3(@RequestParam(value = "Type") String Type,
@RequestParam(value = "value1") Object value1,
@RequestParam(value = "value2") Object value2){
return Service.Func3(value1,value2,Type);
}
@GetMapping("/Func4")
public Object Func4(@RequestParam(value = "Type") String Type,
@RequestParam(value = "value1") Object value1,
@RequestParam(value = "value2") Object value2){
return Service.Func4(value1,value2,Type);
}
}

View File

@ -0,0 +1,23 @@
package ru.ulstu.is.sbapp.interfaces;
public class DoubleType implements ITypeOfData<Double> {
@Override
public Double Func1(Double value1, Double value2) {
return value1 + value2;
}
@Override
public Double Func2(Double value1, Double value2) {
return Math.min(value1, value2);
}
@Override
public Double Func3(Double value1, Double value2) {
return Math.max(value1, value2);
}
@Override
public Double Func4(Double value1, Double value2) {
return Math.pow(value1,value2);
}
}

View File

@ -0,0 +1,8 @@
package ru.ulstu.is.sbapp.interfaces;
public interface ITypeOfData <T>{
T Func1(T value1, T value2);
T Func2(T value1, T value2);
T Func3(T value1, T value2);
T Func4(T value1, T value2);
}

View File

@ -0,0 +1,26 @@
package ru.ulstu.is.sbapp.interfaces;
import java.util.Locale;
public class StringType implements ITypeOfData<String> {
@Override
public String Func1(String value1, String value2) {
return value1 + value2;
}
@Override
public String Func2(String value1, String value2) {
return value1.toUpperCase() + value2.toUpperCase();
}
@Override
public String Func3(String value1, String value2) {
String[] array = value1.split(value2);
return String.join(" ", array);
}
@Override
public String Func4(String value1, String value2) {
return value1.toLowerCase() + value2.toLowerCase();
}
}

View File

@ -0,0 +1,58 @@
package ru.ulstu.is.sbapp.service;
import org.springframework.stereotype.Service;
import org.apache.catalina.core.ApplicationContext;
import ru.ulstu.is.sbapp.interfaces.ITypeOfData;
@Service
public class ServiceType {
private final ApplicationContext applicationContext;
private ITypeOfData _type;
private Object _value1;
private Object _value2;
public ServiceType(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
private void ValidateParams(Object value1, Object value2, String type){
_type = (ITypeOfData)applicationContext.getBean(type);
switch (type) {
case "double" -> {
try {
_value1 = Double.valueOf(value1.toString());
_value2 = Double.valueOf(value2.toString());
}catch (Exception ex){
_value1 = 0.0;
_value2 = 0.0;
}
}
case "str" -> {
_value1 = value1;
_value2 = value2;
}
}
}
public Object Func1(Object value1, Object value2, String type){
ValidateParams(value1,value2,type);
return String.format("%s", _type.Func1(_value1,_value2));
}
public Object Func2(Object value1, Object value2, String type){
ValidateParams(value1,value2,type);
return String.format("%s", _type.Func2(_value1, _value2));
}
public Object Func3(Object value1, Object value2, String type){
ValidateParams(value1,value2,type);
return String.format("%s", _type.Func3(_value1,_value2));
}
public Object Func4(Object value1, Object value2, String type){
ValidateParams(value1,value2,type);
return String.format("%s", _type.Func4(_value1,_value2));
}
}