Untitled

 avatar
unknown
plain_text
9 months ago
2.9 kB
13
Indexable
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    Student studentData = new Student();

    studentData.InputData(sc);
while(true){
    System.out.print("\nDisplay all or search (1-All | 2-Search): ");
    int choice = sc.nextInt();

    switch(choice){
        case 1:
        studentData.displayALL();
        break;
        case 2:
        studentData.searchStudent(sc);
        break;
        default:
        System.out.println("\nInvalid Choice!");
    }
    System.out.print("do you want to continue? y/n:");
    char choose = sc.next().charAt(0);
    switch(choose){
        case 'y':
        case 'Y':
        continue;
        case 'n':
        case 'N':
        break;
        default:
        System.out.println("Invalid choice!");
    }
    }
}
static class Student{
    String[] names;
    int[] grades;
    int input;

   public void InputData(Scanner sc){
    System.out.print("How many Students to enter?: ");
    input = sc.nextInt();
    sc.nextLine();

    names = new String[input];
    grades = new int[input];

    System.out.println("Enter the name of the "+input+" students: ");
    for(int i = 0; i < input; i++){
        names[i] = sc.nextLine();
    }

    System.out.println("\nEnter the grades of the "+input+" students: ");
    for(int i = 0; i < input; i++){
        grades[i] = sc.nextInt();
    }
}
    void sortStudents() {
        for(int i = 0; i < input - 1; i++){
            for(int j = i + 1; j < input; j++){
                if(names[i].compareToIgnoreCase(names[j]) > 0){

                    String tempName = names[i];
                    names[i] = names[j];
                    names[j] = tempName;

                    int tempGrade = grades[i];
                    grades[i] = grades[j];
                    grades[j] = tempGrade;
                }
            }
        }
    }
    void displayALL() {
        sortStudents();
        System.out.println("\nStudent's Grades:");
        for(int i = 0; i < input; i++){
            System.out.println(names[i]+" - "+grades[i]);
        }
    }
    void searchStudent(Scanner sc){
        sc.nextLine();
        System.out.print("\nEnter a name to search: ");
        String search = sc.nextLine();

        boolean found = false;
        for(int i = 0; i < input; i++){
            if(names[i].equalsIgnoreCase(search)){
                System.out.println("Search Result");
                System.out.println("Name: "+ names[i]);
                System.out.println("Grade: "+ grades[i]);

                found = true;
                break;
            }
        }
        if(!found){
            System.out.println("\nStudent not found!");
        }
    }
}
}
Editor is loading...
Leave a Comment