Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gestorHotel;
- import gestorHotel.habitacion.Categoria;
- import gestorHotel.habitacion.Habitacion;
- import gestorHotel.habitacion.Tipo;
- import java.io.File;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.w3c.dom.Attr;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.*;
- import java.io.*;
- import java.util.ArrayList;
- import org.xml.sax.InputSource;
- public class Hotel implements Serializable{
- public ArrayList<Categoria> categorias=new ArrayList<>();
- public static ArrayList<Hotel> hoteles=new ArrayList<>();
- public ArrayList<Tipo> tipos=new ArrayList<>();
- public ArrayList<Habitacion> habitaciones=new ArrayList<>();
- private String nombre;
- /*
- * Necesito para un correcto funcionamiento que el constructor de la clase Hotel tenga esto
- * 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,
- * es necesario que existan tales método en esta clase y no en las otras
- */
- public Hotel(String nombre){
- boolean existe=false;
- this.nombre=nombre;
- for(int i=0;i<hoteles.size();i++){
- if(hoteles.get(i).getNombre().equals(this.nombre)){
- existe=true;
- break;
- }
- }
- if(existe==false){
- hoteles.add(this);
- this.save();
- }
- }
- /**
- * Se guarda en el array y se serializa
- * @param nombre Cambia el nombre al hotel
- */
- public void setNombre(String nombre){
- this.nombre=nombre;
- this.save();
- }
- public String getNombre(){
- return nombre;
- }
- /**
- * Le agrega una habitación al hotel
- * @param numero Número de la habitación
- * @param categoria Tipo de categoría que va a tener la habitación
- * @param tipo El tipo de habitación
- */
- public void setHabitacion(int numero, String categoria, String tipo){
- Categoria c=this.getCategoria(categoria);
- Tipo t=this.getTipo(tipo);
- Habitacion h=new Habitacion(numero,c,t);
- boolean existe=false;
- for(int i=0;i<habitaciones.size();i++){
- if(h.getNumero()==habitaciones.get(i).getNumero()){
- existe=true;
- break;
- }
- }
- if(existe==false){
- habitaciones.add(h);
- h.save(this.getNombre());
- this.save();
- }
- }
- public int numeroHabitaciones(){
- return this.habitaciones.size()-1;
- }
- /**
- *
- * @param numero Número de la habitación a consultar
- * @return Retorna un objeto habitación que tiene como número igual al parámetro
- */
- public Habitacion getHabitacion(int numero){
- for(int i=0;i<this.habitaciones.size();i++){
- if(habitaciones.get(i).getNumero()==numero){
- return habitaciones.get(i);
- }
- }
- return null;
- }
- /**
- * Le agrega una categoría a un hotel
- * @param nombreCategoria
- * @param precio
- * @param espacio
- */
- public void setCategoria(String nombreCategoria, float precio, float espacio){
- Categoria c=new Categoria(nombreCategoria,precio,espacio);
- boolean existe=false;
- for(int i=0;i<categorias.size();i++){
- if(c.getSuplemento()==categorias.get(i).getSuplemento() || c.getEspacio()==categorias.get(i).getEspacio() ||categorias.get(i).getNombre().equals(c.getNombre())){
- existe=true;
- break;
- }
- }
- if(existe==false){
- this.categorias.add(c);
- c.save(this.nombre);
- this.save();
- }
- }
- public Categoria getCategoria(String nombreCategoria){
- for(int i=0;i<hoteles.size();i++){
- if(hoteles.get(i).getNombre().equals(this.nombre)){
- for(int j=0;j<hoteles.get(i).categorias.size();j++){
- if(hoteles.get(i).categorias.get(j).getNombre().equals(nombreCategoria)){
- return hoteles.get(i).categorias.get(j);
- }
- }
- break;
- }
- }
- return null;
- }
- public void setTipo(String nombre, float precio){
- Tipo t=new Tipo(nombre,precio);
- boolean existe=false;
- for(int i=0;i<tipos.size();i++){
- if(t.getPrecio()==tipos.get(i).getPrecio() || t.getNombre().equals(tipos.get(i).getNombre())){
- existe=true;
- break;
- }
- }
- if(existe==false){
- this.tipos.add(t);
- t.save(this);
- this.save();
- }
- }
- public Tipo getTipo(String nombreTipo){
- for(int i=0;i<hoteles.size();i++){
- if(hoteles.get(i).getNombre().equals(this.nombre)){
- for(int j=0;j<hoteles.get(i).tipos.size();j++){
- if(hoteles.get(i).tipos.get(j).getNombre().equals(nombreTipo)){
- return hoteles.get(i).tipos.get(j);
- }
- }
- }
- }
- return null;
- }
- /**
- * Guarda el array con todos los datos y se serializa
- */
- public void save(){
- try{
- FileOutputStream fos=new FileOutputStream("fichero.xml");
- ObjectOutputStream out=new ObjectOutputStream(fos);
- out.writeObject(hoteles);
- }catch(Exception e){
- //System.out.println(e.getMessage());
- }
- }
- /**
- * Carga el array (Se debe hacer al inciar la aplicación)
- */
- public static void cargar(){
- try{
- FileInputStream fis=new FileInputStream("fichero.xml");
- ObjectInputStream in=new ObjectInputStream(fis);
- hoteles=(ArrayList<Hotel>)in.readObject();
- System.out.println(hoteles.get(0).habitaciones.get(1).getNumero());
- }catch(Exception e){
- System.out.println(e.getMessage());
- }
- }
- /**
- *
- * @param nombreHotel El nombre del hotel
- * @return Un objeto de tipo hotel que tiene el nombre del parámetro
- */
- public static Hotel getHotel(String nombreHotel){
- for(int i=0;i<hoteles.size();i++){
- if(hoteles.get(i).nombre.equals(nombreHotel))
- return hoteles.get(i);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment