package csclasspkg;

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

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

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

   public int getMaxEnrollment( ) {
       return myMaxEnrollment;
   }

   public String getTitle( ) {
       return myCourseTitle;
   }

   public boolean inCourse (String name) {
      if (searchStudent(name) == -1)
        return false;   // Implement this using searchStudent
      else
        return true;
   }

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

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

   // 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 (searchStudent(name) != -1 || !isThereRoom())
         return false;
      else {
         myNames[myCurrentEnrollment] = name;
         myCurrentEnrollment++;
         return true;
      }       // Use searchStudent and isThereRoom to implement
   }

   /**
    *  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 loc = searchStudent(name);
      if (loc == -1) return false;
      for (int i = loc; i < myCurrentEnrollment-1; i++)
         myNames[i] = myNames[i+1];
      myCurrentEnrollment--;
      return true;           // Fix this
   }

   // 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 (myNames[i].equals(name)) return i;
      return -1;            // Fix this
   }
 }


