ЛР2 Все кроме тестов

This commit is contained in:
ityurner02@mail.ru 2023-02-28 10:16:20 +04:00
parent f68abec3fa
commit 84c206fb61
8 changed files with 214 additions and 61 deletions

View File

@ -13,20 +13,26 @@
<body>
<form id="frm-items" class="row g-3">
<div class="d-flex flex-column w-25 my-5 mx-3">
Введите строку
<input id="string" class="form-control" type="text" placeholder="word"></input>
укажите разделитель
<input id="place" class="form-control" type="text" placeholder="place"></input>
Выберите тип данных
<div>
<select id="type" class="form-select" >
<option value="int">Число</option>
<option value="string">Строка</option>
</select>
</div>
Введите первое значение
<input id="first" class="form-control"></input>
Введите второе значение
<input id="second" class="form-control"></input>
Выберите операцию
<div>
<button type="button" class="btn btn-primary" id="buttonUpper">Upper</button>
<button type="button" class="btn btn-primary" id="buttonLower">Lower</button>
<button type="button" class="btn btn-primary" id="buttonSum">Sum</button>
<button type="button" class="btn btn-primary" id="buttonMinus">Minus</button>
<button type="button" class="btn btn-primary" id="buttonReverse">Reverse</button>
<button type="button" class="btn btn-primary" id="buttonCount">Length</button>
<button type="button" class="btn btn-primary" id="buttonDel">Del</button>
<button type="button" class="btn btn-primary" id="buttonComparison">Comparison</button>
</div>
Результат
<input id="res" class="form-control" type="text" placeholder="result"></input>
<input id="res" class="form-control"></input>
</div>
</form>
<script src="script.js"></script>

View File

@ -1,40 +1,41 @@
let buttonUpper = document.getElementById("buttonUpper");
let buttonLower = document.getElementById("buttonLower");
let buttonSum = document.getElementById("buttonSum");
let buttonMinus = document.getElementById("buttonMinus");
let buttonReverse = document.getElementById("buttonReverse");
let buttonCount = document.getElementById("buttonCount");
let buttonDel = document.getElementById("buttonDel");
let stringInput = document.getElementById("string");
let placeInput = document.getElementById("place");
let buttonComparison = document.getElementById("buttonComparison");
let numberOneInput = document.getElementById("first");
let numberTwoInput = document.getElementById("second");
let typeInput = document.getElementById("type");
let resultInput = document.getElementById("res");
buttonUpper.onclick = function(event) {
let str = stringInput.value;
fetch(`http://localhost:8080/toUpperCase?string=${str}`)
buttonSum.onclick = function(event) {
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
let type = typeInput.value;
fetch(`http://localhost:8080/sum?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => resultInput.value = res);
}
buttonLower.onclick = function(event) {
let str = stringInput.value;
fetch(`http://localhost:8080/toLowerCase?string=${str}`)
buttonMinus.onclick = function(event) {
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
let type = typeInput.value;
fetch(`http://localhost:8080/minus?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => resultInput.value = res);
}
buttonReverse.onclick = function(event) {
let str = stringInput.value;
fetch(`http://localhost:8080/reverse?string=${str}`)
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
let type = typeInput.value;
fetch(`http://localhost:8080/reverse?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => resultInput.value = res);
}
buttonCount.onclick = function(event) {
let str = stringInput.value;
fetch(`http://localhost:8080/count?string=${str}`)
.then(response => response.text())
.then(res => resultInput.value = res);
}
buttonDel.onclick = function(event) {
let str = stringInput.value;
let plc = placeInput.value;
fetch(`http://localhost:8080/del?string=${str}&place=${plc}`)
buttonComparison.onclick = function(event) {
let num_1 = numberOneInput.value;
let num_2 = numberTwoInput.value;
let type = typeInput.value;
fetch(`http://localhost:8080/comparison?first=${num_1}&second=${num_2}&type=${type}`)
.then(response => response.text())
.then(res => resultInput.value = res);
}

View File

@ -13,32 +13,4 @@ public class Lab1 {
public static void main(String[] args) {
SpringApplication.run(Lab1.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/toUpperCase")
public String toUpperCase(@RequestParam(defaultValue = "word") String string){
return string.toUpperCase();
}
@GetMapping("/toLowerCase")
public String toLowerCase(@RequestParam(defaultValue = "word") String string){
return string.toLowerCase();
}
@GetMapping("/reverse")
public String reverse(@RequestParam(defaultValue = "word") String string){
String newStr="";
for(int i = string.length() - 1; i >= 0; i--){
newStr += string.charAt(i);
}
return newStr;
}
@GetMapping("/count")
public String count(@RequestParam(defaultValue = "word") String string){
return Integer.toString(string.length());
}
@GetMapping("/del")
public String del(@RequestParam(defaultValue = "word") String string, String place){
return String.join(" ", string.split(place));
}
}

View File

@ -0,0 +1,43 @@
package ru.ulstu.is.lab1.speaker.controller;
import ru.ulstu.is.lab1.speaker.service.MethodService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MethodController {
private final MethodService speakerService;
public MethodController(MethodService speakerService) {
this.speakerService = speakerService;
}
@GetMapping("/sum")
public String Sum(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return speakerService.Sum(first, second, type);
}
@GetMapping("/minus")
public String Ras(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return speakerService.Ras(first, second, type);
}
@GetMapping("/reverse")
public String Pros(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return speakerService.Rev(first, second, type);
}
@GetMapping("/comparison")
public String Del(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return speakerService.Com(first, second, type);
}
}

View File

@ -0,0 +1,11 @@
package ru.ulstu.is.lab1.speaker.domain;
public interface IMethod<T> {
T Sum(T first, T second);
T Minus(T first, T second);
T Reverse(T first, T second);
T Comparison(T first, T second);
}

View File

@ -0,0 +1,28 @@
package ru.ulstu.is.lab1.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value="int")
public class MethodInt implements IMethod<Integer>{
public Integer Sum(Integer first, Integer second) {
return Integer.parseInt(first.toString()) + Integer.parseInt(second.toString());
}
public Integer Minus(Integer first, Integer second) {
return Integer.parseInt(first.toString()) - Integer.parseInt(second.toString());
}
public Integer Reverse(Integer first, Integer second) {
return (Integer.parseInt(first.toString()) + Integer.parseInt(second.toString())) * (-1);
}
public Integer Comparison(Integer first, Integer second) {
int num1 = Integer.parseInt(first.toString());
int num2 = Integer.parseInt(second.toString());
if (num1 >= num2){
return num1;
}else{
return num2;
}
}
}

View File

@ -0,0 +1,41 @@
package ru.ulstu.is.lab1.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value="string")
public class MethodString implements IMethod<String>{
@Override
public String Sum(String first, String second) {
return first.concat(second);
}
@Override
public String Minus(String first, String second) {
String arr[] = first.split("");
for(int i = 0; i < first.length(); i++){
if (second.indexOf(arr[i]) >= 0){
arr[i] = "";
}
}
return String.join("", arr);
}
@Override
public String Reverse(String first, String second) {
String ourStr = first.concat(second);
String newStr = "";
for(int i = ourStr.length() - 1; i >= 0; i--){
newStr += ourStr.charAt(i);
}
return newStr;
}
@Override
public String Comparison(String first, String second) {
if (first.length() >= second.length()){
return first;
}else{
return second;
}
}
}

View File

@ -0,0 +1,51 @@
package ru.ulstu.is.lab1.speaker.service;
import ru.ulstu.is.lab1.speaker.domain.IMethod;
import ru.ulstu.is.lab1.speaker.domain.MethodString;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class MethodService {
private final ApplicationContext applicationContext;
public MethodService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String Sum(Object first, Object second, String type) {
final IMethod speaker = (IMethod) applicationContext.getBean(type);
if (speaker instanceof MethodString){
return String.format("%s", speaker.Sum(first,second));
}else{
return String.format("%s", speaker.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
}
public String Ras(Object first, Object second, String type) {
final IMethod speaker = (IMethod) applicationContext.getBean(type);
if (speaker instanceof MethodString){
return String.format("%s", speaker.Minus(first,second));
}else{
return String.format("%s", speaker.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
}
public String Rev(Object first, Object second, String type) {
final IMethod speaker = (IMethod) applicationContext.getBean(type);
if (speaker instanceof MethodString){
return String.format("%s", speaker.Reverse(first,second));
}else{
return String.format("%s", speaker.Reverse(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
}
public String Com(Object first, Object second, String type) {
final IMethod speaker = (IMethod) applicationContext.getBean(type);
if (speaker instanceof MethodString){
return String.format("%s", speaker.Comparison(first,second));
}else {
return String.format("%s", speaker.Comparison(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
}