Java object-oriented array of objects

Java object-oriented array of objects

Abstract: http://www.verejava.com/?id=16992784958543/** Knowledge points: Object array 1. Use of object array 2. Object array foreach enhanced for loop 3.

www.verejava.com/?id=1699278...

/**
       
        1.  
        2.  foreach  for 
        3.  

     2 
     
        1.    (Customer)  ,  (Box)
        2.    1 2  Box->Customer
*/
public class ObjectArray {
    
    public static void main(String[] args) {
       //
        Customer c = new Customer(" ");

       //,  
        Box clothBox = new Box(" ");
        clothBox.setId(1);
        clothBox.setWeight(20);

        Box bookBox = new Box(" ");
        bookBox.setId(2);
        bookBox.setWeight(30);

       //
        c.addBox(clothBox);
        c.addBox(bookBox);

       //
        System.out.println(" : " + c.getName());
        System.out.println(" ,  ,  ");
        Box[] boxes = c.getBoxes();
        for (Box box : boxes) {
            System.out.println(box.getId() + "," + box.getWeight() + "," + box.getDescription());
        }

       //
        Box[] boxes2 = { clothBox, bookBox };
        c.setBoxes();//
        c.setBoxes(clothBox);//
        c.setBoxes(boxes2);//
        boxes = c.getBoxes();
        for (Box box : boxes) {
            System.out.println(box.getId() + "," + box.getWeight() + "," + box.getDescription());
        }
    }
}

class Customer {
    
    private String name;//
    private Box[] boxes = new Box[2];//,  

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Box[] getBoxes() {
        return this.boxes;
    }

   //
   //:  true  false
    public boolean addBox(Box box) {
        for (int i = 0; i < boxes.length; i++) {
            if (boxes[i] == null) {
                boxes[i] = box;
                return true;
            }
        }
        return false;
    }

    public void setBoxes(Box... boxes) {
        this.boxes = boxes;
    }
}

class Box {
    private float weight;//
    private int id;//
    private String description;//

    public Box(String description) {
        this.description = description;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public float getWeight() {
        return this.weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

}