This commit is contained in:
Ismailov_Rovshan 2023-04-02 23:49:02 +04:00
parent eed0b46ad4
commit f3f0e851ed
10 changed files with 323 additions and 97 deletions

View File

@ -20,22 +20,27 @@
</div>
<div class = "col-md-2">
<select class="form-select" name="selected" aria-label="Default select example">
<option selected>Действие</option>
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
<option value="5">%</option>
<option value="6">con</option>
<option value="1">Сложение</option>
<option value="2">Вычитание</option>
<option value="3">Переворачивание</option>
<option value="4">Сравнение</option>
</select>
</div>
<div class="col-md-2">
<select id="type" class="form-select" >
<option value="int">Число</option>
<option value="string">Строка</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-success">Выполнить</button>
<div class="my-2 col-md-3">
<input name="res" input type="text" class="form-control " placeholder="Результат">
<input name="res" input type="text" class="form-control " placeholder="Результат">
</div>
</form>
<script src="./node_modules/bootstrap/dist/js/bootstrap.min.js"></script>

View File

@ -1,5 +1,6 @@
let form = document.getElementById("form");
let info = document.getElementById("res");
let typeInput = document.getElementById("type");
form.onsubmit = async (e) => {
e.preventDefault();
@ -13,58 +14,23 @@ form.onsubmit = async (e) => {
function chooseOperation(oper){
switch(oper){
case "+":
case "Сложение":
return "sum"
case "-":
return "diff"
case "*":
return "mul"
case "/":
return "div"
case "%":
return "ost"
case "con":
return "con"
case "Вычитание":
return "minus"
case "Переворачивание":
return "reverse"
case "Сравнение":
return "comparison"
}
}
let type = typeInput.value;
let operstor = chooseOperation(op)
if(form.num2.value == 0) return;
res = await fetch(`http://localhost:8080/${operstor}?num1=${form.num1.value}&num2=${form.num2.value}`)
if(form.num2.value == 0)
return;
res = await fetch(`http://localhost:8080/${operstor}?first=${form.num1.value}&second=${form.num2.value }&type=${type}`)
res = await res.text();
// switch(op) {
// case "+":
// res = await fetch(`http://localhost:8080/sum?num1=${form.num1.value}&num2=${form.num2.value}`)
// res = await res.text();
// break;
// case "-":
// res = await fetch(`http://localhost:8080/diff?num1=${form.num1.value}&num2=${form.num2.value}`)
// res = await res.text();
// break;
// case "*":
// res = await fetch(`http://localhost:8080/mul?num1=${form.num1.value}&num2=${form.num2.value}`)
// res = await res.text();
// break;
// case "/":
// if(form.num2.value == 0) return;
// res = await fetch(`http://localhost:8080/div?num1=${form.num1.value}&num2=${form.num2.value}`)
// res = await res.text();
// break;
// case "%":
// if(form.num2.value == 0) return;
// res = await fetch(`http://localhost:8080/ost?num1=${form.num1.value}&num2=${form.num2.value}`)
// res = await res.text();
// break;
// case "con":
// res = await fetch(`http://localhost:8080/con?num1=${form.num1.value}&num2=${form.num2.value}`)
// res = await res.text();
// break;
// }
form.res.value = res;
}

View File

@ -13,43 +13,4 @@ public class SbappApplication {
public static void main(String[] args) {
SpringApplication.run(SbappApplication.class, args);
}
@GetMapping("/hello")
public String hello(){
return "Hello, world";
}
@GetMapping("/sum")
public Integer sum(@RequestParam(defaultValue = "0")Integer num1,
@RequestParam(defaultValue = "0")Integer num2){
return num1 + num2;
}
@GetMapping("/div")
public Integer div(@RequestParam(defaultValue = "0")Integer num1,
@RequestParam(defaultValue = "0")Integer num2){
return num1 / num2;
}
@GetMapping("/mul")
public Integer mul(@RequestParam(defaultValue = "0")Integer num1,
@RequestParam(defaultValue = "0")Integer num2){
return num1 * num2;
}
@GetMapping("/diff")
public Integer diff(@RequestParam(defaultValue = "0")Integer num1,
@RequestParam(defaultValue = "0")Integer num2){
return num1 - num2;
}
@GetMapping("/ost")
public Integer ost(@RequestParam(defaultValue = "0")Integer num1,
@RequestParam(defaultValue = "0")Integer num2){
return num1 % num2;
}
@GetMapping("/con")
public String con(@RequestParam(defaultValue = "")String num1,
@RequestParam(defaultValue = "")String num2){
return num1.concat(num2);
}
}

View File

@ -0,0 +1,44 @@
package ru.ulstu.is.sbapp.calculator.controller;
import org.springframework.stereotype.Controller;
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.calculator.service.CalculatorService;
@RestController
public class CalcController {
private final CalculatorService speakerService;
public CalcController(CalculatorService 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.sbapp.calculator.domain;
public interface Calculator<T>{
public T Sum(T first, T second);
public T Minus(T first, T second);
public T Reverse(T first, T second);
public T Comparison(T first, T second);
}

View File

@ -0,0 +1,39 @@
package ru.ulstu.is.sbapp.calculator.domain;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Component(value="date")
public class DateCalculator implements Calculator<LocalDate> {
@Override
public LocalDate Sum(LocalDate first, LocalDate second) {
return first.plusYears(second.getYear()).
plusMonths(second.getMonthValue()).plusDays(second.getDayOfMonth());
}
@Override
public LocalDate Minus(LocalDate first, LocalDate second) {
return first.minusYears(second.getYear()).
minusMonths(second.getMonthValue()).minusDays(second.getDayOfMonth());
}
@Override
public LocalDate Reverse(LocalDate first, LocalDate second) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedFirst = first.format(formatter);
return LocalDate.parse(formattedFirst, formatter);
}
@Override
public LocalDate Comparison(LocalDate first, LocalDate second) {
int comparisonResult = first.compareTo(second);
if (comparisonResult < 0)
return second;
else return first;
}
}

View File

@ -0,0 +1,26 @@
package ru.ulstu.is.sbapp.calculator.domain;
import org.springframework.stereotype.Component;
@Component(value="int")
public class IntCalculator implements Calculator<Integer>{
@Override
public Integer Sum(Integer x, Integer y) {
return x + y;
}
@Override
public Integer Minus(Integer x, Integer y) {
return x - y;
}
@Override
public Integer Reverse(Integer first, Integer second) {
return (first + second) * (-1);
}
@Override
public Integer Comparison(Integer first, Integer second) {
if (first >= second)
return first;
else return second;
}
}

View File

@ -0,0 +1,40 @@
package ru.ulstu.is.sbapp.calculator.domain;
import org.springframework.stereotype.Component;
@Component(value="string")
public class StringCalculator implements Calculator<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.contains(arr[i])){
arr[i] = "";
}
}
return String.join("", arr);
}
@Override
public String Reverse(String first, String second) {
String ourStr = first.concat(second);
StringBuilder newStr = new StringBuilder();
for(int i = ourStr.length() - 1; i >= 0; i--){
newStr.append(ourStr.charAt(i));
}
return newStr.toString();
}
@Override
public String Comparison(String first, String second) {
if (first.length() >= second.length())
return first;
else return second;
}
}

View File

@ -0,0 +1,67 @@
package ru.ulstu.is.sbapp.calculator.service;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import ru.ulstu.is.sbapp.calculator.domain.Calculator;
import ru.ulstu.is.sbapp.calculator.domain.DateCalculator;
import ru.ulstu.is.sbapp.calculator.domain.StringCalculator;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Service
public class CalculatorService {
private final ApplicationContext _applicationContext;
public CalculatorService(ApplicationContext applicationContext) {
this._applicationContext = applicationContext;
}
public String Sum(Object first, Object second, String type) {
final Calculator speaker = (Calculator) _applicationContext.getBean(type);
if (speaker instanceof StringCalculator)
return String.format("%s", speaker.Sum(first, second));
else if (speaker instanceof DateCalculator)
return String.format("%s", (speaker.Sum(LocalDate.parse(first.toString()), LocalDate.parse(second.toString()))));
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 Calculator speaker = (Calculator) _applicationContext.getBean(type);
if (speaker instanceof StringCalculator)
return String.format("%s", speaker.Minus(first,second));
else if (speaker instanceof DateCalculator)
return String.format("%s", (speaker.Minus(LocalDate.parse(first.toString()), LocalDate.parse(second.toString()))));
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 Calculator speaker = (Calculator) _applicationContext.getBean(type);
if (speaker instanceof StringCalculator)
return String.format("%s", speaker.Reverse(first,second));
else if (speaker instanceof DateCalculator)
return String.format("%s", (speaker.Reverse(LocalDate.parse(first.toString()), LocalDate.parse(second.toString()))));
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 Calculator speaker = (Calculator) _applicationContext.getBean(type);
if (speaker instanceof StringCalculator)
return String.format("%s", speaker.Comparison(first,second));
else if (speaker instanceof DateCalculator)
return String.format("%s", (speaker.Comparison(LocalDate.parse(first.toString()), LocalDate.parse(second.toString()))));
else return String.format("%s", speaker.Comparison(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}

View File

@ -1,13 +1,80 @@
package ru.ulstu.is.sbapp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import ru.ulstu.is.sbapp.calculator.domain.Calculator;
import ru.ulstu.is.sbapp.calculator.service.CalculatorService;
@SpringBootTest
class SbappApplicationTests {
@Autowired
CalculatorService calculatorService;
@Test
void contextLoads() {
}
@Test
void testPlusInt() {
final String res = calculatorService.Sum(10, 10, "int");
Assertions.assertEquals(20, Integer.parseInt(res));
}
@Test
void testMinusInt() {
final String res = calculatorService.Ras(8, 4, "int");
Assertions.assertEquals(4, Integer.parseInt(res));
}
@Test
void testReverseInt() {
final String res = calculatorService.Rev(10, 10, "int");
Assertions.assertEquals(-20, Integer.parseInt(res));
}
@Test
void testComparisonInt() {
final String res = calculatorService.Com(8, 4, "int");
Assertions.assertEquals(8, Integer.parseInt(res));
}
@Test
void testPlusStr() {
final String res = calculatorService.Sum("10", "10", "string");
Assertions.assertEquals("1010", res);
}
@Test
void testMinusStr() {
final String res = calculatorService.Ras("846734", "4", "string");
Assertions.assertEquals("8673", res);
}
@Test
void testReverseStr() {
final String res = calculatorService.Rev("846734", "312", "string");
Assertions.assertEquals("213437648", res);
}
@Test
void testComparisonStr() {
final String res = calculatorService.Com("846734", "312", "string");
Assertions.assertEquals("846734", res);
}
@Test
void testNumberFormatException() {
Assertions.assertThrows(NumberFormatException.class, () -> calculatorService.Sum("п", 3, "int"));
}
@Test
void testSumDate(){
final String res = calculatorService.Sum("2023-07-07", "0001-01-03", "date");
Assertions.assertEquals("2024-08-10", res);
}
@Test
void testRasDate(){
final String res = calculatorService.Ras("2023-07-07", "2020-01-03", "date");
Assertions.assertEquals("0003-06-04", res);
}
@Test
void testComDate(){
final String res = calculatorService.Com("2023-07-07", "2020-01-03", "date");
Assertions.assertEquals("2023-07-07", res);
}
}