/*

  This class handles a student's grades.

  It is contained by class Student and accessed by classes StudentAGrade & Calculation &

  manyStudents.

 

  Author: Ahmad Mobasseri

*/

import java.util.Scanner;

 

public class StudentGrades

{

// Data

      private String inHwGrades   = "";

      private String inProjGrades = "";

      private String inExamGrades = "";

      private double sumHw, sumProj, midTerm, finaal, finGrade;

      private char alphaGrade;

      private static final int HWMAX    = 6;

      private static final int PROJMAX  = 4;

      private static final int EXAMMMAX = 2;   

      private String hwArray[]; // = {"", "", "", "", "", ""}; // %? does NOT init. it

      private String projArray[];

      private String examArray[];

     

// Constructor

      public StudentGrades()

      {

            hwArray   = new String[HWMAX];      

            projArray = new String[PROJMAX];

            examArray = new String[EXAMMMAX];

            for(int i=0; i<HWMAX; i++)  // Change from String with null ptr to ptr to an object with

                  hwArray[i] = "";        //  nothing ("")

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

                  projArray[i] = "";

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

                  examArray[i] = "";

      }

     

// Mutator 

      public void setHwArrayElement(String inHwArrayElement, int i)

      {     hwArray[i] = inHwArrayElement;      }    

      public void setProjArrayElement(String inProjArrayElement, int i)

      {     projArray[i] = inProjArrayElement;  }    

      public void setExamArrayElement(String inExamArrayElement, int i)

      {     examArray[i] = inExamArrayElement;  }    

      public void setSumHw(double inSumHw)

      {     sumHw = inSumHw;  }    

      public void setSumProj(double inSumProj)

      {     sumProj = inSumProj;    }

      public void setMidTerm(double inMidTerm)

      {     midTerm = inMidTerm;    }    

      public void setFinal(double inFinal)

      {     finaal = inFinal; }    

      public void setFinGrade(double inFinGrade)

      {     finGrade = inFinGrade;  }    

      public void setAlphaGrade(char inAlphaGrade)

      {     alphaGrade = inAlphaGrade;    }    

     

// Accessors

      public String[] getHwArray()

      {     return hwArray;   }

      public String[] getProjArray()

      {     return projArray; }

      public String[] getExamArray()

      {     return examArray; }

     

// Enter all/any grades.

      public void enterGrades()

      {

            String tempHwGrades, tempProjGrades, tempExamGrades;

            System.out.println("Enter up to 6 homework grades. Enter ',' between grades. Enter 'n' to bypass.");

           

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

            tempHwGrades = scanner.next();

            if( ! tempHwGrades.equals("n")) // %% doesn't take space on 1st ( ,2,3)

            {

                  inHwGrades = tempHwGrades;

              Scanner s = new Scanner(inHwGrades).useDelimiter("\\s*,\\s*");

              int i =0;

              while (s.hasNext())

              {

                  hwArray[i] = s.next();

                  i++;

              }

                  s.close(); 

          }

           

            System.out.println("Enter up to 4 project grades. Enter ',' between grades. Enter 'n' to bypass.");

            tempProjGrades = scanner.next();

            if( ! tempProjGrades.equals("n"))

            {

                  inProjGrades = tempProjGrades;

            Scanner s = new Scanner(inProjGrades).useDelimiter("\\s*,\\s*");

            int i =0;

            while (s.hasNext())

            {

                projArray[i] = s.next();

                i++;

            }

                s.close();

            }

           

            System.out.println("Enter up to 2 exam grades. Enter ',' between grades. Enter 'n' to bypass.");

            tempExamGrades = scanner.next();

            if( ! tempExamGrades.equals("n"))

            {

                  inExamGrades = tempExamGrades;

              Scanner s = new Scanner(inExamGrades).useDelimiter("\\s*,\\s*");

              int i =0;

              while (s.hasNext())

              {

                  examArray[i] = s.next();

                  i++;

              }

                  s.close();

            }

      }

     

// Display all grades. 

      public void displayGrades()

      {

            Object hwArrayTemp[]; hwArrayTemp = new Object[hwArray.length + 1];

            int i =0;

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

            {

                  hwArrayTemp[i] = new String(hwArray[i]);

            }

            hwArrayTemp[i] = new Double(sumHw);

            System.out.format("%4s,%4s,%4s,%4s,%4s,%4s | %5.2f |", hwArrayTemp); 

           

            Object projArrayTemp[]; projArrayTemp = new Object[projArray.length + 1];

            int j =0;

            for(j=0; j<projArray.length; j++)

            {

                  projArrayTemp[j] = new String(projArray[j]);

            }

            projArrayTemp[j] = new Double(sumProj);

            System.out.format("%4s,%4s,%4s,%4s | %5.2f |", projArrayTemp); 

           

            Object arrayTemp[]; arrayTemp = new Object [4]; 

            arrayTemp[0] = new Double(midTerm);

            arrayTemp[1] = new Double(finaal);

            arrayTemp[2] = new Double(finGrade);

            arrayTemp[3] = new Character(alphaGrade);

            System.out.format(" %5.2f | %5.2f | %6.2f | %1s", arrayTemp);

            System.out.println("");

      }

}