Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. /*
  2. I did this while I was drinking wine after my kids went to bed.
  3. My wine brain added an extra challenge to most of the problems that should have been easier.
  4. This is why the code probably doesn't look as clean as it should.
  5. I'm sure there are probably some edge cases somewhere that I'm not seeing and I didn't write any unit tests for any of it.
  6. I could probably update the splitQuakes() method to return a new ArrayList<List<Marker>> instead of mutating the private one.
  7. Also, that method looks goofy anway and there is probably a better way to do it.
  8. My wine brain says it looks legit though so I'm just going with it. Thanks wine brain!
  9.  
  10. I thought this extension would be easier than it turned out to be.
  11. I thought it would be pretty neat to be able to scroll through the list of earthquakes.
  12. I'm sure some people will probably think it's dumb though. If you're one of those people... sorry?
  13.  
  14. */
  15.  
  16. // code from the beginning of the class
  17. // ...
  18.  
  19. // these private fields were added to the EarthquakeCityMap class
  20. Marker[] quakes;
  21. private List<List<Marker>> sortedRanges;
  22. private int rangeIndex;
  23. private int range;
  24. private static final int PRINT_RANGE = 20;
  25.  
  26. // other code in the class
  27.  
  28. public void setup(){
  29. // rest of the code
  30. // ...
  31.  
  32. // i pulled out the sort of the quakes array
  33. // from the sortAndPrint method and sorted it f
  34. // so my extension can use the sorted array
  35. // you could always just make another array and keep
  36. // the sort in the sortAndPrint method
  37. Arrays.sort(quakes);
  38.  
  39. sortAndPrint(PRINT_RANGE);
  40.  
  41. range = PRINT_RANGE;
  42. rangeIndex = 0;
  43.  
  44. sortedRanges = new ArrayList<List<Marker>>();
  45. splitQuakes();
  46. }
  47.  
  48. // i used this for debugging
  49. // just uncomment it to see each list
  50. /*
  51. System.out.println("Earthquakes split into ranges: ");
  52. for(List<Marker> quakesList : sortedRanges){
  53. System.out.println("Next range: ");
  54. for(Marker q : quakesList){
  55. System.out.println("quake: " + q);
  56. }
  57. }
  58. */
  59.  
  60. // method to split the list of markers
  61. // into a list with a list of markers
  62. // for each range
  63. private void splitQuakes(){
  64.  
  65. int rangeCount = 0;
  66. List<Marker> rangeList = new ArrayList<Marker>();
  67. for(int i = 0; i < quakes.length; i++){
  68. Marker quake = quakes[i];
  69.  
  70. rangeList.add(quake);
  71. rangeCount++;
  72.  
  73. if(rangeCount >= range){
  74. sortedRanges.add(rangeList);
  75. rangeList = null;
  76. rangeList = new ArrayList<Marker>();
  77. rangeCount = 0;
  78. }
  79. }
  80.  
  81. if(!rangeList.isEmpty()){ sortedRanges.add(rangeList); }
  82. }
  83.  
  84. // keyPressed() overridden method
  85. // scrolls through the list of markers depending on whether
  86. // 'u', 'd', or 'a' is pressed
  87. @Override
  88. public void keyPressed()
  89. {
  90. if(key == 'u'){
  91.  
  92. System.out.println("up key pressed!");
  93. rangeIndex--;
  94. if(rangeIndex < 0){ rangeIndex = sortedRanges.size() - 1; }
  95. // show only the next top earthquakes
  96. showNextMarkers();
  97.  
  98. }
  99.  
  100. if(key == 'd'){
  101.  
  102. System.out.println("down key pressed!");
  103. rangeIndex++;
  104. if(rangeIndex > sortedRanges.size() - 1){ rangeIndex = 0; }
  105. showNextMarkers();
  106.  
  107. }
  108.  
  109. if(key == 'a'){
  110.  
  111. rangeIndex = 0;
  112. System.out.println("show all pressed!");
  113. for(Marker m : quakeMarkers){ m.setHidden(false); }
  114. sortAndPrint(range);
  115.  
  116. }
  117.  
  118. }
  119.  
  120. // showNextMarker helper method
  121. // this will show the next range of markers on the map
  122. // and print the information to the console
  123. private void showNextMarkers(){
  124.  
  125. List<Marker> showQuakes = sortedRanges.get(rangeIndex);
  126. //System.out.println("Size of quakes list: " + showQuakes.size());
  127. Marker[] printMarkers = new Marker[showQuakes.size()];
  128. int printIndex = 0;
  129.  
  130. for(Marker quake : quakeMarkers){
  131. if(showQuakes.contains(quake)){
  132. quake.setHidden(false);
  133. printMarkers[printIndex] = quake;
  134. printIndex++;
  135. }
  136. else{
  137. quake.setHidden(true);
  138. }
  139. }
  140.  
  141. Arrays.sort(printMarkers);
  142. for(int i = 0; i < printMarkers.length; i++){ System.out.println(printMarkers[i]); }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement