You are viewing a single comment's thread. Return to all comments →
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.PriorityQueue;
/* * Create the Student and Priorities classes here. */
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 void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getCgpa() { return cgpa; } public void setCgpa(double cgpa) { this.cgpa = cgpa; } public int compareTo(Student o){ if (o.cgpa==this.cgpa && o.name==this.name){ return Integer.compare(o.id,this.id); }else if (o.cgpa==this.cgpa){ return this.name.compareTo(o.name); }else { return Double.compare( o.cgpa,this.cgpa); } }
}
class Priorities {
public List<Student> getStudents(List<String> events) { PriorityQueue <Student> queue = new PriorityQueue<>(); for (String event: events) { Scanner scanner= new Scanner(event); String command= scanner.next(); switch (command){ case "ENTER": String name=scanner.next(); Double cgpa=scanner.nextDouble(); int id=scanner.nextInt(); Student student1= new Student(id,name,cgpa); queue.add(student1); break; case "SERVED": queue.poll(); break; } } List<Student> studentList = new ArrayList<>(); while (!queue.isEmpty()){ studentList.add(queue.poll()); } return studentList; }
public class Solution { private final static Scanner scan = new Scanner(System.in); private final static Priorities priorities = new Priorities();
public static void main(String[] args) { int totalEvents = Integer.parseInt(scan.nextLine()); List<String> events = new ArrayList<>(); while (totalEvents-- != 0) { String event = scan.nextLine(); events.add(event); } List<Student> students = priorities.getStudents(events); if (students.isEmpty()) { System.out.println("EMPTY"); } else { for (Student st: students) { System.out.println(st.getName()); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Priority Queue
You are viewing a single comment's thread. Return to all comments →
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.PriorityQueue;
/* * Create the Student and Priorities classes here. */
class Student implements Comparable{ private int id; private String name; private double cgpa;
}
class Priorities {
}
public class Solution { private final static Scanner scan = new Scanner(System.in); private final static Priorities priorities = new Priorities();
}