From 4f86e8cf76f942ed8148c737c6c0074051d83cc2 Mon Sep 17 00:00:00 2001 From: kxtzzl <123520267+romchhh@users.noreply.github.com> Date: Mon, 3 Apr 2023 10:56:11 +0300 Subject: [PATCH] Update lab4 --- lab4 | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lab4 b/lab4 index 10bbbc2..f934e79 100644 --- a/lab4 +++ b/lab4 @@ -2,7 +2,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; -public class lab4 { +public class Lab4 { public static class Student { private int grBook; @@ -58,7 +58,7 @@ public class lab4 { Scanner scanner = new Scanner(System.in); boolean quit = false; 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: "); String input = scanner.nextLine(); @@ -76,13 +76,11 @@ public class lab4 { System.out.print("Enter the sort order (asc or desc): "); String order = scanner.nextLine(); - // Check if the sort order input is valid if (!order.equals("asc") && !order.equals("desc")) { System.out.println("Invalid sort order! Please enter 'asc' or 'desc'."); continue; } - - // Sort the students array + Comparator comparator = null; switch (field) { case "greed book": @@ -105,8 +103,7 @@ public class lab4 { comparator = comparator.reversed(); } Arrays.sort(students, comparator); - - // Print the sorted students array + 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("----------------------------------------------------------------");