Skip to content
Snippets Groups Projects
Commit 77a56416 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Added option for custom queries to Demonstration.java

parent dad36e4a
Branches
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@ public class Demonstration {
return dataSets.get(choice - 1);
}
private static String getData(OpenWeatherConnector weather, String dataSet, Instant now) {
private static String getData(OpenWeatherConnector weather, String dataSet, Instant now, Scanner scanner) {
String query;
switch (dataSet) {
case "weather":
......@@ -51,8 +51,22 @@ public class Demonstration {
query = "";
}
System.out.println("Requesting data at " + now);
if (!query.equals("")) {
System.out.print("Enter query, or press the ENTER key to accept the example query (" + query + "): ");
String userQuery = scanner.nextLine();
if(!userQuery.equals("")) {
System.out.println(" " + "*".repeat(76));
System.out.println(" *** The following example output strings assume default units. ***");
System.out.println(" *** If you specified other units, the values will be correct, but ***");
System.out.println(" *** the stated units will be the defaults, not your specified units. ***");
System.out.println(" " + "*".repeat(76));
query = userQuery;
}
}
String data = null;
if (!query.equals("")) {
System.out.println("Requesting data at " + now);
try {
data = weather.retrieveData(query);
} catch (IOException ioException) {
......@@ -63,6 +77,9 @@ public class Demonstration {
System.err.println("\t" + ioException.getCause().getMessage());
}
}
} else {
System.err.println("Not requesting data at " + now + " due to empty query.");
}
return data;
}
......@@ -133,11 +150,16 @@ public class Demonstration {
System.out.println("The UV Index is " + weather.getUltravioletIndex());
System.out.println("Precipitation in the last hour has been " + weather.getOneHourRainfall() + "mm of" +
" rain and " + weather.getOneHourSnowfall() + "mm of snow.");
if (weather.getTimestamps("minutely").size() > 0) {
System.out.println("There is minutely data from " + weather.getTimestamps("minutely").get(0) + " to " +
weather.getTimestamps("minutely").get(weather.getTimestamps("minutely").size() - 1));
timestamp = weather.getTimestamps("minutely").get(weather.getTimestamps("minutely").size() / 2);
System.out.println("\tAt " + timestamp + " the 1-hour (?) precipitation total will be " +
weather.getMinutelyPrecipitation(timestamp));
} else {
System.out.println("There is no minutely data.");
}
if (weather.getTimestamps("hourly").size() > 0) {
System.out.println("There is hourly data from " + weather.getTimestamps("hourly").get(0) + " to " +
weather.getTimestamps("hourly").get(weather.getTimestamps("hourly").size() - 1));
timestamp = weather.getTimestamps("hourly").get(weather.getTimestamps("hourly").size() / 2);
......@@ -158,6 +180,10 @@ public class Demonstration {
"chance of precipitation, resulting in " + weather.getOneHourRainfall(timestamp) + "mm of " +
"rain and " + weather.getOneHourSnowfall(timestamp) + "mm of snow in the hour before " +
timestamp);
} else {
System.out.println("There is no hourly data.");
}
if (weather.getTimestamps("daily").size() > 0) {
System.out.println("There is daily data from " + weather.getTimestamps("daily").get(0) + " to " +
weather.getTimestamps("daily").get(weather.getTimestamps("daily").size() - 1));
timestamp = weather.getTimestamps("daily").get(weather.getTimestamps("daily").size() / 2);
......@@ -184,6 +210,9 @@ public class Demonstration {
System.out.println("\tThere will be a " + weather.getProbabilityOfPrecipitation(timestamp) * 100 + "% " +
"chance of precipitation, resulting in " + weather.getDailyRainfall(timestamp) + "mm of " +
"rain and " + weather.getDailySnowfall(timestamp) + "mm of snow");
} else {
System.out.println("There is no daily data.");
}
}
private static void reportForecastData(OpenWeatherConnector weather) {
......@@ -238,9 +267,10 @@ public class Demonstration {
String dataSet = getDataSet(scanner);
OpenWeatherConnector weather = new OpenWeatherConnector(dataSet, apiKey);
String data = getData(weather, dataSet, now);
String data = getData(weather, dataSet, now, scanner);
System.out.println("Raw JSON: " + data);
if (data != null) {
System.out.println("Location: " + weather.getLatitude() + " latitude, " + weather.getLongitude() + " " +
"longitude");
Date currentTimeStamp = weather.getTimestamp();
......@@ -270,6 +300,7 @@ public class Demonstration {
}
optionallySaveDataToFile(data, dataSet, scanner);
}
scanner.close();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment