Labwork2 is done.

This commit is contained in:
Yuee Shiness 2023-03-07 00:41:40 +04:00
parent 4264287804
commit 3136b75b1b
19 changed files with 238 additions and 20 deletions

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 String 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 String 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 String 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 String 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,50 @@
package ru.ulstu.is.sbapp.Implementation;
import org.springframework.stereotype.Component;
import ru.ulstu.is.sbapp.Interfaces.Converter;
@Component(value = "AString")
public class StringArrayConverter implements Converter<String []>
{
public StringBuilder fullStr;
@Override
public String toUpper(String [] value) {
fullStr = new StringBuilder();
for (String word : value)
{
fullStr.append(word.toUpperCase() + "|");
}
return fullStr.toString();
}
@Override
public String removeWhitespaces(String[] value) {
fullStr = new StringBuilder();
for (String word : value)
{
fullStr.append(word.replaceAll("\s+","") + "|");
}
return fullStr.toString();
}
@Override
public String removeDigits(String[] value) {
fullStr = new StringBuilder();
for (String word : value)
{
fullStr.append(word.replaceAll("\\d","") + "|");
}
return fullStr.toString();
}
@Override
public String toLower(String[] value) {
fullStr = new StringBuilder();
for (String word : value)
{
fullStr.append(word.toLowerCase() + "|");
}
return fullStr.toString();
}
}

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,9 @@
package ru.ulstu.is.sbapp.Interfaces;
public interface Converter<T>
{
public String toUpper(T value);
public String removeWhitespaces(T value);
public String removeDigits(T value);
public String 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,58 @@
package ru.ulstu.is.sbapp.Services;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import ru.ulstu.is.sbapp.Interfaces.Converter;
import java.util.Arrays;
@Service
public class ConverterService
{
private final ApplicationContext applicationContext;
private String[] str;
public ConverterService(ApplicationContext applicationContext)
{
this.applicationContext = applicationContext;
}
public String toUpper(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String"))
{
return converter.toUpper(value.toString());
}
str = value.toString().split(" ");
return converter.toUpper(str);
}
public String removeWhitespaces(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String"))
{
return converter.removeWhitespaces(value.toString());
}
str = value.toString().split(" ");
return converter.removeWhitespaces(str);
}
public String removeDigits(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String"))
{
return converter.removeDigits(value.toString());
}
str = value.toString().split(" ");
return converter.removeDigits(str);
}
public String toLower(Object value, String converterType)
{
Converter converter = (Converter) applicationContext.getBean(converterType);
if(converterType.equals("String"))
{
return converter.toLower(value.toString());
}
str = value.toString().split(" ");
return converter.toLower(str);
}
}

View File

@ -0,0 +1,56 @@
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()
{
Assertions.assertEquals("LOVE|PROGRAMMING|",converterService.toUpper("love programming","AString"));
}
@Test
public void aStringToLower()
{
Assertions.assertEquals("love|programming|",converterService.toLower("LOVE PROGRAMMING","AString"));
}
@Test
public void aStringRemoveDigits()
{
Assertions.assertEquals("LVE|PRGRAMMNG|",converterService.removeDigits("L0VE PR0GRAMM1NG","AString"));
}
@Test
public void aStringRemoveWhitespaces()
{
Assertions.assertEquals("lo|ve|pro|gramm|i|ng|",converterService.removeWhitespaces("lo ve pro gramm i ng","AString"));
}
}