import java.util.*;
import java.io.*;

public class ManageCars1 {
  // Example line: 1G1KK13WBB2657894,Ford,Taurus,2010,
  // return Automobile object with this data in fields
  public static 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 static Automobile findOldestCar(Automobile[] cars) {
    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 static void main(String[] args) 
    throws FileNotFoundException {
    int numCars = 0;
    
    Scanner input = new Scanner(new File("inventory.dat"));
    // count lines in file--
    while (input.hasNextLine()) {
      numCars++;
      input.nextLine();  // space past line
    } 
    Automobile inventory[] = 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);
      inventory[i] = car;
      i++;
    }
    Automobile oldest = findOldestCar(inventory);
    System.out.println("oldest car is a " + oldest.getYear() + oldest.getMake());
  }
  
}
