Java Priority Queue

  • + 0 comments

    public class Solution {

    private final static Scanner sc = new Scanner(System.in);
    private final static Priorities priorities = new Priorities();
    
    public static void main(String[] args) {
    
       int totalEventos = Integer.parseInt(sc.nextLine().trim());
       List<String> events = new ArrayList<>();
    
       for (int i=0; i < totalEventos; i++){
        events.add(sc.nextLine().trim());
       }
    
        List<Student> students = priorities.getStudents(events);
    
        if (students.isEmpty()) {
                System.out.println("EMPTY");
        } else {
            for (Student s : students) {
                System.out.println(s.getName());
            }
        }
       sc.close();
    }
    
    static class Student {
        private int id;
        private String name;
        private double CGPA;
    
        public Student(int id, String name, double CGPA) {
                this.id = id;
                this.name = name;
                this.CGPA = CGPA;
        }
    
        public int getID() {
                return id;
        }
        public String getName(){
                return name;
        }
        public double getCGPA(){
                return CGPA;
        }
    }
    
    static class Priorities {
    
        public List<Student> getStudents(List<String> events){
            PriorityQueue<Student> fila = new PriorityQueue<>(
                    Comparator.comparing(Student::getCGPA).reversed()
                    .thenComparing(Student::getName)
                    .thenComparing(Student::getID)
            );
    
            for(String enter : events) {
                String[] result = enter.split(" ");
    
                if(result[0].equalsIgnoreCase("ENTER")){
                        fila.add(new Student(Integer.parseInt(result[3]), 
                        result[1], 
                        Double.parseDouble(result[2])
                        ));
                } else if (result[0].equals("SERVED")) {
                        fila.poll();
                }
            }
    
            List<Student> restantes = new ArrayList<>();
            while(!fila.isEmpty()) {
                    restantes.add(fila.poll());
            }
            return restantes;
        }
    
    }
    

    }