Update lab4
This commit is contained in:
parent
9493c4974d
commit
4f86e8cf76
11
lab4
11
lab4
|
@ -2,7 +2,7 @@ import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class lab4 {
|
public class Lab4 {
|
||||||
|
|
||||||
public static class Student {
|
public static class Student {
|
||||||
private int grBook;
|
private int grBook;
|
||||||
|
@ -58,7 +58,7 @@ public class lab4 {
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
boolean quit = false;
|
boolean quit = false;
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
// Ask user which field and order to sort by
|
|
||||||
System.out.print("\nEnter the field to sort by (greed book, name, age, gpa, faculty) or enter 'quit' to exit: ");
|
System.out.print("\nEnter the field to sort by (greed book, name, age, gpa, faculty) or enter 'quit' to exit: ");
|
||||||
String input = scanner.nextLine();
|
String input = scanner.nextLine();
|
||||||
|
|
||||||
|
@ -76,13 +76,11 @@ public class lab4 {
|
||||||
System.out.print("Enter the sort order (asc or desc): ");
|
System.out.print("Enter the sort order (asc or desc): ");
|
||||||
String order = scanner.nextLine();
|
String order = scanner.nextLine();
|
||||||
|
|
||||||
// Check if the sort order input is valid
|
|
||||||
if (!order.equals("asc") && !order.equals("desc")) {
|
if (!order.equals("asc") && !order.equals("desc")) {
|
||||||
System.out.println("Invalid sort order! Please enter 'asc' or 'desc'.");
|
System.out.println("Invalid sort order! Please enter 'asc' or 'desc'.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the students array
|
|
||||||
Comparator<Student> comparator = null;
|
Comparator<Student> comparator = null;
|
||||||
switch (field) {
|
switch (field) {
|
||||||
case "greed book":
|
case "greed book":
|
||||||
|
@ -105,8 +103,7 @@ public class lab4 {
|
||||||
comparator = comparator.reversed();
|
comparator = comparator.reversed();
|
||||||
}
|
}
|
||||||
Arrays.sort(students, comparator);
|
Arrays.sort(students, comparator);
|
||||||
|
|
||||||
// Print the sorted students array
|
|
||||||
System.out.println("\nSorted by " + field + " (in " + order + "ending order):");
|
System.out.println("\nSorted by " + field + " (in " + order + "ending order):");
|
||||||
System.out.println(String.format("%n%-15s | %-20s | %-4s | %-4s | %-10s", "Greed book", "Name", "Age", "GPA", " Faculty"));
|
System.out.println(String.format("%n%-15s | %-20s | %-4s | %-4s | %-10s", "Greed book", "Name", "Age", "GPA", " Faculty"));
|
||||||
System.out.println("----------------------------------------------------------------");
|
System.out.println("----------------------------------------------------------------");
|
||||||
|
|
Loading…
Reference in New Issue