package csclasspkg;

public class ClassList{
   private Student[] myNames;
   private int myMaxEnrollment;
   private int myCurrentEnrollment;
   private String myCourseTitle;

   public ClassList(String title, int maxEnroll){
      myCourseTitle = title;
      myMaxEnrollment = maxEnroll;
      myNames = new Student[myMaxEnrollment];
      myCurrentEnrollment = 0;
   }

   // Accessors
   public int getEnrollment( ) {
       return myCurrentEnrollment;
   }

   public int getMaxEnrollment( ) {
       return myMaxEnrollment;
   }

   public String getTitle( ) {
       return myCourseTitle;
   }

   public boolean inCourse (Student name) {
      if (searchStudent(name) == -1)
        return false;
      else
        return true;
   }

   public boolean isThereRoom(){
      if (myMaxEnrollment <= myCurrentEnrollment)
        return false;
      else
        return true;
   }

   public void printList( ) {
      System.out.println("Enrollment in " +
         myCourseTitle + ": ");
      for (int i = 0; i < myCurrentEnrollment; i++)
         System.out.println("   " + myNames[i]);
      System.out.println();
   }

   // modifiers
   public boolean addStudent(Student name){
      if (searchStudent(name) != -1 || !isThereRoom())
         return false;
      else {
         myNames[myCurrentEnrollment] = name;
         myCurrentEnrollment++;
         return true;
      }
   }

   public boolean dropStudent(Student name){
      int loc = searchStudent(name);
      if (loc == -1) return false;
      for (int i = loc; i < myCurrentEnrollment-1; i++)
         myNames[i] = myNames[i+1];
      myCurrentEnrollment--;
      return true;
   }

   // private helpers
   private int searchStudent(Student name){
      for (int i = 0; i < myCurrentEnrollment; i++)
         if ((myNames[i].getSSN()).equals(name.getSSN()))
            return i;
      return -1;
   }
}
