Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 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. return sightings.stream()
  64. .filter(sighting -> spotter.equals(sighting.getSpotter()))
  65. .map(sighting -> sighting.getCount())
  66. .reduce(0, (total, count) -> return total + count);
  67. /*
  68. for(Sighting record : sightings) {
  69. if(record.getSpotter() == spotter) {
  70. System.out.println(record.getDetails());
  71. }
  72. } */
  73. }
  74.  
  75. /**
  76. * Print a list of the types of animal considered to be endangered.
  77. * @param animalNames A list of animals names.
  78. * @param dangerThreshold Counts less-than or equal-to to this level
  79. * are considered to be dangerous.
  80. */
  81. public void printEndangered(ArrayList<String> animalNames,
  82. int dangerThreshold)
  83. {
  84. for(String animal : animalNames) {
  85. if(getCount(animal) <= dangerThreshold) {
  86. System.out.println(animal + " is endangered.");
  87. }
  88. }
  89. }
  90. /**
  91. * Return a count of the number of sightings of the given animal.
  92. * @param animal The type of animal.
  93. * @return The count of sightings of the given animal.
  94. */
  95. public int getCount(String animal)
  96. {
  97. int total = 0;
  98. for(Sighting sighting : sightings) {
  99. if(animal.equals(sighting.getAnimal())) {
  100. total = total + sighting.getCount();
  101. }
  102. }
  103. return total;
  104. }
  105.  
  106. /**
  107. * Remove from the sightings list all of those records with
  108. * a count of zero.
  109. */
  110. public void removeZeroCounts()
  111. {
  112. Iterator<Sighting> it = sightings.iterator();
  113. while(it.hasNext()) {
  114. Sighting record = it.next();
  115. if(record.getCount() == 0) {
  116. it.remove();
  117. }
  118. }
  119. }
  120.  
  121. /**
  122. * Return a list of all sightings of the given type of animal
  123. * in a particular area.
  124. * @param animal The type of animal.
  125. * @param area The ID of the area.
  126. * @return A list of sightings.
  127. */
  128. public ArrayList<Sighting> getSightingsInArea(String animal, int area)
  129. {
  130. ArrayList<Sighting> records = new ArrayList<>();
  131. for(Sighting record : sightings) {
  132. if(animal.equals(record.getAnimal())) {
  133. if(record.getArea() == area) {
  134. records.add(record);
  135. }
  136. }
  137. }
  138. return records;
  139. }
  140.  
  141. /**
  142. * Return a list of all the sightings of the given animal.
  143. * @param animal The type of animal.
  144. * @return A list of all sightings of the given animal.
  145. */
  146. public ArrayList<Sighting> getSightingsOf(String animal)
  147. {
  148. ArrayList<Sighting> filtered = new ArrayList<>();
  149. for(Sighting record : sightings) {
  150. if(animal.equals(record.getAnimal())) {
  151. filtered.add(record);
  152. }
  153. }
  154. return filtered;
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement