diff --git a/src/main/java/edu/unl/cse/soft160/json_connections/Demonstration.java b/src/main/java/edu/unl/cse/soft160/json_connections/Demonstration.java index 3e67d484dc9a173b9a0e8b48f90ce0d8eef11f82..e2a7baea9c7f20c0039ca5915ce6b150cd12ead2 100644 --- a/src/main/java/edu/unl/cse/soft160/json_connections/Demonstration.java +++ b/src/main/java/edu/unl/cse/soft160/json_connections/Demonstration.java @@ -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,17 +51,34 @@ 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; - try { - data = weather.retrieveData(query); - } catch (IOException ioException) { - System.err.println("IO Exception: " + ioException.getClass()); - System.err.println("\t" + ioException.getMessage()); - System.err.println("Caused by: " + ioException.getCause()); - if (ioException.getCause() != null) { - System.err.println("\t" + ioException.getCause().getMessage()); + if (!query.equals("")) { + System.out.println("Requesting data at " + now); + try { + data = weather.retrieveData(query); + } catch (IOException ioException) { + System.err.println("IO Exception: " + ioException.getClass()); + System.err.println("\t" + ioException.getMessage()); + System.err.println("Caused by: " + ioException.getCause()); + if (ioException.getCause() != null) { + System.err.println("\t" + ioException.getCause().getMessage()); + } } + } else { + System.err.println("Not requesting data at " + now + " due to empty query."); } return data; } @@ -133,57 +150,69 @@ 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."); - 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)); - 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); - System.out.println("\tAt " + timestamp + " it will be " + weather.getWeatherCategories(timestamp)); - System.out.println("\tSpecifically, it will be " + weather.getWeatherDescriptions(timestamp)); - System.out.println("\tThe temperature will be " + weather.getTemperature(timestamp) + "K"); - System.out.println("\tFactoring in the relative humidity of " + weather.getHumidity(timestamp) + "%, the dew " + - "point will be " + weather.getDewPoint(timestamp) + "K, and it will feel like " + - weather.getFeelsLike(timestamp) + "K"); - System.out.println("\tThat probably also factored in winds, which will be " + - weather.getWindSpeed(timestamp) + "m/s from " + weather.getWindDirection(timestamp) + - "˚, gusting to " + weather.getWindGust(timestamp) + "m/s"); - System.out.println("\tVisibility will be " + weather.getVisibility(timestamp) + "m"); - System.out.println("\tThe pressure will be " + weather.getPressure(timestamp) + "hPa"); - System.out.println("\tCloud cover will be " + weather.getCloudCover(timestamp) + "%"); - System.out.println("\tThe UV Index will be " + weather.getUltravioletIndex(timestamp)); - System.out.println("\tThere will be a " + weather.getProbabilityOfPrecipitation(timestamp) * 100 + "% " + - "chance of precipitation, resulting in " + weather.getOneHourRainfall(timestamp) + "mm of " + - "rain and " + weather.getOneHourSnowfall(timestamp) + "mm of snow in the hour before " + - timestamp); - 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); - System.out.println("\tAt " + timestamp + " it will be " + weather.getWeatherCategories(timestamp)); - System.out.println("\tSpecifically, it will be " + weather.getWeatherDescriptions(timestamp)); - System.out.println("\tThe temperatures will range from " + weather.getLowTemperature(timestamp) + "K to " + - weather.getHighTemperature(timestamp) + "K"); - System.out.println("\t\tMorning:" + weather.getMorningTemperature(timestamp) + "K"); - System.out.println("\t\tDaytime:" + weather.getDaytimeTemperature(timestamp) + "K"); - System.out.println("\t\tEvening:" + weather.getEveningTemperature(timestamp) + "K"); - System.out.println("\t\tNighttime:" + weather.getNighttimeTemperature(timestamp) + "K"); - System.out.println("\tFactoring in the relative humidity of " + weather.getHumidity(timestamp) + "%, the dew " + - "point will be " + weather.getDewPoint(timestamp) + "K, and it will feel like:"); - System.out.println("\t\tMorning:" + weather.getMorningFeelsLike(timestamp) + "K"); - System.out.println("\t\tDaytime:" + weather.getDaytimeFeelsLike(timestamp) + "K"); - System.out.println("\t\tEvening:" + weather.getEveningFeelsLike(timestamp) + "K"); - System.out.println("\t\tNighttime:" + weather.getNighttimeFeelsLike(timestamp) + "K"); - System.out.println("\tThat probably also factored in winds, which will be " + - weather.getWindSpeed(timestamp) + "m/s from " + weather.getWindDirection(timestamp) + - "˚, gusting to " + weather.getWindGust(timestamp) + "m/s"); - System.out.println("\tThe pressure will be " + weather.getPressure(timestamp) + "hPa"); - System.out.println("\tCloud cover will be " + weather.getCloudCover(timestamp) + "%"); - System.out.println("\tThe UV Index will be " + weather.getUltravioletIndex(timestamp)); - 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"); + 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); + System.out.println("\tAt " + timestamp + " it will be " + weather.getWeatherCategories(timestamp)); + System.out.println("\tSpecifically, it will be " + weather.getWeatherDescriptions(timestamp)); + System.out.println("\tThe temperature will be " + weather.getTemperature(timestamp) + "K"); + System.out.println("\tFactoring in the relative humidity of " + weather.getHumidity(timestamp) + "%, the dew " + + "point will be " + weather.getDewPoint(timestamp) + "K, and it will feel like " + + weather.getFeelsLike(timestamp) + "K"); + System.out.println("\tThat probably also factored in winds, which will be " + + weather.getWindSpeed(timestamp) + "m/s from " + weather.getWindDirection(timestamp) + + "˚, gusting to " + weather.getWindGust(timestamp) + "m/s"); + System.out.println("\tVisibility will be " + weather.getVisibility(timestamp) + "m"); + System.out.println("\tThe pressure will be " + weather.getPressure(timestamp) + "hPa"); + System.out.println("\tCloud cover will be " + weather.getCloudCover(timestamp) + "%"); + System.out.println("\tThe UV Index will be " + weather.getUltravioletIndex(timestamp)); + System.out.println("\tThere will be a " + weather.getProbabilityOfPrecipitation(timestamp) * 100 + "% " + + "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); + System.out.println("\tAt " + timestamp + " it will be " + weather.getWeatherCategories(timestamp)); + System.out.println("\tSpecifically, it will be " + weather.getWeatherDescriptions(timestamp)); + System.out.println("\tThe temperatures will range from " + weather.getLowTemperature(timestamp) + "K to " + + weather.getHighTemperature(timestamp) + "K"); + System.out.println("\t\tMorning:" + weather.getMorningTemperature(timestamp) + "K"); + System.out.println("\t\tDaytime:" + weather.getDaytimeTemperature(timestamp) + "K"); + System.out.println("\t\tEvening:" + weather.getEveningTemperature(timestamp) + "K"); + System.out.println("\t\tNighttime:" + weather.getNighttimeTemperature(timestamp) + "K"); + System.out.println("\tFactoring in the relative humidity of " + weather.getHumidity(timestamp) + "%, the dew " + + "point will be " + weather.getDewPoint(timestamp) + "K, and it will feel like:"); + System.out.println("\t\tMorning:" + weather.getMorningFeelsLike(timestamp) + "K"); + System.out.println("\t\tDaytime:" + weather.getDaytimeFeelsLike(timestamp) + "K"); + System.out.println("\t\tEvening:" + weather.getEveningFeelsLike(timestamp) + "K"); + System.out.println("\t\tNighttime:" + weather.getNighttimeFeelsLike(timestamp) + "K"); + System.out.println("\tThat probably also factored in winds, which will be " + + weather.getWindSpeed(timestamp) + "m/s from " + weather.getWindDirection(timestamp) + + "˚, gusting to " + weather.getWindGust(timestamp) + "m/s"); + System.out.println("\tThe pressure will be " + weather.getPressure(timestamp) + "hPa"); + System.out.println("\tCloud cover will be " + weather.getCloudCover(timestamp) + "%"); + System.out.println("\tThe UV Index will be " + weather.getUltravioletIndex(timestamp)); + 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,38 +267,40 @@ 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); - System.out.println("Location: " + weather.getLatitude() + " latitude, " + weather.getLongitude() + " " + - "longitude"); - Date currentTimeStamp = weather.getTimestamp(); - System.out.println("Current timestamp: " + - (currentTimeStamp == OpenWeatherConnector.IMPOSSIBLE_DATE ? "not present" : currentTimeStamp)); - System.out.println("Available timestamps: " + weather.getTimestamps()); + if (data != null) { + System.out.println("Location: " + weather.getLatitude() + " latitude, " + weather.getLongitude() + " " + + "longitude"); + Date currentTimeStamp = weather.getTimestamp(); + System.out.println("Current timestamp: " + + (currentTimeStamp == OpenWeatherConnector.IMPOSSIBLE_DATE ? "not present" : currentTimeStamp)); + System.out.println("Available timestamps: " + weather.getTimestamps()); - switch (dataSet) { - case "weather": - reportWeatherData(weather); - break; - case "forecast": - reportForecastData(weather); - break; - case "onecall": - reportOneCallData(weather); - break; - case "air_pollution": - reportAirPollutionData(weather); - break; - case "air_pollution/history": - case "air_pollution/forecast": - reportAirPollutionHistoryOrForecastData(weather, now); - break; - default: - System.out.println("No examples yet for this dataset."); - } + switch (dataSet) { + case "weather": + reportWeatherData(weather); + break; + case "forecast": + reportForecastData(weather); + break; + case "onecall": + reportOneCallData(weather); + break; + case "air_pollution": + reportAirPollutionData(weather); + break; + case "air_pollution/history": + case "air_pollution/forecast": + reportAirPollutionHistoryOrForecastData(weather, now); + break; + default: + System.out.println("No examples yet for this dataset."); + } - optionallySaveDataToFile(data, dataSet, scanner); + optionallySaveDataToFile(data, dataSet, scanner); + } scanner.close(); } }