2 Commits

Author SHA1 Message Date
Katerina881
444e67736e Front output of array was changed to basic for arrays. 2023-03-07 11:57:44 +04:00
3136b75b1b Labwork2 is done. 2023-03-07 00:41:40 +04:00
22 changed files with 220 additions and 20 deletions

Binary file not shown.

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
sbapp

2
.idea/misc.xml generated
View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK" />
</project>

View File

@@ -14,7 +14,8 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'junit:junit:4.13.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {

View File

@@ -5,9 +5,9 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
</head>
<body>
<div class = "flex-container" style = "flex-direction: column; display: flex;">
<div class = "flex-container" style = "flex-direction: row;display: flex; margin-bottom: 10px; gap: 10px;align-items: center; width: 500px;">
<input type="text" class="form-control form-control-sm" id="word" placeholder="Enter word" style="border: 3px solid black;">
<div class = "flex-container" style = "flex-direction: column; display: flex; margin-top: 10px; width: 800px; height: 200px">
<div class = "flex-container" style = "flex-direction: row;display: flex; margin-bottom: 10px; gap: 10px;align-items: center;">
<input type="text" class="form-control form-control-sm" id="value" placeholder="Enter" style="border: 3px solid black;">
<select class = "form-select-sm" id="transformation" aria-label="form-select-sm example">
<option value="toupper" selected> toUpper</option>
<option value="removews"> removeWhitespaces</option>
@@ -16,7 +16,11 @@
</select>
<input id = "result" class="form-control form-control-sm" readonly style="border: 3px dotted black;">
</div>
<div class = "flex-container" style = "flex-direction: row;display: flex; width: 500px; justify-content: center;">
<div class = "flex-container" style = "flex-direction: row;display: flex; justify-content: center; gap: 10px">
<select class = "form-select-sm" id="converter" aria-label="form-select-sm example">
<option value="String" selected> String</option>
<option value="AString"> AString</option>
</select>
<button type="button" class = "btn btn-dark align-self-center" onclick="transform()">
Transform
</button>
@@ -27,9 +31,13 @@
<script>
async function transform()
{
const word = document.getElementById("word").value
const _value = document.getElementById("value").value
const func = document.getElementById("transformation").value
document.getElementById("result").value = await (await fetch(`http://localhost:8080/${func}?word=${word}`)).text()
const convType = document.getElementById("converter").value
document.getElementById("result").value = await (await fetch(`http://localhost:8080/${func}?value=${_value}&type=${convType}`)).text()
}
</script>
</body>

View File

@@ -4,31 +4,41 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.is.sbapp.Services.ConverterService;
@RestController
public class StringController {
@GetMapping("/toupper")
public String toupper(@RequestParam(value = "word",defaultValue = " ") String word)
private final ConverterService converterService;
public StringController(ConverterService converterService)
{
return word.toUpperCase();
this.converterService = converterService;
}
@GetMapping("/toupper")
public Object toUpper(@RequestParam(value = "value",defaultValue = " ") Object value,
@RequestParam(value = "type",defaultValue = "String") String converterType)
{
return converterService.toUpper(value,converterType);
}
@GetMapping("/removews")
public String removewhitespaces(@RequestParam(value = "word",defaultValue = " ") String word)
public Object removeWhitespaces(@RequestParam(value = "value",defaultValue = " ") Object value,
@RequestParam(value = "type",defaultValue = "String") String converterType)
{
return word.replaceAll("\s+","");
return converterService.removeWhitespaces(value,converterType);
}
@GetMapping("/removedigits")
public String removedigits(@RequestParam(value = "word",defaultValue = " ") String word)
public Object removeDigits(@RequestParam(value = "value",defaultValue = " ") Object value,
@RequestParam(value = "type",defaultValue = "String") String converterType)
{
return word.replaceAll("\\d","");
return converterService.removeDigits(value,converterType);
}
@GetMapping("/tolower")
public String tolower(@RequestParam(value = "word", defaultValue = " ") String word)
public Object toLower(@RequestParam(value = "value",defaultValue = " ") Object value,
@RequestParam(value = "type",defaultValue = "String") String converterType)
{
return word.toLowerCase();
return converterService.toLower(value,converterType);
}
}

View File

@@ -0,0 +1,31 @@
package ru.ulstu.is.sbapp.Implementation;
import org.springframework.stereotype.Component;
import ru.ulstu.is.sbapp.Interfaces.Converter;
import java.util.Arrays;
import java.util.stream.Collectors;
@Component(value = "AString")
public class StringArrayConverter implements Converter<String[]>
{
@Override
public String[] toUpper(String[] value) {
return Arrays.stream(value).map(word -> word.toUpperCase()).toArray(String[]::new);
}
@Override
public String[] removeWhitespaces(String[] value) {
return (String[]) Arrays.stream(value).map(word -> word.replaceAll("\s+","")).toArray(String[]::new);
}
@Override
public String[] removeDigits(String[] value) {
return (String[]) Arrays.stream(value).map(word -> word.replaceAll("\\d","")).toArray(String[]::new);
}
@Override
public String[] toLower(String[] value) {
return (String[]) Arrays.stream(value).map(word -> word.toLowerCase()).toArray(String[]::new);
}
}

View File

@@ -0,0 +1,28 @@
package ru.ulstu.is.sbapp.Implementation;
import org.springframework.stereotype.Component;
import ru.ulstu.is.sbapp.Interfaces.Converter;
@Component(value = "String")
public class StringConverter implements Converter<String>
{
@Override
public String toUpper(String value) {
return value.toString().toUpperCase();
}
@Override
public String removeWhitespaces(String value) {
return value.toString().replaceAll("\s+","");
}
@Override
public String removeDigits(String value) {
return value.toString().replaceAll("\\d","");
}
@Override
public String toLower(String value) {
return value.toString().toLowerCase();
}
}

View File

@@ -0,0 +1,11 @@
package ru.ulstu.is.sbapp.Interfaces;
import org.springframework.context.annotation.Bean;
public interface Converter<T>
{
public T toUpper(T value);
public T removeWhitespaces(T value);
public T removeDigits(T value);
public T toLower(T value);
}

View File

@@ -4,10 +4,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SbappApplication {
public static void main(String[] args) {

View File

@@ -0,0 +1,52 @@
package ru.ulstu.is.sbapp.Services;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import ru.ulstu.is.sbapp.Interfaces.Converter;
import java.util.Arrays;
@Service
public class ConverterService
{
private final ApplicationContext applicationContext;
public ConverterService(ApplicationContext applicationContext)
{
this.applicationContext = applicationContext;
}
public Object toUpper(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String")) {
return converter.toUpper(value);
}
return converter.toUpper(value.toString().split("\s+"));
}
public Object removeWhitespaces(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String")) {
return converter.removeWhitespaces(value);
}
return converter.removeWhitespaces(value.toString().split("\s+"));
}
public Object removeDigits(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String"))
{
return converter.removeDigits(value);
}
return converter.removeDigits(value.toString().split("\s+"));
}
public Object toLower(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String"))
{
return converter.toLower(value);
}
return converter.toLower(value.toString().split("\s+"));
}
}

View File

@@ -0,0 +1,60 @@
package ru.ulstu.is.sbapp;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.Services.ConverterService;
@SpringBootTest
public class ConverterServiceTests
{
@Autowired
ConverterService converterService;
@Test
public void stringToUpper()
{
Assertions.assertEquals("LOVE PROGRAMMING",converterService.toUpper("love programming","String"));
}
@Test
public void stringToLower()
{
Assertions.assertEquals("love programming",converterService.toLower("LOVE PROGRAMMING","String"));
}
@Test
public void stringRemoveDigits()
{
Assertions.assertEquals("LVE PRGRAMMNG",converterService.removeDigits("L0VE PR0GRAMM1NG","String"));
}
@Test
public void stringRemoveWhitespaces()
{
Assertions.assertEquals("loveprogramming",converterService.removeWhitespaces("lo ve pro gramm i ng","String"));
}
@Test
public void aStringToUpper()
{
String expected[] = new String[]{"LOVE","PROGRAMMING"};
Assertions.assertArrayEquals(expected,(String[])converterService.toUpper("love programming","AString"));
}
@Test
public void aStringToLower()
{
String expected[] = new String[]{"love","programming"};
Assertions.assertArrayEquals(expected,(String[])converterService.toLower("LOVE PROGRAMMING","AString"));
}
@Test
public void aStringRemoveDigits()
{
String expected[] = new String[]{"LVE","PRGRAMMNG"};
Assertions.assertArrayEquals(expected, (String[]) converterService.removeDigits("L0VE PR0GRAMM1NG","AString"));
}
@Test
public void aStringRemoveWhitespaces()
{
String expected[] = new String[]{"lo","ve","pro","gramm","i","ng"};
Assertions.assertArrayEquals(expected,(String[])converterService.removeWhitespaces("lo ve pro gramm i ng","AString"));
}
}