LabWork02 ready
This commit is contained in:
parent
15bc523a42
commit
194ff8ca33
@ -13,7 +13,7 @@
|
||||
<div class="container row">
|
||||
<div class="form-group col-3">
|
||||
<label>num1</label>
|
||||
<input name="num1" type="number" class="form-control col-3" placeholder="Первый аргумент">
|
||||
<input name="num1" class="form-control col-3" placeholder="Первый аргумент">
|
||||
</div>
|
||||
<div class="form-group col-3">
|
||||
<label>Оператор</label>
|
||||
@ -23,7 +23,12 @@
|
||||
<option>*</option>
|
||||
<option>/</option>
|
||||
</select>
|
||||
|
||||
<label>Тип данных</label>
|
||||
<select name="type" class="form-control">
|
||||
<option selected>Int</option>
|
||||
<option>String</option>
|
||||
<option>FloatArray</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-3">
|
||||
<label>num2</label>
|
||||
|
@ -9,31 +9,51 @@ form.onsubmit = async (e) => {
|
||||
if(form.num2.value === "") return;
|
||||
|
||||
let index = form.selected.selectedIndex;
|
||||
let indexType = form.type.selectedIndex;
|
||||
|
||||
let op = form.selected.options[index].textContent;
|
||||
let type = form.type.options[indexType].textContent;
|
||||
let res = "";
|
||||
|
||||
function calc() {
|
||||
switch(op) {
|
||||
case "+":
|
||||
res = await fetch(`http://localhost:8080/sum?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||||
res = await res.text();
|
||||
return "sum";
|
||||
break;
|
||||
|
||||
case "-":
|
||||
res = await fetch(`http://localhost:8080/diff?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||||
res = await res.text();
|
||||
return "diff";
|
||||
break;
|
||||
|
||||
case "*":
|
||||
res = await fetch(`http://localhost:8080/multiply?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||||
res = await res.text();
|
||||
return "mul";
|
||||
break;
|
||||
|
||||
case "/":
|
||||
if(form.num2.value == 0) return;
|
||||
res = await fetch(`http://localhost:8080/divide?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||||
res = await res.text();
|
||||
return "div";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function typeOfCalc() {
|
||||
switch(type) {
|
||||
case "Int":
|
||||
return "int";
|
||||
break;
|
||||
|
||||
case "String":
|
||||
return "string";
|
||||
break;
|
||||
|
||||
case "FloatArray":
|
||||
return "floatArray";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (form.num2.value == 0 && calc(op) == "divide") return;
|
||||
res = await fetch(`http://localhost:8080/${calc()}?num1=${form.num1.value}&num2=${form.num2.value}&operationType=${typeOfCalc()}`)
|
||||
res = await res.text();
|
||||
|
||||
info.textContent = res;
|
||||
}
|
@ -13,41 +13,4 @@ public class SbappApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SbappApplication.class, args);
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public Integer sum(@RequestParam Integer num1,
|
||||
@RequestParam Integer num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
@GetMapping("/diff")
|
||||
public Integer diff(@RequestParam Integer num1,
|
||||
@RequestParam Integer num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
@GetMapping("/multiply")
|
||||
public Integer multiply(@RequestParam Integer num1,
|
||||
@RequestParam Integer num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
@GetMapping("/divide")
|
||||
public Integer divide(@RequestParam Integer num1,
|
||||
@RequestParam Integer num2) {
|
||||
return num1 / num2;
|
||||
}
|
||||
|
||||
@GetMapping("/divider")
|
||||
public String IsInt(@RequestParam int num1,
|
||||
@RequestParam(defaultValue = "1") int num2) {
|
||||
if (num1 % num2 == 0)
|
||||
{
|
||||
return String.format("Делится на цело, результат: %s", num1 / num2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return String.format("Неделится на цело, результат: %s", num1 / num2 + " Остаток: " + num1 % num2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package ru.ulstu.is.sbapp.calc.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ulstu.is.sbapp.calc.domain.FloatArrayCalculator;
|
||||
import ru.ulstu.is.sbapp.calc.domain.IntCalculator;
|
||||
import ru.ulstu.is.sbapp.calc.domain.StringCalculator;
|
||||
|
||||
@Configuration
|
||||
public class TypeConfiguration {
|
||||
@Bean(value = "int")
|
||||
public IntCalculator createIntType() {
|
||||
return new IntCalculator();
|
||||
}
|
||||
|
||||
@Bean(value = "string")
|
||||
public StringCalculator createStringCalculator() { return new StringCalculator(); }
|
||||
|
||||
@Bean(value = "floatArray")
|
||||
public FloatArrayCalculator createfloatArrayCalculator() { return new FloatArrayCalculator(); }
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.sbapp.calc.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.calc.service.CalculatorService;
|
||||
|
||||
@RestController
|
||||
public class CalculatorController {
|
||||
private final CalculatorService calculatorService;
|
||||
|
||||
public CalculatorController(CalculatorService calculatorService) { this.calculatorService = calculatorService; }
|
||||
|
||||
@GetMapping("/sum")
|
||||
public Object sum(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) {
|
||||
return calculatorService.getSum(num1, num2, operationType);
|
||||
}
|
||||
|
||||
@GetMapping("/diff")
|
||||
public Object diff(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) {
|
||||
return calculatorService.getDiff(num1, num2, operationType);
|
||||
}
|
||||
|
||||
@GetMapping("/mul")
|
||||
public Object mul(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) {
|
||||
return calculatorService.getMul(num1, num2, operationType);
|
||||
}
|
||||
|
||||
@GetMapping("/div")
|
||||
public Object div(@RequestParam Object num1, @RequestParam int num2, @RequestParam(value = "operationType") String operationType) {
|
||||
return calculatorService.getDiv(num1, num2, operationType);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package ru.ulstu.is.sbapp.calc.domain;
|
||||
|
||||
public interface Calculator<T> {
|
||||
public T sum(T num1, int num2);
|
||||
public T diff(T num1, int num2);
|
||||
public T multiply(T num1, int num2);
|
||||
public T divide(T num1, int num2);
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package ru.ulstu.is.sbapp.calc.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FloatArrayCalculator implements Calculator<ArrayList<Float>>{
|
||||
|
||||
@Override
|
||||
public ArrayList<Float> sum(ArrayList<Float> num1, int num2) {
|
||||
ArrayList<Float> res = new ArrayList<>();
|
||||
for (int i = 0; i < num1.size(); ++i) {
|
||||
res.add(num1.get(i) + Float.parseFloat(Integer.toString(num2)));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Float> diff(ArrayList<Float> num1, int num2) {
|
||||
ArrayList<Float> res = new ArrayList<>();
|
||||
for (int i = 0; i < num1.size(); ++i) {
|
||||
res.add(num1.get(i) - Float.parseFloat(Integer.toString(num2)));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Float> multiply(ArrayList<Float> num1, int num2) {
|
||||
ArrayList<Float> res = new ArrayList<>();
|
||||
for (int i = 0; i < num1.size(); ++i) {
|
||||
res.add(num1.get(i) * Float.parseFloat(Integer.toString(num2)));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Float> divide(ArrayList<Float> num1, int num2) {
|
||||
ArrayList<Float> res = new ArrayList<>();
|
||||
for (int i = 0; i < num1.size(); ++i) {
|
||||
res.add(num1.get(i) / Float.parseFloat(Integer.toString(num2)));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package ru.ulstu.is.sbapp.calc.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
public class IntCalculator implements Calculator<Integer>{
|
||||
|
||||
@Override
|
||||
public Integer sum(Integer num1, int num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer diff(Integer num1, int num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer multiply(Integer num1, int num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer divide(Integer num1, int num2) {
|
||||
return num1 / num2;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package ru.ulstu.is.sbapp.calc.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class StringCalculator implements Calculator<String>{
|
||||
@Override
|
||||
public String sum(String num1, int num2) {
|
||||
return num1.concat(String.format(String.valueOf(num2)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String diff(String num1, int num2) {
|
||||
String res = "";
|
||||
if (num1.length() <= num2) {
|
||||
return "Длинна первого аргумента меньше значения второго.";
|
||||
}
|
||||
for (int i = 0; i < num1.length() - num2; ++i) {
|
||||
res += num1.charAt(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String multiply(String num1, int num2) {
|
||||
String res = "";
|
||||
for (int i = 0; i < num2; ++i) {
|
||||
res += num1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String divide(String num1, int num2) {
|
||||
String res = "";
|
||||
int len = num1.length() / num2;
|
||||
if (len == 0) {
|
||||
return "Длинна первого аргумента слишком маленькая относительно значения второго.";
|
||||
}
|
||||
for (int i = 0; i < len; ++i) {
|
||||
res += num1.charAt(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package ru.ulstu.is.sbapp.calc.service;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.sbapp.calc.domain.Calculator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class CalculatorService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public CalculatorService(ApplicationContext applicationContext) { this.applicationContext = applicationContext; }
|
||||
|
||||
private Calculator _calculator;
|
||||
Object arg1 = "";
|
||||
|
||||
private void Num1Type(Object num1, int num2, String operationType) {
|
||||
_calculator = (Calculator) applicationContext.getBean(operationType);
|
||||
switch (operationType) {
|
||||
case "int" -> {
|
||||
try {
|
||||
arg1 = Integer.valueOf(num1.toString());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
arg1 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
case "floatArray" -> {
|
||||
try {
|
||||
arg1 = Arrays.stream(num1.toString().split(";"))
|
||||
.map(Float::parseFloat)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
}
|
||||
catch (Exception ex) {
|
||||
arg1 = num1.toString();
|
||||
}
|
||||
}
|
||||
|
||||
case "string" -> {
|
||||
arg1 = num1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object getSum(Object num1, int num2, String operationType) {
|
||||
Num1Type(num1, num2, operationType);
|
||||
return _calculator.sum(arg1, num2);
|
||||
}
|
||||
|
||||
public Object getDiff(Object num1, int num2, String operationType) {
|
||||
Num1Type(num1, num2, operationType);
|
||||
return _calculator.diff(arg1, num2);
|
||||
}
|
||||
|
||||
public Object getMul(Object num1, int num2, String operationType) {
|
||||
Num1Type(num1, num2, operationType);
|
||||
return _calculator.multiply(arg1, num2);
|
||||
}
|
||||
|
||||
public Object getDiv(Object num1, int num2, String operationType) {
|
||||
Num1Type(num1, num2, operationType);
|
||||
return _calculator.divide(arg1, num2);
|
||||
}
|
||||
}
|
@ -1,13 +1,91 @@
|
||||
package ru.ulstu.is.sbapp;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.sbapp.calc.service.CalculatorService;
|
||||
|
||||
@SpringBootTest
|
||||
class SbappApplicationTests {
|
||||
@Autowired
|
||||
CalculatorService calculatorService;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
void IntSum() {
|
||||
final String res = calculatorService.getSum(3, 6, "int").toString();
|
||||
Assertions.assertEquals("9", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void IntDiff() {
|
||||
final String res = calculatorService.getDiff(15, 3, "int").toString();
|
||||
Assertions.assertEquals("12", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void IntMul() {
|
||||
final String res = calculatorService.getMul(12, 3, "int").toString();
|
||||
Assertions.assertEquals("36", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void IntDiv() {
|
||||
final String res = calculatorService.getDiv(12, 3, "int").toString();
|
||||
Assertions.assertEquals("4", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void StringSum() {
|
||||
final String res = calculatorService.getSum("abc", 6, "string").toString();
|
||||
Assertions.assertEquals("abc6", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void StringDiff() {
|
||||
final String res = calculatorService.getDiff("abc", 2, "string").toString();
|
||||
Assertions.assertEquals("a", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void StringMul() {
|
||||
final String res = calculatorService.getMul("abc", 3, "string").toString();
|
||||
Assertions.assertEquals("abcabcabc", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void StringDiv() {
|
||||
final String res = calculatorService.getDiv("abc", 3, "string").toString();
|
||||
Assertions.assertEquals("a", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void FloatArraySum() {
|
||||
final String res = calculatorService.getSum("3.2;5.6", 3, "floatArray").toString();
|
||||
Assertions.assertEquals("[6.2, 8.6]", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void FloatArrayDiff() {
|
||||
final String res = calculatorService.getDiff("3.2;5.6", 2, "floatArray").toString();
|
||||
Assertions.assertEquals("[1.2, 3.6]", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void FloatArrayMul() {
|
||||
final String res = calculatorService.getMul("3.2;5.6", 2, "floatArray").toString();
|
||||
Assertions.assertEquals("[6.4, 11.2]", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void FloatArrayDiv() {
|
||||
final String res = calculatorService.getDiv("3.2;5.6", 2, "floatArray").toString();
|
||||
Assertions.assertEquals("[1.6, 2.8]", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorErrorWired() {
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calculatorService.getSum(1, 2, "date"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user