-
-
Save alvareztech/5373b9518225d6824353f410e92aabc2 to your computer and use it in GitHub Desktop.
Java: Ejemplo de una cola de prioridad de personas y uso de contantes en Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Constantes { | |
public static final int PERSONA_NORMAL = 1; | |
public static final int PERSONA_DISCAPACITADA = 2; | |
public static final int PERSONA_EMBARAZADA = 3; | |
public static final int PERSONA_ADULTO_MAYOR = 4; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
import java.util.LinkedList; | |
import java.util.PriorityQueue; | |
import java.util.Queue; | |
public class Main { | |
public static void main(String[] args) { | |
Persona p1 = new Persona("AAA", 23, Constantes.PERSONA_EMBARAZADA); | |
Persona p2 = new Persona("BBB", 43, Constantes.PERSONA_ADULTO_MAYOR); | |
Persona p3 = new Persona("CCC", 12, Constantes.PERSONA_NORMAL); | |
Persona p4 = new Persona("DDD", 87, Constantes.PERSONA_NORMAL); | |
Persona p5 = new Persona("EEE", 31, Constantes.PERSONA_DISCAPACITADA); | |
Persona p6 = new Persona("FFF", 10, Constantes.PERSONA_EMBARAZADA); | |
Persona p7 = new Persona("GGG", 55, Constantes.PERSONA_NORMAL); | |
Queue<Persona> cola = new PriorityQueue<Persona>(new Comparator<Persona>() { | |
@Override | |
public int compare(Persona o1, Persona o2) { | |
if (o1.getEdad() > o2.getEdad()) { | |
return -1; | |
} | |
if (o1.getEdad() < o2.getEdad()) { | |
return 1; | |
} | |
return 0; | |
} | |
}); | |
cola.add(p1); | |
cola.add(p2); | |
cola.add(p3); | |
cola.add(p4); | |
cola.add(p5); | |
cola.add(p6); | |
cola.add(p7); | |
mostrar(cola); | |
} | |
private static void mostrar(Queue<Persona> cola) { | |
Queue<Persona> aux = new LinkedList<Persona>(); | |
int n = cola.size(); | |
for (int i = 0; i < n; i++) { | |
Persona p = cola.remove(); | |
System.out.println(p.getNombre() + " " + p.getEdad()); | |
aux.add(p); | |
} | |
int m = aux.size(); | |
for (int i = 0; i < m; i++) { | |
Persona p = aux.remove(); | |
cola.add(p); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Persona { | |
private String nombre; | |
private int edad; | |
private int tipo; | |
public Persona(String nombre, int edad, int tipo) { | |
this.nombre = nombre; | |
this.edad = edad; | |
this.tipo = tipo; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public int getEdad() { | |
return edad; | |
} | |
public void setEdad(int edad) { | |
this.edad = edad; | |
} | |
public int getTipo() { | |
return tipo; | |
} | |
public void setTipo(int tipo) { | |
this.tipo = tipo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment