package com.lovecoding.day08.ex1;

public class TestRectangle {
    public static void main(String[] args) {
        //创建数组
        Rectangle[] arr = new Rectangle[3];
        //创建矩形
        Rectangle r1 = new Rectangle();
        r1.setLength(8);
        r1.setWeight(2);

        Rectangle r2 = new Rectangle();
        r2.setLength(7);
        r2.setWeight(3);

        Rectangle r3 = new Rectangle();
        r3.setLength(6);
        r3.setWeight(4);
        //赋值
        arr[0] = r1;
        arr[1] = r2;
        arr[2] = r3;
        //遍历
        TestRectangle testRectangle = new TestRectangle();

        testRectangle.printRectangleArr(arr);

        //排序
        testRectangle.sort(arr);
        System.out.println("----------------------");
        //排序之后遍历
        testRectangle.printRectangleArr(arr);

        //排序面积
        testRectangle.sortArea(arr);
        System.out.println("----------------------");
        //排序之后遍历
        testRectangle.printRectangleArr(arr);
    }

    //长度排序
    public void sortArea(Rectangle[] rectangles){
        //冒泡排序
        for (int i = 0; i < rectangles.length; i++) {
            for (int j = 0; j < rectangles.length-1-i; j++) {
                if ( rectangles[j].area() > rectangles[j+1].area() ){
                    //调换位置
                    Rectangle temp = rectangles[j];
                    rectangles[j] = rectangles[j+1];
                    rectangles[j+1] = temp;
                }
            }
        }
    }

    //长度排序
    public void sort(Rectangle[] rectangles){
        //冒泡排序
        for (int i = 0; i < rectangles.length; i++) {
            for (int j = 0; j < rectangles.length-1-i; j++) {
                if ( rectangles[j].getLength() > rectangles[j+1].getLength() ){
                    //调换位置
                    Rectangle temp = rectangles[j];
                    rectangles[j] = rectangles[j+1];
                    rectangles[j+1] = temp;
                }
            }
        }
    }

    //遍历
    public void printRectangleArr( Rectangle[] rectangles ){
        for (int i = 0; i < rectangles.length; i++) {
            System.out.println(rectangles[i].getInfo());
        }
    }
}
