Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.37 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <?import javafx.scene.control.Button?>
  4. <?import javafx.scene.control.Label?>
  5. <?import javafx.scene.control.TextArea?>
  6. <?import javafx.scene.layout.AnchorPane?>
  7. <?import javafx.scene.text.Font?>
  8.  
  9. <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">
  10.     <children>
  11.       <TextArea fx:id="zipCodeInputArea" layoutX="576.0" layoutY="396.0" prefHeight="49.0" prefWidth="142.0" promptText="Zip Code" />
  12.       <Label layoutX="620.0" layoutY="365.0" text="Zip Code:" />
  13.       <Button fx:id="weatherButton" layoutX="592.0" layoutY="508.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Get weather data:" />
  14.       <Label fx:id="weatherInfo" layoutX="579.0" layoutY="154.0" prefHeight="32.0" prefWidth="134.0">
  15.          <font>
  16.             <Font name="System Bold" size="25.0" />
  17.          </font></Label>
  18.     </children>
  19. </AnchorPane>
  20.  
  21.  
  22. package weather_scenebuiler;
  23.  
  24. import java.io.InputStream;
  25. import java.net.URL;
  26. import java.util.ResourceBundle;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javafx.event.ActionEvent;
  30. import javafx.fxml.FXML;
  31. import javafx.fxml.Initializable;
  32. import javafx.scene.control.Button;
  33. import javafx.scene.control.Label;
  34. import javafx.scene.control.TextArea;
  35.  
  36. /**
  37.  *
  38.  * @author Raghav
  39.  */
  40. public class FXMLDocumentController implements Initializable {
  41.    
  42.     @FXML
  43.     private static TextArea zipCodeInputArea;
  44.     @FXML
  45.     private static Button weatherButton;
  46.     @FXML
  47.     private Label weatherInfo;
  48.    
  49.     @FXML
  50.     private void handleButtonAction(ActionEvent event){
  51.         if (zipCodeInputArea.getText() != null) {
  52.         try {
  53.             weatherInfo.setText(main.returnWeather(Integer.parseInt(zipCodeInputArea.getText())));
  54.         } catch (Exception ex) {
  55.             Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  56.         }
  57.         }
  58.     }
  59.    
  60.     @Override
  61.     public void initialize(URL url, ResourceBundle rb) {
  62.         // TODO
  63.     }    
  64.    
  65.    
  66.    
  67.      
  68.      
  69.    
  70. }
  71.  
  72.  
  73.  
  74.  
  75. package weather_scenebuiler;
  76.  
  77. /*
  78.  * To change this license header, choose License Headers in Project Properties.
  79.  * To change this template file, choose Tools | Templates
  80.  * and open the template in the editor.
  81.  */
  82.  
  83.  
  84. /**
  85.  *
  86.  * @author raghav
  87.  */
  88. public class Weather {
  89.     private String city;
  90.     private String weatherDescription;
  91.     private int zipCode;
  92.     private double tempF;
  93.     private double tempC;
  94.    
  95.     public String getCity() {return city;}
  96.     public void setCity(String city){
  97.         this.city = city;
  98.     }
  99.    
  100.     public String getWeatherDescription() {return weatherDescription;}
  101.     public void setWeatherDescription(String description){
  102.         this.weatherDescription = description;
  103.     }
  104.  
  105.     public int getZipCode() {return zipCode;}
  106.     public void setZipCode(int zipCode){
  107.         this.zipCode = zipCode;
  108.     }
  109.    
  110.     public double getTempF() {return tempF;}
  111.     public void setTempF(double temp){
  112.         this.tempF = temp;
  113.     }
  114.    
  115.     public double getTempC() {return tempC;}
  116.     public void setTempC(double temp) {
  117.         this.tempC = temp;
  118.     }
  119. }
  120.  
  121.  
  122. package weather_scenebuiler;
  123.  
  124. import org.dom4j.Document;
  125. import java.io.*;
  126. import java.util.*;
  127. import org.dom4j.*;
  128. import org.dom4j.io.SAXReader;
  129.  
  130. public class apiParser {
  131.     private static InputStream inputStream;
  132.    
  133.     public Weather weatherSetter(InputStream inputStream) throws Exception{
  134.         Weather weather = new Weather();
  135.         SAXReader reader = new SAXReader();
  136.         Document doc = reader.read(inputStream);
  137.         System.out.println("Root element :" + doc.getRootElement().getName());
  138.         Element classElement = doc.getRootElement();
  139.         List<Node> nodes = doc.selectNodes("/response/location");
  140.        
  141.         System.out.println("---------");
  142.         //Node townName = doc.selectSingleNode("//response/location/city");
  143.         weather.setCity(doc.selectSingleNode("//response/location/city").getText());
  144.         weather.setWeatherDescription(doc.selectSingleNode("//response/current_observation/weather").getText());
  145.         weather.setTempF(Double.parseDouble(doc.selectSingleNode("//response/current_observation/temp_f").getText()));
  146.         weather.setTempC(Double.parseDouble(doc.selectSingleNode("//response/current_observation/temp_c").getText()));
  147.        
  148.        
  149.         return weather;
  150.     }
  151. }
  152.  
  153.  
  154. package weather_scenebuiler;
  155.  
  156. /*
  157.  * To change this license header, choose License Headers in Project Properties.
  158.  * To change this template file, choose Tools | Templates
  159.  * and open the template in the editor.
  160.  */
  161.  
  162. import java.net.*;
  163. import java.io.*;
  164.  
  165.  
  166. /**
  167.  *
  168.  * @author raghav
  169.  */
  170. public class apiReader {
  171.     private static int zipCode;
  172.    
  173.     public apiReader(int zipCode) {
  174.             this.zipCode = zipCode;
  175.     }
  176.    
  177.     public static InputStream readAPI() throws MalformedURLException, IOException {
  178.         String url = ("http://api.wunderground.com/api/749dca79a858cf56/forecast/geolookup/conditions/q/CA/" + zipCode + ".xml");
  179.         URLConnection connection = new URL(url).openConnection();
  180.         return connection.getInputStream();
  181.     }
  182. }
  183.  
  184.  
  185. package weather_scenebuiler;
  186.  
  187. import java.io.*;
  188. import java.util.*;
  189. import org.dom4j.*;
  190. import org.dom4j.io.SAXReader;
  191.  
  192.  
  193.  
  194. /*
  195.  * To change this license header, choose License Headers in Project Properties.
  196.  * To change this template file, choose Tools | Templates
  197.  * and open the template in the editor.
  198.  */
  199.  
  200. import java.io.IOException;
  201. import javafx.application.Application;
  202. import javafx.event.ActionEvent;
  203. import javafx.event.EventHandler;
  204. import javafx.scene.Scene;
  205. import javafx.scene.control.Button;
  206. import javafx.scene.layout.StackPane;
  207. import javafx.scene.layout.VBox;
  208. import javafx.stage.Stage;
  209. import java.util.Scanner.*;
  210. import java.util.logging.Level;
  211. import java.util.logging.Logger;
  212. import static javafx.application.Application.launch;
  213. import javafx.fxml.FXMLLoader;
  214. import javafx.geometry.Pos;
  215. import javafx.scene.Parent;
  216. import javafx.scene.control.TextArea;
  217. import javafx.scene.control.TextField;
  218. import javafx.scene.layout.BorderPane;
  219. /**
  220.  *
  221.  * @author raghav
  222.  */
  223.  
  224.  
  225. public class main extends Application {
  226.    
  227.    
  228.    
  229.     Scanner scnr = new Scanner(System.in);  
  230.    
  231.     @Override
  232.      public void start(Stage primaryStage) throws IOException {
  233.       String fxmlResource = ("FXMLDocument.fxml");
  234.       Parent panel;
  235.       panel = FXMLLoader.load(getClass().getResource(fxmlResource));
  236.       Scene scene = new Scene(panel);
  237.       Stage stage = new Stage();
  238.       stage.setScene(scene);
  239.       stage.show();
  240.        
  241.     }
  242.  
  243.  
  244.      public static String returnWeather(int zipCode) throws Exception{
  245.         apiReader zipCodeApi = new apiReader(zipCode);
  246.         InputStream inputstream = zipCodeApi.readAPI();
  247.         apiParser parser = new apiParser();
  248.         Weather weatherswag = parser.weatherSetter(inputstream);
  249.         return(weatherswag.getCity() + " " + weatherswag.getTempF() + " " + weatherswag.getWeatherDescription());
  250.  
  251.    
  252.     }
  253.     public static void main (String[] args) throws IOException, Exception {
  254.         launch(args);
  255.         //returnWeather(94555);
  256.     }
  257.    
  258.      
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement