/**
 * @author dreamstar
 * @date 2006.11.3
 */

import java.util.*;

/**
 * This class contains methods to process vectors of {@link Student} objects.
 *
 * @author  author name
 * @version  1.0.0
 * @see  Student
 * @see  Vector
 */
public class  StudentVector  {

	/**
	 * Returns a vector with three elements.
	 *
	 * @param first  a <code>Student</code> object.
	 * @param second  a <code>Student</code> object.
	 * @param third  a <code>Student</code> object.
	 * @return a vector with the objects <code>first</code>,
	 *           <code>second</code>, and <code>third</code>
	 */
	public static Vector makeVector(Student  first, Student  second,
	                                Student  third)  {
		
        Vector vector = new Vector();
        vector.add( first );
        vector.add( second );
        vector.add( third );
        
        return vector;
		/* PLACE YOUR CODE HERE */

		//return null; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Returns a vector with the same elements of the specified array
	 * arranged in the same order.
	 *
	 * @param array  an array with <code>Student</code> objects .
	 * @return a vector with the same elements of the specified array
	 *         arranged in the same order
	 */
	public static Vector makeVectorFromArray(Student[] array) {

		/* PLACE YOUR CODE HERE */
		Vector vector = new Vector();
		for ( int i = 0; i < array.length; i++ )
		{
			vector.add( array[i] );
		}
		
		return vector;

		//return null; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Returns <code>true</code> if the specified vector contains a
	 * student whose ID matches the specified ID.
	 *
	 * @param vector  a vector of <code>Student</code> objects.
	 * @param id  a student ID.
	 * @return  <code>true</code> if the specified vector contains a
	 *          student whose ID matches the specified ID;
	 *          <code>false</code> otherwise.
	 */
	public static boolean hasStudent(Vector  vector, int  id)  {

        for (Iterator  i = vector.iterator() ; i.hasNext(); ) 
        {
        	if ( ( ( Student ) i.next() ).getId() == id )
        	       return true;  // if it has the id  then return true
        }
		/* PLACE YOUR CODE HERE */
        return false;
		//return true; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Returns the number of students in the specified vector whose
	 * grade is greater than or equal to the specified grade.
	 *
	 * @param vector  a vector of <code>Student</code> objects.
	 * @param grade  a grade.
	 * @return  the number of students in the specified vector whose
	 *          grade is greater than or equal to the specified grade.
	 */
	public static int countGradeGreaterOrEqual(Vector vector, int grade)  {

        int count = 0; // the count greater or equal of the grade
        for (Iterator  i = vector.iterator() ; i.hasNext(); ) 
        {
        	int result = ( (Student)i.next() ).getGrade();
        	if ( result >= grade )
        		count++;
        }
		/* PLACE YOUR CODE HERE */
        return count;
		//return 0; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Returns the smallest grade of the students in the specified vector.
	 * <p>
	 * This method assumes that the vector is not empty.
	 *
	 * @param vector  a vector of <code>Student</code> objects.
	 * @return  the smallest grade of the students in the specified vector.
	 */
	public static int getMinGrade(Vector vector)  {

        int min = (( Student)vector.get(0)).getGrade();
        for (Iterator  i = vector.iterator() ; i.hasNext(); ) 
        {
        	if ( ( (Student) i.next() ).getGrade() < min )
        		min = ( (Student) i.next() ).getGrade();
        }
        return min;
		/* PLACE YOUR CODE HERE */

		//return 0; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Returns the average grade of the students in the specified vector.
	 *
	 * @param vector  a vector of <code>Student</code> objects.
	 * @return  the average grade of the students in the specified vector.
	 */
	public static double getGradeAverage(Vector vector)  {
		
        double average = 0;
        double sum = 0;
        
        for (Iterator  i = vector.iterator() ; i.hasNext(); ) 
        {
        	sum += ( ( Student )i.next() ).getGrade();
        }
        average = sum / vector.size();
		/* PLACE YOUR CODE HERE */

		return average; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Removes all students in the specified vector whose grade
	 * is less than the specified grade.
	 *
	 * @param vector  a vector of <code>Student</code> objects.
	 * @param grade  a grade.
	 */
	public static void removeGradeLess(Vector  vector, int  grade)  {
		
		 for ( int i = 0; i < vector.size(); i++ ) 
		 {
			 if ( ( ( Student )vector.get(i) ).getGrade() < grade )
			 {
				 vector.remove(i);
				 i--;
			 }
		 }
		/* PLACE YOUR CODE HERE */
	}

	/**
	 * Returns the string representation of the objects in the specified
	 * vector.
	 * <p>
	 * A new line character ( \n ) should separate the string
	 * representations of the objects. For example:
	 * </p>
	 * <pre>
	 * Student[328,Galileo Galilei,80]\nStudent[123,Albert Einstein,100]
	 * </pre>
	 * <p>
	 * Note that the string does <i>not</i> end with a new line character ( \n ).
	 * </p>
	 *
	 * @param vector  a vector of <code>Student</code> objects.
	 * @return  the string representation of the objects in the specified
	 *          vector.
	 */
	public static String displayAll(Vector  vector)  {

        String string = "";
        for (int i = 0; i < vector.size(); i++ ) 
        {
        	Student student =  ( Student )vector.get(i) ;
        		string += "Student[" + student.getId() + "," 
        	        + student.getName() + "," + student.getGrade() + "]\n";
        }
        
        string = string.substring( 0, string.lastIndexOf( "\n" ) );
        
		/* PLACE YOUR CODE HERE */

		return string; // REMOVE; USED SO THIS FILE COMPILES
	}
}