Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package domain;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- public class Utility {
- /**
- * Static HashMap; apparently needs .unmodifiable Map a region to an
- * ArrayList
- *
- */
- protected static Map<String, List<String>> timeZone2StateMap = Collections.unmodifiableMap(new HashMap<String, List<String>>() {
- {
- put("Pacific", new ArrayList());
- get("Pacific").add("California");
- get("Pacific").add("Oregon");
- get("Pacific").add("Nevada");
- get("Pacific").add("Washington");
- put("Mountain", new ArrayList());
- get("Mountain").add("Utah");
- get("Mountain").add("Arizona");
- get("Mountain").add("New Mexico");
- get("Mountain").add("Colorado");
- get("Mountain").add("Montana");
- get("Mountain").add("Idaho");
- get("Mountain").add("Wyoming");
- put("Central", new ArrayList());
- get("Central").add("Wisconsin");
- get("Central").add("Iowa");
- get("Central").add("Illinois");
- get("Central").add("Missouri");
- get("Central").add("Kansas");
- get("Central").add("Kentucky");
- get("Central").add("Alabama");
- get("Central").add("Mississippi");
- get("Central").add("Louisiana");
- get("Central").add("Texas");
- get("Central").add("Oklahoma");
- get("Central").add("Nebraska");
- get("Central").add("North Dakota");
- get("Central").add("South Dakota");
- get("Central").add("Arkansas");
- get("Central").add("Minnesota");
- get("Central").add("Tennessee");
- put("Eastern", new ArrayList());
- get("Eastern").add("Florida");
- get("Eastern").add("Georgia");
- get("Eastern").add("South Carolina");
- get("Eastern").add("North Carolina");
- get("Eastern").add("Virginia");
- get("Eastern").add("West Virginia");
- get("Eastern").add("Michigan");
- get("Eastern").add("Delaware");
- get("Eastern").add("New York");
- get("Eastern").add("New Hampshire");
- get("Eastern").add("Vermont");
- get("Eastern").add("Connecticut");
- get("Eastern").add("Massachusetts");
- get("Eastern").add("Maine");
- get("Eastern").add("Vermont");
- get("Eastern").add("Rhode Island");
- get("Eastern").add("Ohio");
- get("Eastern").add("Pennsylvania");
- get("Eastern").add("Indiana");
- get("Eastern").add("Maryland");
- get("Eastern").add("New Jersey");
- }
- });
- /**
- * the 50 states in an Array (territories not included); HashMap values
- * toArray then concatenate maybe?
- *
- */
- protected static String[] stateArray = {"California", "Oregon", "Nevada", "Washington", "Utah", "Arizona", "New Mexico", "Colorado", "Montana", "Idaho",
- "Wyoming", "Wisconsin", "Iowa", "Illinois", "Missouri", "Kansas", "Kentucky", "Alabama", "Mississippi", "Louisiana", "Texas", "Oklahoma", "Nebraska", "North Dakota", "South Dakota", "Arkansas", "Minnesota",
- "Tennessee", "Florida", "Georgia", "South Carolina", "North Carolina", "Virginia", "West Virginia", "Michigan", "Delaware", "New York", "New Hampshire", "Vermont", "Connecticut", "Massachusetts", "Maine", "Vermont",
- "Rhode Island", "Ohio", "Pennsylvania", "Indiana", "Maryland", "New Jersey"};
- /**
- * lets pull a random state for distribution
- *
- */
- public static String getState() {
- Random getRandom = new Random();
- int r = getRandom.nextInt(stateArray.length - 1);
- String state = stateArray[r];
- return state;
- }
- /**
- * check for state in ArrayList and get the key. Why is there no existing
- * method in the API? this seems like a no-brainer
- *
- * @param state
- * @return
- */
- public static String regionLookup(String state) {
- //whip out a iterator
- Iterator it = timeZone2StateMap.entrySet().iterator();
- //so long as the hashmap contains another value
- while (it.hasNext()) {
- //each K,V value is stored
- Map.Entry pairs = (Map.Entry) it.next();
- //put the V into a list
- List<String> values = (List<String>) pairs.getValue();
- //Bingo! Now give me that key!
- if (values.contains(state)) {
- return (String) pairs.getKey();
- }
- }
- return null;
- }
- /**
- * Just a random product generator; thinking about reworking with a Map as
- * time permits
- *
- * @return
- */
- public static Product getRandomProduct() {
- int r = new Random().nextInt(5);
- Product randomProduct = null;
- switch (r) {
- case 0:
- randomProduct = new Product("01", "firstProduct", "This is the first product", 12, 14);
- break;
- case 1:
- randomProduct = new Product("02", "secondProduct", "This is the second product", 12, 14);
- break;
- case 2:
- randomProduct = new Product("03", "thirdProduct", "This is the third product", 12, 14);
- break;
- case 3:
- randomProduct = new Product("04", "fourthProduct", "This is the fourth product", 12, 14);
- break;
- case 4:
- randomProduct = new Product("05", "fifthProduct", "This is the fifth product", 12, 14);
- break;
- }
- return randomProduct;
- }
- /**
- * Our stopwatch utility method
- *
- * @param startTime
- */
- public static void getTotalTime(long startTime) {
- System.out.println("Total time: " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement