Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package me.ivo.mexete;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. import org.jsoup.Jsoup;
  7. import org.jsoup.nodes.Document;
  8. import org.jsoup.nodes.Element;
  9. import org.jsoup.select.Elements;
  10.  
  11. public class Main {
  12.    
  13.     public static String obterHorarios(String paragem) throws IOException{
  14.         String results = null;
  15.         Document doc = Jsoup.connect("http://www.stcp.pt/pt/itinerarium/soapclient.php?codigo=" + paragem).get();
  16.         Elements smsBusResults = doc.select("#smsBusResults tr:lt(3)");
  17.         for (Element col : smsBusResults) {
  18.             results = col.text();
  19.             }
  20.         if (doc.getElementsContainingText("Por favor, utilize o codigo SMSBUS indicado na placa da paragem") != null) {
  21.             throw new IOException();
  22.         }
  23.         return results;
  24.     }
  25.  
  26.     public static void main(String[] args) throws IOException {
  27.         Scanner scanner = new Scanner(System.in);
  28.         System.out.println("Qual a paragem que queres ver?\n");
  29.         System.out.println(obterHorarios(scanner.next()));
  30.     }
  31.    
  32.    
  33.    
  34.    
  35.  
  36. }
  37.  
  38. #UI
  39.  
  40. package me.ivo.mexete;
  41.  
  42. import java.awt.EventQueue;
  43.  
  44. import javax.swing.JFrame;
  45. import javax.swing.JLabel;
  46. import javax.swing.JOptionPane;
  47.  
  48. import java.awt.Font;
  49. import java.awt.event.ActionEvent;
  50. import java.awt.event.ActionListener;
  51. import java.io.IOException;
  52.  
  53. import javax.swing.JTextField;
  54. import javax.swing.JTextPane;
  55. import javax.swing.JTable;
  56. import javax.swing.JButton;
  57.  
  58. public class UI implements ActionListener {
  59.  
  60.     private JFrame frame;
  61.     private JTextField textField;
  62.  
  63.     /**
  64.      * Launch the application.
  65.      */
  66.     public static void main(String[] args) {
  67.         EventQueue.invokeLater(new Runnable() {
  68.             public void run() {
  69.                 try {
  70.                     UI window = new UI();
  71.                     window.frame.setVisible(true);
  72.                 } catch (Exception e) {
  73.                     e.printStackTrace();
  74.                 }
  75.             }
  76.         });
  77.     }
  78.  
  79.     /**
  80.      * Create the application.
  81.      */
  82.     public UI() {
  83.         initialize();
  84.     }
  85.  
  86.     /**
  87.      * Initialize the contents of the frame.
  88.      */
  89.     private void initialize() {
  90.         frame = new JFrame();
  91.         frame.setBounds(100, 100, 450, 300);
  92.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93.         frame.getContentPane().setLayout(null);
  94.        
  95.         JLabel lblParagem = new JLabel("Paragem:");
  96.         lblParagem.setFont(new Font("Lucida Grande", Font.PLAIN, 25));
  97.         lblParagem.setBounds(6, 6, 164, 58);
  98.         frame.getContentPane().add(lblParagem);
  99.        
  100.         textField = new JTextField();
  101.         textField.setBounds(6, 61, 238, 26);
  102.         frame.getContentPane().add(textField);
  103.         textField.setColumns(10);
  104.        
  105.         JButton btnSubmeter = new JButton("Submeter");
  106.         btnSubmeter.setBounds(254, 60, 117, 29);
  107.         frame.getContentPane().add(btnSubmeter);
  108.         btnSubmeter.addActionListener(this);
  109.        
  110.         JLabel lblResultado = new JLabel("Resultado:");
  111.         lblResultado.setFont(new Font("Lucida Grande", Font.PLAIN, 25));
  112.         lblResultado.setBounds(6, 99, 164, 58);
  113.         frame.getContentPane().add(lblResultado);
  114.        
  115.        
  116.        
  117.        
  118.        
  119.     }
  120.  
  121.     @Override
  122.     public void actionPerformed(ActionEvent e) {
  123.        
  124.         JLabel lblResultados = new JLabel("");
  125.         lblResultados.setBounds(6, 153, 418, 98);
  126.         frame.getContentPane().add(lblResultados);
  127.        
  128.         try {
  129.             lblResultados.setText(Main.obterHorarios(textField.getText().toString()));
  130.         } catch (IOException e1) {
  131.             JOptionPane.showMessageDialog(null, "Verifica o código da paragem e/ou a ligação à Internet", "ERRO", JOptionPane.ERROR_MESSAGE);
  132.             e1.printStackTrace();
  133.         }
  134.        
  135.        
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement