[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/subham500071430/JavaProgramming/patch-2/HackerRank/Eighteen.java [Back]  [Original]

package HackerRank;
import java.util.*;

class Student {
    private int id;
    private String fname;
    private double cgpa;

    public Student(int id, String fname, double cgpa) {
        super();
        this.id = id;
        this.fname = fname;
        this.cgpa = cgpa;
    }

    public int getId() {
        return id;
    }

    public String getFname() {
        return fname;
    }

    public double getCgpa() {
        return cgpa;
    }
}

// Comparator class to sort according to the problem statement
class SortByCGPA implements Comparator {
    public int compare(Student a, Student b) {
        if (a.getCgpa() != b.getCgpa()) {
            return Double.compare(b.getCgpa(), a.getCgpa()); // Descending CGPA
        } else if (!a.getFname().equals(b.getFname())) {
            return a.getFname().compareTo(b.getFname());     // Ascending Name
        } else {
            return a.getId() - b.getId();                    // Ascending ID
        }
    }
}

public class Eighteen {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());

        List studentList = new ArrayList();

        while (testCases > 0) {
            int id = in.nextInt();
            String fname = in.next();
            double cgpa = in.nextDouble();

            Student st = new Student(id, fname, cgpa);
            studentList.add(st);

            testCases--;
        }

        Collections.sort(studentList, new SortByCGPA());

        for (Student st : studentList) {
            System.out.println(st.getFname());
        }
    }
}

Web Proxy Viewer  |  New URL  |  Original Page