Txerrinko

GestionMenuComida

May 23rd, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.17 KB | None | 0 0
  1. package tanda1;
  2.  
  3. import java.io.IOException;
  4.  
  5.  
  6. import java.io.*;
  7. import java.util.*;
  8. import org.jdom2.*;
  9. import org.jdom2.input.*;
  10. import org.jdom2.output.*;
  11.  
  12.  
  13. public class GestionMenu2 {
  14.  
  15.     private String nomFich;
  16.      private Document doc;
  17.    
  18.     GestionMenu2(String nFich){
  19.        
  20.         nomFich=nFich;
  21.        
  22.        
  23.     }
  24.    
  25.     void parsear() throws JDOMException, IOException{
  26.        
  27.         SAXBuilder builder=new SAXBuilder();
  28.          doc=builder.build(nomFich);
  29.        
  30.     }
  31.    
  32.    
  33.     void ver() throws IOException{
  34.        
  35.         XMLOutputter salida=new XMLOutputter(Format.getPrettyFormat());
  36.        
  37.         salida.output(doc, System.out);
  38.        
  39.     }
  40.    
  41.     Element consultaPlato(String nombreplato){
  42.        
  43.         Element raiz=doc.getRootElement(); //CREAMOS EL ELEMENTO RAIZ
  44.          
  45.         List comidas=raiz.getChildren("comida");
  46.         Iterator it=comidas.iterator();
  47.        
  48.         boolean encontrado;
  49.         Element t = null;
  50.         while (it.hasNext()){
  51.         //Creamos elemento e para devolver next cada recorrido
  52.            
  53.             Element e=(Element) it.next();
  54.             //Anidmos al nuevo elemento el elementohijo de e
  55.              Element nom=e.getChild("nombre");
  56.                  //nom.getText.equals(nombreplato)
  57.                //si el texto  cogido es igual al nombreplato             
  58.              if (nombreplato.equals(nom)){
  59.                 encontrado=true;
  60.               t= e;
  61.                  
  62.              }
  63.              
  64.         }
  65.        
  66.    
  67.         return t;
  68.        
  69.        
  70.     }
  71.    
  72.    ArrayList <Element> consultaPorCalorias(int menosCalorias){
  73.    
  74.         ArrayList consultaPorCalorias=new ArrayList <Element>();
  75.        
  76.         Element raiz=doc.getRootElement(); //CREAMOS EL ELEMENTO RAIZ
  77.          
  78.         List comidas=raiz.getChildren("comida");
  79.  
  80.          Iterator it=comidas.iterator();
  81.             while (it.hasNext()){
  82.              Element b=(Element) it.next();
  83.            
  84.              Element cal=b.getChild("calorias");
  85.              
  86.              if (menosCalorias>Integer.parseInt(cal.getText())){
  87.                  
  88.                  consultaPorCalorias.add(cal);
  89.                  
  90.              }
  91.                
  92.              
  93.              }
  94.        
  95.                
  96.      return consultaPorCalorias;
  97.        
  98.        
  99.        
  100.    }
  101.    
  102.     void platosEconomicos(float precioini ,float preciofin) throws IOException, JDOMException{
  103.        
  104.         SAXBuilder builder=new SAXBuilder();
  105.         System.out.println("Introduce nota");
  106.        
  107.         String momFich=Consola.leeString();  
  108.         Document doc2=builder.build(nomFich);
  109.        
  110.    
  111.        
  112.         XMLOutputter salida=new XMLOutputter();
  113.         FileWriter fw=new FileWriter(new File("platosecon.xml"));
  114.        
  115.         Element raiz=doc2.getRootElement(); //CREAMOS EL ELEMENTO RAIZ
  116.          
  117.         List comidas=raiz.getChildren("comida");
  118.  
  119.        
  120.        
  121.         //ArrayliSt DE TIDOS LOS PLATOS
  122.        
  123.         ArrayList <Element> platosprec=new ArrayList <Element>();
  124.                  
  125.               Iterator it=comidas.iterator();
  126.  
  127.                while (it.hasNext()){
  128.                
  129.            Element cost=  (Element)it.next();
  130.  
  131.             Float prec=Float.parseFloat(cost.getChildText("precio"));        
  132.            
  133.           if (prec >=precioini && prec<=preciofin){
  134.              
  135.               platosprec.add(cost);
  136.              
  137.               Element clon  = cost.clone();
  138.              
  139.               clon.removeChild("precio");
  140.              
  141.               clon.removeChild("calorias");
  142.              
  143.               // el precio sera atributo de la comida , el nombre sera String   y le pasamos el texto del atributo hijo
  144.               //
  145.               clon.setAttribute("precio", "" + clon.getChildText("precio"));
  146.              
  147.               raiz.addContent(clon);
  148.              
  149.           }
  150.      
  151.           XMLOutputter salida2=new XMLOutputter(Format.getPrettyFormat());
  152.          
  153.           salida2.output(doc2,fw);
  154.          
  155.           }
  156.        
  157.        
  158.        
  159.        
  160.     }
  161.    
  162.    
  163.     void nuevaComida(Comida com)throws JDOMException, IOException    {
  164.        
  165.  
  166.        
  167.         SAXBuilder builder=new SAXBuilder();
  168.         Document doc=builder.build(nomFich);
  169.         Element raiz=doc.getRootElement();
  170.        
  171.        
  172.         //al elemento raiz le iremos añadiendo sus debidas etiqtas con su repectivo valor en cada una
  173.        
  174.         Element nom=new Element("Comida");
  175.         Element pre=new Element("Precio");
  176.         Element des=new Element("Descripcion");
  177.         Element cal=new Element("Calorias");
  178.        
  179.         nom.setText(com.getnombre());
  180.         pre.setText(com.getprecio()+"");
  181.         des.setText(com.getdescripcion());
  182.         cal.setText(com.getcalorias()+"");
  183.        
  184.         raiz.addContent(nom);
  185.         raiz.addContent(pre);
  186.         raiz.addContent(des);
  187.         raiz.addContent(cal);
  188.            
  189.         XMLOutputter salida=new XMLOutputter(Format.getPrettyFormat());
  190.         FileWriter fx=new FileWriter(new File(nomFich),true);
  191.         salida.output(doc,fx);
  192.        
  193.     }
  194.    
  195.    
  196.    
  197.    void eliminaMasCara(){
  198.        
  199.        
  200.        Element raiz=doc.getRootElement();
  201.        
  202.        List comidas=raiz.getChildren("comida");
  203.    
  204.        
  205.        Iterator it=comidas.iterator();
  206.        
  207.        ArrayList eliminar=new ArrayList();
  208.    
  209.        
  210.        int max=-1;
  211.        
  212.        int prec;
  213.        Element comcar=null;
  214.        
  215.        while (it.hasNext()){
  216.            
  217.            
  218.            Element comida=(Element)it.next();
  219.            
  220.             prec=(int) Float.parseFloat(comida.getChild("precio").getText());
  221.            
  222.            if (prec > max){
  223.                //si precio es mayor que maximo
  224.                //maximo adquiere el valor de precio
  225.                //y al elemento comcar que hemos creado le pasamos el valor de comida
  226.                max=prec;
  227.                 comcar=comida;
  228.            }
  229.            
  230.            
  231.            raiz.removeContent(comcar);
  232.            
  233.        }
  234.        
  235.        
  236.        
  237.    }
  238.    
  239.    
  240.    
  241.     public static void main(String[] args) throws JDOMException, IOException {
  242.        
  243.  
  244.        
  245.         GestionMenu2 gestmen=new GestionMenu2("menu.xml");
  246.        
  247.         gestmen.parsear();
  248.         gestmen.ver();
  249.        
  250.         Element conspla=gestmen.consultaPlato("Chicken Burger");
  251.        
  252.        
  253.         if (conspla!=null){
  254.            
  255.             XMLOutputter salida=new XMLOutputter();
  256.             salida.output(conspla, System.out);
  257.         }
  258.         else {
  259.            
  260.             System.out.println("No existe ese plato");
  261.         }
  262.        
  263.        
  264.        
  265.         ArrayList <Element> conscal=gestmen.consultaPorCalorias(900);
  266.        
  267.         for (int i=0;i<conscal.size();i++){
  268.            
  269.             Element e=(Element)conscal.get(i);  //generamos nuevo elemento del arralist
  270.                                                 //obteniendo el i
  271.             XMLOutputter salida=new XMLOutputter();
  272.             salida.output(e, System.out);
  273.            
  274.         }
  275.    
  276.         gestmen.platosEconomicos((float)2.3,(float) 9.2);
  277.        
  278.        
  279.        
  280.    
  281.        
  282.        
  283.     }
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment