I have a problem, I am doing a project, it consists of entering data of a person (name, age and place (distance)) when giving the data for example:
Person 1: so-and-so, 32 years old, 34 kilometers
Person 2: Jorge, 54 years old, 44 kilometers
Person 3: esteban, 98 years, 14 kilometers
Person 4: baldo, 28 years, 98 kilometers
Person 5: Lolo, 67 years old, 70 kilometers
These people are keeping me in a queue ... and at the time of (attending) print sends them to me in order of the age from highest to lowest; it gives me as a result:
(with your data each)
Person 3
Person 5
Person 2
Person 1
Person 4
I want to print it with the condition that if the person is over 65 it is printed (attend) as a fence arriving, but if it is less than 65, it will be attended (print) first the person who has more kilometers ... like this:
(with your data each)
Person 3 (is older than 65 leaves first) 98 years
Person 5 (is older than 65 leaves after first) 67 years
Person 4 (is less than 65 but has more kilometers) 98 km
Person 2 (is less than 65 but has less km than person 4) 44 km
Person 1 (is less than 65 but has less km than person 2) 34 km
I hope you understand me: Here the condition with what I do that ,
class person
public class Persona implements Comparable {
private String nombre;//nombre
private int tipo; //edad
private int com;//kilometros
public int getCom() {
return com;
}
public void setCom(int com) {
this.com = com;
}
public Persona(String nombre, int tipo,int com) {
this.nombre = nombre;
this.tipo = tipo;
this.com=com;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getTipo() {
return tipo;
}
public void setTipo(int tipo) {
this.tipo = tipo;
}
@Override
public int compareTo(Persona o) {
if (tipo < o.getTipo()) {
return 1;
} else if (tipo > o.getTipo()) {
return -1;
} else {
return 0;
}
}
}
main class
import queuewaiturgency.Person; import java.util.PriorityQueue; import java.util.Queue; import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
Queue<Persona> cola = new PriorityQueue<Persona>();
//int de=Integer.parseInt(JOptionPane.showInputDialog("edad"));
int de=1;
while (de>0){
String fde=(JOptionPane.showInputDialog("nombre"));
de=Integer.parseInt(JOptionPane.showInputDialog("edad"));
int d=Integer.parseInt(JOptionPane.showInputDialog("distancia"));
//fde= Nombre
//de=edad
//d=kilometros
cola.add(new Persona(fde, de,d));
}
while (!cola.isEmpty()) {
Persona a = cola.remove();
System.out.println(a.getNombre()+ a.getEdad()+a.getKilometros());
}
}
}