Usage demonstration

Let's see how to build a simple class to retrieve weather information!

Our class will run passing the location of the station we want to observe as a parameter.

import it.octograve.weatherlib.*;

public class SimpleWeatherViewer {

	private static void usage() {
		System.out.println(
			"USAGE: java SimpleWeatherViewer "
		);
		System.out.println();
		System.out.println(
			"\teg. java SimpleWeatherViewer Cambridge"
		);
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			usage();
			System.exit(1);
		}

Then we ought to build a StationsList object, to hold all of available stations. The list is fetched from the net when invoking the fetchStationsList() method.

		System.out.println("Fetching stations list ...");
		StationsList list = null;
		try {
			list = StationsList.fetchStationsList();
		} catch (WeatherException e) {
			System.err.println(
				"Unable to fetch stations list."
			);
			System.exit(1);
		}

Now we could start to query the list, looking for the station at requested location.

		String location = args[0];
		Station station = list.getByLocation(location);
		if (station == null) {
			System.out.println("'" + location
				+ "' is not a valid location");
			usage();
			System.exit(1);
		}

Once a Station object has been found, we could fetch/update its observed weather.

		System.out.println("Fetching station's weather ...");
		try {
			station.updateWeather();
		} catch (WeatherException e) {
			System.err.println(
				"Unable to fetch station's weather."
			);
			System.exit(1);
		}

At last, let's print some verbose informations.

		System.out.println(
			"Weather informations for " 
			+ station.getLocation()
		);
		System.out.println(
			station.getWeather().toVerboseString()
		);
	}

}

And this is what the program prints, providing "Cambridge" as argument.

Fetching stations list ...
Fetching station's weather ...
Weather informations for Cambridge
Thu Jun 07 21:20:19 CEST 2007
Temperature: 13.0 C
DewPoint: 12.0 C
Pressure: 1020.0 MBARS
Visibility: EQUALS_TO 8000 METERS
Wind: 10 knots, direction NORTH
Cloudiness: OVERCAST