/*
 * @topic T01805 ArrayList of Rectangle objects
 * @brief Main driver: ArrayList operations
*/
package pckg;
/*
 * Processes ArrayList of Rectangle objects.
 * The size() method of ArrayList indicates the the number of objects in the ArrayList.
 */
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListDemo2
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(20);

        getRectangles(rectangles, sc);

        System.out.println("\n You entered... ");

        displayRectangles(rectangles);

        System.out.println("\n Now, sorted by area...");

        sortAndDisplayRectangles(rectangles);

    } // end secondHalf static method


    // ********************************************

    private static void getRectangles(ArrayList<Rectangle> rectangles, Scanner sc)
    {
        double length, width;
        int k = 0;

        System.out.println("Enter the lengths and widths of any number of Rectangles");
        System.out.println();

        while (true)
        {
            length = Validator.getDouble(sc, "Enter the length for Rectangle "
                    + (k + 1) + " or 999 to quit: ");
            if (length != 999)
            {
                width = Validator.getDouble(sc, "Enter the width for Rectangle "
                    + (k + 1) + ": ");
                rectangles.add(new Rectangle(length, width));
                k++;
            } // end if statements
            else
            {
                break;
            }
        } // end outer while loop


    } // end getRectangles static method

    // *********************************************

    private static void displayRectangles(ArrayList<Rectangle> rectangles)
    {	
        int n = 1;
        for (Rectangle r : rectangles)
        {
            System.out.println("For rectangle " + (n++) + ":");
            r.displayRectangle();
            
        } // end for statements
    } // end displayRectangles static method

    //*************************************************

    private static void sortAndDisplayRectangles(ArrayList<Rectangle> rectangles)
    {
        int s = rectangles.size();
        
        Rectangle [] A = new Rectangle[s];  // Declare and instantiate an array of
                                           // Rectangle objects	to hold the same
                                           // number of objects that are in the
                                           // ArrayList. 
/*******************************************************************************
 *      This syntax assigns each element of the ArrayList to the array and then 
 *      sorts them using the Arrays.sort( ) method.
 *      
 * 		int i = 0;
 *			
 *    	for(Rectangle r : rectangles)
 *				
 *			{	
 *									//NOTE! THIS IS A COMMENT
 *				A[i++]=r;
 *			}
 *		
 *		Arrays.sort(A)
 *		
 *******************************************************************************
 *		
 *   The syntax below updates the array of Rectangle objects named A (which was
 *   instatiated above) with the objects from the rectangles ArrayListList
 *   and sorts them in a single statement!! 	
 *    
 *****************************************************************************/
            
      
            Arrays.sort(rectangles.toArray(A));	       		
         
            int n = 1;
            
            for(Rectangle R : A)
            {
                System.out.println("For rectangle " + (n++) + ":");
                R.displayRectangle();		    	
            }
    } // end sortAndDisplayRectangles static method

    // *************************************************
} // end class

/******************************************************
Enter the lengths and widths of any number of Rectangles

Enter the length for Rectangle 1 or 999 to quit: 9
Enter the width for Rectangle 1: 8
Enter the length for Rectangle 2 or 999 to quit: 7
Enter the width for Rectangle 2: 6
Enter the length for Rectangle 3 or 999 to quit: 5
Enter the width for Rectangle 3: 4
Enter the length for Rectangle 4 or 999 to quit: 3
Enter the width for Rectangle 4: 2
Enter the length for Rectangle 5 or 999 to quit: 999

 You entered...
For rectangle 1:
Rectangle Length = 9.0
Rectangle Width = 8.0
For rectangle 2:
Rectangle Length = 7.0
Rectangle Width = 6.0
For rectangle 3:
Rectangle Length = 5.0
Rectangle Width = 4.0
For rectangle 4:
Rectangle Length = 3.0
Rectangle Width = 2.0

 Now, sorted by area...
For rectangle 1:
Rectangle Length = 3.0
Rectangle Width = 2.0
For rectangle 2:
Rectangle Length = 5.0
Rectangle Width = 4.0
For rectangle 3:
Rectangle Length = 7.0
Rectangle Width = 6.0
For rectangle 4:
Rectangle Length = 9.0
Rectangle Width = 8.0
Press any key to continue...


*/