package arrayclasses;

public class ClassList {
   private static final int DEFAULT_SIZE = 2;
   private String courseTitle;
   private String discipline;
   private String semester;
   private String instructor;
   private StudentGrade[] studentList;
   private int classSize;

   public ClassList(String title, String disc, String sem, String ins) {
      courseTitle = title;
      discipline = disc;
      semester = sem;
      instructor = ins;
      studentList = new StudentGrade[DEFAULT_SIZE];
      classSize = 0;
   }

   
   // This version allows duplicate entries
   public void add(StudentGrade s) {
      if (studentList.length == classSize)
         resize();
      studentList[classSize] = s;
      classSize++;
   }

   public String getDiscipline() {
      return discipline;
   }

   public double getGrade(String studentID) {
      int position = find(studentID);
      if (position == -1)
         return -1;
      return studentList[position].getGrade();
   }

   public String getInstructor() {
      return instructor;
   }

   public String getSemester() {
      return semester;
   }

   public StudentGrade getStudent(String ID) {
      int position = find(ID);
      if (position == -1)
         return null;
      return studentList[position];

   }

   public String getTitle() {
      return courseTitle;
   }

   // This version changes the order of the students in the list
   public void remove(String ID) {
      int position = find(ID);
      if (position == -1)
         return;
      classSize--;
      studentList[position] = studentList[classSize];
   }

   public void setGrade(String studentID, double grade) {
      int position = find(studentID);
      if (position == -1)
         return;
      studentList[position].setGrade(grade);
   }

   public String toString() {
      StringBuffer buf = new StringBuffer(getClass().getName() + "[title=" +
                                          courseTitle + ",discipline=" +
                                          discipline +
                                          ",semester=" + semester +
                                          ",instructor=" +
                                          instructor + "]");
      for (int i = 0; i < classSize; i++)
         buf.append("\n" + studentList[i]);
      return buf.toString();
   }

   private int find(String studentID) {
      for (int i = 0; i < classSize; i++)
         if (studentID.equals(studentList[i].getStudentID()))
            return i;
      return -1;
   }

   private void resize() {
      StudentGrade[] temp = new StudentGrade[2 * studentList.length];
      for (int i = 0; i < studentList.length; i++)
         temp[i] = studentList[i];
      studentList = temp;
   }
   
   // the following are the extensions
   
   public double getAverageGrade() {
       double sum = 0;
       if (classSize == 0)
           return 0.0;
       for (int i=0;i<classSize;i++)
           sum += studentList[i].getGrade();
       return sum/classSize;
   }
   
   public double getMaxGrade() {
       double max;
       if (classSize == 0)
           return 0.0;
       max = studentList[0].getGrade();
       for (int i=1;i<classSize;i++)
           if (max < studentList[i].getGrade())
               max = studentList[i].getGrade();
       return max;
   }
   
   public double getMinGrade() {
       double min;
       if (classSize == 0)
           return 0.0;
       min = studentList[0].getGrade();
       for (int i=1;i<classSize;i++)
           if (min > studentList[i].getGrade())
               min = studentList[i].getGrade();
       return min;
   }
}


