/**
 * Fleet.java
 *
 * A class that illustrates concepts related to inheritance and polymorphism.
 * It also serves as an example of a simple collection class -- because it
 * represents a collection of vehicle objects.
 * 
 * Note: This class includes much of the same functionality as the UsingVehicles
 * client program. The difference is that UsingVehicles is a program that creates
 * and manages an array of vehicle objects. This class is a blueprint class for
 * an object that we can use to manage a collection of vehicle objects for us.
 * It maintains the array of objects -- rather than requiring that the client it.
 *
 * Computer Science S-111
 */

import java.util.*;

public class Fleet {
    // obtains the current year -- you don't need to understand how this works!
    public static final int CURRENT_YEAR = (new GregorianCalendar()).get(GregorianCalendar.YEAR);

    /** a Fleet object's fields **/
    private Vehicle[] vehicles;      // array to hold the vehicles in the fleet
    private int numVehicles;         // number of vehicles in the fleet
    
    /**
     * constructor - takes the maximum number of vehicles that can be part of the fleet
     */
    public Fleet(int maxSize) {
        this.vehicles = new Vehicle[maxSize];
        this.numVehicles = 0;
    }
    
    /**
     * addVehicle - adds the specified Vehicle v to the fleet.
     * Because of polymorphism, we can add a Vehicle object *or*
     * an object of any of the subclasses of Vehicle.
     */
    public void addVehicle(Vehicle v) {
        if (v == null) {
            throw new IllegalArgumentException("cannot add null");
        }
        
        if (this.numVehicles == this.vehicles.length) {
            throw new IllegalStateException("there is no room for more vehicles");
        }
        
        this.vehicles[this.numVehicles] = v;
        this.numVehicles++;
    }
    
    /**
     * printFleet - print the vehicles that are currently part of the fleet.
     */
    public void printFleet() {
        // Print each Vehicle in the array.
        // Thanks to dynamic binding, the correct
        // version of the toString() method will
        // be invoked for each vehicle.
        for (int i = 0; i < this.numVehicles; i++) {
            System.out.println(this.vehicles[i]);
        }
    }
    
    /**
     * computeAverageAge - computes and returns the average age of the
     * vehicles in the fleet
     */
    public double computeAverageAge() {
        int totalAge = 0;

        // We can invoke the getYear() method on each object in the fleet,
        // because they are all subclasses of Vehicle, and the getYear()
        // method is defined in Vehicle.
        for (int i = 0; i < this.numVehicles; i++) {
            int age = CURRENT_YEAR - this.vehicles[i].getYear();
            totalAge += age;
        }
        
        double averageAge = (double)totalAge / this.numVehicles; 
        return averageAge;
    }
 
    /**
     * computeAverageMileage - computes and returns the average mileage of the
     * vehicles in the fleet
     */
    public double computeAverageMileage() {
        int totalMileage = 0;

        // We can invoke the getMileage() method on each object in the
        // fleet, because they are all subclasses of Vehicle, and the
        // getMileage() method is defined in Vehicle.
        for (int i = 0; i < this.numVehicles; i++) {
            totalMileage += this.vehicles[i].getMileage();
        }
  
        double averageMileage = (double)totalMileage / this.numVehicles; 
        return averageMileage;
    }
    
    /**
     * toString - returns a string representation of the fleet
     */
    public String toString() {
        return ("fleet of " + this.numVehicles + " vehicles");
    }
}
