Compare commits

...

1 Commits

Author SHA1 Message Date
ksenianeva
2712dcbfb0 2nd labwork 2023-03-27 21:58:26 +04:00
13 changed files with 444 additions and 80 deletions

View File

@ -14,6 +14,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.testng:testng:7.1.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -0,0 +1,30 @@
package com.example.lab.AllTypeCalc.configuration;
import com.example.lab.AllTypeCalc.domain.CalculationInt;
import com.example.lab.AllTypeCalc.domain.CalculationStr;
import com.example.lab.AllTypeCalc.domain.CalculationArr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
public class CalcConfig {
private final Logger log = LoggerFactory.getLogger(com.example.lab.AllTypeCalc.configuration.CalcConfig.class);
@Bean(value = "int")
public CalculationInt createCalculationInt(){
log.info("Call createCalculationInt()");
return new CalculationInt();
}
@Bean(value = "str")
public CalculationStr createCalculationStr() {
log.info("Call createCalculationStr()");
return new CalculationStr();
}
@Bean (value = "arr")
public CalculationArr createCalculationArr() {
log.info("Call createCalculationArr()");
return new CalculationArr();
}
}

View File

@ -0,0 +1,43 @@
package com.example.lab.AllTypeCalc.controller;
import com.example.lab.AllTypeCalc.service.CalcService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalcController {
private final CalcService calcService;
public CalcController(CalcService calcService) {
this.calcService = calcService;
}
@GetMapping("/GetSum")
public Object getSum(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
return calcService.GetSum(obj1, obj2, typeInp);
}
@GetMapping("/GetMinus")
public Object getMinus(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
return calcService.GetMinus(obj1, obj2, typeInp);
}
@GetMapping("/GetMult")
public Object getMult(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
return calcService.GetMult(obj1, obj2, typeInp);
}
@GetMapping("/GetDiv")
public Object getDiv(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
return calcService.GetDiv(obj1, obj2, typeInp);
}
}

View File

@ -0,0 +1,97 @@
package com.example.lab.AllTypeCalc.domain;
import com.example.lab.AllTypeCalc.domain.ICalculation;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component(value = "arr")
public class CalculationArr implements ICalculation<String[]> {
@Override
public String[] GetSum(String[] firstInp, String[] secondInp) {
String[] resArr;
String[] longArr;
String[] shortArr;
if (secondInp.length > firstInp.length) {
longArr = secondInp.clone();
shortArr = firstInp.clone();
} else {
longArr = firstInp.clone();
shortArr = secondInp.clone();
}
resArr = new String[longArr.length];
for (int i = 0; i < shortArr.length; i++) {
resArr[i] = Integer.toString(Integer.parseInt(longArr[i]) + Integer.parseInt(shortArr[i]));
}
for (int i = shortArr.length; i < longArr.length; i++) {
resArr[i] = longArr[i];
}
return resArr;
}
@Override
public String[] GetMinus(String[] firstInp, String[] secondInp) {
String[] resArr;
String[] longArr;
String[] shortArr;
if (secondInp.length > firstInp.length) {
longArr = secondInp.clone();
shortArr = firstInp.clone();
} else {
longArr = firstInp.clone();
shortArr = secondInp.clone();
}
resArr = new String[longArr.length];
for (int i = 0; i < shortArr.length; i++) {
resArr[i] = Integer.toString(Integer.parseInt(longArr[i]) - Integer.parseInt(shortArr[i]));
}
for (int i = shortArr.length; i < longArr.length; i++) {
resArr[i] = longArr[i];
}
return resArr;
}
@Override
public String[] GetMult(String firstInp[], String secondInp[]) {
String[] resArr;
String[] longArr;
String[] shortArr;
if (secondInp.length > firstInp.length) {
longArr = secondInp.clone();
shortArr = firstInp.clone();
} else {
longArr = firstInp.clone();
shortArr = secondInp.clone();
}
resArr = new String[longArr.length];
for (int i = 0; i < shortArr.length; i++) {
resArr[i] = Integer.toString(Integer.parseInt(longArr[i]) * Integer.parseInt(shortArr[i]));
}
for (int i = shortArr.length; i < longArr.length; i++) {
resArr[i] = longArr[i];
}
return resArr;
}
@Override
public String[] GetDiv(String[] firstInp, String[] secondInp) {
String[] resArr;
String[] longArr;
String[] shortArr;
if (secondInp.length > firstInp.length) {
longArr = secondInp.clone();
shortArr = firstInp.clone();
} else {
longArr = firstInp.clone();
shortArr = secondInp.clone();
}
resArr = new String[longArr.length];
for (int i = 0; i < shortArr.length; i++) {
resArr[i] =Integer.toString(Integer.parseInt(longArr[i]) / Integer.parseInt(shortArr[i]));
}
for (int i = shortArr.length; i < longArr.length; i++) {
resArr[i] = longArr[i];
}
return resArr;
}
}

View File

@ -0,0 +1,33 @@
package com.example.lab.AllTypeCalc.domain;
import com.example.lab.AllTypeCalc.domain.ICalculation;
import org.springframework.stereotype.Component;
@Component(value = "int")
public class CalculationInt implements ICalculation<Integer> {
@Override
public Integer GetSum(Integer firstInp, Integer secondInp) {
return firstInp+secondInp;
}
@Override
public Integer GetMinus(Integer firstInp, Integer secondInp) {
return firstInp - secondInp;
}
@Override
public Integer GetMult(Integer firstInp, Integer secondInp) {
return firstInp*secondInp;
}
@Override
public Integer GetDiv(Integer firstInp, Integer secondInp) {
if (secondInp == 0){
return 0;
}
else{
return (firstInp/secondInp);
}
}
}

View File

@ -0,0 +1,54 @@
package com.example.lab.AllTypeCalc.domain;
import com.example.lab.AllTypeCalc.domain.ICalculation;
import org.springframework.stereotype.Component;
@Component(value = "str")
public class CalculationStr implements ICalculation<String> {
@Override
public String GetSum(String firstInp, String secondInp) {
return (firstInp+secondInp);
}
@Override
public String GetMinus(String firstInp, String secondInp) {
StringBuilder bufStr = new StringBuilder();
if (firstInp.equals(secondInp)){
return "0";
}
else {
for (int i = 0; i < firstInp.length(); i++) {
boolean check = true;
for (int k = 0; k < secondInp.length(); k++) {
if (firstInp.charAt(i) == secondInp.charAt(k)) {
check = false;
break;
}
}
if (check){
bufStr.append(firstInp.charAt(i));}
}
}
return bufStr.toString();
}
@Override
public String GetMult(String firstInp, String secondInp) {
int multInt = secondInp.length();
StringBuilder multResult = new StringBuilder();
for (int i = 0; i < multInt; i++){
multResult.append(firstInp);
}
return multResult.toString();
}
@Override
public String GetDiv(String firstInp, String secondInp) {
if (secondInp.length() !=0) {
double divRes = 1.0 * firstInp.length() / secondInp.length();
return Double.toString(divRes);
}
else return "error";
}
}

View File

@ -0,0 +1,8 @@
package com.example.lab.AllTypeCalc.domain;
public interface ICalculation <V> {
V GetSum(V firstInp, V secondInp);
V GetMinus (V firstInp, V secondInp);
V GetMult (V firstInp, V secondInp);
V GetDiv(V firstInp, V secondInp);
}

View File

@ -0,0 +1,79 @@
package com.example.lab.AllTypeCalc.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import com.example.lab.AllTypeCalc.domain.CalculationInt;
import com.example.lab.AllTypeCalc.domain.ICalculation;
import java.util.Arrays;
@Service
public class CalcService {
private final ApplicationContext applicationContext;
private final Logger log = LoggerFactory.getLogger(CalculationInt.class);
public CalcService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object GetSum(Object firstInp, Object secondInp, String type) {
final ICalculation calculator = (ICalculation) applicationContext.getBean(type);
if(type.equals("str")){
return calculator.GetSum(firstInp.toString(),secondInp.toString());
}
if(type.equals("int")) {
return calculator.GetSum(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString()) );
}
if(type.equals("arr")){
return calculator.GetSum(cleanToArr(firstInp) , cleanToArr(secondInp));
}
return "error";
}
public Object GetMinus(Object firstInp, Object secondInp, String type) {
final ICalculation calculator = (ICalculation) applicationContext.getBean(type);
if(type.equals("str")){
return calculator.GetMinus(firstInp.toString(),secondInp.toString());
}
if (type.equals("int")){
return calculator.GetMinus(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString()));
}
if(type.equals("arr")){
return calculator.GetMinus(cleanToArr(firstInp) , cleanToArr(secondInp));
}
return "error";
}
public Object GetMult(Object firstInp, Object secondInp, String type) {
final ICalculation calculator = (ICalculation) applicationContext.getBean(type);
if(type.equals("str")){
return calculator.GetMult(firstInp.toString(), secondInp.toString());
}
if (type.equals("int")){
return calculator.GetMult(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString()));
}
if (type.equals("arr")){
return calculator.GetMult(cleanToArr(firstInp) , cleanToArr(secondInp));
}
return "error";
}
public Object GetDiv(Object firstInp, Object secondInp, String type) {
final ICalculation calculator = (ICalculation) applicationContext.getBean(type);
if (type.equals("str")){
return calculator.GetDiv(firstInp.toString(), secondInp.toString());
}
if (type.equals("int")){
return calculator.GetDiv(Integer.parseInt(firstInp.toString()), Integer.parseInt(secondInp.toString()));
}
if(type.equals("arr")){
return calculator.GetDiv(cleanToArr(firstInp) , cleanToArr(secondInp));
}
return "error";
}
public String[] cleanToArr(Object arr){
return Arrays.stream(arr.toString().split("[\\(\\)\\,\\.\\[\\] \\!\\?\\:\\;]")).filter(e -> e.trim().length()>0).toArray(String[]::new);
}
}

View File

@ -1,77 +1,16 @@
package com.example.lab;
import com.example.lab.AllTypeCalc.controller.CalcController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
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 LabApplication {
public static void main(String[] args) {
SpringApplication.run(LabApplication.class, args);
}
@GetMapping("/calculateSquare")
public String calculateSquare(@RequestParam int num,
@RequestParam int step) {
double res;
res = Math.pow(num,step);
return ("Результат: " + Double.toString(res));
}
@GetMapping("/calculateFactorial")
public String calculateFactorial(@RequestParam int num) {
long res = 1;
for (int i = 1; i <= num; i++) {
res = res * i;
}
return ("Результат: " + Long.toString(res));
}
public int getFibonacciValue(int n) {
if (n <= 1) {
return 0;
} else if (n == 2) {
return 1;
} else {
return getFibonacciValue(n - 1) + getFibonacciValue(n - 2);
}
}
@GetMapping("/calculateFibbN")
public String calculateFibbN(@RequestParam int num) {
int result = 0;
result = getFibonacciValue(num);
return ("Результат: " + Integer.toString(result));
}
public static boolean checkSimple(int i){
if (i<=1)
return false;
else if (i <=3)
return true;
else if (i%2==0 || i %3 ==0)
return false;
int n = 5;
while (n*n <=i){
if (i % n ==0 || i % (n+2) == 0)
return false;
n=n+6;
}
return true;
}
@GetMapping("/calculateLastSimple")
public String calculateSimple(@RequestParam int num){
int result = 0;
for (int i=2;i<=num;i++){
if(checkSimple(i))
result = i;
}
return ("Результат: " + Integer.toString(result));
}
}

View File

@ -0,0 +1,13 @@
package com.example.lab;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}

View File

@ -1,13 +1,67 @@
package com.example.lab;
import com.example.lab.AllTypeCalc.service.CalcService;
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;
@SpringBootTest
class LabApplicationTests {
@Autowired
CalcService calcService;
@Test
void contextLoads() {
void testIntSum() {
final Object result = calcService.GetSum(1, 2, "int");
Assertions.assertEquals("3", result.toString());
}
@Test
void testStringSum() {
final Object result = calcService.GetSum("ab", "bc", "str");
Assertions.assertEquals("abbc", result.toString());
}
@Test
void testIntMinus() {
final Object result = calcService.GetMinus(5, 4, "int");
Assertions.assertEquals("1", result.toString());
}
@Test
void testStringMinus() {
final Object result = calcService.GetMinus("abc", "bc", "str");
Assertions.assertEquals("a", result.toString());
}
@Test
void testIntMult() {
final Object result = calcService.GetMult(4, 4, "int");
Assertions.assertEquals("16", result.toString());
}
@Test
void testStringMult() {
final Object result = calcService.GetMult("ab", "bc", "str");
Assertions.assertEquals("abab", result.toString());
}
@Test
void testIntDiv() {
final Object result = calcService.GetDiv(12, 6, "int");
Assertions.assertEquals("2", result.toString());
}
@Test
void testStringDiv() {
final Object result = calcService.GetDiv("ab", "bc", "str");
Assertions.assertEquals("1.0", result.toString());
}
@Test
void testCalculatorErrorWired() {
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calcService.GetSum("1", "1", "integer"));
}
}

View File

@ -11,16 +11,21 @@
<body class="ms-3">
<h3 class="mt-2">Вычисления</h3>
<div class="d-flex form-group">
<label class="m-2" for="textFieldNum">Число 1:</label>
<input type="number" class="form-control w-25" id="textFieldNum" placeholder="Введите число...">
<label class="m-2" for="textFieldNum2">Число 2:</label>
<input type="number" class="form-control w-25" id="textFieldNum2" placeholder="Введите число...">
<label class="m-2" for="obj1Input">Поле 1:</label>
<input class="w-25" id="obj1Input" placeholder="Введите...">
<label class="m-2" for="obj2Input">Поле 2:</label>
<input class="w-25" id="obj2Input" placeholder="Введите...">
<select class="custom-select w-auto ms-3" id="typeSelect">
<option value="int">Числа</option>
<option value="str">Строки</option>
<option value="arr">Массив</option>
</select>
</div>
<div>
<button id = "1" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">Степень</button>
<button id = "2" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">Факториал</button>
<button id = "3" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">Фибоначчи</button>
<button id = "4" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">Простое число</button>
<button id = "1" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">+</button>
<button id = "2" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">-</button>
<button id = "3" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">*</button>
<button id = "4" class="btn btn-primary w-auto mt-2 ms-3" onclick="Okclick(this.id)">%</button>
</div>
<a id="res"></a>
</body>

View File

@ -1,26 +1,34 @@
function Okclick(clicked) {
var num = document.getElementById("textFieldNum").value
var num2 = document.getElementById("textFieldNum2").value
var obj1 = document.getElementById("obj1Input").value
var obj2 = document.getElementById("obj2Input").value
var typeInp = document.getElementById("typeSelect").value
var id = clicked;
var argument = "";
console.log(typeInp);
if (document.getElementById("typeSelect").value == "arr"){
console.log("aaa");
obj1= encodeURIComponent(obj1);
obj2 = encodeURIComponent(obj2);
console.log(obj1);
console.log(obj2);
}
if(id == "1"){
argument = ("calculateSquare?num=" + num +"&step=" + num2);
argument = ("GetSum?obj1=" + obj1 +"&obj2=" + obj2 + "&typeInp=" + typeInp);
}
else if (id == "2"){
argument = ("calculateFactorial?num=" + num);
argument = ("GetMinus?obj1=" + obj1 + "&obj2=" + obj2 + "&typeInp=" + typeInp);
}
else if( id == "3"){
argument = ("calculateFibbN?num=" + num);
argument = ("GetMult?obj1=" + obj1 + "&obj2=" + obj2 + "&typeInp=" + typeInp);
}
else if (id == "4"){
argument = ("calculateLastSimple?num=" + num);
argument = ("GetDiv?obj1=" + obj1 + "&obj2=" + obj2 + "&typeInp=" + typeInp);
}
fetch ("http://127.0.0.1:8080/" + argument)
.then(response => response.text())
.then((response) => {
console.log(response)
document.getElementById("res").innerHTML = response
document.getElementById("res").innerHTML = "Результат = " + response
})
}