Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <?import javafx.scene.control.Button?>
- <?import javafx.scene.control.Label?>
- <?import javafx.scene.control.TextArea?>
- <?import javafx.scene.layout.AnchorPane?>
- <?import javafx.scene.text.Font?>
- <AnchorPane id="AnchorPane" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="weather_scenebuiler.FXMLDocumentController">
- <children>
- <TextArea fx:id="zipCodeInputArea" layoutX="576.0" layoutY="396.0" prefHeight="49.0" prefWidth="142.0" promptText="Zip Code" />
- <Label layoutX="620.0" layoutY="365.0" text="Zip Code:" />
- <Button fx:id="weatherButton" layoutX="592.0" layoutY="508.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Get weather data:" />
- <Label fx:id="weatherInfo" layoutX="579.0" layoutY="154.0" prefHeight="32.0" prefWidth="134.0">
- <font>
- <Font name="System Bold" size="25.0" />
- </font></Label>
- </children>
- </AnchorPane>
- package weather_scenebuiler;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.ResourceBundle;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- /**
- *
- * @author Raghav
- */
- public class FXMLDocumentController implements Initializable {
- @FXML
- private static TextArea zipCodeInputArea;
- @FXML
- private static Button weatherButton;
- @FXML
- private Label weatherInfo;
- @FXML
- private void handleButtonAction(ActionEvent event){
- if (zipCodeInputArea.getText() != null) {
- try {
- weatherInfo.setText(main.returnWeather(Integer.parseInt(zipCodeInputArea.getText())));
- } catch (Exception ex) {
- Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- // TODO
- }
- }
- package weather_scenebuiler;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author raghav
- */
- public class Weather {
- private String city;
- private String weatherDescription;
- private int zipCode;
- private double tempF;
- private double tempC;
- public String getCity() {return city;}
- public void setCity(String city){
- this.city = city;
- }
- public String getWeatherDescription() {return weatherDescription;}
- public void setWeatherDescription(String description){
- this.weatherDescription = description;
- }
- public int getZipCode() {return zipCode;}
- public void setZipCode(int zipCode){
- this.zipCode = zipCode;
- }
- public double getTempF() {return tempF;}
- public void setTempF(double temp){
- this.tempF = temp;
- }
- public double getTempC() {return tempC;}
- public void setTempC(double temp) {
- this.tempC = temp;
- }
- }
- package weather_scenebuiler;
- import org.dom4j.Document;
- import java.io.*;
- import java.util.*;
- import org.dom4j.*;
- import org.dom4j.io.SAXReader;
- public class apiParser {
- private static InputStream inputStream;
- public Weather weatherSetter(InputStream inputStream) throws Exception{
- Weather weather = new Weather();
- SAXReader reader = new SAXReader();
- Document doc = reader.read(inputStream);
- System.out.println("Root element :" + doc.getRootElement().getName());
- Element classElement = doc.getRootElement();
- List<Node> nodes = doc.selectNodes("/response/location");
- System.out.println("---------");
- //Node townName = doc.selectSingleNode("//response/location/city");
- weather.setCity(doc.selectSingleNode("//response/location/city").getText());
- weather.setWeatherDescription(doc.selectSingleNode("//response/current_observation/weather").getText());
- weather.setTempF(Double.parseDouble(doc.selectSingleNode("//response/current_observation/temp_f").getText()));
- weather.setTempC(Double.parseDouble(doc.selectSingleNode("//response/current_observation/temp_c").getText()));
- return weather;
- }
- }
- package weather_scenebuiler;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.net.*;
- import java.io.*;
- /**
- *
- * @author raghav
- */
- public class apiReader {
- private static int zipCode;
- public apiReader(int zipCode) {
- this.zipCode = zipCode;
- }
- public static InputStream readAPI() throws MalformedURLException, IOException {
- String url = ("http://api.wunderground.com/api/749dca79a858cf56/forecast/geolookup/conditions/q/CA/" + zipCode + ".xml");
- URLConnection connection = new URL(url).openConnection();
- return connection.getInputStream();
- }
- }
- package weather_scenebuiler;
- import java.io.*;
- import java.util.*;
- import org.dom4j.*;
- import org.dom4j.io.SAXReader;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.IOException;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.StackPane;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- import java.util.Scanner.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import static javafx.application.Application.launch;
- import javafx.fxml.FXMLLoader;
- import javafx.geometry.Pos;
- import javafx.scene.Parent;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- /**
- *
- * @author raghav
- */
- public class main extends Application {
- Scanner scnr = new Scanner(System.in);
- @Override
- public void start(Stage primaryStage) throws IOException {
- String fxmlResource = ("FXMLDocument.fxml");
- Parent panel;
- panel = FXMLLoader.load(getClass().getResource(fxmlResource));
- Scene scene = new Scene(panel);
- Stage stage = new Stage();
- stage.setScene(scene);
- stage.show();
- }
- public static String returnWeather(int zipCode) throws Exception{
- apiReader zipCodeApi = new apiReader(zipCode);
- InputStream inputstream = zipCodeApi.readAPI();
- apiParser parser = new apiParser();
- Weather weatherswag = parser.weatherSetter(inputstream);
- return(weatherswag.getCity() + " " + weatherswag.getTempF() + " " + weatherswag.getWeatherDescription());
- }
- public static void main (String[] args) throws IOException, Exception {
- launch(args);
- //returnWeather(94555);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement