/*

  This class calculates grades total and alpha grade.

  It is contained by class Student and accesses class StudentGrades.

  It is called only by display function.

 

  Author: Ahmad Mobasseri

*/

 

public class Calculation

{

// Data

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

      private char alphaGrade;

      private static final int SUMHWMAX   = 18;

      private static final int SUMPROJMAX = 12;

      private static final int MIDTERMMAX = 50;

      private static final int FINALMAX   = 50;

      private static final int HWRATE      = 25;

      private static final int PROJRATE    = 25;

      private static final int MIDTERMRATE = 15;     

      private static final int FINALRATE   = 35;     

      private String hwArray[];

      private String projArray[];

      private String examArray[];

     

// Constructor

//    public Calculation() {  }

     

// Calculates total grades and alpha grade

      public void calculGrades(StudentGrades studentGrades)

      {

// NOTE: can make this method 'static' same as class StudentAGrade

//       And make class data, to local (this method) data, same as class StudentAGrade.        

            hwArray = studentGrades.getHwArray();

            sumHw = 0;

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

            {

//                if( ! hwArray[i].equals(null))  // NOTE: didn't work

//                if( hwArray[i] != null)

                  if( ! hwArray[i].equals(""))

                  {

                        double tempDouble = Double.parseDouble(hwArray[i]);

                      sumHw += tempDouble;

                  }

            }

            studentGrades.setSumHw(sumHw);

//  ***  This is how to get numbers from String array inHwGrades       

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

//        while (s.hasNext())

//        {      

//            String tempString = s.next();

//          if( ! tempString.equals(""))    // reads 'space' as empty string ""

//            {  

//                double tempDouble = Double.parseDouble(tempString);

//                sumHw += tempDouble; 

//            }

//        }

//          s.close();                  // Closes this scanner.

     

            projArray = studentGrades.getProjArray();      

            sumProj = 0;

            for(int i=0; i<projArray.length; ++i)

            {

                  if( ! projArray[i].equals(""))

                  {

                        double tempDouble = Double.parseDouble(projArray[i]);

                      sumProj += tempDouble;

                  }

            }

            studentGrades.setSumProj(sumProj);

     

            examArray = studentGrades.getExamArray();

            if( ! examArray[0].equals(""))

            {

                  midTerm = Double.parseDouble(examArray[0]);

                  studentGrades.setMidTerm(midTerm);

            }

            if( ! examArray[1].equals(""))

            {

                  finaal = Double.parseDouble(examArray[1]);

                  studentGrades.setFinal(finaal);

            }

//  ***  This is how to get numbers from String array inExamGrades     

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

//        if(scn.hasNext())

//          midTerm = Double.parseDouble(scn.next());

//        if(scn.hasNext())

//            finaal   = Double.parseDouble(scn.next());

//          scn.close();                  // Closes this scanner. 

 

            finGrade = sumHw/SUMHWMAX*HWRATE + sumProj/SUMPROJMAX*PROJRATE + midTerm/MIDTERMMAX*MIDTERMRATE +

                      finaal/FINALMAX*FINALRATE; 

            studentGrades.setFinGrade(finGrade);

           

            if(finGrade >= 90)  

                  alphaGrade = 'A';

            else if(finGrade < 90 && finGrade >= 80)

                  alphaGrade = 'B';

            else if(finGrade < 80 && finGrade >= 70)

                  alphaGrade = 'C';

            else if(finGrade < 70 && finGrade >= 60)

                  alphaGrade = 'D';

            else

                  alphaGrade = 'F';

            studentGrades.setAlphaGrade(alphaGrade);

      }

}