Brought to you by EarthWeb
ITKnowledge Logo Login Graphic 60-day trial version of Microsoft Project 98
60-day trial version of Microsoft Project 98
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


FlightsPanel.java

FlightsPanel.java class is a panel containing a graphical map. Which map is displayed depends on the departure and destination airport locations. This panel also displays the routes. Listing 10-9 contains the source code for this class.

Listing 10-9: FlightsPanel.java.

//
// FlightsPanel.java - a panel showing flights information and routes.
//
// Copyright (C) 1996 by Connect Software. All rights reserved.
//
// Written by Gionata Mettifogo, Peter Ham.
//
package airplet; // airplet package
import connect.sql.*; // import connect's jdbc libraries
import java.awt.*; // import java windowing library
class FlightsPanel extends Panel
{
  public FlightsPanel()
  {
      LayoutManager layout = new ColumnLayout(0,10); // column layout with 10
        pixels between components
      setLayout(layout); // use this layout for the panel
      map = new MapCanvas(); add(map); // add a map to the panel
      label = new MultilineLabel(Label.CENTER); // label that can display multiple lines
        of text (draw with subtle good looking shadow)
      setText("Welcome to jdbc airlines!\n \nPlease pick an origin and a
        destination\nthen click Search or RoundTrip.");
      add(label); // add label to panel
  }
  /** Converts a time object into a string in the form hh:mm am/pm   */
  String time2string(Time time)
  {
      int hour = time.getHours(); // get hours (0..23) and minutes (0..59)
      int minute = time.getMinutes(); // format the string as hh:mm then append am or
        pm
      return (hour % 12 < 10 ? "0" : "") + Integer.toString(hour % 12) + ":" + (minute < 10
        ? "0" : "") + Integer.toString(minute) + (hour < 12 ? " AM" : " PM");
  }
  void setAirports(Airport iFrom,Airport iTo) throws SQLException
  {
      String str = null;
      try
      {
           map.setAirports(iFrom,iTo); // show best map for these two airports (and
             a route between them)
           if(iFrom.getCode().equals(iTo.getCode()) = false) // if the two airports are
             not the same
           {
               FlightsVector flights = new FlightsVector(iFrom,iTo); // create a
                 vector containing all the flights between the two airports
               int numFlights = flights.size(); // number of flights found
               if(numFlights > 0) // if there are flights
               {
                    str = "Flights from " + iFrom.getName() + " to " +
                      iTo.getName() + "\n \n";
                    for(int i = 0 ; i < numFlights ; i++) // scan flights
                      between these two airports
                    {
                         Flight flight = (Flight) flights.elementAt(i); //
                          retrieve i-th flight
                         str += flight.code + " leaves at " +
                           time2string(flight.departure) + " arrives at "
                           + time2string(flight.arrival) + " (frequency "
                           + flight.frequency + ").\n";
                    }
               }
               else str = "There are no flights between " + iFrom.getName() + "
                 and " + iTo.getName() + ".";
           }
           else str = "Please pick two different airports, then retry."; // if there are no
             flights or airports are the same show an error message
      }
      catch(SQLException sqlEx) // some sql exception was raised, notify the user
      {
           str = "Sorry, your request didn't go through,\nthe server is probably
           down or busy,\nplease try again later.\n \n" + sqlEx;
      }
      setText(str); // show the string with the flights or the warning
      layout(); // we may need to redo this panel's layout (the label may have changed its
      size)
  }
  public void setText(String text)
  {
      label.setText(text);
  }
  private MapCanvas map = null; // map and route canvas
  private MultilineLabel label = null; // label with flights or error message
}

FlightsVector.java

FlightsVector.java is a vector containing all flights between the departure and arrival airports. A query is sent to the database server to get information about flights with the given airport codes for departure and arrival. Listing 10-10 shows the source code for this class.

Listing 10-10: FlightsVector.java.

//
// FlightsVector.java - a vector containing a bunch of flights
//
// Copyright (C) 1996 by Connect Software. All rights reserved.
//
// Written by Gionata Mettifogo, Peter Ham.
//
package airplet;
import connect.sql.*; // import jdbc and other sql libraries
import java.util.*; // java utility classes
class FlightsVector extends Vector // this is just a vector of Flight objects
{
  /**
      * Initialize this vector with all flights between two given airports.
      * The method will select all rows in the flights table having the given airport
      * codes in the from_city and to_city fields. An entry in the vector will then
      * be created for each flight and each entry will be added to the vector.
      *
      * @param iConnection connection to the database
      * @param iFrom the airport we're leaving from
      * @param iTo the airport we're arriving to
      */
  public FlightsVector(Airport iFrom,Airport iTo) throws SQLException
  {
      // executes something like: select * from flights where from_city
        = 'SFO' and to_city = 'JFK'
      String sql = "select * from flights where from_city = '" + iFrom.getCode() + "' and
        to_city = '" + iTo.getCode() + "' order by departure";
      Statement s = Airplet.createStatement(); // create normal sql statement
      for(ResultSet r = s.executeQuery(sql) ; r.next() ; ) // scan all flights between given
        airports
      {
           Flight flight = new Flight(r); // create a new flight from current row
           // System.out.println("FlightsVector - adding flight " + flight + " to
             vector");
           addElement(flight); // add this flight to the vector
      }
      s.close(); // we don't have to do this (but it could help jdbc optimize access)
  }
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.