package classlistbasicpkg;

/**
 * Title:        Class list of names
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */

public class ClassListBasic{
   private Student[] myStudents;
   private int myMaxEnrollment;
   private int myCurrentEnrollment;
   private String myCourseTitle;

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

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

   public int getMaxEnrollment( ) {
       return myMaxEnrollment;
   }

   public String getTitle( ) {
       return myCourseTitle;
   }

   public boolean inCourse (Student name) {
       return searchStudent(name) != -1;   // Implement this using searchStudent
   }

   public boolean isThereRoom(){
        return myCurrentEnrollment < myMaxEnrollment;   // Fix this
   }

   public void printList( ) {  // output class list with title
       System.out.println(myCourseTitle);
       for (int i = 0; i < myCurrentEnrollment; i++)
          System.out.println("[" + i + "]:" + myNames[i]);
   }

   public String toString( ) {
      String temp = myCourseTitle + '\n';
      for (int i = 0; i < myCurrentEnrollment; i++)
         temp +=  "[" + i + "]:" + myNames[i] +'\n';
      return temp;
   }

   // modifiers
   /**
    *  Return false if there is no room in the course or name already
    *  appears in myNames.  Otherwise add name to myNames and return true.
    */
   public boolean addStudent( String name){
       if (!isThereRoom() || searchStudent(name) != -1)
          return false;
       myNames[myCurrentEnrollment] = name;
       myCurrentEnrollment++;
       return true;
   }

   /**
    *  Return false if name is not in myNames.
    *  Otherwise,  eliminate name by moving all of the entries
    *  is up and return true.
    */
   public boolean dropStudent(String name){
       int pos = searchStudent(name);
       if (pos == -1)
          return false;
       for (int i = pos+1; i < myCurrentEnrollment; i++)
          myNames[i-1] = myNames[i];
       myCurrentEnrollment--;
       return true;
   }

   // private helpers
   /**
     *  Return the index of name in myNames if name is present
     *  or -1 if name does not appear in myNames.
     */
   private int searchStudent(String name){
       for (int i = 0; i < myCurrentEnrollment; i++) {
           if (name.compareTo(myNames[i]) == 0)
              return i;
       }
       return -1;
   }
 }


