Reid Posted March 24, 2015 Share Posted March 24, 2015 (edited) Currently working on a weather application for fun and experience. Feel free to use as well as give me some feedback package com.reiddacosta.rweather; import java.io.IOException; import com.reiddacosta.rweather.api.Weather; /** * rWeather v1.0 * * @author Reid DaCosta * */ public class rWeather { public static void main(String[] args) throws IOException { System.out.println(new Weather().getTemperature()); } } package com.reiddacosta.rweather.api; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; /** * Weather Class * * @author Reid DaCosta * */ public class Weather { private static final String BASE = "https://api.forecast.io/forecast/477268b98a0bc386f1e6e624777a6077/"; private String userLatitude; private String userLongitude; private int userTemperature; private Location location; /** * Default Constructor * * @throws IOException */ public Weather() throws IOException { location = new Location(); this.userLatitude = location.getLatitude(); this.userLongitude = location.getLongitude(); userTemperature = (int) toCelsius(getUserTeperature()); } /** * Returns the temperature for the user based on their location. * * @return userTemperature */ public int getTemperature() { return userTemperature; } /** * Sets the temperature for the user. * * @param userTemperature */ public void setTemperature(int userTemperature){ this.userTemperature = userTemperature; } /** * Returns the temperature based on the users location. * * @return userTeperature * @throws IOException */ private String getUserTeperature() throws IOException { final URL url = new URL(BASE + userLatitude + "," + userLongitude); BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream())); String line, almost; String userTeperature = null; while ((line = file.readLine()) != null) { if (line.contains("apparentTemperature")) { userTeperature = (line); } } file.close(); almost = userTeperature.substring(userTeperature.indexOf("apparentTemperature"),userTeperature.indexOf("dewPoint")); return almost.substring(almost.indexOf(":") + 1, almost.indexOf(",")); } /** * Returns the temperature in Celsius. * * @param fahTemp * @return tempCelsius */ private double toCelsius(String fahTemp) { return (int) (Double.parseDouble(fahTemp) - 32) * 5 / 9; } } package com.reiddacosta.rweather.api; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; /** * Location Class * * @author Reid DaCosta * */ public class Location { private static final String BASE = "http://whatsmylatlng.com"; private String userLocation; private String userLatitude; private String userLongitude; /** * Default Constructor * * @throws IOException */ public Location() throws IOException { this.userLocation = getuserLocation(); this.userLatitude = userLocation.substring(0, userLocation.indexOf(",")); this.userLongitude = userLocation.substring(userLocation.indexOf(",") + 1, userLocation.length()); } /** * Returns the Latitude of the user. * * @return userLatitude */ public String getLatitude() { return userLatitude.trim(); } /** * Sets the Latitude of the user. * * @param userLatitude */ public void setLatitude(String userLatitude) { this.userLatitude = userLatitude; } /** * Returns the Longitude of the user. * * @return userLongitude */ public String getLongitude() { return userLongitude.trim(); } /** * Sets the Longitude of the user. * * @param userLongitude */ public void setLongitude(String userLongitude){ this.userLongitude = userLongitude; } /** * Returns the Latitude & Longitude of the user. * * @return userLocation * @throws IOException */ private String getuserLocation() throws IOException { final URL url = new URL(BASE); BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream())); String line; String userLocation = null; while ((line = file.readLine()) != null) { if (line.contains("geo.initialize")) { userLocation = (line); } } file.close(); return userLocation.substring(userLocation.indexOf("(") + 1,userLocation.indexOf(")")); } } Edited March 24, 2015 by Reid 1 Quote Link to comment Share on other sites More sharing options...
Volta Posted March 24, 2015 Share Posted March 24, 2015 Nice work, tho. 1 Quote Link to comment Share on other sites More sharing options...
Botre Posted March 24, 2015 Share Posted March 24, 2015 Imo you should probably favor try/catch blocks over methods trowing errors. Looks neato 1 Quote Link to comment Share on other sites More sharing options...
Augury13 Posted April 7, 2015 Share Posted April 7, 2015 neato Quote Link to comment Share on other sites More sharing options...
gebotter Posted April 7, 2015 Share Posted April 7, 2015 rofl neato Quote Link to comment Share on other sites More sharing options...