import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class StudentInventory {
static class Student {
String name;
int id;
double gpa;
public Student(String name, int id, double gpa) {
this.name = name;
this.id = id;
this.gpa = gpa;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getGpa() {
return gpa;
}
@Override
public String toString() {
return "Name: " + name + ", ID: " + id + ", GPA: " + gpa;
}
}
static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name;
int id;
double gpa;
// Add students to the inventory
System.out.println("Enter student information (name, id, gpa):");
while (true) {
System.out.print("> ");
name = scanner.next();
if (name.equals("done")) {
break;
}
id = scanner.nextInt();
gpa = scanner.nextDouble();
students.add(new Student(name, id, gpa));
}
// Sort the students by name
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
// Print the sorted students
System.out.println("Sorted by name:");
for (Student student : students) {
System.out.println(student);
}
// Sort the students by ID
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getId() - s2.getId();
}
});
// Print the sorted students
System.out.println("Sorted by ID:");
for (Student student : students) {
System.out.println(student);
}
// Sort the students by GPA
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return Double.compare(s2.getGpa(), s1.getGpa());
}
});
// Print the sorted students
System.out.println("Sorted by GPA:");
for (Student student : students) {
System.out.println(student);
}
}
}