Autor Tema: Interface api java Set SortedSet clase HashSet TreeSet Ejercic Resuelto CU00924C  (Leído 1674 veces)

@mayakoski

  • Sin experiencia
  • *
  • Mensajes: 43
    • Ver Perfil
Buenas tardes, dejo planteada mi solución al ejercicio CU00924C del tutorial de programación java avanzado, espero algún comentario sobre el mismo

Código: [Seleccionar]
package claseCU00924C;

import java.util.Objects;

public class Hotel implements Comparable<Hotel> {
private int idHotel;
private String zona;
private int precio;

public Hotel(int pIdHotel, String pZona, int pPrecio) {
this.idHotel = pIdHotel;
this.zona = pZona;
this.precio = pPrecio;
}


public String getZona() {
return zona;
}


@Override
public int compareTo(Hotel o) {
return this.precio-o.precio;
}

@Override
public String toString() {
return "Hotel-> ID: "+idHotel+" Zona: "+zona+" Precio: "+precio+"\n";
}

@Override
public int hashCode() { return precio + zona.hashCode() + idHotel; }


@Override
    public boolean equals(Object obj) {
        if (obj == null) {   return false;  }
        if (getClass() != obj.getClass()) {  return false;   }
        final Hotel other = (Hotel) obj;
        if (this.idHotel != other.idHotel) {  return false;    }
        if (!Objects.equals(this.zona, other.zona)) {    return false;     }
        if (this.precio != other.precio) {     return false;   }
        return true;
    }

}


Código: [Seleccionar]
package claseCU00924C;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Programa2 {
public static void main(String[] args) {
Set<Hotel> cjsh = new HashSet<Hotel>();
SortedSet<Hotel> cjssh = new TreeSet<Hotel>();
Hotel h;
Scanner entradaTeclado = new Scanner(System.in);
int eleccion;
String zonaEleccion;

h = new Hotel(14, "Rural", 146);
cjsh.add(h);
h = new Hotel(5,"Rural", 92);
cjsh.add(h);
h = new Hotel(7,"Montaña", 63);
cjsh.add(h);
h = new Hotel(1,"Playa", 77);
cjsh.add(h);
h = new Hotel(3,"Playa", 109);
cjsh.add(h);
h = new Hotel(10,"Montaña", 79);
cjsh.add(h);
h = new Hotel(4,"Rural", 43);
cjsh.add(h);
h = new Hotel(6,"Montaña", 142);
cjsh.add(h);
h = new Hotel(11,"Playa", 53);
cjsh.add(h);
h = new Hotel(2,"Montaña", 108);
cjsh.add(h);
h = new Hotel(0,"Rural", 135);
cjsh.add(h);
h = new Hotel(8,"Rural", 66);
cjsh.add(h);
h = new Hotel(9,"Rural", 65);
cjsh.add(h);

System.out.println("Hoteles en el conjunto: \n");
System.out.println(cjsh + "\n");

System.out.print("Elige zona de hotel. (1) Playa (2) Montaña (3) Rural \n" + "\nEleccion: ");
eleccion = entradaTeclado.nextInt();

zonaEleccion = (eleccion == 1) ? "Playa" :(eleccion == 2) ? "Montaña" : (eleccion == 3) ? "Rural" : "Elección no valida";

System.out.println("\nHoteles en el conjunto para la selección '" + zonaEleccion + "' ordenados por precio:\n");

Hotel temp = null;
Iterator<Hotel> it = cjsh.iterator();

while (it.hasNext()) {
temp = it.next();
if(temp.getZona().equalsIgnoreCase(zonaEleccion)) {
cjssh.add(temp);
}
}

if(cjssh.size() > 0) {
System.out.println(cjssh);
}
}
}
« Última modificación: 14 de Octubre 2020, 18:11 por Ogramar »

Ogramar

  • Moderador Global
  • Experto
  • *******
  • Mensajes: 2660
    • Ver Perfil
Buenas, el ejercicio está correctamente resuelto y cumple con lo que se pedía. Si se quiere, se puede hacer que los datos de los hoteles se generen aleatoriamente como se hace en la solución dada en https://aprenderaprogramar.com/foros/index.php?topic=5897.0

Salu2

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".