priority queue java

0

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());
    }

}

}

    
asked by Emmanuel Mancillas 04.11.2017 в 06:36
source

1 answer

0

I think the problem you have is that you incorrectly implement the compareTo. In the comparison, there is only the condition that makes it order from highest to lowest, depending on age. But the condition indicated by mileage is not included anywhere.

My solution:

public class Persona implements Comparable<Persona> {

private String nombre;// nombre
private int tipo; // edad
private int com;// kilometros

public Persona(String nombre, int tipo, int com) {
    this.nombre = nombre;
    this.tipo = tipo;
    this.com = com;
}

public int getCom() {
    return com;
}

public void setCom(int com) {
    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) {
    Integer miTipo = Integer.valueOf(this.tipo);
    Integer otroTipo = Integer.valueOf(o.getTipo());

    return miTipo < 65 && otroTipo < 65 ? Integer.valueOf(o.getCom()).compareTo(this.com) : otroTipo.compareTo(mTipo);

}

}

Here I have implemented the condition that, if the two people are under 65, the comparison is made by mileage. In another case, it is compared by age.

Main test:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
public static void main(String[] args) {
    Persona p1 = new Persona("p1", 32, 34);
    Persona p2 = new Persona("p2", 54, 44);
    Persona p3 = new Persona("p3", 98, 14);
    Persona p4 = new Persona("p4", 28, 98);
    Persona p5 = new Persona("p5", 67, 70);

    List<Persona> lista = new ArrayList<Persona>();

    lista.add(p1);
    lista.add(p2);
    lista.add(p3);
    lista.add(p4);
    lista.add(p5);

    Collections.sort(lista);

    for (Persona persona : lista) {
        System.out.println("La persona tomada es: " + persona.getNombre());
    }

    System.exit(0);
}

}

My results:
The person taken is: p3
The person taken is: p5
The person taken is: p4
The person taken is: p2
The person taken is: p1

Clarification: My test was based on checking that the comparator works correctly. I used another main because I found the test simpler when not doing optionpane or anything like that.

    
answered by 06.11.2017 / 03:26
source