diff --git a/Examination_queue.java b/Examination_queue.java new file mode 100644 index 00000000..62117b72 --- /dev/null +++ b/Examination_queue.java @@ -0,0 +1,104 @@ +import java.util.ArrayList; +import java.util.PriorityQueue; +import java.util.Scanner; +import java.util.List; +import java.util.Comparator; + +public class Examination_queue { // Main class + + private final static Scanner scan = new Scanner(System.in); + private final static Priorities priorities = new Priorities(); + + public static void main(String[] args) { + + System.out.println("\n Enter number of entries"); + int totalEntries = Integer.parseInt(scan.nextLine()); // To get number of entries in the queue + + System.out.println("\n Input entries"); + System.out.println("\n Name | GPA | Index No"); + + List entries = new ArrayList<>(); // Assign a array list as 'entries' + + for (int i = totalEntries-1; i >= 0; i -= 1) { + String name = scan.nextLine(); + entries.add(name); // Add entered names to the array list + } + + List result_set = priorities.getResult(entries); + System.out.println("\n --Result Sheet--\n"); + + if (result_set.isEmpty()) { // Check if the queue is empty + System.out.println("No entries in the list"); + } + + else { + System.out.println("Index No | GPA"); + for (Examination sheet:result_set) { // iterate through each entry + System.out.println(sheet.getId()+" | "+sheet.getGpa()); // Print the index number and GPA of each candidate + } + } + } +} + +class Examination { // Class to get inputs + private int id; + private float gpa; + + + public Examination(int id, float gpa) { + super(); // To call parent class constructor + this.id = id; + this.gpa = gpa; + } + + public int getId() { // To return the index no + return id; + } + + + public float getGpa() { // To return GPA + return gpa; + } +} + + +class Priorities { // Class to assign priorities + + // Implement a method to process all the inputted entries and return the entries yet to be served in the priority order + public List < Examination > getResult(List < String > entries) { + + // Assign a queue as 'result_queue' + PriorityQueue < Examination > result_queue = new PriorityQueue(Comparator.comparing(Examination::getGpa).reversed().thenComparing(Examination::getId)); + // The students having highest GPA is served first. + // If any students having same GPA, served by index number ascending order. + + List < Examination > result_set = new ArrayList < Examination > (); // Assign a list 'result_set' to return entries at the end + + for (String e: entries) { // loop through the 'e' entries in list 'entries' + Scanner sc =new Scanner(e); // To get total number of entries in the list + + String name = sc.next(); + float gpa = sc.nextFloat(); + int id = sc.nextInt(); + + Examination examination = new Examination(id, gpa); + result_queue.add(examination); // add 'id' and 'gpa' values to the queue + + } + + Examination first = result_queue.poll(); // To remove and return entries one by one from the queue + if (first == null) { // If no more entries in the list return the result set + return result_set; + } + else { + while (first != null) { // If there is another entry in the list,, + + result_set.add(first); // add it to the priority list,, + first = result_queue.poll(); // Remove and return added entry from the queue + } + return result_set; + } + + } +} +