Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tanda1;
- import java.io.IOException;
- import java.io.*;
- import java.util.*;
- import org.jdom2.*;
- import org.jdom2.input.*;
- import org.jdom2.output.*;
- public class GestionMenu2 {
- private String nomFich;
- private Document doc;
- GestionMenu2(String nFich){
- nomFich=nFich;
- }
- void parsear() throws JDOMException, IOException{
- SAXBuilder builder=new SAXBuilder();
- doc=builder.build(nomFich);
- }
- void ver() throws IOException{
- XMLOutputter salida=new XMLOutputter(Format.getPrettyFormat());
- salida.output(doc, System.out);
- }
- Element consultaPlato(String nombreplato){
- Element raiz=doc.getRootElement(); //CREAMOS EL ELEMENTO RAIZ
- List comidas=raiz.getChildren("comida");
- Iterator it=comidas.iterator();
- boolean encontrado;
- Element t = null;
- while (it.hasNext()){
- //Creamos elemento e para devolver next cada recorrido
- Element e=(Element) it.next();
- //Anidmos al nuevo elemento el elementohijo de e
- Element nom=e.getChild("nombre");
- //nom.getText.equals(nombreplato)
- //si el texto cogido es igual al nombreplato
- if (nombreplato.equals(nom)){
- encontrado=true;
- t= e;
- }
- }
- return t;
- }
- ArrayList <Element> consultaPorCalorias(int menosCalorias){
- ArrayList consultaPorCalorias=new ArrayList <Element>();
- Element raiz=doc.getRootElement(); //CREAMOS EL ELEMENTO RAIZ
- List comidas=raiz.getChildren("comida");
- Iterator it=comidas.iterator();
- while (it.hasNext()){
- Element b=(Element) it.next();
- Element cal=b.getChild("calorias");
- if (menosCalorias>Integer.parseInt(cal.getText())){
- consultaPorCalorias.add(cal);
- }
- }
- return consultaPorCalorias;
- }
- void platosEconomicos(float precioini ,float preciofin) throws IOException, JDOMException{
- SAXBuilder builder=new SAXBuilder();
- System.out.println("Introduce nota");
- String momFich=Consola.leeString();
- Document doc2=builder.build(nomFich);
- XMLOutputter salida=new XMLOutputter();
- FileWriter fw=new FileWriter(new File("platosecon.xml"));
- Element raiz=doc2.getRootElement(); //CREAMOS EL ELEMENTO RAIZ
- List comidas=raiz.getChildren("comida");
- //ArrayliSt DE TIDOS LOS PLATOS
- ArrayList <Element> platosprec=new ArrayList <Element>();
- Iterator it=comidas.iterator();
- while (it.hasNext()){
- Element cost= (Element)it.next();
- Float prec=Float.parseFloat(cost.getChildText("precio"));
- if (prec >=precioini && prec<=preciofin){
- platosprec.add(cost);
- Element clon = cost.clone();
- clon.removeChild("precio");
- clon.removeChild("calorias");
- // el precio sera atributo de la comida , el nombre sera String y le pasamos el texto del atributo hijo
- //
- clon.setAttribute("precio", "" + clon.getChildText("precio"));
- raiz.addContent(clon);
- }
- XMLOutputter salida2=new XMLOutputter(Format.getPrettyFormat());
- salida2.output(doc2,fw);
- }
- }
- void nuevaComida(Comida com)throws JDOMException, IOException {
- SAXBuilder builder=new SAXBuilder();
- Document doc=builder.build(nomFich);
- Element raiz=doc.getRootElement();
- //al elemento raiz le iremos añadiendo sus debidas etiqtas con su repectivo valor en cada una
- Element nom=new Element("Comida");
- Element pre=new Element("Precio");
- Element des=new Element("Descripcion");
- Element cal=new Element("Calorias");
- nom.setText(com.getnombre());
- pre.setText(com.getprecio()+"");
- des.setText(com.getdescripcion());
- cal.setText(com.getcalorias()+"");
- raiz.addContent(nom);
- raiz.addContent(pre);
- raiz.addContent(des);
- raiz.addContent(cal);
- XMLOutputter salida=new XMLOutputter(Format.getPrettyFormat());
- FileWriter fx=new FileWriter(new File(nomFich),true);
- salida.output(doc,fx);
- }
- void eliminaMasCara(){
- Element raiz=doc.getRootElement();
- List comidas=raiz.getChildren("comida");
- Iterator it=comidas.iterator();
- ArrayList eliminar=new ArrayList();
- int max=-1;
- int prec;
- Element comcar=null;
- while (it.hasNext()){
- Element comida=(Element)it.next();
- prec=(int) Float.parseFloat(comida.getChild("precio").getText());
- if (prec > max){
- //si precio es mayor que maximo
- //maximo adquiere el valor de precio
- //y al elemento comcar que hemos creado le pasamos el valor de comida
- max=prec;
- comcar=comida;
- }
- raiz.removeContent(comcar);
- }
- }
- public static void main(String[] args) throws JDOMException, IOException {
- GestionMenu2 gestmen=new GestionMenu2("menu.xml");
- gestmen.parsear();
- gestmen.ver();
- Element conspla=gestmen.consultaPlato("Chicken Burger");
- if (conspla!=null){
- XMLOutputter salida=new XMLOutputter();
- salida.output(conspla, System.out);
- }
- else {
- System.out.println("No existe ese plato");
- }
- ArrayList <Element> conscal=gestmen.consultaPorCalorias(900);
- for (int i=0;i<conscal.size();i++){
- Element e=(Element)conscal.get(i); //generamos nuevo elemento del arralist
- //obteniendo el i
- XMLOutputter salida=new XMLOutputter();
- salida.output(e, System.out);
- }
- gestmen.platosEconomicos((float)2.3,(float) 9.2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment