Advertisement
crownedzero

Utility

Nov 15th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. package domain;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Random;
  10.  
  11. public class Utility {
  12.  
  13.     /**
  14.      * Static HashMap; apparently needs .unmodifiable Map a region to an
  15.      * ArrayList
  16.      *
  17.      */
  18.     protected static Map<String, List<String>> timeZone2StateMap = Collections.unmodifiableMap(new HashMap<String, List<String>>() {
  19.         {
  20.             put("Pacific", new ArrayList());
  21.             get("Pacific").add("California");
  22.             get("Pacific").add("Oregon");
  23.             get("Pacific").add("Nevada");
  24.             get("Pacific").add("Washington");
  25.  
  26.             put("Mountain", new ArrayList());
  27.             get("Mountain").add("Utah");
  28.             get("Mountain").add("Arizona");
  29.             get("Mountain").add("New Mexico");
  30.             get("Mountain").add("Colorado");
  31.             get("Mountain").add("Montana");
  32.             get("Mountain").add("Idaho");
  33.             get("Mountain").add("Wyoming");
  34.  
  35.             put("Central", new ArrayList());
  36.             get("Central").add("Wisconsin");
  37.             get("Central").add("Iowa");
  38.             get("Central").add("Illinois");
  39.             get("Central").add("Missouri");
  40.             get("Central").add("Kansas");
  41.             get("Central").add("Kentucky");
  42.             get("Central").add("Alabama");
  43.             get("Central").add("Mississippi");
  44.             get("Central").add("Louisiana");
  45.             get("Central").add("Texas");
  46.             get("Central").add("Oklahoma");
  47.             get("Central").add("Nebraska");
  48.             get("Central").add("North Dakota");
  49.             get("Central").add("South Dakota");
  50.             get("Central").add("Arkansas");
  51.             get("Central").add("Minnesota");
  52.             get("Central").add("Tennessee");
  53.  
  54.             put("Eastern", new ArrayList());
  55.             get("Eastern").add("Florida");
  56.             get("Eastern").add("Georgia");
  57.             get("Eastern").add("South Carolina");
  58.             get("Eastern").add("North Carolina");
  59.             get("Eastern").add("Virginia");
  60.             get("Eastern").add("West Virginia");
  61.             get("Eastern").add("Michigan");
  62.             get("Eastern").add("Delaware");
  63.             get("Eastern").add("New York");
  64.             get("Eastern").add("New Hampshire");
  65.             get("Eastern").add("Vermont");
  66.             get("Eastern").add("Connecticut");
  67.             get("Eastern").add("Massachusetts");
  68.             get("Eastern").add("Maine");
  69.             get("Eastern").add("Vermont");
  70.             get("Eastern").add("Rhode Island");
  71.             get("Eastern").add("Ohio");
  72.             get("Eastern").add("Pennsylvania");
  73.             get("Eastern").add("Indiana");
  74.             get("Eastern").add("Maryland");
  75.             get("Eastern").add("New Jersey");
  76.         }
  77.     });
  78.     /**
  79.      * the 50 states in an Array (territories not included); HashMap values
  80.      * toArray then concatenate maybe?
  81.      *
  82.      */
  83.     protected static String[] stateArray = {"California", "Oregon", "Nevada", "Washington", "Utah", "Arizona", "New Mexico", "Colorado", "Montana", "Idaho",
  84.         "Wyoming", "Wisconsin", "Iowa", "Illinois", "Missouri", "Kansas", "Kentucky", "Alabama", "Mississippi", "Louisiana", "Texas", "Oklahoma", "Nebraska", "North Dakota", "South Dakota", "Arkansas", "Minnesota",
  85.         "Tennessee", "Florida", "Georgia", "South Carolina", "North Carolina", "Virginia", "West Virginia", "Michigan", "Delaware", "New York", "New Hampshire", "Vermont", "Connecticut", "Massachusetts", "Maine", "Vermont",
  86.         "Rhode Island", "Ohio", "Pennsylvania", "Indiana", "Maryland", "New Jersey"};
  87.  
  88.     /**
  89.      * lets pull a random state for distribution
  90.      *
  91.      */
  92.     public static String getState() {
  93.         Random getRandom = new Random();
  94.         int r = getRandom.nextInt(stateArray.length - 1);
  95.         String state = stateArray[r];
  96.         return state;
  97.     }
  98.  
  99.     /**
  100.      * check for state in ArrayList and get the key. Why is there no existing
  101.      * method in the API? this seems like a no-brainer
  102.      *
  103.      * @param state
  104.      * @return
  105.      */
  106.     public static String regionLookup(String state) {
  107.         //whip out a iterator
  108.         Iterator it = timeZone2StateMap.entrySet().iterator();
  109.         //so long as the hashmap contains another value
  110.         while (it.hasNext()) {
  111.             //each K,V value is stored
  112.             Map.Entry pairs = (Map.Entry) it.next();
  113.             //put the V into a list
  114.             List<String> values = (List<String>) pairs.getValue();
  115.             //Bingo! Now give me that key!
  116.             if (values.contains(state)) {
  117.                 return (String) pairs.getKey();
  118.             }
  119.         }
  120.         return null;
  121.     }
  122.  
  123.     /**
  124.      * Just a random product generator; thinking about reworking with a Map as
  125.      * time permits
  126.      *
  127.      * @return
  128.      */
  129.     public static Product getRandomProduct() {
  130.         int r = new Random().nextInt(5);
  131.         Product randomProduct = null;
  132.         switch (r) {
  133.             case 0:
  134.                 randomProduct = new Product("01", "firstProduct", "This is the first product", 12, 14);
  135.                 break;
  136.             case 1:
  137.                 randomProduct = new Product("02", "secondProduct", "This is the second product", 12, 14);
  138.                 break;
  139.             case 2:
  140.                 randomProduct = new Product("03", "thirdProduct", "This is the third product", 12, 14);
  141.                 break;
  142.             case 3:
  143.                 randomProduct = new Product("04", "fourthProduct", "This is the fourth product", 12, 14);
  144.                 break;
  145.             case 4:
  146.                 randomProduct = new Product("05", "fifthProduct", "This is the fifth product", 12, 14);
  147.                 break;
  148.         }
  149.         return randomProduct;
  150.     }
  151.  
  152.     /**
  153.      * Our stopwatch utility method
  154.      *
  155.      * @param startTime
  156.      */
  157.     public static void getTotalTime(long startTime) {
  158.         System.out.println("Total time: " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement