|
|
Airport.java
Airport.java holds the information regarding an airport. The constructor executes a query returning airport details such as its code, its name, its description, and its geographical coordinates in terms of x and y positions on different maps.
A hash table of airports is also created in Listing 10-5, which shows the source code of Airport.java.
Listing 10-5: Airport.java.
//
// Airport.java - this objects holds information regarding an airport
//
// Copyright (C) 1996 by Connect Software. All rights reserved.
//
// Written by Gionata Mettifogo, Peter Ham.
//
package airplet;
import connect.sql.*; // import sql server access classes
import java.awt.*; // java's windowing toolkit and other ui classes
import java.util.*; // utility classes
/** Information regarding an airport. */
class Airport
{
public Airport(String iAirport) throws SQLException
{
iAirport = iAirport.trim(); // remove leading and trailing spaces
ResultSet r = null; Statement s = Airplet.createStatement(); // use normal statement
to query the Airports table
if(iAirport.length() == 3) // if this is likely to be an airport code
{
r = s.executeQuery("select * from airports where code = '" + iAirport +
"'");
if(r.next() == false) // move over to the first (and only) row in the result
set
{
r = null; // there are no entries with given airport code
}
}
if(r == null) // search for airports whose name contain given airport string (like
%string%)
{
r = s.executeQuery("select * from airports where name like '%" + iAirport
+ "%'");
if(r.next() == false) // move to first (and probably only) row in result set
{
String nocase = ""; // case insensitive name, eg.
[mM][iI][lL][aA][nN] instead of Milan
for(int i = 0 ; i < iAirport.length() ; i++) // scan all characters in
the string
{
String single = iAirport.substring(i,i+1); // extract
character then convert to lowercase and uppercase
String lower = single.toLowerCase(), upper =
single.toUpperCase();
if(lower.equals(upper) == false) // if lowercase is
different from uppercase (that is if this character is
alpha)
{
nocase += "[" + lower + upper + "]"; //
regular expression for both lower or
uppercase, e.g. [aA]
}
else nocase += single;
}
// System.out.println("Airport - searching with case insensitive
string '" + nocase + "'");
r = s.executeQuery("select * from airports where name like '%" +
nocase + "%'");
if(r.next() == false) r = null; // if this one didn't work either there's no such airport
}
}
if(r != null)
{
code = r.getString("code"); // airport code, eg. 'SFO'
name = r.getString("name"); // airport name, eg. 'San Francisco, CA'
description = r.getString("description"); // description of this airport
StringTokenizer sTokenizer = new StringTokenizer
(r.getString("maps"),";");
while(sTokenizer.hasMoreTokens()) // scan each token, maps entry looks
something like "california(45,60);usa(123,3);world(56,78)"
{
MapInfo info = new MapInfo(sTokenizer.nextToken()); // create
an object containing information regarding this airport on a
single map
if(maps != null) // if there's other maps already
{
maps.append(info); // add this map to the linked list
of maps
}
else maps = info; // this is the first map in the list
// System.out.println(code + " map " + info);
}
}
else throw new SQLException("Can't find '" + iAirport + "' in the airports
database.");
}
private String code, name, description; // airport code, name and description, eg. 'SFO',
'San Francisco, CA', 'International Airport, ...'
private MapInfo maps; // linked list of maps available for this airport (and coordinates on
each map) in preferred order (eg. 'california', 'usa', 'world')
public String getCode()
{
return code; // return airport code
}
public String getName()
{
return name;
}
public MapInfo getMaps()
{
return maps;
}
/** Returns airport with the given name or code. */
static public Airport getAirport(String iName)
{
try
{
if(airports = null) // if there's no hash table for airports
{
airports = new Hashtable(); // create an
empty hash table
}
Airport airport = (Airport) airports.get(iName); // try getting
the airport from the hash table
if(airport = null) // if airport was not found
{
airport = new Airport(iName); // create a new airport
from that name (will query the database)
airports.put(airport.getName(),airport); // add airport
, to the hash table (by name)
airports.put(airport.getCode(),airport); // add also by
code
}
return airport; // return the airport
}
catch(SQLException sqlException) { return null; } // airport could not be
found
}
static private Hashtable airports = null; // hash table of airports (used to minimize
database access)
public String toString()
{
return "Airport[" + code + "," + name + "]"; // convert object to string
}
}
|