/*  
Name: Jason Cameron  
Date: 2024-07-14  
Purpose of program: To create a gradebook analyzer that allows users to input student names and grades, and then displays various statistics and information about the students and their grades.  
 */  
  
import java.util.Scanner;  
  
public class GradebookAnalyzer {  
    private static Student[] students = new Student[10];  
    static final float CANADIAN_AVERAGE = 77.0F;  
  
    public static void main(String[] args) {  
        Scanner scanner = new Scanner(System.in);  
        addStudents();  
  
        System.out.println("Welcome to the Gradebook Analyzer");  
  
        System.out.println("Would you like to add any more than the default students? (Y/N)");  
        String addMoreStudents = scanner.nextLine().strip();  
        if (addMoreStudents.equalsIgnoreCase("Y")) {  
            System.out.println("How many students would you like to add?");  
            int numStudents = scanner.nextInt();  
            if (numStudents > 5) {  
                System.out.println("You can only add up to 5 students in addition to the default students. Exiting...");  
                System.exit(0);  
            }  
            scanner.nextLine(); // consume the newline character  
            for (int i = 5; i < numStudents + 5; i++) {  
                System.out.println("Enter the student's name:");  
                String name = scanner.nextLine().strip();  
                System.out.println("Enter the student's grades separated by commas:");  
                String[] gradesStr = scanner.nextLine().split(","); // split the input by commas  
                int[] grades = new int[gradesStr.length]; // create an array to store the grades  
                for (int j = 0; j < gradesStr.length; j++) { // convert the string grades to integers  
                    grades[j] = Integer.parseInt(gradesStr[j].strip()); // remove any leading/trailing whitespace  
                }  
                students[i] = new Student(name, grades); // create a new student object and add it to the array  
            }  
        }  
  
  
        // adds (Above CN Average) to the end of the student's name if they are above the CANADIAN_AVERAGE constant  
        System.out.println("Would you like to display if the student is above the Canadian average? (Y/N)");  
        String response = scanner.nextLine().strip(); // remove any leading/trailing whitespace  
        boolean showAboveCanadianAverage = response.equalsIgnoreCase("Y");  
  
        // shows the student's grades ALONG with their average grade  
        System.out.println("Would you like to show all of the students' grades? (Y/N)");  
        response = scanner.nextLine().strip();  
        boolean showAllGrades = response.equalsIgnoreCase("Y");  
  
        // shows the student's average grade  
        System.out.println("Would you like to show the average grade of each student? (Y/N)");  
        response = scanner.nextLine().strip();  
        boolean showAverageGrade = response.equalsIgnoreCase("Y");  
  
        // shows the average grade of the entire class  
        System.out.println("Would you like to show the class average? (Y/N)");  
        response = scanner.nextLine().strip();  
        boolean showClassAverage = response.equalsIgnoreCase("Y");  
  
  
        // Display the students and their grades based on user input  
        System.out.println("Here are the students and their grades:");  
        for (Student student : students) {  
            if (student == null) { // Allow for empty slots in the array  
                break;  
            }  
            String output = student.name + " - ";  
            if (showAllGrades) { // Display all grades if the user wants  
                output += "Grades: ";  
                for (int grade : student.grades) {  
                    output += grade + ","; // add each grade followed by a comma  
                }  
                output = output.substring(0, output.length() - 1); // remove the last comma  
  
            }  
            if (showAverageGrade) { // Display the average grade if the user wants  
                output += " - Average Grade: " + round(student.getAverageGrade()) + "%";  
            }  
            if (showAboveCanadianAverage && student.getAverageGrade() > CANADIAN_AVERAGE) {  // Display if the student is above the Canadian average  
                output += " (Above CN Average)";  
            }  
  
            System.out.println(output) ;  
        }  
  
        if (showClassAverage) {  
  
        double classAverage = calculateClassAverage(); // Calculate the class average  
        System.out.println("Class Average: " + round(classAverage) + "%");  
        }  
  
        scanner.close();  
  
    }  
  
    // Helper function to round a double to 2 decimal places  
    private static float round(double num) {  
        return (float) (Math.round(num * 100.0) / 100.0);  
    }  
  
    // Helper function to calculate the class average  
    private static double calculateClassAverage() {  
        double sum = 0;  
        short student_length = 0;  
        for (Student student : students) {  
            if (student == null) {  // Allow for empty slots in the array  
                break;  
            }  
            student_length++;  
            sum += student.getAverageGrade();  
        }  
        return sum / student_length;  
    }  
  
    // Helper function to add default students  
    private static void addStudents() {  
        students[0] = new Student("Alice", new int[]{85, 90, 88, 92, 87});  
        students[1] = new Student("Bob", new int[]{78, 85, 80, 82, 79});  
        students[2] = new Student("Charlie", new int[]{92, 95, 90, 88, 94});  
        students[3] = new Student("David", new int[]{70, 65, 68, 72, 75});  
        students[4] = new Student("Eve", new int[]{95, 98, 96, 92, 97});  
    }  
  
}  
  
class Student {  
    public String name;  
    public int[] grades;  
  
    // Constructor to initialize the student's name and grades  
    public Student(String name, int[] grades) {  
        this.name = name;  
        this.grades = grades;  
    }  
  
    // Method to calculate the average grade of the student  
    public double getAverageGrade() {  
        int sum = 0;  
        for (int grade : grades) {  
            sum += grade;  
        }  
        return (double) sum / grades.length;  
    }  
}

Diagram

graph TD

    A[Start] --> B[Welcome Message]

    B --> C{Add more students?}

    C --> |Yes| D[Get number of students to add]

    D --> E[Get student details] --> G

    E --> E1[Get student name]

    E1 --> E2[Get student grades]

    E2 --> E3[Parse grades into array]

    E3 --> F[Create Student object]

    F --> E

    C --> |No| G[Get display preferences]

    G --> H[Display students and grades]

    H --> I{Show class average?}

    I --> |Yes| J[Calculate and display class average]

    I --> |No| K[End]

    J --> K

Sample output

Welcome to the Gradebook Analyzer
Would you like to add any more than the default students? (Y/N)
y
How many students would you like to add?
4
Enter the student's name:
Jason
Enter the student's grades separated by commas:
4,5,6,7
Enter the student's name:
Jason
Enter the student's grades separated by commas:
3,3,3
Enter the student's name:
jas
Enter the student's grades separated by commas:
4,6,340,5
Enter the student's name:
Peter
Enter the student's grades separated by commas:
43,56,99,99
Would you like to display if the student is above the Canadian average? (Y/N)
y
Would you like to show all of the students' grades? (Y/N)
y
Would you like to show the average grade of each student? (Y/N)
y
Would you like to show the class average? (Y/N)
n
Here are the students and their grades:
Alice - Grades: 85,90,88,92,87 - Average Grade: 88.4% (Above CN Average)
Bob - Grades: 78,85,80,82,79 - Average Grade: 80.8% (Above CN Average)
Charlie - Grades: 92,95,90,88,94 - Average Grade: 91.8% (Above CN Average)
David - Grades: 70,65,68,72,75 - Average Grade: 70.0%
Eve - Grades: 95,98,96,92,97 - Average Grade: 95.6% (Above CN Average)
Jason - Grades: 4,5,6,7 - Average Grade: 5.5%
Jason - Grades: 3,3,3 - Average Grade: 3.0%
jas - Grades: 4,6,340,5 - Average Grade: 88.75% (Above CN Average)
Peter - Grades: 43,56,99,99 - Average Grade: 74.25%