From 3549c765e32e62ff6ed91580e18e887c504b9843 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Fri, 14 Apr 2023 19:33:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=201?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1.=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lab/springbapp/SpringbappApplication.java | 92 ++++++++++++++++++- .../com/lab/springbapp/WebConfiguration.java | 12 +++ 2 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/lab/springbapp/WebConfiguration.java diff --git a/src/main/java/com/lab/springbapp/SpringbappApplication.java b/src/main/java/com/lab/springbapp/SpringbappApplication.java index 934fff3..f88b857 100644 --- a/src/main/java/com/lab/springbapp/SpringbappApplication.java +++ b/src/main/java/com/lab/springbapp/SpringbappApplication.java @@ -2,12 +2,98 @@ 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); + } - 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(); + } } diff --git a/src/main/java/com/lab/springbapp/WebConfiguration.java b/src/main/java/com/lab/springbapp/WebConfiguration.java new file mode 100644 index 0000000..d5b3e22 --- /dev/null +++ b/src/main/java/com/lab/springbapp/WebConfiguration.java @@ -0,0 +1,12 @@ +package com.lab.springbapp; +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("*"); + } +}