Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.net.UnknownHostException;
  5. import java.util.HashMap;
  6.  
  7. public class ClientMeteo {
  8.     public static void main(String[] args){
  9.         try {
  10.  
  11.             HashMap<String, String> nomVille = new HashMap<String, String>();
  12.             nomVille.put("MTL", "Montréal");
  13.             nomVille.put("TOR", "Toronto");
  14.             nomVille.put("NY", "New York");
  15.             String[] villes = new String[] {"MTL", "TOR", "NY"};
  16.             double[] temperatures = new double[] {0.0, 0.0, 0.0};
  17.             int[] humidites = new int[] {0, 0, 0};
  18.             int[] vents = new int[] {0, 0, 0};
  19.  
  20.             for(int i = 0; i >= 0; i++){
  21.                 URL url = new URL(args[0]);
  22.                 InputStream is = url.openStream();
  23.  
  24.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  25.                 PrintWriter writer = new PrintWriter(
  26.                         new BufferedWriter(
  27.                                 new FileWriter("index.html")));
  28.  
  29.                 writer.println("<!doctype html>");
  30.                 writer.println("<html>");
  31.                 writer.println("<head>");
  32.                 writer.println("<meta charset=\"utf-8\">");
  33.                 writer.println("<title>Meteo-Mediocre.ca/PrévisionDeLaSemaine</title>");
  34.                 writer.println("<meta http-equiv=\"refresh\" content=\"5\">");
  35.                 writer.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">\n");
  36.                 writer.println("<script src=\"ExtremeTemperature.js\"></script>");
  37.                 writer.println("</head>");
  38.                 writer.println("<body>");
  39.                 writer.println("<table class=\"table table-bordered\">");
  40.                 writer.println("<tr>");
  41.                 writer.println("<th>Ville</th>");
  42.                 writer.println("<th>Température</th>");
  43.                 writer.println("<th>Humidité</th>");
  44.                 writer.println("<th>Vents</th>");
  45.                 writer.println("</tr>");
  46.  
  47.                 boolean pasFini = true;
  48.                 String ligne;
  49.  
  50.                 while (pasFini){
  51.                     ligne = reader.readLine();
  52.  
  53.                     if(ligne != null){
  54.                         int index = 0;
  55.                         if(ligne.split(",")[0].equals("MTL")){
  56.                             index = 0;
  57.                         } else if(ligne.split(",")[0].equals("TOR")){
  58.                             index = 1;
  59.                         } else {
  60.                             index = 2;
  61.                         }
  62.  
  63.                         temperatures[index] = Double.parseDouble(ligne.split(",")[1]);
  64.                         humidites[index] = Integer.parseInt(ligne.split(",")[2]);
  65.                         vents[index] = Integer.parseInt(ligne.split(",")[3]);
  66.                     } else {
  67.                         pasFini = false;
  68.                     }
  69.                 }
  70.  
  71.                 for(int b = 0; b != villes.length; b++){
  72.                     writer.println("<tr>");
  73.                     writer.println("<td>" + nomVille.get(villes[b]) + "</td>");
  74.                     if(temperatures[b] <= -30 || temperatures[b] >= 30){
  75.                         writer.println("<td bgcolor=\"red\">" + temperatures[b] + "</td>");
  76.                     } else {
  77.                         writer.println("<td>" + temperatures[b] + "</td>");
  78.                     }
  79.                     if(humidites[b] >= 90){
  80.                         writer.println("<td bgcolor=\"red\">" + humidites[b] + "</td>");
  81.                     } else {
  82.                         writer.println("<td>" + humidites[b] + "</td>");
  83.                     }
  84.                     if(vents[b] >= 100){
  85.                         writer.println("<td bgcolor=\"red\">" + vents[b] + "</td>");
  86.                     } else {
  87.                         writer.println("<td>" + vents[b] + "</td>");
  88.                     }
  89.                     writer.println("</tr>");
  90.                 }
  91.  
  92.                 writer.println("</table>");
  93.                 writer.println("</body>");
  94.                 writer.println("</html>");
  95.  
  96.                 writer.close();
  97.                 reader.close();
  98.  
  99.                 Thread.sleep(5000);
  100.             }
  101.  
  102.         } catch (MalformedURLException mue) {
  103.             System.err.println("Erreur, l'URL est invalide");
  104.         } catch (UnknownHostException uhe) {
  105.             System.err.println("Erreur, le site est introuvable");
  106.         } catch (FileNotFoundException fnfe) {
  107.             System.err.println("Erreur, le fichier est introuvable!");
  108.         } catch (IOException ioe) {
  109.             ioe.printStackTrace();
  110.             System.err.println("Erreur, Vous devez entrer une URL");
  111.         } catch (InterruptedException ie){
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement