Java Cart .

Java Cart State java

package cart;

import cart.utils.ValidationUtils;
import java.util.HashMap;

//State - has methods to store and verify state and abbreviation
//        will build a state given it's abbreviation or name,
//        but probably should only build with abbreviation
//author - Lawrence Truett - FluffyCat.com
//date - August 12, 2003 - San Diego, CA
public class State implements Validated {
    
    private String stateAbbrev = null;
    private String stateName = null;
    private boolean isRequired = false;
    
    private static HashMap stateMap;
    private static String[] abbrevArray;
    private static String[] nameArray;
    
    static {createStateMap();};
    
    public State() {}
    
    public State(String stateIn) {
        if (null == stateIn) {
            this.setStateName(null);
        } else {    
            if (stateIn.length() > 2) {
                this.setStateName(stateIn.toUpperCase());
            } else {
                this.setStateAbbrev(stateIn.toUpperCase());
            }
        }    
    }
    
    
    public boolean isRequired() {
        return isRequired();
    }    
    public void setRequired(boolean isRequiredIn) {
        isRequired = isRequiredIn;
    }
    
    //need stuff here to get state for abbrev & vice versa
    //maybe using 2 static hashmaps
    public String getStateName() {return stateName;}
    private void setStateName(String stateNameIn) {
        if (null == stateNameIn) {
            this.stateName = null;
            setStateAbbrev(null);
        } else {     
          this.stateName = stateNameIn.toUpperCase();
          if ((null == this.stateAbbrev) && 
              (this.isNameValid(this.stateName))) {
              setStateAbbrev(this.getAbbrevForName(this.stateName));
          }
        }  
    }
    
    public String getStateAbbrev() {return stateAbbrev;}
    private void setStateAbbrev(String stateAbbrevIn) {
        this.stateAbbrev = stateAbbrevIn;
        if ((null == this.stateName) && 
            (this.isAbbrevValid(this.stateAbbrev))) {
            this.setStateName(this.getNameForAbbrev(this.stateAbbrev));
        }
    }
    
    public boolean isValid() {
        if (stateMap.containsKey(this.getStateAbbrev())) {
            return true;
        } else {
            return false;
        }
    }
    public boolean isNull() {
        if ((ValidationUtils.isStringBlank(this.getStateAbbrev()))
        &&  (ValidationUtils.isStringBlank(this.getStateName()))) {
            return true;
        } else {
            return false;
        }
    }
    public boolean isValidOrNull() {
        if ((this.isValid()) || (this.isNull())) { 
            return true;
        } else {
            return false;
        }
    }
    
    //method used for data input
    public String getStateForInput() {
        if (null == this.getStateAbbrev()) {
            if (null == this.getStateName()) {
                return "";
            } else {
                return this.getStateName();
            }
        } else {
            return this.getStateAbbrev();
        }
    }
    
    //since we actually need a more specific error message than isValid()
    //would derive, we determine the actual error string here
    public String getErrorMessage() {
        if ( (this.isValid()) || (this.isNull()) ) {
            return " ";
        } else {
            if  (!ValidationUtils.isStringBlank(this.getStateAbbrev())) {
                return this.getStateAbbrev() + " is not a state ";
            } else {
                return this.getStateName() + " is not a state ";
            }
        }
    }
    
    //used to get an error message when null is also not a valid value
    public String getErrorOrNullMessage() {
        if (this.isNull()) { return "State is blank"; }
        else {return this.getErrorMessage();}
    }
    
    public String toString() {
        return ("State abbrev: " + 
                this.getStateAbbrev() + 
                " name: " + 
                this.getStateName());
    }
    
    public static String[] getAbbrevArray() {return abbrevArray;}
    public static String[] getNameArray() {return nameArray;}
    public static boolean isAbbrevValid(String abbrevIn) {
        return stateMap.containsKey(abbrevIn);
    }
    public static boolean isNameValid(String nameIn) {
        return stateMap.containsValue(nameIn);
    }
    public static String getNameForAbbrev(String abbrevIn) {
        return (String)stateMap.get(abbrevIn);
    }
    public static String getAbbrevForName(String nameIn) {
        //A somewhat clumbsy way to do this, 
        //    which assumes it is rarely used.
        //If you use this often a second HashMap 
        //    with a key of name and value of abbrev could be added
        for (int i = 0; i < nameArray.length; i++)
            if (nameArray[i].equalsIgnoreCase(nameIn)) {
                return abbrevArray[i];
            }
        //name not found
        return null;
    }
    
    //Hardcoded data here could come from a table instead.
    //As this data doesn't change often I went hardcoded.
    private static void createStateArrays() {
        
        abbrevArray = new String[75];
        nameArray = new String[75];
        
        abbrevArray[0]="AK";  nameArray[0]= "ALASKA";
        abbrevArray[1]="AL";  nameArray[1]= "ALABAMA";
        abbrevArray[2]="AR";  nameArray[2]= "ARKANSAS";
        abbrevArray[3]="AZ";  nameArray[3]= "ARIZONA";
        abbrevArray[4]="CA";  nameArray[4]= "CALIFORNIA";
        abbrevArray[5]="CO";  nameArray[5]= "COLORADO";
        abbrevArray[6]="CT";  nameArray[6]= "CONNECTICUT";
        abbrevArray[7]="DC";  nameArray[7]= "DISTRICT OF COLUMBIA";
        abbrevArray[8]="DE";  nameArray[8]= "DELAWARE";
        abbrevArray[9]="FL";  nameArray[9]= "FLORIDA";
        abbrevArray[10]="GA"; nameArray[10]="GEORGIA";
        abbrevArray[11]="HI"; nameArray[11]="HAWAII";
        abbrevArray[12]="IA"; nameArray[12]="IOWA";
        abbrevArray[13]="ID"; nameArray[13]="IDAHO";
        abbrevArray[14]="IL"; nameArray[14]="ILLINOIS";
        abbrevArray[15]="IN"; nameArray[15]="INDIANA";
        abbrevArray[16]="KS"; nameArray[16]="KANSAS";
        abbrevArray[17]="KY"; nameArray[17]="KENTUCKY";
        abbrevArray[18]="LA"; nameArray[18]="LOISIANA";
        abbrevArray[19]="MA"; nameArray[19]="MASSACHUSETTS";
        abbrevArray[20]="MD"; nameArray[20]="MARYLAND";
        abbrevArray[21]="ME"; nameArray[21]="MAINE";
        abbrevArray[22]="MI"; nameArray[22]="MICHIGAN";
        abbrevArray[23]="MN"; nameArray[23]="MINNESOTA";
        abbrevArray[24]="MO"; nameArray[24]="MISSOURI";
        abbrevArray[25]="MS"; nameArray[25]="MISSISSIPPI";
        abbrevArray[26]="MT"; nameArray[26]="MONTANA";
        abbrevArray[27]="NC"; nameArray[27]="NORTH CAROLINA";
        abbrevArray[28]="ND"; nameArray[28]="NORTH DAKOTA";
        abbrevArray[29]="NE"; nameArray[29]="NEBRASKA";
        abbrevArray[30]="NH"; nameArray[30]="NEW HAMPSHIRE";
        abbrevArray[31]="NJ"; nameArray[31]="NEW JERSEY";
        abbrevArray[32]="NM"; nameArray[32]="NEW MEXICO";
        abbrevArray[33]="NV"; nameArray[33]="NEVADA";
        abbrevArray[34]="NY"; nameArray[34]="NEW YORK";
        abbrevArray[35]="OH"; nameArray[35]="OHIO";
        abbrevArray[36]="OK"; nameArray[36]="OKLAHOMA";
        abbrevArray[37]="OR"; nameArray[37]="OREGON";
        abbrevArray[38]="PA"; nameArray[38]="PENNSYLVANIA";
        abbrevArray[39]="RI"; nameArray[39]="RHODE ISLAND";
        abbrevArray[40]="SC"; nameArray[40]="SOUTH CAROLINA";
        abbrevArray[41]="SD"; nameArray[41]="SOUTH DAKOTA";
        abbrevArray[42]="TN"; nameArray[42]="TENNESSEE";
        abbrevArray[43]="TX"; nameArray[43]="TEXAS";
        abbrevArray[44]="UT"; nameArray[44]="UTAH";
        abbrevArray[45]="VA"; nameArray[45]="VIRGINIA";
        abbrevArray[46]="VT"; nameArray[46]="VERMONT";
        abbrevArray[47]="WA"; nameArray[47]="WASHINGTON";
        abbrevArray[48]="WI"; nameArray[48]="WISCONSIN";
        abbrevArray[49]="WV"; nameArray[49]="WEST VIRGINIA";
        abbrevArray[50]="WY"; nameArray[50]="WYOMING";
        //us military
        abbrevArray[51]="AA"; nameArray[51]="US MILITARY AMERICAS";
        abbrevArray[52]="AE"; nameArray[52]=
            "US MILITARY AFRICA, CANADA, EUROPE, MIDDLE EAST";
        abbrevArray[53]="AP"; nameArray[53]="US MILITARY PACIFIC";
        //not states or DC
        abbrevArray[54]="AS"; nameArray[54]="AMERICAN SAMOA";
        abbrevArray[55]="FM"; nameArray[55]=
            "FEDERATED STATESD OF MICRONESIA";
        abbrevArray[56]="GU"; nameArray[56]="GUAM";
        abbrevArray[57]="MH"; nameArray[57]="MARSHALL ISLANDS";
        abbrevArray[58]="MP"; nameArray[58]="NORTHERN MARIANA ISLANDS";
        abbrevArray[59]="PR"; nameArray[59]="PUERTO RICO";
        abbrevArray[60]="PW"; nameArray[60]="PALUA";
        abbrevArray[61]="VI"; nameArray[61]="US VIRGIN ISLANDS";
        //Canada:
        abbrevArray[62]="AB"; nameArray[62]="ALBERTA";
        abbrevArray[63]="BC"; nameArray[63]="BRITISH COLUMBIA";
        abbrevArray[64]="MB"; nameArray[64]="MANITOBA";
        abbrevArray[65]="NB"; nameArray[65]="NEW BRUNSWICK";
        abbrevArray[66]="NF"; nameArray[66]="NEWFOUNDLAND";
        abbrevArray[67]="NS"; nameArray[67]="NOVA SCOTIA";
        abbrevArray[68]="NT"; nameArray[68]="NORTHWEST TERRITORIES";
        abbrevArray[69]="NU"; nameArray[69]="NUNAVUT";
        abbrevArray[70]="ON"; nameArray[70]="ONTARIO";
        abbrevArray[71]="PE"; nameArray[71]="PRINCE EDWARD ISLANDS";
        abbrevArray[72]="QC"; nameArray[72]="QUEBEC";
        abbrevArray[73]="SK"; nameArray[73]="SASKATCHEWAN";
        abbrevArray[74]="TY"; nameArray[74]="YUKON TERRITORY";
    }
    
    private static void createStateMap() {
        createStateArrays();
        stateMap = new HashMap(75);
        for (int i = 0; i < abbrevArray.length; i++) {
            stateMap.put(abbrevArray[i], nameArray[i]);
        }
    }
    
}
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart State java.