import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;

/**
 * Class for reading in and filtering UFO sightings.
 *   @author Dave Reed
 *   @version 10/10/17
 */
public class UFOlookupDone {
    private ArrayList<UFOsighting> sightings;

    /**
     * Constructs a UFOlookup object, with UFO sightings read in from a file.
     *   @param filename the name of the UFO sighting file
     */
    public UFOlookupDone(String filename) {
        this.sightings = new ArrayList<UFOsighting>();
        
	    try {
	        Scanner infile = new Scanner(new File(filename));
	        
    	    while (infile.hasNextLine()) {
    	        String date = infile.next();
    	        String state = infile.next();
    	        String city = infile.nextLine().trim();
    	        UFOsighting sight = new UFOsighting(date, state, city);
    	        this.sightings.add(sight);
    	    }
    	    infile.close();
    	}
    	catch (java.io.FileNotFoundException e) {
    	    System.out.println("No such file: " + filename);
    	}
    }

    /**
     * Accesses the total number of sightings stored.
     *   @return the number of sightings
     */
    public int numSightings() {
        return this.sightings.size();
    }

    /**
     * Shows all UFO sightings that occured in the specified state.
     *   @param state the desired state
     */
    public void showByState(String state) {
        int count = 0;
        for (UFOsighting sight : this.sightings) {
            if (sight.getState().equals(state)) {
                System.out.println(sight);
                count++;
            }
        }
        System.out.println();
        System.out.println("# of sightings = " + count);
    }
    
    /**
     * Shows all UFO sightings between two specified dates.
     *   @param startDate the starting date in the range (inclusive)
     *   @param endDate the ending date in the range (inclusive)
     */
    public void showByDates(String startDate, String endDate) {
        int count = 0;
        for (UFOsighting sight : this.sightings) {
            if (sight.getDate().compareTo(startDate) >= 0 &&
                sight.getDate().compareTo(endDate) <= 0) {
                System.out.println(sight);
                count++;
            }
        }
        System.out.println();
        System.out.println("# of sightings = " + count);
    }
}
