Java Priority Queue

  • + 0 comments

    class Student implements Comparable{

     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;
     }
    
     @Override
     public int compareTo(Student s){
         if(cgpa==s.cgpa){
                if(name.compareTo(s.name)!=0){
                    return name.compareTo(s.name);
                }else if(id>s.id){
                    return 1;
                }else{
                    return -1;
                }
            }else if(cgpa<s.cgpa){
                return 1;
            }else{
                return -1;
            }
     }
    

    }

    class Priorities{

     private PriorityQueue<Student> pq;
     public Priorities(){
         pq = new PriorityQueue<>();
     }
    
     public List<Student> getStudents(List<String> events){
         List<Student> result = new ArrayList<>();
         for(String event : events){
             String[] s = event.split(" ");
             String op = s[0];
    
             if(op.equals("ENTER")){
                 Student stu = new Student(Integer.parseInt(s[3]),s[1],Double.parseDouble(s[2]));
                 pq.add(stu);
             }else if(op.equals("SERVED")){
                 pq.poll();
             }
         }
         Iterator iter = pq.iterator();
         while(iter.hasNext()){
             result.add(pq.poll());
         }
    
             return result;
         }
     }
    
    
    
         return result;
     }
    

    }