Txerrinko

PruebaVuelos

May 23rd, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package tanda1;
  2.  
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import org.jdom2.input.SAXBuilder;
  9. import org.jdom2.output.Format;
  10. import org.jdom2.output.XMLOutputter;
  11. import org.jdom2.*;
  12.  
  13. public class PruebaVuelosNEREA {
  14.  
  15.    
  16.     public static void main(String[] args) throws JDOMException, IOException {
  17.        
  18.         SAXBuilder builder=new SAXBuilder();
  19.         Document doc=builder.build("vuelos.xml");
  20.        
  21.         Element raiz=doc.getRootElement();
  22.        
  23.         List hijos=raiz.getChildren("vuelo");
  24.        
  25.         //1
  26.         System.out.println("Hay " + hijos.size() + " vuelos");
  27.        
  28.         //2
  29.        
  30.         System.out.println("Destinos desde Madrid:");
  31.         Iterator it=hijos.iterator();
  32.         while (it.hasNext()){
  33.            
  34.             Element v =(Element) it.next();
  35.            
  36.             // v.getChildText("origen").equals("Madrid")
  37.            
  38.             Element origen=v.getChild("origen");
  39.             String txtorigen=origen.getText();
  40.             if (txtorigen.equals("Madrid")){
  41.                 System.out.print(v.getChildText("destino")+" ");
  42.             }
  43.         }
  44.        
  45.         //3
  46.        
  47.         System.out.println("Introduce id-vuelo:");
  48.         String idbuscado=Consola.leeString();
  49.        
  50.         boolean encontrado=false;
  51.         Iterator it2=hijos.iterator();
  52.         while (it2.hasNext()  &&   !encontrado ){
  53.            
  54.             Element v =(Element) it2.next();
  55.             String strid=v.getAttributeValue("id");//obtenemoes el valor del atributo
  56.             if (idbuscado.equals(strid)){
  57.                 System.out.println(  // mostramos  del elemento el valor hijo del texto del origen destino y la hora
  58.                         v.getChildText("origen")+" " +
  59.                         v.getChildText("destino")+" " +
  60.                         v.getChildText("hora")
  61.                        
  62.                         );
  63.                 encontrado=true;//y damso el valor verdadero a la variabble enccontrado
  64.             }
  65.         }
  66.        
  67.         if (!encontrado)
  68.             System.out.println("El vuelo con id "+ idbuscado + " no existe");
  69.        
  70.         //4
  71.        
  72.         System.out.println("\nVuelos posteriores a las 14:");
  73.        
  74.         for (int i=0; i<hijos.size(); i++){ //de la cantidad de hijos
  75.             Element v=(Element) hijos.get(i);
  76.          //obtenemos los elementos hijos  de v
  77.            
  78.             String strhora=v.getChildText("hora");
  79.             //obtenemos
  80.             if (esPosterior14(strhora)==true){  // si la hora  que obtenemos de la etiqueta hijo  es verdadera
  81.                 System.out.println(
  82.                         v.getChildText("origen")+" " + // mostramos el  texto de los atributos hijos
  83.                         v.getChildText("destino")+" "                      
  84.                         );
  85.             }
  86.         }
  87.        
  88.        
  89.         //5
  90.         System.out.print("Introduzca id vuelo:");
  91.         String idv=Consola.leeString();
  92.         System.out.print("Introduzca origen:");
  93.         String or=Consola.leeString();
  94.         System.out.print("Introduzca destino:");
  95.         String des=Consola.leeString();
  96.         System.out.print("Introduzca hora:");
  97.         String hor=Consola.leeString();
  98.        
  99.         Element v=new Element("vuelo");
  100.         Element origen=new Element("origen").addContent(or);
  101.         Element destino=new Element("destino").setText(des);
  102.         Element hora=new Element("hora").setText(hor);
  103.        
  104.         v.setAttribute("id",idv);
  105.         v.addContent(origen);
  106.         v.addContent(destino);
  107.         v.addContent(hora);
  108.                
  109.         raiz.addContent(v);
  110.        
  111.         XMLOutputter salida= new XMLOutputter(Format.getPrettyFormat());
  112.         salida.output(doc, new FileWriter("vuelos.xml"));
  113.        
  114.         //6
  115.        
  116.         Element v2 =(Element) hijos.get(hijos.size()-1 ); //eliminamos el contenido de  el tamaño de los elementos
  117.                                                           // hijos obtenidos menos el ultimo que hayamos añadido
  118.         raiz.removeContent(v2);// y nos encargaremos de borrar dicho contenido
  119.         salida.output(doc, System.out);
  120.        
  121.        
  122.     }
  123.  
  124.     private static boolean esPosterior14(String strhora) {
  125.        
  126.         int idp=strhora.indexOf(":");
  127.        
  128.         String strh =strhora.substring(0,idp);
  129.         //obtener el substring de las horas entre 0 y el rango establecido entre los minutos
  130.         String strm=strhora.substring(idp+1,strhora.length());
  131.                         //sacamos el rango de valores entra la hora y sus respectivos+1  y la longitud de dicha hora                            
  132.        
  133.        
  134.         //String[] partes=strhora.split(":");
  135.         //partes[0] tendrá la hora
  136.         //partes[1] tendra los minutos
  137.        
  138.         int h=Integer.parseInt(strh);  // posteriormente parseamos los valores a int
  139.         int m=Integer.parseInt(strm);
  140.        
  141.        
  142.         if (h>=14)
  143.             return true;
  144.         else
  145.             return false;
  146.        
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment