BalloonFamily.java
1    /* 
2     * BalloonFamily Class  
3     * Create an ArrayList of BalloonPerson Objects  
4     */
5    
6    package balloonpeople;
7    
8    import painter.SPainter;
9    
10   import javax.swing.*;
11   import java.util.ArrayList;
12   
13   public class BalloonFamily {
14   
15       //instance variables
16       private ArrayList<BalloonPerson> people;
17   
18       //constructor
19       public BalloonFamily(int nrOfPeople) {
20           ArrayList<BalloonPerson> people = new ArrayList<>();
21           this.people = people;
22           //create input window
23           fillArray(nrOfPeople);
24       }
25       //toString() method
26       public String toString() {
27           String representation = "The members) ";
28           for(int x = 0; x < people.size(); x = x + 1) {
29               representation = representation + people.get(x).toString() + " --- ";
30           }
31           return representation;
32       }
33       //paint() method
34       public void paint(SPainter painter){
35           //move into place
36           int nrOfPeople = people.size()-1;
37           int amountToMoveLft = (nrOfPeople)*(100);
38           painter.mlt(amountToMoveLft);
39           //paint the people
40           for(int i = 0; i < people.size(); i = i+1) {
41               BalloonPerson token = people.get(i);
42               token.paint(painter);
43               painter.mrt(200);
44           }
45       }
46   
47       //fillArray() method to complete construction of BalloonFamily object
48       private void fillArray(int nrOfPeople) {
49           for(int i = 1; i <= nrOfPeople; i = i +1) {
50               String command = JOptionPane.showInputDialog(null,
51                       "Please enter a String in the form NAME:AGE,HEIGHT for each member."
52               + "\n Remember to hit ENTER or 'ok' in between each submission.");
53               //get balloonperson instance variables out
54               int colon = command.indexOf(":");
55               int comma = command.indexOf(",");
56               String personName = command.substring(0,colon);
57               int age = Integer.parseInt(command.substring(colon + 1, comma));
58               int height = Integer.parseInt(command.substring(comma+1));;
59               //create balloon person
60               BalloonPerson token = new BalloonPerson(personName, age, height);
61               //add to ArrayList
62               people.add(token);
63           }
64       }
65   }