Weather Forecaster App in Java Java Applet: JavaFX for the UI and you can set up
ID: 3740198 • Letter: W
Question
Weather Forecaster App in Java
Java Applet: JavaFX for the UI and you can set up the UI using fxml files or pure Java code
Goal: Create an app to display a weather forecast for the user’s current location,
Main Screen-- display the location (City, State), and high and low temperature for the current day
2) Edit screen - update the city and state and save it to the database
Input validation for the zip code (only valid 5 digit zip codes should be accepted)
Display High Temp, Low Temp, Humidity Pressure Sunrise and Sun Set
Display images for the forecast (eg: clouds for cloudy weather, a sun for clear weather)
Database: Store location in database, and allow it to be updated via JDBC
Some publicly available weather APIs (any API is acceptable):
https://developer.yahoo.com/weather/
Explanation / Answer
package com.weather.app;
//Main Application Class That will accept on zip code as command line argument
import java.io.InputStream;
import com.weather.app.service.Weather;
import com.weather.app.service.YahooParser;
import com.weather.app.service.YahooRetriever;
public class Main {
public static void main(String[] args) throws Exception {
// Read the zip code from the command line
// (if none supplied, use 60202)
System.setProperty("http.agent", "Fake Agent");
String zipcode = "60202";
try {
zipcode = args[0];
} catch( Exception e ) {}
// Start the program
new Main(zipcode).start();
}
private String zip;
public Main(String zip) {
this.zip = zip;
}
public void start() throws Exception {
// Retrieve Data
InputStream dataIn = new YahooRetriever().retrieve( zip );
// Parse Data
Weather weather = new YahooParser().parse( dataIn );
// Format (Print) Data
// System.out.print( new WeatherFormatter().format( weather ) );
}
}
package com.weather.app.service;
// Model Class that will responsible for storing data
public class Weather {
private String city;
private String region;
private String country;
private String condition;
private String temp;
private String chill;
private String humidity;
public Weather() {}
public String getCity() { return city; }
public void setCity(String city) {
this.city = city;
}
public String getRegion() { return region; }
public void setRegion(String region) {
this.region = region;
}
public String getCountry() { return country; }
public void setCountry(String country) {
this.country = country;
}
public String getCondition() { return condition; }
public void setCondition(String condition) {
this.condition = condition;
}
public String getTemp() { return temp; }
public void setTemp(String temp) {
this.temp = temp;
}
public String getChill() { return chill; }
public void setChill(String chill) {
this.chill = chill;
}
public String getHumidity() { return humidity; }
public void setHumidity(String humidity) {
this.humidity = humidity;
}
}
// Weather service impl
package com.weather.app.service;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;
public class YahooParser {
public Weather parse(InputStream inputStream) throws Exception {
Weather weather = new Weather();
SAXReader xmlReader = createXmlReader();
Document doc = xmlReader.read( inputStream );
weather.setCity(
doc.valueOf("/rss/channel/y:location/@city") );
weather.setRegion(
doc.valueOf("/rss/channel/y:location/@region") );
weather.setCountry(
doc.valueOf("/rss/channel/y:location/@country") );
weather.setCondition(
doc.valueOf("/rss/channel/item/y:condition/@text") );
weather.setTemp(
doc.valueOf("/rss/channel/item/y:condition/@temp") );
weather.setChill(
doc.valueOf("/rss/channel/y:wind/@chill") );
weather.setHumidity(
doc.valueOf("/rss/channel/y:atmosphere/@humidity") );
return weather;
}
private SAXReader createXmlReader() {
Map<String,String> uris = new HashMap<String,String>();
uris.put( "y", "http://xml.weather.yahoo.com/ns/rss/1.0" );
DocumentFactory factory = new DocumentFactory();
factory.setXPathNamespaceURIs( uris );
SAXReader xmlReader = new SAXReader();
xmlReader.setDocumentFactory( factory );
return xmlReader;
}
}
package com.weather.app.service;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class YahooRetriever {
public InputStream retrieve(String zipcode) throws Exception {
String url = "http://weather.yahooapis.com/forecastrss?p="
+ zipcode;
URLConnection conn = new URL(url).openConnection();
return conn.getInputStream();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.