|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.billpringle.utils.WrpZipCode
public class WrpZipCode
This class encapsulates a USPS zip code with its associated state and city. It also provides an interface to store zip code information in memory as an array of WrpZipCode objects.
![]() |
![]() |
Unless noted otherwise, all materials available for download from my site are copyrighted by Bill Pringle, and are licensed under a Creative Commons License. |
Field Summary | |
---|---|
private java.util.Vector<java.lang.String> |
city
a list of cities associated with the zipcode |
private int |
id
unique ID from database |
private static java.lang.String |
query
stored procedure to retrieve zip codes from database |
private java.lang.String |
state
the state abbreviation |
private java.lang.String |
zipcode
the zipcode |
private WrpZipCode[] |
zipList
An array of all zip codes. |
Constructor Summary | |
---|---|
WrpZipCode()
The default Constructor |
|
WrpZipCode(java.lang.String zipcode)
Constructor with only zipcode |
|
WrpZipCode(java.lang.String zipcode,
java.lang.String state)
Constructor with state abbreviation |
|
WrpZipCode(java.lang.String zipcode,
java.lang.String state,
java.lang.String city)
Constructor with initial state and city combination |
|
WrpZipCode(WrpDb db)
Constructor that will load the current list of zipcodes. |
|
WrpZipCode(WrpZipCode zc)
Constructor to clone a zip code object |
Method Summary | |
---|---|
void |
addCity(java.lang.String city)
Add another city for this zipcode. |
int |
compareTo(WrpZipCode zip)
Compare this zipcode with another one |
java.util.Vector<java.lang.String> |
findCities(java.lang.String strZip)
Get the list of cities associated with the specified zip code. |
private WrpZipCode |
findLoadingZipCode(java.util.Vector<WrpZipCode> lst,
java.lang.String strZipCode)
Search for zipcode entry in vector. |
WrpZipCode |
findZipCode(java.lang.String strZip)
This routine will search for the specified zipcode |
java.util.Vector<java.lang.String> |
getCities()
Get the list of cities assigned to this zipcode |
java.lang.String |
getCity(int loc)
Get the corresponding city from the list of cities |
int |
getId()
Get the unique ID assigned by the database for this zip code. |
int |
getNumberCities()
Get the number of possible city names (or abbreviations) for zipcode |
java.lang.String |
getState()
Get the state. |
java.lang.String |
getZipcode()
Get the zip code. |
WrpZipCode[] |
getZipList()
Get array of zipcodes. |
boolean |
hasCity(java.lang.String city)
Determine if the specified city is defined for zip code |
void |
init()
initialize instance variables |
boolean |
isCityValid(java.lang.String strZip,
java.lang.String strCity)
Determine if city is valid for a zipcode. |
boolean |
isStateValid(java.lang.String strZip,
java.lang.String strState)
Determine if zipcode is defined within a state. |
boolean |
isZipCodeValid(java.lang.String strZip)
Determine if specified string is a valid zipcode. |
boolean |
isZipCodeValid(java.lang.String strZip,
java.lang.String strState)
Determine if a zipcode is valid for a state. |
boolean |
isZipCodeValid(java.lang.String strZip,
java.lang.String strState,
java.lang.String strCity)
Determine if city / state / zipcode combination valid |
void |
loadZipCodes(WrpDb db)
Load the zip codes from a database into an array. |
static void |
main(java.lang.String[] args)
Test driver for zip codes. |
void |
setId(int id)
Set the unique ID for this zip code. |
void |
setState(java.lang.String state)
Set the state. |
void |
setZipcode(java.lang.String zipcode)
Set the zip code. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final java.lang.String query
private int id
private java.lang.String zipcode
private java.lang.String state
private java.util.Vector<java.lang.String> city
private WrpZipCode[] zipList
This list can be built using the loadZipCodes method. The list can then be searched using the findZipCode method
Constructor Detail |
---|
public WrpZipCode()
public WrpZipCode(java.lang.String zipcode)
zipcode
- the zipcodepublic WrpZipCode(java.lang.String zipcode, java.lang.String state)
zipcode
- the zipcodestate
- the state abbreviationpublic WrpZipCode(java.lang.String zipcode, java.lang.String state, java.lang.String city)
zipcode
- the zipcodestate
- the state abbreviation associated with this zipcodecity
- a city associated with this zipcodepublic WrpZipCode(WrpZipCode zc)
zc
- the zip code to clonepublic WrpZipCode(WrpDb db) throws java.sql.SQLException
db
- database connection to zipcodes
java.sql.SQLException
Method Detail |
---|
public int compareTo(WrpZipCode zip)
This method compares the zipcode with the argument zipcode. The fields tested are the zipcode, the state, and the city. (The state should always be the same.)
If this zipcode comes before the other, a negative number is returned. If this zipcode comes after the other, a positive number is returned. If the two zipcodes are identical, a zero is returned.
The method compares the zip code, the state, and any cities. If a difference is found, then the appropriate value is returned. If no differences are found (including the two lists of cities), then zero is returned.
zip
- the other zipcode to be compared
public java.lang.String getCity(int loc)
loc
- index of city
public boolean hasCity(java.lang.String city)
city
- the city name to test for
public int getId()
public void setId(int id)
id
- the id to setpublic java.lang.String getZipcode()
public void setZipcode(java.lang.String zipcode)
zipcode
- the zipcode to setpublic java.lang.String getState()
public void setState(java.lang.String state)
state
- the state to setpublic void addCity(java.lang.String city)
city
- the city (or abbreviation) to be addedpublic int getNumberCities()
public java.util.Vector<java.lang.String> getCities()
public void init()
public void loadZipCodes(WrpDb db) throws java.sql.SQLException
The query returns a collection of lines with zipcode, state, and city entries. There will be as many entries for a zip code as there are cities defined for that zip code. Each zip code should only have one state assocateid with it.
db
- database connection that contains zipcode definitions
java.sql.SQLException
public WrpZipCode[] getZipList()
This method can be used if the client wants to create some customized searches or lists for zip codes.
private WrpZipCode findLoadingZipCode(java.util.Vector<WrpZipCode> lst, java.lang.String strZipCode)
This routine is called when zipcodes are being read from a database. Since the entries are sorted by zipcode, we check the last entry first. If the zipcode matches, we return that entry, but if the zipcode is greater than the one at the end, we return null.
lst
- vector containing zipcodesstrZipCode
- string containing the zipcode
public WrpZipCode findZipCode(java.lang.String strZip)
This routine will perform a binary search on the array zipList to find the corresponding zipcode.
This method requires the loadZipCodes(WrpDb) method to have been called.
strZip
- the desired zipcode
public java.util.Vector<java.lang.String> findCities(java.lang.String strZip)
This method requires the loadZipCodes(WrpDb) method to have been called.
strZip
- the desired zip code
public boolean isZipCodeValid(java.lang.String strZip)
This method requires the loadZipCodes(WrpDb) method to have been called.
strZip
- zipcode string to be validated
public boolean isZipCodeValid(java.lang.String strZip, java.lang.String strState)
strZip
- string with a zipcodestrState
- string with a state abbreviation
public boolean isZipCodeValid(java.lang.String strZip, java.lang.String strState, java.lang.String strCity)
This method requires the loadZipCodes(WrpDb) method to have been called.
strZip
- the zipcode to be validatedstrState
- the state abbreviation to be validatedstrCity
- the city name to be validated
public boolean isStateValid(java.lang.String strZip, java.lang.String strState)
This method requires the loadZipCodes(WrpDb) method to have been called.
strZip
- the zipcode to be validatedstrState
- the state abbreviation to be validated
public boolean isCityValid(java.lang.String strZip, java.lang.String strCity)
This routine only compares the city name, not the state. The caller should also validate that the state is valid for the zipcode.
This method requires the loadZipCodes(WrpDb) method to have been called.
strZip
- the zipcode to be validatedstrCity
- the city to be validated
public static void main(java.lang.String[] args)
This driver connects to the database, loads the zipcodes, and then lets the user validate city/state/zipcodes
args
- not used
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |