import java.util.*;
import java.io.*;

public class CarManager {
  private Automobile cars[];
  
  // Example line: 1G1KK13WBB2657894,Ford,Taurus,2010,
  // return Automobile object with this data in fields
  private Automobile processLine(String textLine) {
    Scanner data = new Scanner(textLine);
    data.useDelimiter(",");   // scanner delimited by comma
    String vin = data.next();
    String make= data.next();
    String model = data.next();
    System.out.println("model = " + model);
    int year = data.nextInt();
    Automobile car = new Automobile(make, model, year, vin);
    return car;
  }
  
  // find oldest car
  public Automobile findOldestCar() {
    Automobile oldCar = cars[0];  // init running min as first
    for (int i=0; i< cars.length; i++) {
      if (cars[i].getYear() < oldCar.getYear()) {
        oldCar = cars[i];
      }
    }
    return oldCar;
  }
  
  public void loadInventory(String inventoryFilename)
    throws FileNotFoundException {
    int numCars = 0;
    
    Scanner input = new Scanner(new File(inventoryFilename));
    // count lines in file--
    while (input.hasNextLine()) {
      numCars++;
      input.nextLine();  // space past line
    } 
    cars = new Automobile[numCars];
    input = new Scanner(new File ("inventory.dat"));
    input.useDelimiter(",");
    int i = 0;
    while (input.hasNextLine()) {
      String textLine = input.nextLine();
      Automobile car = processLine(textLine);
      cars[i] = car;
      i++;
    }
  }
  
}
