
import java.util.ArrayList;

public class Sleigh {

    protected ArrayList<Toy> contents;
    protected ArrayList<Reindeer> reindeer;
    protected double capacity;
    protected double sleighWeight;

    Sleigh(double sleighWeight, double capacity) {
        this.sleighWeight = sleighWeight;
        this.capacity = capacity;
        this.contents = new ArrayList<Toy>();
        this.reindeer = new ArrayList<Reindeer>();
    }

    void harness(Reindeer r) throws Exception {
        if (this.reindeer.contains(r)) {
            throw new Exception();
        }
        this.reindeer.add(r);
    }

    final void load(ArrayList<Toy> bunch) throws CapacityExceededException {
        double weight = 0.0;
        for (Toy toy : bunch) {
            weight += toy.getWeight();
        }
        for (Toy toy : contents) {
            weight += toy.getWeight();
        }
        if (this.capacity < weight) {
            throw new CapacityExceededException();
        }
        this.contents.addAll(bunch);
    }

    double getSpeed() {
        double weight = 0.0;
        for (Toy toy : this.contents) {
            weight += toy.getWeight();
        }
        return 1000 * this.reindeer.size() / (weight + this.sleighWeight);
    }

    public String toString() {
        String retval = "Sleigh(";
        for (int i=0; i<this.reindeer.size(); i++) {
            retval += this.reindeer.get(i);
            if (i < this.reindeer.size() - 1) {
                retval += ",";
            } else {
                retval += ")";
            }
        }
        return retval;
    }
}
