        // A Pump is connected to one or more tanks and can deliver
        // fuel to a single customer from one of the tanks.
        // It is assumed that a Pump will not be connected to more than
        // one tank of the same fuel type.

class Pump extends Lockable {
    public Pump(Tank[] connectedTanks){
        tanks = connectedTanks;
    }

        // An indication that the request fuel type is not available.
    public static class FuelTypeNotAvailable extends Exception {
    }

        // Can we deliver the required amount of the given fuel?
    public long deliver(FuelType type,long amount) throws FuelTypeNotAvailable {
        // Make sure we can supply the right type of fuel.
        Tank tank = getTankWithType(type);
        if(tank == null){
            throw new FuelTypeNotAvailable();
        }
        else{
            // Remove the fuel from the tank.
            return tank.deliver(amount);
        }
    }

        // Can we deliver some of the required fuel type?
    public boolean canSupply(FuelType type){
        Tank tank = getTankWithType(type);
        return tank.canDeliver();
    }

        // Has this pump the required fuel type?
    public boolean hasFuelType(FuelType type){
        return getTankWithType(type) != null;
    }

        // Return the tank with the correct fuel type, or null
        // if there isn't one..
    protected Tank getTankWithType(FuelType type){
        Tank[] pumpTanks = getTanks();
        for(int i = 0; i < pumpTanks.length; i++){
            if(pumpTanks[i].getFuelType().equals(type)){
                return pumpTanks[i];
            }
        }
        return null;
    }

    protected Tank[] getTanks(){
        return tanks;
    }

    // The tanks to which this pump is connected.
    private final Tank[] tanks;
}
