Question #2 of 3
QUESTION #2:
The following Java code is responsible for creating an HTML “SELECT”
list of U.S. states, allowing a user to specify his or her state. This might
be used, for instance, on a credit card transaction screen.
Please rewrite this code to be “better”. Submit your replacement code, and
please also submit a few brief comments explaining why you think your code
is better than the sample.
(For brevity, this sample works for only 5 states. The real version would
need to work for all 50 states. But it is fine if your rewrite shows only
the 5 states here.)
public class StateUtils { // // Generates an HTML select list that can be used to select a specific // U.S. state. // public static String createStateSelectList() { return "<select name=\"state\">\n" + "<option value=\"Alabama\">Alabama</option>\n" + "<option value=\"Alaska\">Alaska</option>\n" + "<option value=\"Arizona\">Arizona</option>\n" + "<option value=\"Arkansas\">Arkansas</option>\n" + "<option value=\"California\">California</option>\n" // more states here + "</select>\n" ; } // // Parses the state from an HTML form submission, converting it to // the two-letter abbreviation. // public static String parseSelectedState(String s) { if (s.equals("Alabama")) { return "AL"; } if (s.equals("Alaska")) { return "AK"; } if (s.equals("Arizona")) { return "AZ"; } if (s.equals("Arkansas")) { return "AR"; } if (s.equals("California")) { return "CA"; } // more states here } // // Displays the full name of the state specified by the two-letter code. // public static String displayStateFullName(String abbr) { { if (abbr.equals("AL")) { return "Alabama"; } if (abbr.equals("AK")) { return "Alaska"; } if (abbr.equals("AZ")) { return "Arizona"; } if (abbr.equals("AR")) { return "Arkansas"; } if (abbr.equals("CA")) { return "California"; } // more states here } }
Solution:
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.*; public class Q2_StateUtils { private final static Map<String, String> statesMap = new HashMap<String, String>(); static { statesMap.put("AL", "Alabama"); statesMap.put("AK", "Alaska"); statesMap.put("AS", "American Samoa"); statesMap.put("AZ", "Arizona"); statesMap.put("AR", "Arkansas"); statesMap.put("CA", "California"); // ... etc. } // // Generates an HTML select list that can be used to select a specific // U.S. state. // Modified: Now the select list returns the two-letter state. // public static String createStateSelectList() { StringBuilder html = new StringBuilder(); html.append("<select name=\"state\">\n"); for (Map.Entry<String, String> stateEntry : statesMap.entrySet()) { String stateShortName = stateEntry.getKey(); String stateFullName = stateEntry.getValue(); html.append("<option value=\"" + stateShortName + "\">" + stateFullName + "</option>\n"); } html.append("</select>\n"); return html.toString(); } // // Parses the state from an HTML form submission, converting it to // the two-letter abbreviation. // @deprecated use {@link #saveSelectedState()} instead. // This method has been deprecated for the following reason: // * We've taken extra steps of getting a short name. // @Deprecated public static String parseSelectedState(String stateFullName) { for (Map.Entry<String, String> stateEntry : statesMap.entrySet()) if (stateFullName == stateEntry.getValue()) return stateEntry.getKey(); return ""; } // // Safely returns the two-letter abbreviation of the U.S. state. // public static String safeSelectedState(String stateShortName) { return statesMap.containsKey(stateShortName) ? stateShortName : ""; } // // Displays the full name of the state specified by the two-letter abbreviation. // public static String displayStateFullName(String stateShortName) { return statesMap.getOrDefault(stateShortName, ""); } public static void main(String[] args) throws IOException { System.out.println("#1. createStateSelectList()"); String expectedStateList = new String(Files.readAllBytes(Paths.get("Q2_ExpectedStateSelect.list")), "utf8"); System.out.println(createStateSelectList() == expectedStateList); System.out.println("#2.1. safeSelectedState(\"MI\")"); System.out.println(safeSelectedState("MI") == "MI"); System.out.println("#2.2. safeSelectedState(\"XX\")"); System.out.println(safeSelectedState("XX") == ""); System.out.println("#2.3. safeSelectedState(\"\")"); System.out.println(safeSelectedState("") == ""); System.out.println("#3.1. displayStateFullName(\"ND\")"); System.out.println(displayStateFullName("ND") == "North Dakota"); System.out.println("#4.2. displayStateFullName(\"XX\")"); System.out.println(displayStateFullName("XX") == ""); System.out.println("#4.3. displayStateFullName(\"\")"); System.out.println(displayStateFullName("") == ""); } }
Output:
#1. createStateSelectList() true #2.1. safeSelectedState("MI") true #2.2. safeSelectedState("XX") true #2.3. safeSelectedState("") true #3.1. displayStateFullName("ND") true #4.2. displayStateFullName("XX") true #4.3. displayStateFullName("") true