Advertisement
daniel199410

Untitled

May 24th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. package gestorHotel;
  2. import gestorHotel.habitacion.Categoria;
  3. import gestorHotel.habitacion.Habitacion;
  4. import gestorHotel.habitacion.Tipo;
  5. import java.io.File;
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.parsers.ParserConfigurationException;
  9. import javax.xml.transform.Transformer;
  10. import javax.xml.transform.TransformerException;
  11. import javax.xml.transform.TransformerFactory;
  12. import javax.xml.transform.dom.DOMSource;
  13. import javax.xml.transform.stream.StreamResult;
  14. import org.w3c.dom.Attr;
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.Element;
  17. import org.w3c.dom.*;
  18. import java.io.*;
  19. import java.util.ArrayList;
  20. import org.xml.sax.InputSource;
  21.  
  22. public class Hotel implements Serializable{
  23.     public ArrayList<Categoria> categorias=new ArrayList<>();
  24.     public static ArrayList<Hotel> hoteles=new ArrayList<>();
  25.     public ArrayList<Tipo> tipos=new ArrayList<>();
  26.     public ArrayList<Habitacion> habitaciones=new ArrayList<>();
  27.    
  28.     private String nombre;
  29.    
  30.     /*
  31.      * Necesito para un correcto funcionamiento que el constructor de la clase Hotel tenga esto
  32.      * Además necesito que agregue esos métdos a la clase Hotel, ya que como el objeto al que se le agregan las habitaciones, categorías y tipos es de tipo Hotel,
  33.      * es necesario que existan tales método en esta clase y no en las otras
  34.      */
  35.     public Hotel(String nombre){
  36.         boolean existe=false;
  37.         this.nombre=nombre;
  38.         for(int i=0;i<hoteles.size();i++){
  39.             if(hoteles.get(i).getNombre().equals(this.nombre)){
  40.                 existe=true;
  41.                 break;
  42.             }
  43.         }
  44.         if(existe==false){
  45.             hoteles.add(this);
  46.             this.save();
  47.         }      
  48.     }
  49.    
  50.     /**
  51.      * Se guarda en el array y se serializa
  52.      * @param nombre Cambia el nombre al hotel
  53.      */
  54.     public void setNombre(String nombre){
  55.         this.nombre=nombre;
  56.         this.save();
  57.     }
  58.    
  59.     public String getNombre(){
  60.         return nombre;
  61.     }
  62.    
  63.     /**
  64.      * Le agrega una habitación al hotel
  65.      * @param numero Número de la habitación
  66.      * @param categoria Tipo de categoría que va a tener la habitación
  67.      * @param tipo El tipo de habitación
  68.      */
  69.     public void setHabitacion(int numero, String categoria, String tipo){
  70.         Categoria c=this.getCategoria(categoria);
  71.         Tipo t=this.getTipo(tipo);
  72.         Habitacion h=new Habitacion(numero,c,t);
  73.         boolean existe=false;
  74.         for(int i=0;i<habitaciones.size();i++){
  75.             if(h.getNumero()==habitaciones.get(i).getNumero()){
  76.                 existe=true;
  77.                 break;
  78.             }
  79.         }
  80.         if(existe==false){
  81.             habitaciones.add(h);
  82.             h.save(this.getNombre());
  83.             this.save();
  84.         }
  85.     }
  86.    
  87.     public int numeroHabitaciones(){
  88.         return this.habitaciones.size()-1;
  89.     }
  90.    
  91.     /**
  92.      *
  93.      * @param numero Número de la habitación a consultar
  94.      * @return Retorna un objeto habitación que tiene como número igual al parámetro
  95.      */
  96.     public Habitacion getHabitacion(int numero){
  97.         for(int i=0;i<this.habitaciones.size();i++){
  98.             if(habitaciones.get(i).getNumero()==numero){
  99.                 return habitaciones.get(i);
  100.             }
  101.         }
  102.         return null;
  103.     }
  104.     /**
  105.      * Le agrega una categoría a un hotel
  106.      * @param nombreCategoria
  107.      * @param precio
  108.      * @param espacio
  109.      */
  110.     public void setCategoria(String nombreCategoria, float precio, float espacio){
  111.         Categoria c=new Categoria(nombreCategoria,precio,espacio);
  112.         boolean existe=false;
  113.         for(int i=0;i<categorias.size();i++){
  114.             if(c.getSuplemento()==categorias.get(i).getSuplemento() || c.getEspacio()==categorias.get(i).getEspacio() ||categorias.get(i).getNombre().equals(c.getNombre())){
  115.                 existe=true;
  116.                 break;
  117.             }
  118.         }
  119.         if(existe==false){
  120.             this.categorias.add(c);
  121.             c.save(this.nombre);
  122.             this.save();
  123.         }
  124.     }
  125.    
  126.     public Categoria getCategoria(String nombreCategoria){
  127.         for(int i=0;i<hoteles.size();i++){
  128.            if(hoteles.get(i).getNombre().equals(this.nombre)){
  129.                for(int j=0;j<hoteles.get(i).categorias.size();j++){
  130.                    if(hoteles.get(i).categorias.get(j).getNombre().equals(nombreCategoria)){
  131.                        return hoteles.get(i).categorias.get(j);
  132.                    }
  133.                }
  134.                break;
  135.            }
  136.         }
  137.         return null;
  138.     }
  139.    
  140.     public void setTipo(String nombre, float precio){
  141.         Tipo t=new Tipo(nombre,precio);
  142.         boolean existe=false;
  143.         for(int i=0;i<tipos.size();i++){
  144.             if(t.getPrecio()==tipos.get(i).getPrecio() || t.getNombre().equals(tipos.get(i).getNombre())){
  145.                 existe=true;
  146.                 break;
  147.             }
  148.         }
  149.         if(existe==false){
  150.             this.tipos.add(t);
  151.             t.save(this);
  152.             this.save();
  153.         }
  154.     }
  155.    
  156.     public Tipo getTipo(String nombreTipo){
  157.         for(int i=0;i<hoteles.size();i++){
  158.             if(hoteles.get(i).getNombre().equals(this.nombre)){
  159.                 for(int j=0;j<hoteles.get(i).tipos.size();j++){
  160.                     if(hoteles.get(i).tipos.get(j).getNombre().equals(nombreTipo)){
  161.                         return hoteles.get(i).tipos.get(j);
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.         return null;
  167.     }
  168.     /**
  169.      * Guarda el array con todos los datos y se serializa
  170.      */
  171.     public void save(){
  172.         try{
  173.             FileOutputStream fos=new FileOutputStream("fichero.xml");
  174.             ObjectOutputStream out=new ObjectOutputStream(fos);
  175.             out.writeObject(hoteles);
  176.         }catch(Exception e){
  177.             //System.out.println(e.getMessage());
  178.         }
  179.     }
  180.    
  181.     /**
  182.      * Carga el array (Se debe hacer al inciar la aplicación)
  183.      */
  184.     public static void cargar(){
  185.         try{
  186.            FileInputStream fis=new FileInputStream("fichero.xml");
  187.            ObjectInputStream in=new ObjectInputStream(fis);
  188.            hoteles=(ArrayList<Hotel>)in.readObject();
  189.            System.out.println(hoteles.get(0).habitaciones.get(1).getNumero());
  190.         }catch(Exception e){
  191.            System.out.println(e.getMessage());          
  192.         }
  193.     }
  194.     /**
  195.      *
  196.      * @param nombreHotel El nombre del hotel
  197.      * @return Un objeto de tipo hotel que tiene el nombre del parámetro
  198.      */
  199.     public static Hotel getHotel(String nombreHotel){
  200.         for(int i=0;i<hoteles.size();i++){
  201.             if(hoteles.get(i).nombre.equals(nombreHotel))
  202.                 return hoteles.get(i);
  203.         }
  204.         return null;
  205.     }      
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement