Skip to content

Commit 660a0fb

Browse files
author
Sergey Lukichev
committed
weatherforecast
1 parent 54ffcb2 commit 660a0fb

11 files changed

Lines changed: 326 additions & 0 deletions

File tree

Drafts/weatherforecast/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/

Drafts/weatherforecast/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.telran</groupId>
8+
<artifactId>weatherforecast</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.12</version>
16+
<scope>test</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>com.fasterxml.jackson.core</groupId>
20+
<artifactId>jackson-databind</artifactId>
21+
<version>2.9.8</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.mockito</groupId>
25+
<artifactId>mockito-core</artifactId>
26+
<version>3.3.3</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.telran.weather;
2+
3+
import de.telran.weather.service.InputOutputService;
4+
import de.telran.weather.service.WeatherService;
5+
6+
public class WeatherForecastApp {
7+
private InputOutputService inputOutputService;
8+
private WeatherService service;
9+
10+
public WeatherForecastApp(InputOutputService inputOutputService, WeatherService service) {
11+
this.inputOutputService = inputOutputService;
12+
this.service = service;
13+
}
14+
15+
public void execute() throws Exception {
16+
String s = inputOutputService.readValue();
17+
String result = service.getWeatherByCityName(s);
18+
inputOutputService.print(result);
19+
}
20+
21+
public static void main(String[] args) {
22+
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.telran.weather.entity;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(ignoreUnknown = true)
6+
public class ConsolidatedWeather {
7+
private String theTemp;
8+
9+
public ConsolidatedWeather() {
10+
11+
}
12+
13+
public ConsolidatedWeather(String theTemp) {
14+
this.theTemp = theTemp;
15+
}
16+
17+
public String getTheTemp() {
18+
return theTemp;
19+
}
20+
21+
public void setTheTemp(String theTemp) {
22+
this.theTemp = theTemp;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "ConsolidatedWeather{" +
28+
"theTemp='" + theTemp + '\'' +
29+
'}';
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.telran.weather.entity;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import java.util.Arrays;
6+
7+
@JsonIgnoreProperties(ignoreUnknown = true)
8+
public class Forecast {
9+
private ConsolidatedWeather[] consolidatedWeather;
10+
11+
public Forecast() {
12+
13+
}
14+
15+
public Forecast(ConsolidatedWeather[] consolidatedWeather) {
16+
this.consolidatedWeather = consolidatedWeather;
17+
}
18+
19+
public ConsolidatedWeather[] getConsolidatedWeather() {
20+
return consolidatedWeather;
21+
}
22+
23+
public void setConsolidatedWeather(ConsolidatedWeather[] consolidatedWeather) {
24+
this.consolidatedWeather = consolidatedWeather;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Forecast{" +
30+
"consolidatedWeather=" + Arrays.toString(consolidatedWeather) +
31+
'}';
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package de.telran.weather.entity;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(ignoreUnknown = true)
6+
public class SearchResult {
7+
8+
private String woeid;
9+
10+
public SearchResult() {
11+
12+
}
13+
14+
public SearchResult(String woeid) {
15+
this.woeid = woeid;
16+
}
17+
18+
public String getWoeid() {
19+
return woeid;
20+
}
21+
22+
public void setWoeid(String woeid) {
23+
this.woeid = woeid;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "SearchResult{" +
29+
"woeid='" + woeid + '\'' +
30+
'}';
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.telran.weather.service;
2+
3+
import java.util.Scanner;
4+
5+
public class InputOutputService {
6+
7+
private Scanner scanner;
8+
9+
public String readValue() {
10+
return null;
11+
}
12+
13+
public void print(String value) {
14+
System.out.println(value);
15+
}
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package de.telran.weather.service;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import de.telran.weather.entity.Forecast;
5+
import de.telran.weather.entity.SearchResult;
6+
7+
import java.net.URL;
8+
9+
public class WeatherGateway {
10+
11+
private final String SEARCH_URL = "https://www.metaweather.com/api/location/search/?query=";
12+
private final String FORECAST_URL = "https://www.metaweather.com/api/location/";
13+
14+
private ObjectMapper mapper;
15+
16+
public WeatherGateway(ObjectMapper mapper) {
17+
this.mapper = mapper;
18+
}
19+
20+
public SearchResult[] findCityByName(String cityName) throws Exception {
21+
22+
URL searchUrl = new URL(SEARCH_URL + cityName);
23+
24+
SearchResult[] searchResults = mapper.readValue(searchUrl, SearchResult[].class);
25+
26+
return searchResults;
27+
}
28+
29+
public Forecast getWeatherByWoeid(String woeid) throws Exception {
30+
URL forecastUrl = new URL(FORECAST_URL + woeid);
31+
32+
Forecast forecast = mapper.readValue(forecastUrl, Forecast.class);
33+
34+
return forecast;
35+
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.telran.weather.service;
2+
3+
import de.telran.weather.entity.Forecast;
4+
import de.telran.weather.entity.SearchResult;
5+
6+
public class WeatherService {
7+
8+
private WeatherGateway gateway;
9+
10+
public WeatherService(WeatherGateway gateway) {
11+
this.gateway = gateway;
12+
}
13+
14+
public String getWeatherByCityName(String cityName) throws Exception {
15+
SearchResult[] cityByName = gateway.findCityByName(cityName);
16+
if(cityByName.length == 0) {
17+
throw new RuntimeException("No results for "+cityName);
18+
}
19+
Forecast forecast = gateway.getWeatherByWoeid(cityByName[0].getWoeid());
20+
return forecast.getConsolidatedWeather()[0].getTheTemp();
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package de.telran.weather.service;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
5+
import de.telran.weather.entity.Forecast;
6+
import de.telran.weather.entity.SearchResult;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertTrue;
10+
11+
public class WeatherGatewayTest {
12+
13+
@Test
14+
public void testFindCityByName() throws Exception {
15+
WeatherGateway gateway = new WeatherGateway(new ObjectMapper());
16+
17+
SearchResult[] berlins = gateway.findCityByName("Berlin");
18+
19+
assertTrue(berlins.length == 1);
20+
}
21+
22+
@Test
23+
public void testGetWeatherByWoeid() throws Exception {
24+
ObjectMapper mapper = new ObjectMapper();
25+
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
26+
WeatherGateway gateway = new WeatherGateway(mapper);
27+
Forecast weatherByWoeid = gateway.getWeatherByWoeid("638242");
28+
System.out.println(weatherByWoeid);
29+
}
30+
}

0 commit comments

Comments
 (0)