// Arup Guha
// 4/5/2010
// for 2008 AP Free Response Question #1: Flights

import java.util.*;

public class Trip {
	
	private ArrayList<Flight> flights;
	
	// Creates an empty trip.
	public Trip() {
		flights = new ArrayList<Flight>();
	}
	
	// Adds f as the next leg of this flight.
	public void addNextLeg(Flight f) {
		flights.add(f);
	}
	
	public int getDuration() {
		
		// Trivial case.
		if (flights.size() == 0)
			return 0;
		
		// Get two times in question.	
		Time start = flights.get(0).getDepartureTime();
		Time end = flights.get(flights.size()-1).getArrivalTime();
		
		// We want the difference in minutes.
		return start.minutesUntil(end);
			   
	}
	
	public int getShortestLayover() {
		
		int min = -1;
			
		// Go through each flight "gap"
		for (int i=0; i<flights.size()-1; i++) {
			
			// The start and end times of the layover.
			Time start = flights.get(i).getArrivalTime();
			Time end = flights.get(i+1).getDepartureTime();
			
			// Always update the first time around.
			if (i == 0)
				min = start.minutesUntil(end);
				
			// Or update if this gap is the smallest so far.
			else if (start.minutesUntil(end) < min)
				min = start.minutesUntil(end);
		}
		
		return min;
	}
	
	// Not necessary for the solution, added for debugging purposes.
	public String toString() {
		String ans = "";
		for (int i=0; i<flights.size(); i++)
			ans = ans + flights.get(i) + "\n";
		return ans;
	}
	
	public static void main(String[] args) {
		
		Trip t = new Trip();
		
		// Add each of the flights in the example to Trip t.
		Flight f = new Flight(new Time(11,30), new Time(12,15));
		t.addNextLeg(f);
		f = new Flight(new Time(13,15), new Time(15,45));
		t.addNextLeg(f);
		f = new Flight(new Time(16,00), new Time(18,45));
		t.addNextLeg(f);
		f = new Flight(new Time(22,15), new Time(23,00));
		t.addNextLeg(f);
		
		System.out.println(t);
		
		// Test out both methods on the trip in their sample.
		System.out.println("This trip will take "+t.getDuration()+" minutes.");
		System.out.println("The shortest layover is "+t.getShortestLayover()+" minutes.");
	}
}