diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6efb2c5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.5 + + + com.ironhack + lab-java-springboot-fundamentals-solutions + 0.0.1-SNAPSHOT + lab-java-springboot-fundamentals-solutions + lab-java-springboot-fundamentals-solutions + + + + + + + + + + + + + + + 21 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/LabJavaSpringbootFundamentalsSolutionsApplication.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/LabJavaSpringbootFundamentalsSolutionsApplication.java new file mode 100644 index 0000000..16f5acd --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/LabJavaSpringbootFundamentalsSolutionsApplication.java @@ -0,0 +1,13 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LabJavaSpringbootFundamentalsSolutionsApplication { + + public static void main(String[] args) { + SpringApplication.run(LabJavaSpringbootFundamentalsSolutionsApplication.class, args); + } + +} diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/GreetingController.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/GreetingController.java new file mode 100644 index 0000000..32e8181 --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/GreetingController.java @@ -0,0 +1,43 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.controller; + +import com.ironhack.labjavaspringbootfundamentalssolutions.service.GreetingService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +//A GreetingService-service that contains a getGreeting-method + +@RestController +public class GreetingController { + + // Service variable name of Service + private final GreetingService greetingService; + + //Constructor for Controller for DI + public GreetingController(GreetingService greetingService) { + this.greetingService = greetingService; + } + + //Method + @GetMapping("/hello") + public String getGreeting(){ + return greetingService.getGreeting(); + } + + @GetMapping("/hello/{name}") + public String getGreetingName(@PathVariable String name){ + return greetingService.getGreetingName(name); + } + + @GetMapping("/add/{num1}/{num2}") + public String addition(@PathVariable Double num1,@PathVariable Double num2){ + Double sum = greetingService.addition(num1,num2); + return "The sum of this addition is: " + sum; + } + + @GetMapping("/multiply/{num1}/{num2}") + public String multiplication(@PathVariable Double num1,@PathVariable Double num2){ + Double multi = greetingService.multiplication(num1,num2); + return "The product of this multiplication is: " + multi; + } +} diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/TimeController.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/TimeController.java new file mode 100644 index 0000000..dd9fa6e --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/TimeController.java @@ -0,0 +1,40 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.controller; + +import com.ironhack.labjavaspringbootfundamentalssolutions.service.TimeService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping() +public class TimeController { + + private final TimeService timeService; + + public TimeController(TimeService timeService){ + this.timeService = timeService; + } + //Methods + @GetMapping("/time") + public String getCurrentTime(){ + return timeService.getCurrentTime(); + } + + @GetMapping("/date") + public String getCurrentDate(){ + return timeService.getCurrentDate(); + } + + @GetMapping("/day") + public String getCurrentDay(){ + return timeService.getCurrentDay(); + } + + @GetMapping("/all") + public String getCurrentAll(){ + return timeService.getCurrentAll(); + } + + + +} diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/WeatherController.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/WeatherController.java new file mode 100644 index 0000000..de99de2 --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/controller/WeatherController.java @@ -0,0 +1,43 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.controller; + +import com.ironhack.labjavaspringbootfundamentalssolutions.model.Weather; +import com.ironhack.labjavaspringbootfundamentalssolutions.service.WeatherService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/weather") +public class WeatherController { + + private final WeatherService weatherService; + + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + //Methods + @GetMapping("/temperature") + public String getCurrentTemperature() { + Weather weather = weatherService.getRandomWeatherAll(); + return "The current temperature is: "+ weather.getCurrentTemperature(); + } + + @GetMapping("/condition") + public String getWeatherCondition() { + Weather weather = weatherService.getRandomWeatherAll(); + return "The weather is: "+ weather.getWeatherCondition(); + } + + @GetMapping("/wind") + public String getWindSpeed() { + Weather weather = weatherService.getRandomWeatherAll(); + return "The speed of the wind is: "+ weather.getWindSpeed(); + } + + @GetMapping("/all") + public String getRandomWeatherAll() { + Weather weather = weatherService.getRandomWeatherAll(); + return weather.toString(); + } +} diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/enums/WeatherCondition.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/enums/WeatherCondition.java new file mode 100644 index 0000000..0573de5 --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/enums/WeatherCondition.java @@ -0,0 +1,5 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.enums; + +public enum WeatherCondition { + Sunny,Rainy, Cloudy, Windy; +} diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/model/Weather.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/model/Weather.java new file mode 100644 index 0000000..49de65c --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/model/Weather.java @@ -0,0 +1,54 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.model; + +import com.ironhack.labjavaspringbootfundamentalssolutions.enums.WeatherCondition; + +public class Weather { + private Double currentTemperature; + private WeatherCondition weatherCondition; + private Double windSpeed; + + //Getters + public Double getCurrentTemperature() { + return currentTemperature; + } + + public WeatherCondition getWeatherCondition() { + return weatherCondition; + } + + public Double getWindSpeed() { + return windSpeed; + } + + //Setters + public void setCurrentTemperature(Double currentTemperature) { + this.currentTemperature = currentTemperature; + } + + public void setWeatherCondition(WeatherCondition weatherCondition) { + this.weatherCondition = weatherCondition; + } + + public void setWindSpeed(Double windSpeed) { + this.windSpeed = windSpeed; + } + + @Override + public String toString(){ + return "The current temperature is: " + currentTemperature + "\n" + + "The weather is: " + weatherCondition + "\n" + + "The speed of the wind is: " + windSpeed;} + + //Constructor Empty + public Weather(){ + } + + //Constructor All + public Weather(Double currentTemperature,WeatherCondition weatherCondition,Double windSpeed){ + this.currentTemperature =currentTemperature; + this.weatherCondition=weatherCondition; + this.windSpeed =windSpeed; + } +} + + diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/GreetingService.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/GreetingService.java new file mode 100644 index 0000000..bbf5002 --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/GreetingService.java @@ -0,0 +1,24 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.service; + +import org.springframework.stereotype.Service; + +@Service +public class GreetingService { + + // Methods + public String getGreeting() { + return "Hello World !"; + } + + public String getGreetingName(String name){ + return "Hello " + name + " !"; + } + + public Double addition(Double num1,Double num2){ + return num1 + num2; + } + + public Double multiplication(Double num1,Double num2){ + return num1 * num2; + } +} \ No newline at end of file diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/TimeService.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/TimeService.java new file mode 100644 index 0000000..1bceecd --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/TimeService.java @@ -0,0 +1,37 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.service; + +import org.springframework.stereotype.Service; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalTime; + +@Service +public class TimeService { + + public String getCurrentTime(){ + LocalTime time = LocalTime.now(); + return "The current time is: " + time; + } + + public String getCurrentDate(){ + LocalDate date = LocalDate.now(); + return "The current date is: " + date; + } + + public String getCurrentDay(){ + DayOfWeek day = LocalDate.now().getDayOfWeek(); + return "The current day of the week is: " + day; + } + + public String getCurrentAll(){ + LocalTime time = LocalTime.now(); + LocalDate date = LocalDate.now(); + DayOfWeek day = LocalDate.now().getDayOfWeek(); + return "The current time is: " + time + "\n"+ + "The current date is: " + date + "\n"+ + "The current day of the week is: " + day; + } + +} + diff --git a/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/WeatherService.java b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/WeatherService.java new file mode 100644 index 0000000..13c825f --- /dev/null +++ b/src/main/java/com/ironhack/labjavaspringbootfundamentalssolutions/service/WeatherService.java @@ -0,0 +1,30 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions.service; + +import com.ironhack.labjavaspringbootfundamentalssolutions.enums.WeatherCondition; +import com.ironhack.labjavaspringbootfundamentalssolutions.model.Weather; +import org.springframework.stereotype.Service; + +@Service +public class WeatherService { + + // We declare them outside if we dont want change for every refresh!! + //____________________________________________________________________ + //WeatherCondition[] conditions = WeatherCondition.values(); + //WeatherCondition condition = conditions[(int)(Math.random() * conditions.length)]; + + + public Weather getRandomWeatherAll() { + double currentTemperature = -10 + Math.random() * 50; + double windSpeed = 0 + Math.random() * 100; + + WeatherCondition[] conditions = WeatherCondition.values(); + WeatherCondition condition = conditions[(int)(Math.random() * conditions.length)]; + + Weather weather = new Weather( + currentTemperature, + condition, + windSpeed + ); + return weather; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e367d13 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=lab-java-springboot-fundamentals-solutions diff --git a/src/test/java/com/ironhack/labjavaspringbootfundamentalssolutions/LabJavaSpringbootFundamentalsSolutionsApplicationTests.java b/src/test/java/com/ironhack/labjavaspringbootfundamentalssolutions/LabJavaSpringbootFundamentalsSolutionsApplicationTests.java new file mode 100644 index 0000000..aa80287 --- /dev/null +++ b/src/test/java/com/ironhack/labjavaspringbootfundamentalssolutions/LabJavaSpringbootFundamentalsSolutionsApplicationTests.java @@ -0,0 +1,13 @@ +package com.ironhack.labjavaspringbootfundamentalssolutions; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class LabJavaSpringbootFundamentalsSolutionsApplicationTests { + + @Test + void contextLoads() { + } + +}