
/**
 * AirlineMap.java
 *
 *
 * Created: Thu Oct 10 06:42:49 2002
 *
 * @author <a href="mailto:bina@cse.buffalo.edu "</a>
 * @version
 */
import java.util.*;

public class AirlineMap 
   implements AirlineMapInterface{

   LinkedList aList;

   public AirlineMap()
   { 
      aList = new LinkedList();
   }

   public AirlineMap (Route[] r){
      for (int i = 0; i< r.length; i++) {
	 addRoute(r[i]);
      } // end of for ()
   }   

   public void addRoute(Route r)
   {
      // locate the position as per alphabetical order of origin
      int index = findLocation(r);
      if ( index >= 0) {
      aList.add(index, r);
      } // end of if ()
      
   }

   public void addRoute(Airport a, Airport b)
   {
      Route r = new Route(a,b);
      addRoute(r);
   }
   
   
   
   private int findLocation(Route r)
   {
      int location = 0;
      ListIterator lst = aList.listIterator();
      while (lst.hasNext()) {
	 Route current = (Route)lst.next();
	 if (r.compareTo(current) > 0) {
	    location++;
	    continue; } // end of if ()
	 else {
	    if ( r.compareTo(current)<0) {
	       return location;} // end of if ()
	    else {
	       return -1; // equals
	    } // end of else
	 } // end of else
      } // end of while ()
      return location;
   }
   


   public boolean deleteRoute(Route r)
   {
      int index = aList.indexOf(r);
      if ( index >= 0) {
	 aList.remove(r);
	 return true;
      } // end of if ()
      return false;
   }



    public ListIterator getRoutesByOrg( Airport a)
    {
	ArrayList temp = new ArrayList();
	ListIterator lst = aList.listIterator();
	while (lst.hasNext()) {
	    Route current = (Route) lst.next();
	    if ( (current.getOrigin()).equals(a)) { 
		temp.add(current);
	    } // end of if ()
	    
	} // end of while ()
	return(temp.listIterator());
    }

    public ListIterator getRoutesByDest( Airport a)
    {
	ArrayList temp = new ArrayList();
	ListIterator lst = aList.listIterator();
	while (lst.hasNext()) {
	    Route current = (Route)lst.next();
	    if ( (current.getDest()).equals(a)) { 
		temp.add(current);
	    } // end of if ()
	    
	} // end of while ()
	return(temp.listIterator());
    }

   public String toString()
   {
      StringBuffer s = new StringBuffer();
      ListIterator lst = aList.listIterator();
      while (lst.hasNext()) {
	 Route current = (Route) lst.next();
	 s.append("" +current +"\n");
	} // end of while ()
      return (s.toString());
   }
}// AirlineMap
