import objectdraw.*;
import java.awt.*;

// A class that makes and keeps track of a collection
// of basketballs.  There are methods to add, select,
// and randomly pick a ball from the collection.
public class BallCollection {

   // max number of balls in collection
   private static final int MAX_BALLS = 100;   
    
   private BBall[] balls; // the entire collection of balls.
   private int count;     // number of balls
   
   // use to pick random balls from the collection
   private RandomIntGenerator picker;

   // Create a new collection of basketballs with the given size
   public BallCollection() {
       count = 0;
       balls = new BBall[MAX_BALLS];
   }

   // Add a new ball to the collection
   public void add(BBall ball) {
       if (count < balls.length) {  // make sure there is room
           balls[count] = ball;
           count++;
           picker = new RandomIntGenerator(0, count-1);
       } else {
           System.out.println("No space left in collection");
       }
   }

   // Find a ball under a given point.  Return null if no ball is found.
   public BBall selectedBall(Location point) {
       for (int i = 0; i < count; i++) {
           if (balls[i].contains(point)) {
               return balls[i];
           }
       }
       return null;
   }
   
   // Return a random ball from the collection.
   // Pre: at least one ball is on collection.
   public BBall pickRandomly() {
       return balls[picker.nextValue()];
   }
}
