
import java.util.Scanner;

abstract class Vehicle {
  protected int mileage;
  protected double milesPerGallon;
  protected String color;

  protected Vehicle() {
    mileage = 0;
    milesPerGallon = 20;
    color = new String();
  }
  protected Vehicle(int mileage, double milesPerGallon, String color) {
    this.mileage = mileage;
    this.milesPerGallon = milesPerGallon;
    this.color = new String(color);
  }

  public String getColor() {
    return color;
  }
  public int getMileage() {
    return mileage;
  }
  public double getMilesPerGallon() {
    return milesPerGallon;
  }
  public void setColor(String color) {
    this.color = new String(color);
  }
  public void setMileage(int mileage) {
    this.mileage = mileage;
  }
  public void setMilesPerGallon(double milesPerGallon) {
    this.milesPerGallon = milesPerGallon;
  }

  abstract public void load();

  public String toString() {
    return "Color is " + color + "\n" +
           "Mileage is " + mileage + "\n" +
           "Miles Per Gallon is " + milesPerGallon + "\n";
  }
}

class Car extends Vehicle implements Comparable<Car>{
  private int numberOfPassengers;

  public Car() {
    super();
    numberOfPassengers = 4;
  }
  public Car(int numberOfPassengers, int mileage, double mpg, String color) {
    super(mileage, mpg, color);
    this.numberOfPassengers = numberOfPassengers;
  }
  public String toString() {
    return super.toString() +
           "Number of Passengers is " + numberOfPassengers + "\n";
  }

  public int getNumberOfPassengers(){
  	return numberOfPassengers;
  }

  public void setNumberOfPassengers(int n){
  	numberOfPassengers = n;
  }

  public void load() {
      Scanner scanner = new Scanner(System.in);
      System.out.print("How many people to load into this car? ");
      int n = scanner.nextInt();

      if( n > numberOfPassengers) {
          System.out.println("This car is too small.");
      }
      else if( n < 0 ) {
          System.out.println("Invalid number");
      }
      else System.out.println("You have load this car with " + n + " people.");
  }

  public int compareTo(Car c2) {
	int output = 0;

	//---- compare color -------
	if( color.compareTo(c2.getColor()) < 0 )
		output -= 8;
	else if( color.compareTo(c2.getColor()) > 0 )
		output += 8;

	//---- compare number of passengers -------
	if( numberOfPassengers < c2.getNumberOfPassengers() )
		output -= 4;
	else if( numberOfPassengers > c2.getNumberOfPassengers() )
		output += 4;
	
	//---- compare mileage -------
	if( mileage < c2.getMileage())
		output -= 2;
	else if( mileage > c2.getMileage() )
		output += 2;

	//---- compare miles per gallon -------
	if( milesPerGallon < c2.getMilesPerGallon())
		output -= 1;
	else if( milesPerGallon > c2.getMilesPerGallon() )
		output += 1;

	return output;
  }
}

class Truck extends Vehicle implements Comparable<Truck>
{
	private double weightCapacity;

	public Truck()
	{
		super();
		weightCapacity = 1000.00D;
	}
	public Truck(double weightCapacity, int mileage, double mpg, String color)
	{
		super(mileage, mpg, color);
		this.weightCapacity = weightCapacity;
	}

	public double getWeightCapacity()
	{
		return weightCapacity;
	}

	public void setWeightCapacity(double w)
	{
		weightCapacity = w;
	}

        public void load() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("How much freight to load into this truck? ");
            double n = scanner.nextDouble();

            if( n > weightCapacity) {
               System.out.println("This truck is too small.");
            }
            else if( n < 0 ) {
               System.out.println("Invalid number");
            }
            else System.out.println("You have load this truck with " + n + " pounds of freight.");
        }
	public String toString()
	{
		return super.toString() +
			"Weight Capacity is " + weightCapacity + "\n";
	}

  	public int compareTo(Truck t2) {
		int output = 0;

		//---- compare color -------
		if( color.compareTo(t2.getColor()) < 0 )
			output -= 8;
		else if( color.compareTo(t2.getColor()) > 0 )
			output += 8;

		//---- compare weight capacity  -------
		if( weightCapacity < t2.getWeightCapacity() )
			output -= 4;
		else if( weightCapacity > t2.getWeightCapacity() )
			output += 4;
	
		//---- compare mileage -------
		if( mileage < t2.getMileage())
			output -= 2;
		else if( mileage > t2.getMileage() )
			output += 2;

		//---- compare miles per gallon -------
		if( milesPerGallon < t2.getMilesPerGallon())
			output -= 1;
		else if( milesPerGallon > t2.getMilesPerGallon() )
			output += 1;

		return output;
  	}	
}


class ParkingGarage
{
        public Vehicle spaces[];
	private int spacesFilled;

	public ParkingGarage()
	{
		spaces = new Vehicle[100];
		spacesFilled = 0;
	}
	public ParkingGarage(int size)
	{
		spaces = new Vehicle[size];
		spacesFilled = 0;
	}
	public ParkingGarage(Vehicle s[], int sf)
	{
		spacesFilled = sf;
		spaces = s;
	}
	public int findVehicle(Vehicle vehicle)
	{
		for(int i = 0; i < spacesFilled; i++)
		{
			if ( spaces[i] == null )
			{
				continue;
			}
			else
			{
				if ( (spaces[i] instanceof Car) && (vehicle instanceof Car) ) {
					Car c1 = (Car) spaces[i];
					Car c2 = (Car) vehicle;

					if( c1.compareTo(c2) == 0 )
						return i;
				}
				else if ((spaces[i] instanceof Truck) && (vehicle instanceof Truck)) {
					Truck t1 = (Truck) spaces[i];
					Truck t2 = (Truck) vehicle;

					if( t1.compareTo(t2) == 0 )
						return i;
				}
			}
		}
		return -1;	// Not Found!
	}

	public boolean parkVehicle(Vehicle vehicle)
	{
		if(spaces.length <= spacesFilled)
		{
			System.out.println("No space left!");
			return false;
		}
		spaces[spacesFilled++] = vehicle;
		return true;
	}
}

class Driver
{
	public static void main(String [] args){
		Car c1 = new Car(4,1000,30,"blue");
		System.out.println("Car 1 is created "+c1);
		Car c2 = new Car(4,1000,30,"blue");
		System.out.println("Car 2 is created "+c2);
		Car c3 = new Car(3,10000,25,"white");
		System.out.println("Car 3 is created "+c3);

		Truck t1 = new Truck(2000,25000, 15, "yellow");
		System.out.println("Truck 1 is created "+t1);
		Truck t2 = new Truck(5000,45000, 10, "purple");
		System.out.println("Truck 2 is created "+t2);
		Truck t3 = new Truck(20000,95000, 7, "lily");
		System.out.println("Truck 3 is created "+t3);

		ParkingGarage pg = new ParkingGarage(4);

		System.out.println("car 1 is parking "+pg.parkVehicle(c1));
		System.out.println("truck 1 is parking "+pg.parkVehicle(t1));
		System.out.println("car 2 is parking "+pg.parkVehicle(c2));
		System.out.println("truck 1 is parking "+pg.parkVehicle(t2));
		System.out.println("truck 3 is parking "+pg.parkVehicle(t3));

		System.out.println("Is car 1 in the parking garage? "+pg.findVehicle(c1));
		System.out.println("Is truck 1 in the parking garage? "+pg.findVehicle(t1));
		System.out.println("Is truck 3 in the parking garage? "+pg.findVehicle(t3));
	}
}


