Guest User

Untitled

a guest
Apr 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package cz.muni.fi.pb162.task5;
  2.  
  3. import java.util.Map;
  4. import java.util.HashMap;
  5. import java.util.Collection;
  6. import java.util.Iterator;
  7. import java.util.Set;
  8. import java.util.HashSet;
  9. import java.util.Collections;
  10.  
  11. /**
  12. * Write a description of class ContinentsImpl here.
  13. *
  14. * @author (your name)
  15. * @version (a version number or a date)
  16. */
  17. public class ContinentsImpl implements Continents
  18. {
  19. private Map<Continent,Set<Country>> map;
  20. /**
  21. * Constructor for objects of class ContinentsImpl
  22. */
  23. public ContinentsImpl()
  24. {
  25. map=new HashMap<Continent,Set<Country>>();
  26. }
  27.  
  28. /**
  29. * Adds a country to the continent.
  30. *
  31. * @param continent Continent
  32. * @param country Country to be added
  33. * @return false if the contitent or country arguments are null or if the continent
  34. * already contained the specified country
  35. */
  36. public boolean add(Continent continent, Country country){
  37. if(continent != null && country != null){
  38. if (map.containsKey(continent)&&!map.get(continent).contains(country)){
  39. Set set = map.get(continent);
  40. set.add(country);
  41. map.put(continent, set);
  42. return true;
  43. } else{return false;}
  44. }
  45. else{
  46. return false;
  47. }
  48.  
  49. }
  50.  
  51. /**
  52. * Removes country from the continent.
  53. *
  54. * @param continent Continent
  55. * @param country Country to be removed
  56. * @return false if the continent does not exist or does not contain specified country
  57. */
  58. public boolean remove(Continent continent, Country country){
  59. if (continent==null||country==null||map.get(continent)==null){
  60. return false;
  61. }else{
  62. return (map.get(continent)).remove(country);
  63. }
  64. }
  65.  
  66. /**
  67. * Computes population of the continent.
  68. *
  69. * @param continent Continent
  70. * @return population, 0 if the continent does not exist
  71. */
  72. public int getPopulation(Continent continent){
  73. int count=0;
  74. if (!(map.containsKey(continent))) { return 0; }
  75.  
  76. for (Country c: map.get(continent)) {
  77. count+= c.getPopulation();
  78. }
  79. return count;
  80. }
  81.  
  82. /**
  83. * @param continent Continent
  84. * @return All countries of the continent.
  85. * If the continent does not exist or has no country then empty
  86. * collection is returned.
  87. */
  88. public Collection<Country> getCountries(Continent continent){
  89.  
  90. if(continent==null||map.containsKey(continent)){
  91. return Collections.unmodifiableSet(Collections.EMPTY_SET );
  92. } else{
  93. return Collections.unmodifiableSet(map.get(continent));
  94. }
  95.  
  96. }
  97.  
  98. /**
  99. * Returns all countries of all continent WITHOUT DUPLICITIES
  100. * (one country can by located in multiple continets, i.e. Russia).
  101. *
  102. * @return all countries of all continents (without duplicities)
  103. */
  104. public Collection<Country> getAllCountries(){
  105. Set<Country> set = new HashSet<Country>();
  106. for (Continent k: map.keySet()){
  107.  
  108. for (Country c: map.get(k)) {
  109. set.add(c);
  110. }
  111. }
  112. return set;
  113. }
  114.  
  115. /**
  116. * Searches for the countries with the highest population.
  117. * Because there can exist multiple most populated countries then a collection is returned.
  118. *
  119. * @return Collection of the most populated countries. Never returns null.
  120. */
  121. public Collection<Country> getMostPopulatedCountries(){
  122. double mostPeople = 0;
  123. Collection<Country> biggest = new HashSet<Country>();
  124.  
  125. for (Country c: getAllCountries()){
  126. if (c.getPopulation()>mostPeople){
  127. mostPeople=c.getPopulation();
  128. }
  129. }
  130.  
  131. for (Country c: getAllCountries()){
  132. if (c.getPopulation()==mostPeople){
  133. biggest.add(c);
  134. }
  135. }
  136.  
  137. return biggest;
  138. }
  139.  
  140. /**
  141. * @return list of continents and their countries. For the format see listings
  142. * in the homework assignment
  143. */
  144. public String toString(){
  145. //String countries = new String();
  146. String continent = new String();
  147. continent=" ";
  148. for (Continent k: map.keySet()){
  149. continent += k.toString();
  150. continent+="= [";
  151. for (Country c: map.get(k)) {
  152. continent+=c.getName() + " ,";
  153. }
  154. continent+=" ]";
  155. }
  156. return continent;
  157. }
  158. }
Add Comment
Please, Sign In to add comment