import java.util.List;
import java.util.ArrayList;
/**
 Person stores the information for a person (Student, Teacher, ...)
 *
 * @author Fred Kral
 * @version 1.1
 *
 */

public class Person {

   /** Maximum size of the course array, used by BillBoard to make the week grid */
   public static final int MAXNUMCOURSEARRAY = 7;

   String firstName = "aPerson";
   String lastName = "";
   private final List<Course> courses;
   
   public Person(){
      this("aPerson", "");
   }
   public Person(String firstName) {
      this(firstName, "");
   }
   public Person(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.courses = new ArrayList<Course>();
   
   }

   public void addCourse (Course course) {
      courses.add(course);
   
   }

   public void setFirstName (String name) {
      firstName = name;     
   }
   public String getFirstName () {
      return firstName;     
   }
   public void setLastName (String name) { 
      lastName = name;
   }
   public String getLastName () { 
      return lastName;
   }
   
   public List<Course> getCourses () {
      return courses;
   }
   
   /** If course name is good, return its number, else return 0 */
   public int getCourseNumber (String courseName) {
      int i = 0;
      for (Course course : getCourses() ) {
         if ( course.equals(courseName) ){
            i++; 
            return i;
         }
      }
      // found nothing
      return 0;
   }
   
   /** If course name is good, return the Course, else return null */
   public Course getCourse (String courseName) {
      for (Course course : getCourses() ) {
         if ( course.equals(courseName) ){ 
            return course;
         }
      }
      // found nothing
      return null;
   }
   
   
   /* Number of courses taken by this student */
   public int getNumCourses() {
      return courses.size();
   }
   
   /* Slots in course in array */
   public int getCourseArrayLength() {
      Course [] courseArray = buildCourseArray();
      return courseArray.length;
   }
   
   public int getNumActiveCoursesInArray() {
      Course [] courseArray = buildCourseArray();
      int i = 0;
      for (Course course : courses) {   
         if ( course.getActive() ) {// active
            i++;
         }
      }
      return i;
   }
   
   
   /** Return course based on its number 1, 2, 3, ... NumCourses */
   /* Use for array only, has restrictions */
   public Course getCourse(int courseNumber) {
      Course [] courseArray = buildCourseArray();
      return courseArray[courseNumber - 1];
   }
   
   
   private Course [] buildCourseArray () {
      // make an array of all courses (there is a "toArray" method but it does not know "active")
      Course [] courseArray = new Course[MAXNUMCOURSEARRAY];
      int i = 0;
      for (Course course : courses) {   
         if ( course.getActive() && i < 7) {// active and fewer than 8 only <-- warning
            courseArray[i] = course;
            i++;
         }
      }
      return courseArray;
   }
   
   public void sysPrint () {
      System.out.print (firstName);
      if (lastName.equals("")) 
         System.out.println();
      else 
         System.out.println(" " + lastName);
   }
   public void sysPrintCourses () {
      for (Course course : courses) {
         course.sysPrint();
      }
   }

}// END class