diff --git a/front/index.html b/front/index.html
index 7b35432..b3c095c 100644
--- a/front/index.html
+++ b/front/index.html
@@ -20,22 +20,27 @@
+
+
+
+
+
-
-
+
+
+
diff --git a/front/js/script.js b/front/js/script.js
index 2cf9c4c..42363bd 100644
--- a/front/js/script.js
+++ b/front/js/script.js
@@ -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;
}
\ No newline at end of file
diff --git a/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java b/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java
index 8f81206..90ba03e 100644
--- a/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java
+++ b/src/main/java/ru/ulstu/is/sbapp/SbappApplication.java
@@ -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);
- }
-
-
}
diff --git a/src/main/java/ru/ulstu/is/sbapp/calculator/controller/CalcController.java b/src/main/java/ru/ulstu/is/sbapp/calculator/controller/CalcController.java
new file mode 100644
index 0000000..b0d8998
--- /dev/null
+++ b/src/main/java/ru/ulstu/is/sbapp/calculator/controller/CalcController.java
@@ -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);
+ }
+}
diff --git a/src/main/java/ru/ulstu/is/sbapp/calculator/domain/Calculator.java b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/Calculator.java
new file mode 100644
index 0000000..6a450c5
--- /dev/null
+++ b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/Calculator.java
@@ -0,0 +1,11 @@
+package ru.ulstu.is.sbapp.calculator.domain;
+
+public interface Calculator{
+ 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);
+}
diff --git a/src/main/java/ru/ulstu/is/sbapp/calculator/domain/DateCalculator.java b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/DateCalculator.java
new file mode 100644
index 0000000..8c06985
--- /dev/null
+++ b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/DateCalculator.java
@@ -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 {
+
+ @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;
+ }
+}
diff --git a/src/main/java/ru/ulstu/is/sbapp/calculator/domain/IntCalculator.java b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/IntCalculator.java
new file mode 100644
index 0000000..e695f2c
--- /dev/null
+++ b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/IntCalculator.java
@@ -0,0 +1,26 @@
+package ru.ulstu.is.sbapp.calculator.domain;
+
+import org.springframework.stereotype.Component;
+
+@Component(value="int")
+public class IntCalculator implements Calculator{
+ @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;
+ }
+}
diff --git a/src/main/java/ru/ulstu/is/sbapp/calculator/domain/StringCalculator.java b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/StringCalculator.java
new file mode 100644
index 0000000..f024a5a
--- /dev/null
+++ b/src/main/java/ru/ulstu/is/sbapp/calculator/domain/StringCalculator.java
@@ -0,0 +1,40 @@
+package ru.ulstu.is.sbapp.calculator.domain;
+
+import org.springframework.stereotype.Component;
+
+@Component(value="string")
+public class StringCalculator implements Calculator{
+
+ @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;
+ }
+}
diff --git a/src/main/java/ru/ulstu/is/sbapp/calculator/service/CalculatorService.java b/src/main/java/ru/ulstu/is/sbapp/calculator/service/CalculatorService.java
new file mode 100644
index 0000000..7689d64
--- /dev/null
+++ b/src/main/java/ru/ulstu/is/sbapp/calculator/service/CalculatorService.java
@@ -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())));
+ }
+}
diff --git a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java
index bcde5ac..261b6be 100644
--- a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java
+++ b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java
@@ -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);
+ }
}