/* EdSoft Project

  This is class controls many students thru array of class Student.

  It receives control from class Grades and contains class Student array.

  It handles functions requested by menu() in class Grades.

 

  Author: Ahmad Mobasseri

*/

 

import java.util.Scanner;

 

public class ManyStudents

{

// Data

      private int nbrStudents;

      private int studentInstanceCount = 0;

      private Student[] student;   

     

// Constructor

      public ManyStudents()

      {

            System.out.println("Enter number of students: ");

            Scanner scn = new Scanner(System.in);

            nbrStudents = Integer.parseInt(scn.next());

           

            student = new Student[nbrStudents];

      }

     

// Enter name and grades of many students.

// Displays a menu for user to choose what function wants to do for each student. 

      public void enterStudents()

      {

            String cmd, resp="";

            Scanner scn = new Scanner(System.in);

            for( int i=studentInstanceCount; i < student.length && !resp.equals("n"); ++i)

            {

                  student[i] = new Student();

                  studentInstanceCount++;

                  do

                  {

                        menu();

                        cmd = scn.next();

                        if (cmd.equals("one"))

                              student[i].enterAGrade();

                        else if (cmd.equals("all"))

                              student[i].enterGrades();

                        else if (cmd.equals("d"))

                              student[i].displayGrades();

                        else if (cmd.equals("s"))

                              System.out.println("Done with this student\n");

                        else

                              System.out.println("\n!?! Invalid entry. Try again");

                  }while( ! cmd.equals("s"));

                 

                  System.out.println("Enter another student ?" + "\n Enter 'y' or 'n'");

                  resp = scn.next();

            }

      }

 

// Display a menu for actions to be taken for a student.   

      public static void menu( )

      {

            System.out.println("one  to enter one grade for this student");

            System.out.println("all  to enter all grades for this student");

            System.out.println("d    to display record for this student");

            System.out.println("s    to stop working on this student");

      }

 

// Find and display a student from records.    

      public void findAStudent()

      {

            System.out.println("Enter student's name");

            Scanner scn = new Scanner(System.in).useDelimiter("\\s*\n\\s*");   

            String inputName = scn.next();

            int i;

            for(i=0; i < student.length; ++i)

            {

                  if(! (student[i] == null))

                  {

                        String nameInRecord = student[i].getStudentInfo().getName();

                        if(inputName.equals(nameInRecord)) 

                        {

                              System.out.println("Here is the found student");

                              student[i].displayGrades();

                              i = student.length;

                        }

                  }

                  else

                  {

                        i = student.length;

                        System.out.println("Student was NOT found\n");

                  }

            }

      }

     

// Display all students in records 

      public void displayStudents()

      {

            int i;

            System.out.println("Student name |   ID     |        Homeworks             | Tot  " +

            " |    Projects        | Tot   | Mid   |  Fin  | Grade  | A.G.");            

            for(i=0; i < student.length; ++i)

            {

                  if(! (student[i] == null))

                  {

                        student[i].getStudentInfo().displayName();

                        student[i].getCalculation().calculGrades(student[i].getStudentGrades());

                        student[i].getStudentGrades().displayGrades();

                  }

                  else

                        i = student.length;

            }

      }

 

// Display a blank form for entering students grades.

      public void displayForm()

      {          

        int intNum = nbrStudents;

            System.out.println("\n\nStudent name |   ID     |        Homeworks             | Tot  " +

            " |    Projects        | Tot   | Mid   |  Fin  | Grade  | A.G.");  

            System.out.println("             |          | HW1, HW2, HW3, HW4, HW5, HW6 |      " +

            " | PJ1, PJ2, PJ3, PJ4 |       |       |       |        |     ");  

        for(int i=0; i <= intNum; ++i)

        {

            System.out.println("             |          |    |    |    |    |    |     |      " +

                        " |    |    |   |      |       |       |       |        |     ");

        }

      }

 

// Find a student from records and modify his record.

      public void modifyStudents()

      {

            boolean goOn = true;

            Scanner scn = new Scanner(System.in).useDelimiter("\\s*\n\\s*");

            do

            {

                  System.out.println("Enter student's name");

                  String inputName = scn.next();

                  int i;

                  for(i=0; i < student.length; ++i)

                  {

                        if(! (student[i] == null))

                        {

                              String nameInRecord = student[i].getStudentInfo().getName();

                              if(inputName.equals(nameInRecord)) 

                              {

                                    System.out.println("Here is the student's record");

                                    student[i].displayGrades();

                                    student[i].enterAGrade();

                                    i = student.length;

                              }

                        }

                        else

                        {

                              i = student.length;

                              System.out.println("Student was NOT found\n");

                        }

                  }

                  System.out.println("Modify another student ?" + "\n Enter 'y' or 'n'");

                  if(scn.next().equals("n"))

                        goOn = false;

            }while(goOn);

      }

}