Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. /**
  5. * Monitor counts of different types of animal.
  6. * Sightings are recorded by spotters.
  7. *
  8. * @author David J. Barnes and Michael Kölling
  9. * @version 2016.02.29 (imperative)
  10. */
  11. public class AnimalMonitor
  12. {
  13. // Records of all the sightings of animals.
  14. private ArrayList<Sighting> sightings;
  15.  
  16. /**
  17. * Create an AnimalMonitor.
  18. */
  19. public AnimalMonitor()
  20. {
  21. this.sightings = new ArrayList<>();
  22. }
  23.  
  24. /**
  25. * Add the sightings recorded in the given filename to the current list.
  26. * @param filename A CSV file of Sighting records.
  27. */
  28. public void addSightings(String filename)
  29. {
  30. SightingReader reader = new SightingReader();
  31. sightings.addAll(reader.getSightings(filename));
  32. }
  33.  
  34. /**
  35. * Print details of all the sightings.
  36. */
  37. public void printList()
  38. {
  39. for(Sighting record : sightings) {
  40. System.out.println(record.getDetails());
  41. }
  42. }
  43.  
  44. /**
  45. * Print the details of all the sightings of the given animal.
  46. * @param animal The type of animal.
  47. */
  48. public void printSightingsOf(String animal)
  49. {
  50. for(Sighting record : sightings) {
  51. if(animal.equals(record.getAnimal())) {
  52. System.out.println(record.getDetails());
  53. }
  54. }
  55. }
  56.  
  57. /**
  58. * Print all the sightings by the given spotter.
  59. * @param spotter The ID of the spotter.
  60. */
  61. public void printSightingsBy(int spotter)
  62. {
  63. int total = 0;
  64. for(Sighting sighting: sightings)
  65. {
  66. if(animal.equals(sighting.getAnimal())) {
  67. total = total + sighting.getCount();
  68. }
  69. }
  70. return total;
  71. /*return sightings.stream()
  72. .filter(sighting -> spotter.equals(sighting.getSpotter()))
  73. .map(sighting -> sighting.getCount())
  74. .reduce(0, (total, count) -> return total + count);
  75.  
  76. for(Sighting record : sightings) {
  77. if(record.getSpotter() == spotter) {
  78. System.out.println(record.getDetails());
  79. }
  80. } */
  81. }
  82.  
  83. /**
  84. * Print a list of the types of animal considered to be endangered.
  85. * @param animalNames A list of animals names.
  86. * @param dangerThreshold Counts less-than or equal-to to this level
  87. * are considered to be dangerous.
  88. */
  89. public void printEndangered(ArrayList<String> animalNames,
  90. int dangerThreshold)
  91. {
  92. for(String animal : animalNames) {
  93. if(getCount(animal) <= dangerThreshold) {
  94. System.out.println(animal + " is endangered.");
  95. }
  96. }
  97. }
  98. /**
  99. * Return a count of the number of sightings of the given animal.
  100. * @param animal The type of animal.
  101. * @return The count of sightings of the given animal.
  102. */
  103. public int getCount(String animal)
  104. {
  105. int total = 0;
  106. for(Sighting sighting : sightings) {
  107. if(animal.equals(sighting.getAnimal())) {
  108. total = total + sighting.getCount();
  109. }
  110. }
  111. return total;
  112. }
  113.  
  114. /**
  115. * Remove from the sightings list all of those records with
  116. * a count of zero.
  117. */
  118. public void removeZeroCounts()
  119. {
  120. Iterator<Sighting> it = sightings.iterator();
  121. while(it.hasNext()) {
  122. Sighting record = it.next();
  123. if(record.getCount() == 0) {
  124. it.remove();
  125. }
  126. }
  127. }
  128.  
  129. /**
  130. * Return a list of all sightings of the given type of animal
  131. * in a particular area.
  132. * @param animal The type of animal.
  133. * @param area The ID of the area.
  134. * @return A list of sightings.
  135. */
  136. public ArrayList<Sighting> getSightingsInArea(String animal, int area)
  137. {
  138. ArrayList<Sighting> records = new ArrayList<>();
  139. for(Sighting record : sightings) {
  140. if(animal.equals(record.getAnimal())) {
  141. if(record.getArea() == area) {
  142. records.add(record);
  143. }
  144. }
  145. }
  146. return records;
  147. }
  148.  
  149. /**
  150. * Return a list of all the sightings of the given animal.
  151. * @param animal The type of animal.
  152. * @return A list of all sightings of the given animal.
  153. */
  154. public ArrayList<Sighting> getSightingsOf(String animal)
  155. {
  156. ArrayList<Sighting> filtered = new ArrayList<>();
  157. for(Sighting record : sightings) {
  158. if(animal.equals(record.getAnimal())) {
  159. filtered.add(record);
  160. }
  161. }
  162. return filtered;
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement