Готовая 2 лаб. работа.

This commit is contained in:
Anastasia 2023-05-16 09:00:53 +04:00
parent 3549c765e3
commit 1a08548c91
3 changed files with 55 additions and 90 deletions

View File

@ -15,6 +15,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'junit:junit:4.13.1'
testImplementation 'junit:junit:4.13.1'
testImplementation 'org.testng:testng:7.1.0'
}
tasks.named('test') {

View File

@ -2,98 +2,13 @@ package com.lab.springbapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import static java.lang.Math.*;
@SpringBootApplication
@RestController
public class SpringbappApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbappApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/calculateSquare")
public String calculateSquare(@RequestParam(defaultValue = "1") int num,
@RequestParam(defaultValue = "1") int step) {
double res;
res = Math.pow(num, step);
return Double.toString(res);
}
@GetMapping("/calculateFactorial")
public String calculateFactorial(@RequestParam(required = true) 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(required = true) 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(required = true) int num) {
int result = 0;
for (int i = 2; i <= num; i++) {
if (checkSimple(i))
return ("число простое");
}
return ("число непростое");
}
@GetMapping("/addDays")
public String addDays(@RequestParam(required = true) int num) {
Date currentDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.add(Calendar.DATE, num);
Date newDate = cal.getTime();
return newDate.toString();
}
}

View File

@ -1,13 +1,60 @@
package com.lab.springbapp;
import com.lab.springbapp.service.TypeService;
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;
@SpringBootTest
class SpringbappApplicationTests {
@Autowired
TypeService Service;
@Test
void contextLoads() {
void testIntPlus() {
final String res = (String) Service.Sum( "100", "10", "int");
Assertions.assertEquals("110", res);
}
}
@Test
void testIntMinus() {
final String res = (String)Service.Min( "100", "10", "int");
Assertions.assertEquals("90", res);
}
@Test
void testIntMultiply() {
final String res = (String)Service.Mul( "100", "10", "int");
Assertions.assertEquals("1000", res);
}
@Test
void testIntDivision() {
final String res = (String)Service.Del( "100", "10", "int");
Assertions.assertEquals("10", res);
}
@Test
void testStringPlus() {
final String res = (String)Service.Sum( "abc", "dfe", "str");
Assertions.assertEquals("abcdfe", res);
}
@Test
void testStringMin() {
final String res = (String)Service.Min( "abcd", "ef", "str");
Assertions.assertEquals("cd", res);
}
@Test
void testStringMul() {
final String res = (String)Service.Mul( "adc", "de", "str");
Assertions.assertEquals("adcdeadcde", res);
}
@Test
void testStringDiv() {
final String res = (String)Service.Del( "adcdef", "de", "str");
Assertions.assertEquals("adcf", res);
}
}